繁体   English   中英

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

[英]Auto Image Rotated from Portrait to Landscape

我正在拍摄照片并将其存储到SD Card ,然后将其从SD Card查看到ImageView ,但获得轮换...

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

如果图像(照片)是由您制作的程序拍摄的,则必须使用正确的旋转值设置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;
    }
}

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

您需要使用带有ORIENTATION_UNDEFINED的 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);  

参考链接

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM