来自图库的图像会自动旋转 – Android

在我的Android应用程序中,我正在从设备库加载图像.在那,我面临着关于图像方向的问题.当我从图库中加载大分辨率图像时,它们会自动旋转,然后在我的视图中显示.我尝试了有关此问题的各种解决方案但无法得到正确的解决方案我提到了getOrientation()this链接.我已经尝试了两种解决方案,但无法得到理想的结果.ExifInterface返回正确的数据,但随后由于分辨率较大而不是因为相机方向而导致图像旋转也没有用.请帮我解决这个问题.

谢谢.

解决方法:

创建一个名为ExifUtils的类

public class ExifUtils {
/**
 * @see http://sylvana.net/jpegcrop/exif_orientation.html
 */
public static Bitmap rotateBitmap(String src, Bitmap bitmap) {
    try {
        int orientation = getExifOrientation(src);

        if (orientation == 1) {
            return bitmap;
        }

        Matrix matrix = new Matrix();
        switch (orientation) {
        case 2:
            matrix.setScale(-1, 1);
            break;
        case 3:
            matrix.setRotate(180);
            break;
        case 4:
            matrix.setRotate(180);
            matrix.postScale(-1, 1);
            break;
        case 5:
            matrix.setRotate(90);
            matrix.postScale(-1, 1);
            break;
        case 6:
            matrix.setRotate(90);
            break;
        case 7:
            matrix.setRotate(-90);
            matrix.postScale(-1, 1);
            break;
        case 8:
            matrix.setRotate(-90);
            break;
        default:
            return bitmap;
        }

        try {
            Bitmap oriented = Bitmap.createBitmap(bitmap, 0, 0,
                    bitmap.getWidth(), bitmap.getHeight(), matrix, true);
            bitmap.recycle();
            return oriented;
        } catch (OutOfMemoryError e) {
            e.printstacktrace();
            return bitmap;
        }
    } catch (IOException e) {
        e.printstacktrace();
    }

    return bitmap;
}

private static int getExifOrientation(String src) throws IOException {
    int orientation = 1;

    try {
        /**
         * if your are targeting only api level >= 5 ExifInterface exif =
         * new ExifInterface(src); orientation =
         * exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
         */
        if (Build.VERSION.SDK_INT >= 5) {
            Class<?> exifClass = Class
                    .forName("android.media.ExifInterface");
            Constructor<?> exifConstructor = exifClass
                    .getConstructor(new Class[] { String.class });
            Object exifInstance = exifConstructor
                    .newInstance(new Object[] { src });
            Method getAttributeInt = exifClass.getmethod("getAttributeInt",
                    new Class[] { String.class, int.class });
            Field tagOrientationField = exifClass
                    .getField("TAG_ORIENTATION");
            String tagOrientation = (String) tagOrientationField.get(null);
            orientation = (Integer) getAttributeInt.invoke(exifInstance,
                    new Object[] { tagOrientation, 1 });
        }
    } catch (ClassNotFoundException e) {
        e.printstacktrace();
    } catch (SecurityException e) {
        e.printstacktrace();
    } catch (NoSuchMethodException e) {
        e.printstacktrace();
    } catch (IllegalArgumentException e) {
        e.printstacktrace();
    } catch (InstantiationException e) {
        e.printstacktrace();
    } catch (illegalaccessexception e) {
        e.printstacktrace();
    } catch (InvocationTargetException e) {
        e.printstacktrace();
    } catch (NoSuchFieldException e) {
        e.printstacktrace();
    }

    return orientation;
}

现在您可以在Activity中调用它:

  ExifUtils.rotateBitmap("your Image path here", "your bitmap object here");

编辑:

 public void decodeFile(String filePath) {

    // Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, o);

    // The new size we want to scale to
    final int required_SIZE = 1024;

    // Find the correct scale value. It should be the power of 2.
    int width_tmp = o.outWidth, height_tmp = o.outHeight;
    int scale = 1;
    while (true) {
        if (width_tmp < required_SIZE && height_tmp < required_SIZE)
            break;
        width_tmp /= 2;
        height_tmp /= 2;
        scale *= 2;
    }

    // Decode with inSampleSize
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    Bitmap b1 = BitmapFactory.decodeFile(filePath, o2);
    Bitmap b= ExifUtils.rotateBitmap(filePath, b1);

    // image.setimageBitmap(bitmap);
}

现在称这种方法

  decodeFile(imagepath);

谢谢!

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...