Advertisement
Guest User

Bitmap Resizer

a guest
Mar 21st, 2011
3,517
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. public class BitmapResizer {
  2.  
  3. public static Bitmap decodeFile(File f,int requiredSize){
  4. try {
  5. //decode image size
  6. BitmapFactory.Options o = new BitmapFactory.Options();
  7. o.inJustDecodeBounds = true;
  8. BitmapFactory.decodeStream(new FileInputStream(f),null,o);
  9.  
  10. //Find the correct scale value. It should be the power of 2.
  11. final int REQUIRED_SIZE=requiredSize;
  12. int width_tmp=o.outWidth, height_tmp=o.outHeight;
  13. int scale=1;
  14. while(true){
  15. if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
  16. break;
  17. width_tmp/=2;
  18. height_tmp/=2;
  19. scale*=2;
  20. }
  21.  
  22. //decode with inSampleSize
  23. BitmapFactory.Options o2 = new BitmapFactory.Options();
  24. o2.inSampleSize=scale;
  25. return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
  26. } catch (FileNotFoundException e) {
  27.  
  28. }
  29. return null;
  30. }
  31.  
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement