自动图像从纵向旋转为横向

Auto Image Rotated from Portrait to Landscape

我正在拍摄照片并将其存储到 SD Card 中,然后从 SD Card 中查看它到 ImageView,但是旋转了...

我在 Portrait mode 中捕获它,但在 Landscape mode 中获取结果图像...

有什么我遗漏的吗?

ExifUtil.java class 在这里找到

/**
 * Displaying captured image/video on the screen
 * */
private void previewMedia(boolean isImage) {
    // Checking whether captured media is image or video
    if (isImage) {
        imgPreview.setVisibility(View.VISIBLE);

        final Bitmap bitmap = BitmapFactory.decodeFile(filePath);
        Bitmap orientedBitmap = ExifUtil.rotateBitmap(filePath, bitmap);

        imgPreview.setImageBitmap(orientedBitmap);
    } else {
        imgPreview.setVisibility(View.GONE);
    }
}

但仍然在 ImageView 中显示旋转的图像 ...

您需要将 EXIFORIENTATION_UNDEFINED 一起使用以获得正确的方向。

ExifInterface exif = null;
try {
    exif = new ExifInterface(path);
} catch (IOException e) {
    e.printStackTrace();
}  
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 
                                       ExifInterface.ORIENTATION_UNDEFINED);

并旋转位图:

Bitmap bmRotated = rotateBitmap(bitmap, orientation);  

Reference link

如果图像(照片)是由您制作的程序拍摄的,则必须使用正确的旋转值设置Parameters.setRotation。

这取决于相机驱动,在保存之前旋转图像或将旋转值保存到 exif TAG_ORIENTATION。

因此,如果 TAG_ORIENTATION 为 null 或零,则图像方向正确,否则必须根据 TAG_ORIENTATION 中的值旋转图像。

代码

从 EXIF 获取方位:

ExifInterface exif = null;
try {
    exif = new ExifInterface(path);
} catch (IOException e) {
    e.printStackTrace();
}  
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 
                                       ExifInterface.ORIENTATION_UNDEFINED);

旋转位图:

Bitmap bmRotated = rotateBitmap(bitmap, orientation); 

旋转位图的方法:

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

来源 -