I implemented a camera for my application, take a snapshot so:

public void savedPhoto(View v) { camera.takePicture(null, null, new Camera.PictureCallback() { @Override public void onPictureTaken(byte[] data, Camera camera) { try { FileOutputStream fos = new FileOutputStream(photoFile); fos.write(data); fos.close(); String path = photoFile.getPath(); intent = new Intent(CameraActivity.this, PhotoActivity.class); intent.putExtra("image", path); startActivity(intent); } catch (Exception e) { e.printStackTrace(); } } }); } 

where I transfer the image path to another activity . In PhotoActivity I catch the path of the snapshot:

  String path = getIntent().getStringExtra("image"); imgView = (ImageView) findViewById(R.id.Image); imgView.setImageDrawable(Drawable.createFromPath(path)); 

Then draw it in the ImageView . Everything works, but only the image is rotated by -90 degrees. How to rotate the ImageView or the image itself by 90 degrees to make it look normal?

  • Ideally, you need to rotate the image based on exif information - ermak0ff
  • it will not always be rotated, I think. - Vladyslav Matviienko
  • If you take a picture in horizontal mode, then its display is normal, and if in the vertical mode, it rotates by -90 degrees. - Amandi
  • So you need to get the right angle from the multimedia database - Vladyslav Matviienko
  • Can you give an example? - Amandi

1 answer 1

Get orientation:

 ExifInterface exif = null; try { exif = new ExifInterface(path); } catch (IOException e) { e.printStackTrace(); } int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED); 

Rotate the image accordingly onentiation:

 public static Bitmap rotateBitmap(Bitmap bitmap, int orientation) { Matrix matrix = new Matrix(); switch (orientation) { case ExifInterface.ORIENTATION_NORMAL: return bitmap; case ExifInterface.ORIENTATION_FLIP_HORIZONTAL: matrix.setScale(-1, 1); break; case ExifInterface.ORIENTATION_ROTATE_180: matrix.setRotate(180); break; case ExifInterface.ORIENTATION_FLIP_VERTICAL: matrix.setRotate(180); matrix.postScale(-1, 1); break; case ExifInterface.ORIENTATION_TRANSPOSE: matrix.setRotate(90); matrix.postScale(-1, 1); break; case ExifInterface.ORIENTATION_ROTATE_90: matrix.setRotate(90); break; case ExifInterface.ORIENTATION_TRANSVERSE: matrix.setRotate(-90); matrix.postScale(-1, 1); break; case ExifInterface.ORIENTATION_ROTATE_270: matrix.setRotate(-90); break; default: return bitmap; } try { Bitmap bmRotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); bitmap.recycle(); return bmRotated; } catch (OutOfMemoryError e) { e.printStackTrace(); return null; } }