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

来自图库的图像会自动旋转 –Android,第1张

概述在我的Android应用程序中,我正在从设备库加载图像.在那,我面临着关于图像方向的问题.当我从图库中加载大分辨率图像时,它们会自动旋转,然后在我的视图中显示.我尝试了有关此问题的各种解决方案但无法得到正确的解决方案我提到了getOrientation()和this链接.我已经尝试了两种解决方

在我的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全部内容,希望文章能够帮你解决来自图库的图像会自动旋转 – Android所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1098224.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存