Guest User

Untitled

a guest
Apr 20th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. public Bitmap getResizedBitmap(Bitmap image, int bitmapWidth, int bitmapHeight) {
  2. return Bitmap.createScaledBitmap(image, bitmapWidth, bitmapHeight, true);
  3. }
  4.  
  5. public Bitmap getResizedBitmap(Bitmap image, int maxSize) {
  6. int width = image.getWidth();
  7. int height = image.getHeight();
  8.  
  9. float bitmapRatio = (float) width / (float) height;
  10. if (bitmapRatio > 1) {
  11. width = maxSize;
  12. height = (int) (width / bitmapRatio);
  13. } else {
  14. height = maxSize;
  15. width = (int) (height * bitmapRatio);
  16. }
  17.  
  18. return Bitmap.createScaledBitmap(image, width, height, true);
  19. }
  20.  
  21. /** getResizedBitmap method is used to Resized the Image according to custom width and height
  22. * @param image
  23. * @param newHeight (new desired height)
  24. * @param newWidth (new desired Width)
  25. * @return image (new resized image)
  26. * */
  27. public static Bitmap getResizedBitmap(Bitmap image, int newHeight, int newWidth) {
  28. int width = image.getWidth();
  29. int height = image.getHeight();
  30. float scaleWidth = ((float) newWidth) / width;
  31. float scaleHeight = ((float) newHeight) / height;
  32. // create a matrix for the manipulation
  33. Matrix matrix = new Matrix();
  34. // resize the bit map
  35. matrix.postScale(scaleWidth, scaleHeight);
  36. // recreate the new Bitmap
  37. Bitmap resizedBitmap = Bitmap.createBitmap(image, 0, 0, width, height,
  38. matrix, false);
  39. return resizedBitmap;
  40. }
  41.  
  42. Bitmap.createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter);
  43.  
  44. import android.graphics.Bitmap;
  45. import android.graphics.BitmapFactory;
  46. import android.os.AsyncTask;
  47. import android.os.Bundle;
  48. import android.support.v7.app.AppCompatActivity;
  49. import android.widget.ImageView;
  50. import android.widget.Toast;
  51.  
  52. public class ImageProcessActivity extends AppCompatActivity {
  53.  
  54.  
  55. private static final String TAG = "ImageProcessActivity";
  56. private static final String IMAGE_PATH = "/sdcard/DCIM/my_image.jpg";
  57.  
  58. public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
  59.  
  60. final int height = options.outHeight;
  61. final int width = options.outWidth;
  62. int inSampleSize = 1;
  63.  
  64. if (height > reqHeight || width > reqWidth) {
  65.  
  66. final int halfHeight = height / 2;
  67. final int halfWidth = width / 2;
  68.  
  69. // Calculate the largest inSampleSize value that is a power of 2 and keeps both
  70. // height and width larger than the requested height and width.
  71. while ((halfHeight / inSampleSize) >= reqHeight && (halfWidth / inSampleSize) >= reqWidth) {
  72.  
  73. inSampleSize *= 2;
  74. }
  75. }
  76.  
  77. return inSampleSize;
  78. }
  79.  
  80. public static Bitmap decodeSampleDrawableFromFile(String file, int reqWidth, int reqHeight) {
  81.  
  82. // First decode with inJustDecodeBounds=true to check dimensions
  83. final BitmapFactory.Options options = new BitmapFactory.Options();
  84. options.inJustDecodeBounds = true;
  85. BitmapFactory.decodeFile(file, options);
  86.  
  87. // Calculate inSampleSize
  88. options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
  89.  
  90. // Decode bitmap with inSampleSize set
  91. options.inJustDecodeBounds = false;
  92. return BitmapFactory.decodeFile(file, options);
  93. }
  94.  
  95. @Override
  96. protected void onCreate(Bundle savedInstanceState) {
  97. super.onCreate(savedInstanceState);
  98. setContentView(R.layout.activity_image_process);
  99.  
  100. new AsyncTask<Object, Object, Bitmap>() {
  101.  
  102. @Override
  103. protected Bitmap doInBackground(Object... objects) {
  104.  
  105. try {
  106.  
  107. return decodeSampleDrawableFromFile(IMAGE_PATH, 640, 640);
  108.  
  109. } catch (Exception e) {
  110.  
  111. e.printStackTrace();
  112.  
  113. }
  114. return null;
  115. }
  116.  
  117. @Override
  118. protected void onPostExecute(Bitmap bitmap) {
  119. super.onPostExecute(bitmap);
  120.  
  121. ((ImageView) findViewById(R.id.img)).setImageBitmap(bitmap);
  122. }
  123. }.execute();
  124. }
  125. }
Add Comment
Please, Sign In to add comment