Captured photo orientation changes in android

I open the application for the camera with the click of a button. And displaying the captured photo in the next step. But the captured photo rotates 90 degrees. When I display an image in a view after it is captured, its orientation is always landscape. Why is the photograph not displayed in the portrait, as when photographing in portrait mode?

onPress the button:

Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(APP_DIR+"/latest.png"))); startActivityForResult(i, CAPTURE_PHOTO_CONSTANT); 

Inside onActvityresult:

 bmp = BitmapFactory.decodeFile(APP_DIR+"/latest.png"); startActivity(new Intent(this, DisplayActivity.class)); 

Display taken photo:

 photoViewRelativeLayout.setBackgroundDrawable(new BitmapDrawable(getResources(), CaptureActivity.bmp)); 
+8
android bitmap camera
source share
4 answers

I had the same problem mainly with Samsung phones. Obviously, Samsung phones set the EXIF ​​orientation tag rather than rotating individual pixels. Running Bitmap using BitmapFactory does not support this tag. What I found, the solution to this problem was to use the ExifInterface in the onActivityResult activity.Which method checks the orientation associated with the URI of the captured camera image.

  int rotate = 0; try { 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.v(Common.TAG, "Exif orientation: " + orientation); } catch (Exception e) { e.printStackTrace(); } /****** Image rotation ****/ Matrix matrix = new Matrix(); matrix.postRotate(orientation); Bitmap cropped = Bitmap.createBitmap(scaled, x, y, width, height, matrix, true); 
+15
source share

I did a little work. When I take a photo in landscape mode, everything works fine, as before. If I take a photo in portrait mode, I need to turn the camera upside down, and the picture looks good. If you notice that I changed the code a bit by adding "ExifInterface" before each orientation. I used the following code:

  try { ExifInterface exif = new ExifInterface(filename); int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); int rotate = 0; switch(orientation) { case ExifInterface.ORIENTATION_ROTATE_270: rotate-=90;break; case ExifInterface.ORIENTATION_ROTATE_180: rotate-=90;break; case ExifInterface.ORIENTATION_ROTATE_90: rotate-=90;break; } Log.d("Fragment", "EXIF info for file " + filename + ": " + rotate); } catch (IOException e) { Log.d("Fragment", "Could not get EXIF info for file " + filename + ": " + e); } 
+1
source share

you just can just rotate it

get the binding of the image obtained using this code in onActvityresult ....

 File imageFile = new File(imageUri.toString()); ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath()); int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); int rotate = 0; switch(orientation) { case ORIENTATION_ROTATE_270: rotate-=90;break; case ORIENTATION_ROTATE_180: rotate-=90;break case ORIENTATION_ROTATE_90: rotate-=90;break } 
0
source share

Use Glide instead. It captures the orientation itself and regardless of the mobile device you use. Here's the sample

 Glide.with(_c).load(horizontalList.get(position).imagePath).into(holder.img_injury); 
0
source share

All Articles