【问题标题】:Auto Image Rotated from Portrait to Landscape自动图像从纵向旋转到横向
【发布时间】:2015-10-25 04:44:33
【问题描述】:

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

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

有什么我想念的吗?

ExifUtil.java 类在这里找到

/**
 * 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 中显示旋转的图像 ...

【问题讨论】:

标签: java android android-camera orientation


【解决方案1】:

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

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

因此,如果 TAG_ORIENTATION 为空或零,则图像方向正确,否则必须根据 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;
    }
}

来源 - https://stackoverflow.com/a/20480741/3036759

【讨论】:

  • 感谢您的解决方案,抱歉我已经接受了@SweetWisher 的回答,你们都帮了我很多,但我认为您的解决方案很有帮助
  • Np,很高兴为您提供帮助 :) 这本身就是一种奖励 :)
【解决方案2】:

您需要使用 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 2012-01-08
    • 1970-01-01
    • 1970-01-01
    • 2011-12-07
    • 2011-09-23
    相关资源
    最近更新 更多