-2

I am capturing image from camera and selecting image from gallery. In samsung devices the images gets rotate after captured.

I want to rotate image to straight if they are rotated.

I tried to do it but its not working. I am getting EXIF orientation as 0 always though the image is rotated.

private void onCaptureImageResult(Intent data) {

    try {

    Bitmap thumbnail = (Bitmap) data.getExtras().get("data");

    Uri tempUri = getImageUri(getApplicationContext(), thumbnail);

    // CALL THIS METHOD TO GET THE ACTUAL PATH
    File finalFile = new File(getRealPathFromURI(tempUri));

        ExifInterface ei = new ExifInterface(String.valueOf(finalFile));
        int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_UNDEFINED);

        Bitmap bitmap = BitmapFactory.decodeFile(String.valueOf(finalFile));

        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;
                break;
        }

        if(rotatedBitmap != null)
        {
            profile_image.setImageBitmap(rotatedBitmap);
            thumbnail = rotatedBitmap;
        }
    }
    catch (IOException ex) {     }


  public Uri getImageUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.PNG, 100, bytes);
    String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
    return Uri.parse(path);
}

public String getRealPathFromURI(Uri uri) {
    Cursor cursor = getContentResolver().query(uri, null, null, null, null);
    cursor.moveToFirst();
    int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
    return cursor.getString(idx);
}

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);
}

What is going wrong here? Please help.. Thank you..

Sid
  • 2,792
  • 9
  • 55
  • 111
  • This link might help you http://stackoverflow.com/questions/14066038/why-image-captured-using-camera-intent-gets-rotated-on-some-devices-in-android – Febi M Felix Oct 21 '16 at 09:10
  • I have implemented same. But its not working. @FebiMathew – Sid Oct 21 '16 at 09:11
  • It is a shame that you posted this question again. We were not ready yet. You did not give the requested info. http://stackoverflow.com/questions/40155743/how-rotate-image-taken-from-camera-or-gallery – greenapps Oct 21 '16 at 10:19
  • It seems you learned nothing of the answers you got for this question during several posts. You can go on posting your question again and again and delete your posts but if you do not listen and answer comments you will get nowhere. Start acting normal please. – greenapps Oct 21 '16 at 14:19

2 Answers2

1

Replace

int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);

With

int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

And tell if it works

earthw0rmjim
  • 19,027
  • 9
  • 49
  • 63
Mohammad Haidar
  • 1,109
  • 1
  • 16
  • 35
  • Why would this solve the problem? You're just changing the default value `getAttributeInt()` returns in case the orientation tag is not available. – earthw0rmjim Oct 21 '16 at 09:27
  • @earthw0rmjim i compared my method by his i and found this difference so i guessed that this is the problem – Mohammad Haidar Oct 21 '16 at 09:28
0

I also faced the same issue once in my project and got it going by below code :

//imageFileUri is fileUri.getPath() that is being passed
private Bitmap getImageBitmap(String imageFileUri) {

    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inSampleSize = 3;
    Bitmap bm = BitmapFactory.decodeFile(imageFileUri, opts);

    ExifInterface exif;
    String orientString = null;
    try {
        exif = new ExifInterface(imageFileUri);
        orientString = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
    } catch (IOException e) {
        if (BuildConfig.DEBUG) {
            e.printStackTrace();
        }
    }

    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;

    int w = bm.getWidth();
    int h = bm.getHeight();
    Matrix mtx = new Matrix();
    mtx.postRotate(rotationAngle);
    return Bitmap.createBitmap(bm, 0, 0, w, h, mtx, true);
}
Sanjeet
  • 2,385
  • 1
  • 13
  • 22