0

I am working on an application where I take a picture in portrait orientation, The problem is when I then retrieve the image later on it is in landscape orientation (the picture has been rotated 90 degrees anti-clockwise). I have used below class but here orientation giving 0 (zero) every time. So, i can't figure out how to solve it.

public Bitmap rotateBitmapOrientation(String photoFilePath) {

// Create and configure BitmapFactory
BitmapFactory.Options bounds = new BitmapFactory.Options();
bounds.inJustDecodeBounds = true;
BitmapFactory.decodeFile(file, bounds);
BitmapFactory.Options opts = new BitmapFactory.Options();
Bitmap bm = BitmapFactory.decodeFile(file, opts);
// Read EXIF Data
ExifInterface exif = new ExifInterface(file);
String orientString = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
int orientation = orientString != null ? Integer.parseInt(orientString) : ExifInterface.ORIENTATION_NORMAL;
int rotationAngle = 0;
if (orientation == ExifInterface.ORIENTATION_ROTATE_90) rotationAngle = 90;
if (orientation == ExifInterface.ORIENTATION_ROTATE_180) rotationAngle = 180;
if (orientation == ExifInterface.ORIENTATION_ROTATE_270) rotationAngle = 270;
// Rotate Bitmap
Matrix matrix = new Matrix();
matrix.setRotate(rotationAngle, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);
Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0, bounds.outWidth, bounds.outHeight, matrix, true);
// Return result
return rotatedBitmap;
}
Ronak Joshi
  • 1,553
  • 2
  • 20
  • 43
  • You have to pass your image path to this method and save the opt with bitmap and then set it to your ImageView – Bhavik Mehta Jan 09 '15 at 11:30
  • I have done that, image is showing. But, main fault is that when image taken by camera at portrait mode it rotate image by 90 degree anticlockwise. I have mention this in my question. Please refer it. @BhavikMehta – Ronak Joshi Jan 09 '15 at 11:35
  • You can checkout this answer: http://stackoverflow.com/questions/21776802/taking-picture-with-camera-intent-rotate-picture-in-portrait-mode-android/21778562#21778562 – Hamid Shatu Jan 09 '15 at 11:38
  • No,its give same output. And yeah its gives orientation 0(zero) every time. @HamidShatu – Ronak Joshi Jan 09 '15 at 11:49
  • @RonakJoshi i know my code is lengthy but atleast try it once – Bhavik Mehta Jan 09 '15 at 12:01

2 Answers2

1

There are several methods available over stackoverflow but i am using a mixture of them, if you want the image to be in the orientation it was captured you can use the following instruction and classes to do this

Your onActivityResult

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
        case SELECT_IMAGE_FROM_CAMERA:
            if (requestCode == SELECT_IMAGE_FROM_CAMERA
                    && resultCode == RESULT_OK) {
                int targetW = reviewImageView.getWidth();
                int targetH = reviewImageView.getHeight();
                BitmapFactory.Options bmOptions = new BitmapFactory.Options();
                bmOptions.inJustDecodeBounds = true;
                BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
                int photoW = bmOptions.outWidth;
                int photoH = bmOptions.outHeight;

                // Determine how much to scale down the image
                int scaleFactor = Math.min(photoW / targetW, photoH / targetH);

                // Decode the image file into a Bitmap sized to fill the View
                bmOptions.inJustDecodeBounds = false;
                bmOptions.inSampleSize = scaleFactor;
                bmOptions.inPurgeable = true;

                Bitmap rotatedBitmap = decodeFile(new File(mCurrentPhotoPath),
                        photoW, photoH, getImageOrientation(mCurrentPhotoPath));
                reviewImageView.setImageBitmap(rotatedBitmap);
                uploadMessage.setVisibility(View.INVISIBLE);
                UploadSuccess.setVisibility(View.INVISIBLE);
            } else if (requestCode == SELECT_IMAGE_FROM_CAMERA
                    && resultCode == RESULT_CANCELED) {
                mCurrentPhotoPath = null;
                photo = null;
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

Helper Methods & Class

public static int getImageOrientation(String imagePath) {
        int rotate = 0;
        try {

            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;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return rotate;
    }

public static Bitmap decodeFile(File f, double REQUIRED_WIDTH,
            double REQUIRED_HEIGHT, int rotation) {
        try {
            if (REQUIRED_WIDTH == 0 || REQUIRED_HEIGHT == 0) {
                return BitmapFactory.decodeFile(f.getAbsolutePath());
            } else {
                BitmapFactory.Options o = new BitmapFactory.Options();
                o.inJustDecodeBounds = true;
                BitmapFactory.decodeFile(f.getAbsolutePath(), o);

                o.inSampleSize = calculateInSampleSize(o, REQUIRED_WIDTH,
                        REQUIRED_HEIGHT);

                o.inJustDecodeBounds = false;
                o.inPurgeable = true;
                Bitmap b = BitmapFactory.decodeFile(f.getAbsolutePath(), o);
                if (rotation != 0)
                    b = rotate(b, rotation);
                if (b.getWidth() > REQUIRED_WIDTH
                        || b.getHeight() > REQUIRED_HEIGHT) {
                    double ratio = Math.max((double) b.getWidth(),
                            (double) b.getHeight())
                            / (double) Math
                                    .min(REQUIRED_WIDTH, REQUIRED_HEIGHT);

                    return Bitmap.createScaledBitmap(b,
                            (int) (b.getWidth() / ratio),
                            (int) (b.getHeight() / ratio), true);
                } else
                    return b;
            }

        } catch (Throwable ex) {
            ex.printStackTrace();
        }
        return null;
    }

public static int calculateInSampleSize(BitmapFactory.Options options,
            double reqWidth, double 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 the largest inSampleSize value that is a power of 2 and
            // keeps both
            // height and width larger than the requested height and width.
            while ((height / inSampleSize) > reqHeight
                    || (width / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
        }
        inSampleSize = Math.max(1, inSampleSize / 2);
        return inSampleSize;
    }

    public static Bitmap rotate(Bitmap b, int degrees) {
        if (degrees != 0 && b != null) {
            Matrix m = new Matrix();
            m.setRotate(degrees, (float) b.getWidth() / 2,
                    (float) b.getHeight() / 2);
            try {
                Bitmap b2 = Bitmap.createBitmap(b, 0, 0, b.getWidth(),
                        b.getHeight(), m, true);
                if (b != b2) {
                    b.recycle();
                    b = b2;
                }
            } catch (OutOfMemoryError ex) {
                // We have no memory to rotate. Return the original bitmap.
            }
        }
        return b;
    }

You can customize each method according to your needs, my code follows android guideline, so its quite lengthy

Bhavik Mehta
  • 573
  • 8
  • 21
0

Use below code to fix your problem -

int rotate;
String URI = getImageURI();
                    try {
                        File file = new File(URI);
                        ExifInterface exif = new ExifInterface(
                                file.getAbsolutePath());
                        int orientation = exif.getAttributeInt(
                                ExifInterface.TAG_ORIENTATION,
                                ExifInterface.ORIENTATION_NORMAL);
                        Log.v("my", "Exif orientation: " + orientation);
                        switch (orientation) {
                        case ExifInterface.ORIENTATION_ROTATE_270:
                            Log.v("", "rotated " +270);
                            rotate = 270;
                            Log.e("rotate", ""+rotate);
                            ImageOrientation(file,rotate);
                            break;
                        case ExifInterface.ORIENTATION_ROTATE_180:
                            Log.v("", "rotated " +180);
                            rotate = 180;
                            Log.e("rotate", ""+rotate);
                            ImageOrientation(file,rotate);
                            break;
                        case ExifInterface.ORIENTATION_ROTATE_90:
                            Log.v("", "rotated " +90);
                            rotate = 90;
                            ImageOrientation(file,rotate);
                            break;

                        case 1:
                            Log.v("", "rotated1-" +90);
                            rotate = 90;
                            ImageOrientation(file,rotate);
                            break;

                        case 2:
                            Log.v("", "rotated1-" +0);
                            rotate = 0; 
                            ImageOrientation(file,rotate);
                            break;
                        case 4:
                            Log.v("", "rotated1-" +180);
                            rotate = 180;
                            ImageOrientation(file,rotate);
                            break;

                        case 0:
                            Log.v("", "rotated 0-" +90);
                            rotate = 90;
                            ImageOrientation(file,rotate);
                            break;
                        }
                        filePath = file.getAbsolutePath();

                    } catch (Exception e) {
                        e.printStackTrace();
                    }

   private void ImageOrientation(File file,int rotate){
    try {
        FileInputStream fis = new FileInputStream(file);
        filePath = file.getAbsolutePath();
        Bitmap photo = BitmapFactory.decodeStream(fis);
        Matrix matrix = new Matrix();
        matrix.preRotate(rotate); // clockwise by 90 degrees
        photo = Bitmap.createBitmap(photo , 0, 0, photo.getWidth(), photo.getHeight(), matrix, true);
        customerImageView.setImageBitmap(photo);
        imageBitmap = photo;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

Hope it will help you.

Ravi Bhandari
  • 4,682
  • 8
  • 40
  • 68