Advertisement
Guest User

Untitled

a guest
Aug 20th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.21 KB | None | 0 0
  1. public static Bitmap decodeStrem(File fil, Uri selectedImage,
  2. Context mContext) {
  3.  
  4. Bitmap bitmap = null;
  5. try {
  6.  
  7. bitmap = BitmapFactory.decodeStream(mContext.getContentResolver()
  8. .openInputStream(selectedImage));
  9.  
  10. final int THUMBNAIL_SIZE = getThumbSize(bitmap);
  11.  
  12. bitmap = Bitmap.createScaledBitmap(bitmap, THUMBNAIL_SIZE,
  13. THUMBNAIL_SIZE, false);
  14.  
  15. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  16. bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
  17. bitmap = BitmapFactory.decodeStream(new ByteArrayInputStream(baos
  18. .toByteArray()));
  19.  
  20. return bitmap = rotateImage(bitmap, fil.getAbsolutePath());
  21.  
  22. } catch (Exception e) {
  23. e.printStackTrace();
  24. }
  25. return bitmap;
  26. }
  27. public static Bitmap rotateImages(Bitmap bmp, String imageUrl) {
  28. if (bmp != null) {
  29. ExifInterface ei;
  30. int orientation = 0;
  31. try {
  32. ei = new ExifInterface(imageUrl);
  33. orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
  34. ExifInterface.ORIENTATION_NORMAL);
  35.  
  36. } catch (IOException e) {
  37. e.printStackTrace();
  38. }
  39. int bmpWidth = bmp.getWidth();
  40. int bmpHeight = bmp.getHeight();
  41. Matrix matrix = new Matrix();
  42. switch (orientation) {
  43. case ExifInterface.ORIENTATION_UNDEFINED:
  44. matrix.postRotate(90);
  45. break;
  46. case ExifInterface.ORIENTATION_ROTATE_90:
  47. matrix.postRotate(90);
  48. break;
  49. case ExifInterface.ORIENTATION_ROTATE_180:
  50. matrix.postRotate(180);
  51. break;
  52. case ExifInterface.ORIENTATION_ROTATE_270:
  53. matrix.postRotate(270);
  54. break;
  55. default:
  56. break;
  57. }
  58. Bitmap resizedBitmap = Bitmap.createBitmap(bmp, 0, 0, bmpWidth,
  59. bmpHeight, matrix, true);
  60. return resizedBitmap;
  61. } else {
  62. return bmp;
  63. }
  64. }
  65.  
  66. public static Bitmap decodeFile(File f, int sampling) {
  67. try {
  68. BitmapFactory.Options o2 = new BitmapFactory.Options();
  69. o2.inJustDecodeBounds = true;
  70. BitmapFactory.decodeStream(
  71. new FileInputStream(f.getAbsolutePath()), null, o2);
  72.  
  73. o2.inSampleSize = sampling;
  74. o2.inTempStorage = new byte[48 * 1024];
  75.  
  76. o2.inJustDecodeBounds = false;
  77. Bitmap bitmap = BitmapFactory.decodeStream(
  78. new FileInputStream(f.getAbsolutePath()), null, o2);
  79. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  80. bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
  81. bitmap = rotateImage(bitmap, f.getAbsolutePath());
  82. return bitmap;
  83.  
  84. } catch (FileNotFoundException e) {
  85. e.printStackTrace();
  86. } catch (OutOfMemoryError e) {
  87. e.printStackTrace();
  88. }
  89. return null;
  90. }
  91.  
  92. public static Bitmap rotateImage(Bitmap bmp, String imageUrl) {
  93. if (bmp != null) {
  94. ExifInterface ei;
  95. int orientation = 0;
  96. try {
  97. ei = new ExifInterface(imageUrl);
  98. orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
  99. ExifInterface.ORIENTATION_NORMAL);
  100.  
  101. } catch (IOException e) {
  102. e.printStackTrace();
  103. }
  104. int bmpWidth = bmp.getWidth();
  105. int bmpHeight = bmp.getHeight();
  106. Matrix matrix = new Matrix();
  107. switch (orientation) {
  108. case ExifInterface.ORIENTATION_UNDEFINED:
  109. matrix.postRotate(90);
  110. break;
  111. case ExifInterface.ORIENTATION_ROTATE_90:
  112. matrix.postRotate(90);
  113. break;
  114. case ExifInterface.ORIENTATION_ROTATE_180:
  115. matrix.postRotate(180);
  116. break;
  117. case ExifInterface.ORIENTATION_ROTATE_270:
  118. matrix.postRotate(270);
  119. break;
  120. default:
  121. break;
  122. }
  123. Bitmap resizedBitmap = Bitmap.createBitmap(bmp, 0, 0, bmpWidth,
  124. bmpHeight, matrix, true);
  125. return resizedBitmap;
  126. } else {
  127. return bmp;
  128. }
  129. }
  130.  
  131. public static int getThumbSize(Bitmap bitmap) {
  132.  
  133. int THUMBNAIL_SIZE = 250;
  134. if (bitmap.getWidth() < 300) {
  135. THUMBNAIL_SIZE = 250;
  136. } else if (bitmap.getWidth() < 600) {
  137. THUMBNAIL_SIZE = 500;
  138. } else if (bitmap.getWidth() < 1000) {
  139. THUMBNAIL_SIZE = 750;
  140. } else if (bitmap.getWidth() < 2000) {
  141. THUMBNAIL_SIZE = 1500;
  142. } else if (bitmap.getWidth() < 4000) {
  143. THUMBNAIL_SIZE = 2000;
  144. } else if (bitmap.getWidth() > 4000) {
  145. THUMBNAIL_SIZE = 2000;
  146. }
  147. return THUMBNAIL_SIZE;
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement