0

In my app, the image is captured from the camera and then it displays in a ImageView
I've done this approach successfully, but when my image is displayed in Imageview, Image display after rotate.
I want to rotate Image and then Display in ImageView.
When I click image from front camera then image is display proper with the help of bellow code

Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap rotate = Bitmap.createBitmap(scale,0,0,scale.getWidth(),scale.getHeight(),matrix,true);
displayImage.setImageBitmap(rotate); 


But when I click from back camera Image will display opposite to front camera. One more thing when I click image from camera from different angle then image will display in ImageView in different angle.

Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
Trusha Savsani
  • 489
  • 1
  • 11
  • 31
  • Check my answer [here](http://stackoverflow.com/questions/27832881/animate-rotation-of-an-image-in-android/27832916#27832916) – Tushar Gogna Feb 18 '15 at 09:35
  • when I click image from camera , and From any angle,from back camera or from front camera. Image must display in proper angle... – Trusha Savsani Feb 18 '15 at 09:43
  • See [this](http://stackoverflow.com/questions/14066038/why-image-captured-using-camera-intent-gets-rotated-on-some-devices-in-android) then. – Tushar Gogna Feb 18 '15 at 09:44
  • not get proper way or example. Please give me some examples ... – Trusha Savsani Feb 18 '15 at 10:20
  • This is a right way of doing it, all you have to do is use some validations reading the sensors of the device and checking it's angle while clicking pic and passing that angle to this method. – Tushar Gogna Feb 18 '15 at 10:22

1 Answers1

2

I use following code in my application.
It works for me...!!!

    File mediaFile = new File(mediaPath);
            Bitmap bitmap;
            if (mediaFile.exists()) {

                if (isImage(mediaPath)) {

                    Bitmap myBitmap = BitmapFactory.decodeFile(mediaFile.getAbsolutePath());
                    int height = (myBitmap.getHeight() * 512 / myBitmap.getWidth());
                    Bitmap scale = Bitmap.createScaledBitmap(myBitmap, 512, height, true);
                    int rotate = 0;
                    try {
                        exif = new ExifInterface(mediaFile.getAbsolutePath());
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                            ExifInterface.ORIENTATION_UNDEFINED);
                    switch (orientation) {
                    case ExifInterface.ORIENTATION_NORMAL:
                        rotate = 0;
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_270:
                        rotate = 270;
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_180:
                        rotate = 180;
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_90:
                        rotate = 90;
                        break;
                    }

                    Matrix matrix = new Matrix();
                    matrix.postRotate(rotate);
                    Bitmap rotateBitmap = Bitmap.createBitmap(scale, 0, 0, scale.getWidth(),
                            scale.getHeight(), matrix, true);
                    displayImage.setImageBitmap(rotateBitmap);
       }
}
public static boolean isImage(String str) {
        boolean temp = false;
        String[] arr = { ".jpeg", ".jpg", ".png", ".bmp", ".gif" };
        for (int i = 0; i < arr.length; i++) {
            temp = str.endsWith(arr[i]);
            if (temp) {
                break;
            }
        }
        return temp;
    }
Chirag Savsani
  • 6,020
  • 4
  • 38
  • 74