minafaw3

imageCompressing

Mar 15th, 2017
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.49 KB | None | 0 0
  1. public class ImageCompression extends AsyncTask<String, Void, String> {
  2. private Context context;
  3. private static final float maxHeight = 1280.0f;
  4. private static final float maxWidth = 1280.0f;
  5. public ImageCompression(Context context){
  6. this.context=context;
  7. }
  8. @Override
  9. protected String doInBackground(String... strings) {
  10. if(strings.length == 0 || strings[0] == null)
  11. return null;
  12. return compressImage(strings[0]);
  13. }
  14. protected void onPostExecute(String imagePath){
  15. // imagePath is path of new compressed image.
  16. }
  17. public String compressImage(String imagePath) {
  18. Bitmap scaledBitmap = null;
  19. BitmapFactory.Options options = new BitmapFactory.Options();
  20. options.inJustDecodeBounds = true;
  21. Bitmap bmp = BitmapFactory.decodeFile(imagePath, options);
  22. int actualHeight = options.outHeight;
  23. int actualWidth = options.outWidth;
  24. float imgRatio = (float) actualWidth / (float) actualHeight;
  25. float maxRatio = maxWidth / maxHeight;
  26. if (actualHeight > maxHeight || actualWidth > maxWidth) {
  27. if (imgRatio < maxRatio) {
  28. imgRatio = maxHeight / actualHeight;
  29. actualWidth = (int) (imgRatio * actualWidth);
  30. actualHeight = (int) maxHeight;
  31. } else if (imgRatio > maxRatio) {
  32. imgRatio = maxWidth / actualWidth;
  33. actualHeight = (int) (imgRatio * actualHeight);
  34. actualWidth = (int) maxWidth;
  35. } else {
  36. actualHeight = (int) maxHeight;
  37. actualWidth = (int) maxWidth;
  38. }
  39. }
  40. options.inSampleSize = calculateInSampleSize(options, actualWidth, actualHeight);
  41. options.inJustDecodeBounds = false;
  42. options.inDither = false;
  43. options.inPurgeable = true;
  44. options.inInputShareable = true;
  45. options.inTempStorage = new byte[16 * 1024];
  46. try {
  47. bmp = BitmapFactory.decodeFile(imagePath, options);
  48. } catch (OutOfMemoryError exception) {
  49. exception.printStackTrace();
  50. }
  51. try {
  52. scaledBitmap = Bitmap.createBitmap(actualWidth, actualHeight, Bitmap.Config.RGB_565);
  53. } catch (OutOfMemoryError exception) {
  54. exception.printStackTrace();
  55. }
  56. float ratioX = actualWidth / (float) options.outWidth;
  57. float ratioY = actualHeight / (float) options.outHeight;
  58. float middleX = actualWidth / 2.0f;
  59. float middleY = actualHeight / 2.0f;
  60. Matrix scaleMatrix = new Matrix();
  61. scaleMatrix.setScale(ratioX, ratioY, middleX, middleY);
  62. Canvas canvas = new Canvas(scaledBitmap);
  63. canvas.setMatrix(scaleMatrix);
  64. canvas.drawBitmap(bmp, middleX - bmp.getWidth() / 2, middleY - bmp.getHeight() / 2, new Paint(Paint.FILTER_BITMAP_FLAG));
  65. if(bmp!=null)
  66. {
  67. bmp.recycle();
  68. }
  69. ExifInterface exif;
  70. try {
  71. exif = new ExifInterface(imagePath);
  72. int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0);
  73. Matrix matrix = new Matrix();
  74. if (orientation == 6) {
  75. matrix.postRotate(90);
  76. } else if (orientation == 3) {
  77. matrix.postRotate(180);
  78. } else if (orientation == 8) {
  79. matrix.postRotate(270);
  80. }
  81. scaledBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);
  82. } catch (IOException e) {
  83. e.printStackTrace();
  84. }
  85. FileOutputStream out = null;
  86. String filepath = getFilename();
  87. try {
  88. out = new FileOutputStream(filepath);
  89. //write the compressed bitmap at the destination specified by filename.
  90. scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out);
  91. } catch (FileNotFoundException e) {
  92. e.printStackTrace();
  93. }
  94. return filepath;
  95. }
  96. public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
  97. final int height = options.outHeight;
  98. final int width = options.outWidth;
  99. int inSampleSize = 1;
  100. if (height > reqHeight || width > reqWidth) {
  101. final int heightRatio = Math.round((float) height / (float) reqHeight);
  102. final int widthRatio = Math.round((float) width / (float) reqWidth);
  103. inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
  104. }
  105. final float totalPixels = width * height;
  106. final float totalReqPixelsCap = reqWidth * reqHeight * 2;
  107. while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap) {
  108. inSampleSize++;
  109. }
  110. return inSampleSize;
  111. }
  112. public String getFilename() {
  113. File mediaStorageDir = new File(Environment.getExternalStorageDirectory()
  114. + "/Android/data/"
  115. + context.getApplicationContext().getPackageName()
  116. + "/Files/Compressed");
  117. // Create the storage directory if it does not exist
  118. if (! mediaStorageDir.exists()){
  119. mediaStorageDir.mkdirs();
  120. }
  121. String mImageName="IMG_"+ String.valueOf(System.currentTimeMillis()) +".jpg";
  122. String uriString = (mediaStorageDir.getAbsolutePath() + "/"+ mImageName);;
  123. return uriString;
  124. }
  125. }
Add Comment
Please, Sign In to add comment