New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong picture orientation on Samsung devices #22

Open
ivacf opened this Issue Sep 26, 2016 · 18 comments

Comments

Projects
None yet
@ivacf
Contributor

ivacf commented Sep 26, 2016

Hi,

After taking a picture with this library I create a Bitmap and set it in an ImageView as follow:

 public void onPictureTaken(CameraView cameraView, byte[] data) {
     Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
     imageView.setImageBitmap(bitmap);
}

This works fine on a Nexus 5X, however on Samsung devices the picture orientation appears to be incorrectly rotated 90 degrees to the left when the device orientation is portrait. Is there a way to fix this issue?

@yaraki yaraki added the bug label Sep 29, 2016

@rockar06

This comment has been minimized.

Show comment
Hide comment
@rockar06

rockar06 Oct 5, 2016

LG-K500h (X screen) has the same behaviour, after the photo is taken, is rotated 90 degrees to the left.

rockar06 commented Oct 5, 2016

LG-K500h (X screen) has the same behaviour, after the photo is taken, is rotated 90 degrees to the left.

@xCatG

This comment has been minimized.

Show comment
Hide comment
@xCatG

xCatG Oct 27, 2016

This is because Samsung (also Sony) devices write EXIF orientation = 6 but BitmapFactory.decodeByteArray() ignores the EXIF rotation tag. (I think it will work if you write the data out to a file and then read it using BitmapFactory.decodeFile) You have to rotate the image yourself before displaying it.

xCatG commented Oct 27, 2016

This is because Samsung (also Sony) devices write EXIF orientation = 6 but BitmapFactory.decodeByteArray() ignores the EXIF rotation tag. (I think it will work if you write the data out to a file and then read it using BitmapFactory.decodeFile) You have to rotate the image yourself before displaying it.

@samy-baili

This comment has been minimized.

Show comment
Hide comment
@samy-baili

samy-baili Nov 28, 2016

Did someone find a workaround ?

samy-baili commented Nov 28, 2016

Did someone find a workaround ?

@damianMolinski

This comment has been minimized.

Show comment
Hide comment
@damianMolinski

damianMolinski Nov 28, 2016

I've created pull request[https://github.com//pull/45/files] which adds option to have, as result, byte array and angle of image so u can easily rotate bitmap.

All u have to do is copy ExifUtil class and then use getOrientation with byte array.
Then u can rotate bitmap, e.g.
private Bitmap rotateBitmap(Bitmap source, int angle) { Matrix matrix = new Matrix(); matrix.postRotate(angle); return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true); }

I've tested it on samsung galaxy s6/nexus5 and it does work for me.

damianMolinski commented Nov 28, 2016

I've created pull request[https://github.com//pull/45/files] which adds option to have, as result, byte array and angle of image so u can easily rotate bitmap.

All u have to do is copy ExifUtil class and then use getOrientation with byte array.
Then u can rotate bitmap, e.g.
private Bitmap rotateBitmap(Bitmap source, int angle) { Matrix matrix = new Matrix(); matrix.postRotate(angle); return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true); }

I've tested it on samsung galaxy s6/nexus5 and it does work for me.

@linhnguyen106

This comment has been minimized.

Show comment
Hide comment
@linhnguyen106

linhnguyen106 Dec 26, 2016

is anyone gonna fix this ?

linhnguyen106 commented Dec 26, 2016

is anyone gonna fix this ?

@ivacf

This comment has been minimized.

Show comment
Hide comment
@ivacf

ivacf Dec 27, 2016

Contributor

I don't think this is a issue with the library anymore, the application should be in charge of rotating the image if necessary by reading the image Exif data. This can be easily achieved using the new Exif support library

The exif library allows you to easily find out how much you have to rotate the image before you display it to the user. This is how it could be done:

@Override
public void onPictureTaken(CameraView cameraView, byte[] data) {
            // Find out if the picture needs rotating by looking at its Exif data
            ExifInterface exifInterface = new ExifInterface(new ByteArrayInputStream(data));
            int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
            int rotationDegrees = 0;
            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    rotationDegrees = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    rotationDegrees = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    rotationDegrees = 270;
                    break;
            }
            // Create and rotate the bitmap by rotationDegrees
}

I think this issue can be closed now.

Contributor

ivacf commented Dec 27, 2016

I don't think this is a issue with the library anymore, the application should be in charge of rotating the image if necessary by reading the image Exif data. This can be easily achieved using the new Exif support library

The exif library allows you to easily find out how much you have to rotate the image before you display it to the user. This is how it could be done:

@Override
public void onPictureTaken(CameraView cameraView, byte[] data) {
            // Find out if the picture needs rotating by looking at its Exif data
            ExifInterface exifInterface = new ExifInterface(new ByteArrayInputStream(data));
            int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
            int rotationDegrees = 0;
            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    rotationDegrees = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    rotationDegrees = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    rotationDegrees = 270;
                    break;
            }
            // Create and rotate the bitmap by rotationDegrees
}

I think this issue can be closed now.

@petergayduchik

This comment has been minimized.

Show comment
Hide comment
@petergayduchik

petergayduchik Nov 14, 2017

its still not working . more than 5 years. And bug still here. I cant believe it . Samsungs devices have a lot of bugs. I cant understand why they cant fix this problem.

petergayduchik commented Nov 14, 2017

its still not working . more than 5 years. And bug still here. I cant believe it . Samsungs devices have a lot of bugs. I cant understand why they cant fix this problem.

@sibelius

This comment has been minimized.

Show comment
Hide comment
@sibelius

sibelius Dec 10, 2017

you should follow @ivacf comment #22 (comment)

sibelius commented Dec 10, 2017

you should follow @ivacf comment #22 (comment)

@mohitajwani

This comment has been minimized.

Show comment
Hide comment
@mohitajwani

mohitajwani Feb 5, 2018

@ivacf I am using this code. normalizeImageForUri method is used to fix these things. But, I am getting a problem in Nougat device. I am not sure if the same issue persists in lower versions. The issue is I have sent the Uri to this method by calling Uri.fromFile(photoFile) where photoFile is the file that is made from the Util class and Uri is extracted using this code FileProvider.getUriForFile(mCallerActivity, BuildConfig.APPLICATION_ID + ".provider", mPhotoFile);

I am unable get the ExifInterface object, I get a FileNotFoundException. Whereas, if I am using the same file to upload the image to my S3 bucket and that is successful. Can you please let me know why the ExifInterface object is not getting the File from the File path?

public static void normalizeImageForUri(Context context, Uri uri) {
    try {
        Log.d(TAG, "URI value = " + uri.getPath());
        ExifInterface exif = new ExifInterface(uri.getPath());
        Log.d(TAG, "Exif value = " + exif);
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
        Bitmap bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), uri);
        Bitmap rotatedBitmap = rotateBitmap(bitmap, orientation);
        if (!bitmap.equals(rotatedBitmap)) {
            saveBitmapToFile(context, rotatedBitmap, uri);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

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

private static void saveBitmapToFile(Context context, Bitmap croppedImage, Uri saveUri) {
    if (saveUri != null) {
        OutputStream outputStream = null;
        try {
            outputStream = context.getContentResolver().openOutputStream(saveUri);
            if (outputStream != null) {
                croppedImage.compress(Bitmap.CompressFormat.JPEG, 90, outputStream);
            }
        } catch (IOException e) {

        } finally {
            closeSilently(outputStream);
            croppedImage.recycle();
        }
    }
}

private static void closeSilently(@Nullable Closeable c) {
    if (c == null) {
        return;
    }
    try {
        c.close();
    } catch (Throwable t) {
        // Do nothing
    }
}

mohitajwani commented Feb 5, 2018

@ivacf I am using this code. normalizeImageForUri method is used to fix these things. But, I am getting a problem in Nougat device. I am not sure if the same issue persists in lower versions. The issue is I have sent the Uri to this method by calling Uri.fromFile(photoFile) where photoFile is the file that is made from the Util class and Uri is extracted using this code FileProvider.getUriForFile(mCallerActivity, BuildConfig.APPLICATION_ID + ".provider", mPhotoFile);

I am unable get the ExifInterface object, I get a FileNotFoundException. Whereas, if I am using the same file to upload the image to my S3 bucket and that is successful. Can you please let me know why the ExifInterface object is not getting the File from the File path?

public static void normalizeImageForUri(Context context, Uri uri) {
    try {
        Log.d(TAG, "URI value = " + uri.getPath());
        ExifInterface exif = new ExifInterface(uri.getPath());
        Log.d(TAG, "Exif value = " + exif);
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
        Bitmap bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), uri);
        Bitmap rotatedBitmap = rotateBitmap(bitmap, orientation);
        if (!bitmap.equals(rotatedBitmap)) {
            saveBitmapToFile(context, rotatedBitmap, uri);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

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

private static void saveBitmapToFile(Context context, Bitmap croppedImage, Uri saveUri) {
    if (saveUri != null) {
        OutputStream outputStream = null;
        try {
            outputStream = context.getContentResolver().openOutputStream(saveUri);
            if (outputStream != null) {
                croppedImage.compress(Bitmap.CompressFormat.JPEG, 90, outputStream);
            }
        } catch (IOException e) {

        } finally {
            closeSilently(outputStream);
            croppedImage.recycle();
        }
    }
}

private static void closeSilently(@Nullable Closeable c) {
    if (c == null) {
        return;
    }
    try {
        c.close();
    } catch (Throwable t) {
        // Do nothing
    }
}
@sibelius

This comment has been minimized.

Show comment
Hide comment
@tomtheunit

This comment has been minimized.

Show comment
Hide comment
@tomtheunit

tomtheunit Feb 16, 2018

@ivacf does your code require API level 24?

add this stage i'm just considering screenshotting the fking viewfinder. the camera api drives me nuts

tomtheunit commented Feb 16, 2018

@ivacf does your code require API level 24?

add this stage i'm just considering screenshotting the fking viewfinder. the camera api drives me nuts

ewolfox pushed a commit to ewolfox/cameraview that referenced this issue Apr 13, 2018

Merge pull request #22 from dataticket/master
fix NPE crash with onMeasure when previewSize is null

ewolfox pushed a commit to ewolfox/cameraview that referenced this issue Apr 13, 2018

Improve coverage (#22)
* New tests

* Try adjust codecov

* CropHelper and CameraUtils tests

* Set target coverage to 40%

* OrientationHelperTest

* Preview tests

* Fix SurfaceView

* Introduce Task for async tests

* Gesture tests

* Improve Gesture Tests

* Add wake lock

* Fix view size

* Add build info logs

* Revert info

* Try fix bug

* Test timeout

* Try fix tests

* Fix TextureView tests if not hardware accelerated

* Fix bug
@MoustafaElsaghier

This comment has been minimized.

Show comment
Hide comment
@MoustafaElsaghier

MoustafaElsaghier Apr 22, 2018

Solved on Samsung and sony devices (tested devices).

I've solved this issue by checking if the width > height of the image then rotate by 90

private static int fixOrientation(Bitmap bitmap) {
        if (bitmap.getWidth() > bitmap.getHeight()) {
            return 90;
        }
        return 0;
    }

then I call this method to apply the rotation if needed

public static Bitmap flipIMage(Bitmap bitmap) {
        //Moustafa: fix issue of image reflection due to front camera settings
        Matrix matrix = new Matrix();
        int rotation = fixOrientation(bitmap);
        matrix.postRotate(rotation);
        matrix.preScale(-1, 1);
        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    }

the reason why I set scale by (-1,1) because I use front camera and need to fix the refliction issue of camera.

hope this solves the issue you have.

MoustafaElsaghier commented Apr 22, 2018

Solved on Samsung and sony devices (tested devices).

I've solved this issue by checking if the width > height of the image then rotate by 90

private static int fixOrientation(Bitmap bitmap) {
        if (bitmap.getWidth() > bitmap.getHeight()) {
            return 90;
        }
        return 0;
    }

then I call this method to apply the rotation if needed

public static Bitmap flipIMage(Bitmap bitmap) {
        //Moustafa: fix issue of image reflection due to front camera settings
        Matrix matrix = new Matrix();
        int rotation = fixOrientation(bitmap);
        matrix.postRotate(rotation);
        matrix.preScale(-1, 1);
        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    }

the reason why I set scale by (-1,1) because I use front camera and need to fix the refliction issue of camera.

hope this solves the issue you have.

@MrBrown09

This comment has been minimized.

Show comment
Hide comment
@MrBrown09

MrBrown09 Jul 26, 2018

@MoustafaElsaghier - Thanks for this solution. I have implemented this and tested. Working good on Samsung J pro but image turns upside down(i.e. 180 degree) in Samsung J Core. Don't know why it is. Please suggest a solution.

MrBrown09 commented Jul 26, 2018

@MoustafaElsaghier - Thanks for this solution. I have implemented this and tested. Working good on Samsung J pro but image turns upside down(i.e. 180 degree) in Samsung J Core. Don't know why it is. Please suggest a solution.

@MoustafaElsaghier

This comment has been minimized.

Show comment
Hide comment
@MoustafaElsaghier

MoustafaElsaghier Jul 26, 2018

@MrBrown09 sorry for the late answer
I've solved this by using OrientationEventListener and in onOrientationChanged I reversed the orientation that means if it is 0 put it 180 and if 180 set it 0

the same with 270 and 90.

if you need any help how calc angel tell me, please

MoustafaElsaghier commented Jul 26, 2018

@MrBrown09 sorry for the late answer
I've solved this by using OrientationEventListener and in onOrientationChanged I reversed the orientation that means if it is 0 put it 180 and if 180 set it 0

the same with 270 and 90.

if you need any help how calc angel tell me, please

@weichao66666

This comment has been minimized.

Show comment
Hide comment
@weichao66666

weichao66666 Aug 3, 2018

关键原因:bitmap 不包含 exif 信息
解决方法1:读文件前获取 exif 信息,bitmap 写入到文件以后写入 exif 信息。

public static ExifInterface getExif(String filePath) {
    try {
        return new ExifInterface(filePath);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

public static void setExif(String filePath, ExifInterface exif) {
    try {
        ExifInterface newExif = new ExifInterface(filePath);
        final Class<ExifInterface> cls = ExifInterface.class;
        final Field[] fields = cls.getFields();
        for (Field field : fields) {
            final String fieldName = field.getName();
            if (!TextUtils.isEmpty(fieldName) && fieldName.startsWith("TAG")) {
                final String tag = field.get(cls).toString();
                final String value = exif.getAttribute(tag);
                if (value != null) {
                    newExif.setAttribute(tag, value);
                }
            }
        }
        newExif.saveAttributes();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

解决方法2:读取 exif 信息,按 exif 信息旋转,最后一定不能再写入原来的ExifInterface.TAG_ORIENTATION的值,该值旋转后应该为0。

public static Bitmap resetOrientation(String filePath) {
    if (TextUtils.isEmpty(filePath)) {
        return null;
    }

    final Bitmap bitmap = decodeBitmapFromFile(filePath);// 略
    final int degree = getOrientationDegree(filePath);
    return rotateBitmap(bitmap, degree);
}

public static int getOrientationDegree(String imagePath) {
    if (TextUtils.isEmpty(imagePath)) {
        return 0;
    }

    try {
        final ExifInterface exifInterface = new ExifInterface(imagePath);
        final int attributeInt = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
        switch (attributeInt) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                return 90;
            case ExifInterface.ORIENTATION_ROTATE_180:
                return 180;
            case ExifInterface.ORIENTATION_ROTATE_270:
                return 270;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return 0;
}

public static Bitmap rotateBitmap(Bitmap bitmap, int degree) {
    if (degree == 0 || bitmap == null) {
        return bitmap;
    }

    final Matrix matrix = new Matrix();
    matrix.setRotate(degree, bitmap.getWidth() / 2, bitmap.getHeight() / 2);
    return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}

weichao66666 commented Aug 3, 2018

关键原因:bitmap 不包含 exif 信息
解决方法1:读文件前获取 exif 信息,bitmap 写入到文件以后写入 exif 信息。

public static ExifInterface getExif(String filePath) {
    try {
        return new ExifInterface(filePath);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

public static void setExif(String filePath, ExifInterface exif) {
    try {
        ExifInterface newExif = new ExifInterface(filePath);
        final Class<ExifInterface> cls = ExifInterface.class;
        final Field[] fields = cls.getFields();
        for (Field field : fields) {
            final String fieldName = field.getName();
            if (!TextUtils.isEmpty(fieldName) && fieldName.startsWith("TAG")) {
                final String tag = field.get(cls).toString();
                final String value = exif.getAttribute(tag);
                if (value != null) {
                    newExif.setAttribute(tag, value);
                }
            }
        }
        newExif.saveAttributes();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

解决方法2:读取 exif 信息,按 exif 信息旋转,最后一定不能再写入原来的ExifInterface.TAG_ORIENTATION的值,该值旋转后应该为0。

public static Bitmap resetOrientation(String filePath) {
    if (TextUtils.isEmpty(filePath)) {
        return null;
    }

    final Bitmap bitmap = decodeBitmapFromFile(filePath);// 略
    final int degree = getOrientationDegree(filePath);
    return rotateBitmap(bitmap, degree);
}

public static int getOrientationDegree(String imagePath) {
    if (TextUtils.isEmpty(imagePath)) {
        return 0;
    }

    try {
        final ExifInterface exifInterface = new ExifInterface(imagePath);
        final int attributeInt = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
        switch (attributeInt) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                return 90;
            case ExifInterface.ORIENTATION_ROTATE_180:
                return 180;
            case ExifInterface.ORIENTATION_ROTATE_270:
                return 270;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return 0;
}

public static Bitmap rotateBitmap(Bitmap bitmap, int degree) {
    if (degree == 0 || bitmap == null) {
        return bitmap;
    }

    final Matrix matrix = new Matrix();
    matrix.setRotate(degree, bitmap.getWidth() / 2, bitmap.getHeight() / 2);
    return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}
@Rohit-Me-Redmart

This comment has been minimized.

Show comment
Hide comment
@Rohit-Me-Redmart

Rohit-Me-Redmart Aug 27, 2018

You need to add default state in switch case with for Samsung and Sony device.

below is a working code.

@override
public void onPictureTaken(CameraView cameraView, byte[] data) {
// Find out if the picture needs rotating by looking at its Exif data
ExifInterface exifInterface = new ExifInterface(new ByteArrayInputStream(data));
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
int rotationDegrees = 0;
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
rotationDegrees = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotationDegrees = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
rotationDegrees = 270;
break;

          default:
                matrix.postRotate(IMAGE_ROTATION_ANGLE_90);
                break;
        }
        // Create and rotate the bitmap by rotationDegrees

}

Rohit-Me-Redmart commented Aug 27, 2018

You need to add default state in switch case with for Samsung and Sony device.

below is a working code.

@override
public void onPictureTaken(CameraView cameraView, byte[] data) {
// Find out if the picture needs rotating by looking at its Exif data
ExifInterface exifInterface = new ExifInterface(new ByteArrayInputStream(data));
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
int rotationDegrees = 0;
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
rotationDegrees = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotationDegrees = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
rotationDegrees = 270;
break;

          default:
                matrix.postRotate(IMAGE_ROTATION_ANGLE_90);
                break;
        }
        // Create and rotate the bitmap by rotationDegrees

}

@petergayduchik

This comment has been minimized.

Show comment
Hide comment
@petergayduchik

petergayduchik Sep 3, 2018

anyone fixed this bullshit for samsung android 8 + ?? (for example on samsung s8 with OS 8.0 + its stilll returned bad rotation)

petergayduchik commented Sep 3, 2018

anyone fixed this bullshit for samsung android 8 + ?? (for example on samsung s8 with OS 8.0 + its stilll returned bad rotation)

@Rohit-Me-Redmart

This comment has been minimized.

Show comment
Hide comment
@Rohit-Me-Redmart

Rohit-Me-Redmart Sep 3, 2018

@petergayduchik able to solve the issue by this.

you have to rotate image by 90 degree. have to add default case handling

`[@override
public void onPictureTaken(CameraView cameraView, byte[] data) {
// Find out if the picture needs rotating by looking at its Exif data
ExifInterface exifInterface = new ExifInterface(new ByteArrayInputStream(data));
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
int rotationDegrees = 0;
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
rotationDegrees = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotationDegrees = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
rotationDegrees = 270;
break;

      default:
            matrix.postRotate(IMAGE_ROTATION_ANGLE_90);
            break;
    }
    // Create and rotate the bitmap by rotationDegrees

}](url)`

Rohit-Me-Redmart commented Sep 3, 2018

@petergayduchik able to solve the issue by this.

you have to rotate image by 90 degree. have to add default case handling

`[@override
public void onPictureTaken(CameraView cameraView, byte[] data) {
// Find out if the picture needs rotating by looking at its Exif data
ExifInterface exifInterface = new ExifInterface(new ByteArrayInputStream(data));
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
int rotationDegrees = 0;
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
rotationDegrees = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotationDegrees = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
rotationDegrees = 270;
break;

      default:
            matrix.postRotate(IMAGE_ROTATION_ANGLE_90);
            break;
    }
    // Create and rotate the bitmap by rotationDegrees

}](url)`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment