Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Captured Photo orientation is changing in android

I'am opening camera app on click of a button. And displaying the captured photo in next activity. But the captured photo is rotating by 90 degrees. When I display the image in a view after I capture it, it's orientation is always landscape. Why is the photo not being shown in portrait as is when the photo is taken in portrait mode?

onClick of a 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));

Displaying captured photo:

photoViewRelativeLayout.setBackgroundDrawable(new BitmapDrawable(getResources(), CaptureActivity.bmp));
like image 661
Santhosh Avatar asked Jun 14 '12 04:06

Santhosh


People also ask

Why do pictures change orientation?

Photos taken with a smartphone or digital camera contain “Exif data,” all sorts of information about where the photo was taken, when it was taken, and even how the camera was oriented. When uploaded to File Manager, this data is preserved, and that can often cause the orientation of the picture to be rotated.

How do I stop my pictures from rotating on my Android?

1 – Tap the Settings icon (it looks like a “gear” or “cog“). 2 – Tap Display. 3 – Tap Advanced. 4 – Tap Auto-rotate screen to “uncheck” the box beside of it.

How do I fix the orientation of a picture?

Right-click the image and select Details to reveal a screen with metadata, including EXIF data, that you can adjust if the image supports it. Force a preferred orientation. Rotate the image, then save it. That process reconstructs the image along the requested dimensions.

Why are my pictures sideways on my Android?

The reason your photo would appear this way is because the photo was taken that way (either with the phone sideways or upside down) and the image file itself is in this orientation. For example, if you hold your phone upright and take a photo, the photo is saved in portrait mode or "sideways".


2 Answers

I had the same problem mostly with the Samsung handsets.Apparently Samsung phones set the EXIF orientation tag, rather than rotating individual pixels.Reading the Bitmap using BitmapFactory does not support this tag.What i found the solution to this problem was using ExifInterface in onActivityResult method of the activity.Which checks for orientation associated with URI of the captured image from the camera.

                        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);
like image 170
Shabbir Panjesha Avatar answered Oct 18 '22 15:10

Shabbir Panjesha


call this method with path of bitmap and bitmap

   getRotateImage(path, bm)

Add getRotateImage method as static in any util class and use it.

public static Bitmap getRotateImage(String photoPath, Bitmap bitmap) throws IOException {
        ExifInterface ei = new ExifInterface(photoPath);
        int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_UNDEFINED);

        Bitmap rotatedBitmap = null;
        switch (orientation) {

            case ExifInterface.ORIENTATION_ROTATE_90:
                rotatedBitmap = rotateImage(bitmap, 90);
                break;

            case ExifInterface.ORIENTATION_ROTATE_180:
                rotatedBitmap = rotateImage(bitmap, 180);
                break;

            case ExifInterface.ORIENTATION_ROTATE_270:
                rotatedBitmap = rotateImage(bitmap, 270);
                break;

            case ExifInterface.ORIENTATION_NORMAL:
            default:
                rotatedBitmap = bitmap;
        }

        return rotatedBitmap;

    }


  public static Bitmap rotateImage(Bitmap source, float angle) {
        Matrix matrix = new Matrix();
        matrix.postRotate(angle);
        return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(),
                matrix, true);
    }
like image 28
jai khambhayta Avatar answered Oct 18 '22 15:10

jai khambhayta