Advertisement
Guest User

Bitmap Resize

a guest
Nov 4th, 2011
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.52 KB | None | 0 0
  1. public static Bitmap decodeBitmap(Bitmap SourceBmpm, int ReqWidth, int ReqHeight)
  2.     {
  3.    
  4.          try {
  5.                 //decode image size
  6.                 BitmapFactory.Options o = new BitmapFactory.Options();
  7.                 o.inJustDecodeBounds = true;
  8.                 ByteArrayOutputStream bos = new ByteArrayOutputStream();
  9.                 SourceBmpm.compress(CompressFormat.PNG, 100 /*ignored for PNG*/, bos);
  10.                 byte[] bitmapdata = bos.toByteArray();
  11.                
  12.                BitmapFactory.decodeByteArray(bitmapdata, 0, bitmapdata.length, o);
  13.              
  14.                 //Find the correct scale value. It should be the power of 2.
  15.                  
  16.               //  final int REQUIRED_SIZE=requiredSize;
  17.                 int width_tmp=o.outWidth, height_tmp=o.outHeight;
  18.                 int scale=1;
  19.                 while(true){
  20.                     if(width_tmp/2<ReqWidth || height_tmp/2<ReqHeight)
  21.                         break;
  22.                     width_tmp/=2;
  23.                     height_tmp/=2;
  24.                     scale*=2;
  25.                 }
  26.                
  27.                 //decode with inSampleSize
  28.                 BitmapFactory.Options o2 = new BitmapFactory.Options();
  29.                 o2.inSampleSize=scale;
  30.                 SourceBmpm.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
  31.                bitmapdata = bos.toByteArray();
  32.                
  33.                 return  BitmapFactory.decodeByteArray(bitmapdata, 0, bitmapdata.length, o2);
  34.             } catch (Exception e) {
  35.                 Log.e("DD"," Exp "+e);
  36.                 return null;
  37.             }
  38.        
  39.        
  40.        
  41.        
  42.     }
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement