如何获得从默认图像库中选择的图像的正确方向 [英] How to get the Correct orientation of the image selected from the Default Image gallery

查看:16
本文介绍了如何获得从默认图像库中选择的图像的正确方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经浏览了一些链接,以获得从默认图片库中选择的图片的正确图片方向,以便在所有设备中正常工作,exif 标签始终返回 0.

I have gone through some of the links to get the correct image orientation of the image selected from the default image gallery to be worked standard in all devices the exif tag always returns 0.

EXIF对于使用人像相机应用程序 android 拍摄的图像,方向标签值始终为 0

Exif 方向标签返回 0

Exif 数据 TAG_ORIENTATION 始终为 0

http://mobisocial.stanford.edu/news/2011/08/rotating-images-in-android/

如何获得适用于所有设备的精确解决方案?

How to get an exact solution that will work on all devices?

推荐答案

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

If the image(photo) was taken by a program made by you, you must set Parameters.setRotation with the correct rotation value.

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

This, depending of camera drive, rotates the image before save or save the rotation value to exif TAG_ORIENTATION.

因此,如果 TAG_ORIENTATION 为空或为零,则图像处于正确的方向,否则您必须根据 TAG_ORIENTATION 中的值旋转图像.

Therefore, if TAG_ORIENTATION is null or zero, the image are in the correct orientation, otherwise you must rotate image according the value in TAG_ORIENTATION.

代码

从 EXIF 获取方向:

Get orientation from 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;
    }
}

这篇关于如何获得从默认图像库中选择的图像的正确方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆