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

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

问题描述

我已经通过一些环节去获取从默认的图片库中选择图像的正确形象定位中的所有设备的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.

<一个href="http://stackoverflow.com/questions/13245556/exif-orientation-tag-value-always-0-for-image-taken-with-portrait-camera-app-and">EXIF方向标记值始终为0与人像摄像头应用程序的Andr​​oid 拍摄的图像

的Exif方向标记返回0

的Exif数据TAG_ORIENTATION始终为0

<一个href="http://mobisocial.stanford.edu/news/2011/08/rotating-images-in-android/">http://mobisocial.stanford.edu/news/2011/08/rotating-images-in-android/

可以请你帮我弄一个,将工作在所有设备上确切的解决方案。 Thaks提前

can you please help me to get a exact solution that will work on all devices. Thaks in Advance

推荐答案

如果图像(照片),采取由你犯了一个程序,你必须设置Parameters.setRotation用正确的旋转值。

If the image(photo) were 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.

code

获取方向从EXIF:

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

获取位图旋转:

Get bitmap rotated:

Bitmap bmRotated = rotateBitmap(bitmap, orientation);  

方法来旋转位图:

Method to rotate bitmap:

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天全站免登陆