Advertisement
Guest User

Untitled

a guest
Jul 27th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.93 KB | None | 0 0
  1. import android.app.Activity;
  2. import android.content.Context;
  3. import android.graphics.Bitmap;
  4. import android.graphics.BitmapFactory;
  5. import android.graphics.Canvas;
  6. import android.graphics.Matrix;
  7. import android.graphics.Paint;
  8. import android.media.ExifInterface;
  9. import android.os.Environment;
  10. import android.view.inputmethod.InputMethodManager;
  11.  
  12. import java.io.File;
  13. import java.io.FileNotFoundException;
  14. import java.io.FileOutputStream;
  15. import java.io.IOException;
  16.  
  17. /**
  18. * Created by Grishma on 14/3/16.
  19. */
  20. public class CommonBitmapUtils {
  21. private static final float maxHeight = 1280.0f;
  22. private static final float maxWidth = 1280.0f;
  23.  
  24. public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
  25. final int height = options.outHeight;
  26. final int width = options.outWidth;
  27. int inSampleSize = 1;
  28.  
  29. if (height > reqHeight || width > reqWidth) {
  30. final int heightRatio = Math.round((float) height / (float) reqHeight);
  31. final int widthRatio = Math.round((float) width / (float) reqWidth);
  32. inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
  33. }
  34. final float totalPixels = width * height;
  35. final float totalReqPixelsCap = reqWidth * reqHeight * 2;
  36. while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap) {
  37. inSampleSize++;
  38. }
  39. return inSampleSize;
  40. }
  41.  
  42.  
  43. /**
  44. * Reduces the size of an image without affecting its quality.
  45. *
  46. * @param imagePath -Path of an image
  47. * @return
  48. */
  49. public static String compressImage(String imagePath) {
  50. Bitmap scaledBitmap = null;
  51. BitmapFactory.Options options = new BitmapFactory.Options();
  52. options.inJustDecodeBounds = true;
  53. Bitmap bmp = BitmapFactory.decodeFile(imagePath, options);
  54.  
  55. int actualHeight = options.outHeight;
  56. int actualWidth = options.outWidth;
  57.  
  58. float imgRatio = (float) actualWidth / (float) actualHeight;
  59. float maxRatio = maxWidth / maxHeight;
  60.  
  61. if (actualHeight > maxHeight || actualWidth > maxWidth) {
  62. if (imgRatio < maxRatio) {
  63. imgRatio = maxHeight / actualHeight;
  64. actualWidth = (int) (imgRatio * actualWidth);
  65. actualHeight = (int) maxHeight;
  66. } else if (imgRatio > maxRatio) {
  67. imgRatio = maxWidth / actualWidth;
  68. actualHeight = (int) (imgRatio * actualHeight);
  69. actualWidth = (int) maxWidth;
  70. } else {
  71. actualHeight = (int) maxHeight;
  72. actualWidth = (int) maxWidth;
  73. }
  74. }
  75. options.inSampleSize = calculateInSampleSize(options, actualWidth, actualHeight);
  76. options.inJustDecodeBounds = false;
  77. options.inDither = false;
  78. options.inPurgeable = true;
  79. options.inInputShareable = true;
  80. options.inTempStorage = new byte[16 * 1024];
  81. try {
  82. bmp = BitmapFactory.decodeFile(imagePath, options);
  83. } catch (OutOfMemoryError exception) {
  84. exception.printStackTrace();
  85. }
  86. try {
  87. scaledBitmap = Bitmap.createBitmap(actualWidth, actualHeight, Bitmap.Config.RGB_565);
  88. } catch (OutOfMemoryError exception) {
  89. exception.printStackTrace();
  90. }
  91.  
  92. float ratioX = actualWidth / (float) options.outWidth;
  93. float ratioY = actualHeight / (float) options.outHeight;
  94. float middleX = actualWidth / 2.0f;
  95. float middleY = actualHeight / 2.0f;
  96. Matrix scaleMatrix = new Matrix();
  97. scaleMatrix.setScale(ratioX, ratioY, middleX, middleY);
  98. Canvas canvas = new Canvas(scaledBitmap);
  99. canvas.setMatrix(scaleMatrix);
  100. canvas.drawBitmap(bmp, middleX - bmp.getWidth() / 2, middleY - bmp.getHeight() / 2, new Paint(Paint.FILTER_BITMAP_FLAG));
  101. if (bmp != null) {
  102. bmp.recycle();
  103. }
  104. ExifInterface exif;
  105. try {
  106. exif = new ExifInterface(imagePath);
  107. int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0);
  108. Matrix matrix = new Matrix();
  109. if (orientation == 6) {
  110. matrix.postRotate(90);
  111. } else if (orientation == 3) {
  112. matrix.postRotate(180);
  113. } else if (orientation == 8) {
  114. matrix.postRotate(270);
  115. }
  116. scaledBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);
  117. } catch (IOException e) {
  118. e.printStackTrace();
  119. }
  120. FileOutputStream out = null;
  121. String filepath = MainActivity.imageFilePath;//getFilename();
  122. try {
  123. //new File(imageFilePath).delete();
  124. out = new FileOutputStream(filepath);
  125.  
  126. //write the compressed bitmap at the destination specified by filename.
  127. scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out);
  128.  
  129. } catch (FileNotFoundException e) {
  130. e.printStackTrace();
  131. }
  132.  
  133. return filepath;
  134. }
  135.  
  136. public static String getFilename() {
  137. File mediaStorageDir = new File(Environment.getExternalStorageDirectory()
  138. + "/ImageCompApp/Images");
  139.  
  140. // Create the storage directory if it does not exist
  141. if (!mediaStorageDir.exists()) {
  142. mediaStorageDir.mkdirs();
  143. }
  144.  
  145. String mImageName = "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg";
  146. String uriString = (mediaStorageDir.getAbsolutePath() + "/" + mImageName);
  147. return uriString;
  148. }
  149.  
  150. public static void hideKeyboard(Activity context) {
  151. try {
  152. if (context == null) return;
  153. InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
  154. imm.hideSoftInputFromWindow(context.getWindow().getCurrentFocus().getWindowToken(), 0);
  155. } catch (Exception e) {
  156. }
  157. }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement