微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

来自图库的图像会自动旋转 – 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);

谢谢!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐