Advertisement
Guest User

Untitled

a guest
Oct 28th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. Intent captureImage = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
  2. captureImage.putExtra(MediaStore.EXTRA_OUTPUT, imageFileUri);
  3. startActivityForResult(captureImage, CAMERA_PIC_REQUEST);
  4.  
  5. private int resolveBitmapOrientation(File bitmapFile) throws IOException {
  6. ExifInterface exif = null;
  7. exif = new ExifInterface(bitmapFile.getAbsolutePath());
  8.  
  9. return exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
  10. }
  11.  
  12. private Bitmap applyOrientation(Bitmap bitmap, int orientation) {
  13. int rotate = 0;
  14. switch (orientation) {
  15. case ExifInterface.ORIENTATION_ROTATE_270:
  16. rotate = 270;
  17. break;
  18. case ExifInterface.ORIENTATION_ROTATE_180:
  19. rotate = 180;
  20. break;
  21. case ExifInterface.ORIENTATION_ROTATE_90:
  22. rotate = 90;
  23. break;
  24. default:
  25. return bitmap;
  26. }
  27.  
  28. int w = bitmap.getWidth();
  29. int h = bitmap.getHeight();
  30. Matrix mtx = new Matrix();
  31. mtx.postRotate(rotate);
  32. return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
  33. }
  34.  
  35. public static String getPathFromURI(Context context, Uri contentUri) {
  36. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT &&
  37. DocumentsContract.isDocumentUri(context, contentUri)) {
  38. return getPathForV19AndUp(context, contentUri);
  39. } else {
  40. return getPathForPreV19(context, contentUri);
  41. }
  42. }
  43.  
  44. private static String getPathForPreV19(Context context, Uri contentUri) {
  45. String[] projection = { MediaStore.Images.Media.DATA };
  46. Cursor cursor = context.getContentResolver().query(contentUri, projection, null, null, null);
  47. if (cursor != null && cursor.moveToFirst()) {
  48. try {
  49. int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
  50. return cursor.getString(columnIndex);
  51. } finally {
  52. cursor.close();
  53. }
  54. }
  55.  
  56. return null;
  57. }
  58.  
  59. @TargetApi(Build.VERSION_CODES.KITKAT)
  60. private static String getPathForV19AndUp(Context context, Uri contentUri) {
  61. String documentId = DocumentsContract.getDocumentId(contentUri);
  62. String id = documentId.split(":")[1];
  63.  
  64. String[] column = { MediaStore.Images.Media.DATA };
  65. String sel = MediaStore.Images.Media._ID + "=?";
  66. Cursor cursor = context.getContentResolver().
  67. query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
  68. column, sel, new String[]{ id }, null);
  69.  
  70. if (cursor != null) {
  71. try {
  72. int columnIndex = cursor.getColumnIndex(column[0]);
  73. if (cursor.moveToFirst()) {
  74. return cursor.getString(columnIndex);
  75. }
  76. } finally {
  77. cursor.close();
  78. }
  79. }
  80.  
  81. return null;
  82. }
  83.  
  84. static Uri image_uri;
  85. static Bitmap taken_image=null;
  86.  
  87. image_uri=fileUri; // file where image has been saved
  88.  
  89. taken_image=BitmapFactory.decodeFile(image_uri.getPath());
  90. try
  91. {
  92. ExifInterface exif = new ExifInterface(image_uri.getPath());
  93. //Since API Level 5
  94. int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
  95.  
  96.  
  97. switch(orientation) {
  98. case ExifInterface.ORIENTATION_ROTATE_90:
  99. taken_image=decodeScaledBitmapFromSdCard(image_uri.getPath(), 200, 200);
  100. RotateBitmap(taken_image, 90);
  101. break;
  102. case ExifInterface.ORIENTATION_ROTATE_180:
  103. taken_image=decodeScaledBitmapFromSdCard(image_uri.getPath(), 200, 200);
  104. RotateBitmap(taken_image, 180);
  105.  
  106. break;
  107. case ExifInterface.ORIENTATION_ROTATE_270:
  108. taken_image=decodeScaledBitmapFromSdCard(image_uri.getPath(), 200, 200);
  109. RotateBitmap(taken_image, 270);
  110.  
  111. break;
  112. case ExifInterface.ORIENTATION_NORMAL:
  113. taken_image=decodeScaledBitmapFromSdCard(image_uri.getPath(), 200, 200);
  114. RotateBitmap(taken_image, 0);
  115.  
  116. break;
  117. }
  118.  
  119. }
  120. catch (OutOfMemoryError e)
  121. {
  122. Toast.makeText(getActivity(),e+""memory exception occured"",Toast.LENGTH_LONG).show();
  123.  
  124.  
  125. }
  126.  
  127.  
  128.  
  129. public Bitmap RotateBitmap(Bitmap source, float angle) {
  130. Matrix matrix = new Matrix();
  131. matrix.postRotate(angle);
  132.  
  133. round_Image = source;
  134. round_Image = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
  135.  
  136.  
  137. return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement