@@ -40,7 +40,7 @@ public static void saveDrawable2Bitmap(Context ctx, int resId, String fName, int
4040 public static Bitmap createVideoThumbnail (String path ) {
4141 return ThumbnailUtils .createVideoThumbnail (path , MediaStore .Images .Thumbnails .MINI_KIND );
4242 }
43-
43+
4444 public static void setImageDrawable (ImageView imgView , Context ctx , int resId ) {
4545 imgView .setImageBitmap (BitmapFactory .decodeResource (ctx .getResources (), resId ));
4646 }
@@ -68,7 +68,7 @@ public static Bitmap createResizedBitmap(byte[] img, int newHeight,
6868 }
6969
7070 public static int calculateSampleSize (BitmapFactory .Options options ,
71- int newHeight , int newWidth ) {
71+ int newHeight , int newWidth ) {
7272 int sampleSize = 1 ;
7373
7474 while (((options .outHeight / 2 ) / sampleSize ) > newHeight
@@ -90,7 +90,7 @@ public static BitmapFactory.Options getBitmapDimesions(byte[] img) {
9090
9191 return options ;
9292 }
93-
93+
9494 public static BitmapFactory .Options getBitmapDimesions (final String img ) {
9595
9696 final BitmapFactory .Options options = new BitmapFactory .Options ();
@@ -100,15 +100,15 @@ public static BitmapFactory.Options getBitmapDimesions(final String img) {
100100
101101 return options ;
102102 }
103-
103+
104104 public static Bitmap getBitmapForVisibleRegion (WebView webview ) {
105105 Bitmap returnedBitmap = null ;
106106 webview .setDrawingCacheEnabled (true );
107107 returnedBitmap = Bitmap .createBitmap (webview .getDrawingCache ());
108108 webview .setDrawingCacheEnabled (false );
109109 return returnedBitmap ;
110110 }
111-
111+
112112 public static Bitmap getThumbnail (Context context , Uri uri , int size )
113113 throws FileNotFoundException , IOException {
114114 InputStream input = context .getContentResolver ().openInputStream (uri );
@@ -136,15 +136,15 @@ public static Bitmap getThumbnail(Context context, Uri uri, int size)
136136 input .close ();
137137 return bitmap ;
138138 }
139-
139+
140140 private static int getPowerOfTwoForSampleRatio (double ratio ) {
141141 int k = Integer .highestOneBit ((int ) Math .floor (ratio ));
142142 if (k == 0 )
143143 return 1 ;
144144 else
145145 return k ;
146146 }
147-
147+
148148 public Bitmap scaleRelative2View (View view , Bitmap bitmap , float inScaleX , float inScaleY ) {
149149 // create a matrix for the manipulation
150150 Matrix matrix = new Matrix ();
@@ -170,31 +170,56 @@ public Bitmap scaleRelative2View(View view, Bitmap bitmap, float inScaleX, float
170170
171171 return Bitmap .createBitmap (bitmap , 0 , 0 , bitmap .getWidth (), bitmap .getHeight (), matrix , true );
172172 }
173-
173+
174+ public static Bitmap resizeKeepScale (final Bitmap bitmap , final int max ) {
175+ //Log.d(TAG, "resizeKeepScale " + max);
176+ int bmpWidth = bitmap .getWidth ();
177+ int bmpHeight = bitmap .getHeight ();
178+ if (bmpWidth < bmpHeight ) {
179+ bmpWidth = Math .max (1 , (bmpWidth * max / bmpHeight ));
180+ bmpHeight = max ;
181+ } else {
182+ bmpHeight = Math .max (1 , (bmpHeight * max / bmpWidth ));
183+ bmpWidth = max ;
184+ }
185+ final Bitmap createScaledBitmap = Bitmap .createScaledBitmap (bitmap , bmpWidth , bmpHeight , true );
186+ final Rect rect = new Rect ((max - bmpWidth )/2 , (max - bmpHeight )/2 , bmpWidth , bmpHeight );
187+
188+ final Bitmap output = Bitmap .createBitmap (max , max , Bitmap .Config .ARGB_8888 );
189+ final Canvas canvas = new Canvas (output );
190+ canvas .drawARGB (0 , 0 , 0 , 0 );
191+
192+ final Paint paint = new Paint ();
193+ paint .setXfermode (new PorterDuffXfermode (PorterDuff .Mode .SRC_OUT ));
194+ canvas .drawBitmap (createScaledBitmap , rect , rect , paint );
195+ createScaledBitmap .recycle ();
196+ return output ;
197+ }
198+
174199 /**
175200 * scale the bitmap to a long edge of max
176201 *
177202 * @param Bitmap
178203 * @param Integer
179204 * @return Bitmap
180205 */
181- public static Bitmap scaleBmp (Bitmap bmp , int max ) {
182- float bmpWidth = bmp .getWidth ();
183- float bmpHeight = bmp .getHeight ();
206+ public static Bitmap scaleBmp (final Bitmap bmp , final int max ) {
207+ int bmpWidth = bmp .getWidth ();
208+ int bmpHeight = bmp .getHeight ();
184209 if (bmpWidth < bmpHeight ) {
185- bmpWidth = ( bmpWidth * max / bmpHeight );
210+ bmpWidth = Math . max ( 1 , ( bmpWidth * max / bmpHeight ) );
186211 bmpHeight = max ;
187212 } else {
188- bmpHeight = ( bmpHeight * max / bmpWidth );
213+ bmpHeight = Math . max ( 1 , ( bmpHeight * max / bmpWidth ) );
189214 bmpWidth = max ;
190215 }
191- if (bmpWidth > 0 && bmpWidth > 0 ) {
192- bmp = Bitmap .createScaledBitmap (bmp , ( int ) bmpWidth ,
193- ( int ) bmpHeight , true );
194- }
195- return bmp ;
216+ // if (bmpWidth > 0 && bmpWidth > 0) {
217+ return Bitmap .createScaledBitmap (bmp , bmpWidth ,
218+ bmpHeight , true );
219+ // }
220+ // return bmp;
196221 }
197-
222+
198223 public static File saveBitmap (final Bitmap bitmap ,
199224 final String filename ) {
200225 OutputStream outStream = null ;
@@ -213,7 +238,7 @@ public static File saveBitmap(final Bitmap bitmap,
213238 }
214239 return out ;
215240 }
216-
241+
217242 public static void recycleBitmap (ImageView iv ) {
218243 Drawable d = iv .getDrawable ();
219244 if (d instanceof BitmapDrawable ) {
@@ -222,7 +247,7 @@ public static void recycleBitmap(ImageView iv) {
222247 }
223248 d .setCallback (null );
224249 }
225-
250+
226251 public static Bitmap rotate (Bitmap bitmap , int rotation ) {
227252
228253 int targetWidth = bitmap .getWidth ();
@@ -243,7 +268,7 @@ public static Bitmap rotate(Bitmap bitmap, int rotation) {
243268 bitmap .recycle ();
244269 return targetBitmap ;
245270 }
246-
271+
247272 public static Bitmap rotateImageView (int angle , Bitmap bitmap ) {
248273
249274 if (bitmap == null )
@@ -260,17 +285,17 @@ public static Bitmap rotateImageView(int angle, Bitmap bitmap) {
260285 bitmap .recycle ();
261286 return resizedBitmap ;
262287 }
263-
288+
264289 public static Bitmap loadScaledBitmap (Context context , String bitmapFilePath , int widthDp , int heightDp ) throws IOException {
265290 //create movie icon
266291 Bitmap bitmap ;
267- bitmap = BitmapFactory .decodeStream (context .openFileInput (bitmapFilePath ));
268- bitmap = Bitmap .createScaledBitmap (bitmap , widthDp , heightDp , true );
292+ bitmap = BitmapFactory .decodeStream (context .openFileInput (bitmapFilePath ));
293+ bitmap = Bitmap .createScaledBitmap (bitmap , widthDp , heightDp , true );
269294 return bitmap ;
270295 }
271-
296+
272297 public static Bitmap loadBitmapAndScale (final String filePath ,
273- final int width , final int height ) {
298+ final int width , final int height ) {
274299 final BitmapFactory .Options options = new BitmapFactory .Options ();
275300 options .inPreferredConfig = Bitmap .Config .ARGB_8888 ;
276301 options .inDither = false ;
@@ -318,7 +343,7 @@ private static int calculateInSampleSize(
318343 }
319344 return inSampleSize ;
320345 }
321-
346+
322347 public static Bitmap circularBitmap (Bitmap bitmap ) {
323348 Bitmap output = Bitmap .createBitmap (bitmap .getWidth (),
324349 bitmap .getHeight (), Bitmap .Config .ARGB_8888 );
@@ -340,7 +365,7 @@ public static Bitmap circularBitmap(Bitmap bitmap) {
340365 //return _bmp;
341366 return output ;
342367 }
343-
368+
344369 public static Bitmap centerCrop (Bitmap srcBmp ) {
345370 Bitmap dstBmp = null ;
346371 if (srcBmp .getWidth () >= srcBmp .getHeight ()) {
@@ -362,7 +387,7 @@ public static Bitmap centerCrop(Bitmap srcBmp) {
362387 }
363388 return dstBmp ;
364389 }
365-
390+
366391 public static Bitmap cropCenter (Bitmap bitmap ) {
367392
368393 int minSize = Math .min (bitmap .getWidth (), bitmap .getHeight ());
@@ -382,33 +407,33 @@ public static Bitmap cropCenter(Bitmap bitmap) {
382407 bitmap .recycle ();
383408 return targetBitmap ;
384409 }
385-
410+
386411 public static Bitmap drawableToBitmap (int res_id , Context context ) {
387412 BitmapDrawable drawable = (BitmapDrawable ) context .getResources ().getDrawable (res_id );
388413 Bitmap bitmap = drawable .getBitmap ();
389414 return bitmap ;
390415 }
391416
392417 public static Bitmap drawableToBitmap (Drawable drawable ) {
393- Bitmap bitmap = Bitmap .createBitmap (drawable .getIntrinsicWidth (),drawable .getIntrinsicHeight (),
418+ Bitmap bitmap = Bitmap .createBitmap (drawable .getIntrinsicWidth (), drawable .getIntrinsicHeight (),
394419 drawable .getOpacity () != PixelFormat .OPAQUE ? Bitmap .Config .ARGB_8888 : Bitmap .Config .RGB_565 );
395420 Canvas canvas = new Canvas (bitmap );
396- drawable .setBounds (0 , 0 , drawable .getIntrinsicWidth (),drawable .getIntrinsicHeight ());
421+ drawable .setBounds (0 , 0 , drawable .getIntrinsicWidth (), drawable .getIntrinsicHeight ());
397422 drawable .draw (canvas );
398423 return bitmap ;
399424 }
400-
425+
401426 public static Drawable bitmapToDrawable (Bitmap bitmap ) {
402427 return new BitmapDrawable (bitmap );
403428 }
404-
429+
405430 public static Bitmap bytes2Bimap (byte [] b ) {
406431 if (b .length == 0 ) {
407432 return null ;
408433 }
409434 return BitmapFactory .decodeByteArray (b , 0 , b .length );
410435 }
411-
436+
412437 public static Bitmap getScaledScreenshot (final Activity activity , int scaleWidth , int scaleHeight , boolean relativeScaleIfTrue ) {
413438 final View someView = activity .findViewById (android .R .id .content );
414439 final View rootView = someView .getRootView ();
0 commit comments