An image taken from a camera or gallery when used in an image, its orientation changes, and sometimes vertically stretches on Android

In my application, I need to capture an image from the camera or import from the gallery, show it in the image view mode in action. Everything is in order, I get an image from both and can set it on the image without any exceptions. But sometimes the image does not scale properly and becomes vertically stretched or with a changed orientation. Please help me.

Here is my code for decoding an image from the official Android documentation:

public static Bitmap decodeSampledBitmapFromResource(File photoFile, int reqWidth, int reqHeight) { // First decode with inJustDecodeBounds=true to check dimensions final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; try { BitmapFactory.decodeStream(new FileInputStream(photoFile), null, options); // Calculate inSampleSize options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; return BitmapFactory.decodeStream(new FileInputStream(photoFile), null, options); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { // Raw height and width of image final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { // Calculate ratios of height and width to requested height and // width final int heightRatio = Math.round((float) height / (float) reqHeight); final int widthRatio = Math.round((float) width / (float) reqWidth); // Choose the smallest ratio as inSampleSize value, this will // guarantee // a final image with both dimensions larger than or equal to the // requested height and width. inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; } return inSampleSize; } 
+6
source share
1 answer

Images have different orientations, so they rotate in accordance with the orientation when putting on the image. You can check the orientation of the photo by image properties. To set the image correctly, you can use the following code ...

  int rot=getCameraPhotoOrientation(this,Uri,picturePath); if(rot!=0) bitmap=new RotateOrientation().RotateOrientationCall(bitmap,rot); 

GetCameraPhotoOrientation Method: -

  public static int getCameraPhotoOrientation(Context context, Uri imageUri, String imagePath){ int rotate = 0; try { context.getContentResolver().notifyChange(imageUri, null); File imageFile = new File(imagePath); ExifInterface exif = new ExifInterface( imageFile.getAbsolutePath()); int orientation = exif.getAttributeInt( ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); switch (orientation) { case ExifInterface.ORIENTATION_ROTATE_270: rotate = 270; break; case ExifInterface.ORIENTATION_ROTATE_180: rotate = 180; break; case ExifInterface.ORIENTATION_ROTATE_90: rotate = 90; break; } Log.d(TAG, "Exit orientation: " + orientation); } catch (Exception e) { e.printStackTrace(); } return rotate; } 

Add a RotateOrientation class to rotate the class according to orientation.

  public class RotateOrientation { Bitmap RotateOrientationCall(Bitmap src,float degree) { Matrix matrix=new Matrix(); matrix.postRotate(degree); Bitmap bmOut = Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true); return bmOut; } } 
+7
source

Source: https://habr.com/ru/post/945763/


All Articles