【问题标题】:Android ImageView Displaying Rotated Images Although Source Is Not RotatedAndroid ImageView显示旋转的图像,虽然源没有旋转
【发布时间】:2014-09-15 18:12:05
【问题描述】:

我正在尝试在网格视图中显示图库图像,并在单击图像时打开图像的全屏视图。奇怪的是,当图像在画廊中显示时,它们遵循正确的旋转。但是当我只是在我的自定义图库中查询照片时,它们总是逆时针旋转 90 度。

我已经看到了一些使用 exif 接口旋转的解决方案,因为它会加载原始位图和新旋转的位图,因此需要内存来旋转。

我的问题是:为什么只是在 ImageView 中显示图库中的图像会导致图像被旋转?

谢谢。

【问题讨论】:

  • 发生这种情况是因为您选择的图像的高度和宽度与其原始位置显示不成比例
  • 好吧,我怀疑是这样的。但问题是每个图像都有不同的高度和宽度。因此,让他们在网格视图中采用他们尊重的尺寸将使网格视图看起来不规则。你有什么建议让它看起来对称并同时显示正确的旋转?

标签: android image image-processing bitmap rotation


【解决方案1】:

这样尝试可以避免OOM错误 参考Load Bitmap EffieientlyWhatsapp bitmap loading

public static int calculateInSampleSize(BitmapFactory.Options options,
                int reqWidth, int reqHeight) {
            // Raw height and width of image
            final int height = options.outHeight;
            final int width = options.outWidth;
            int inSampleSize = 1;

            if (height > reqHeight || width > reqWidth) {

                final int halfHeight = height / 2;
                final int halfWidth = width / 2;

                // Calculate the largest inSampleSize value that is a power of 2 and
                // keeps both
                // height and width larger than the requested height and width.
                while ((halfHeight / inSampleSize) > reqHeight
                        && (halfWidth / inSampleSize) > reqWidth) {
                    inSampleSize *= 2;
                }
            }
            return inSampleSize;
        }


    public static Bitmap decodeSampledBitmapFromResource(Resources res,
                int resId, int reqWidth, int reqHeight) {
            // First decode with inJustDecodeBounds=true to check dimensions
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeResource(res, resId, options);
            // Calculate inSampleSize
            options.inSampleSize = calculateInSampleSize(options, reqWidth,
                    reqHeight);
            // Decode bitmap with inSampleSize set
            options.inJustDecodeBounds = false;
            return BitmapFactory.decodeResource(res, resId, options);
        }

检查图像的旋转并正确显示它...作为

    scaledBitmap=decodeSampledBitmapFromResource(.....);
    ExifInterface exif=null;
            try {
                exif = new ExifInterface(filePath);
                int orientation = exif.getAttributeInt(
                        ExifInterface.TAG_ORIENTATION, 0);
                Log.d("EXIF", "Exif: " + orientation);
                Matrix matrix = new Matrix();
                if (orientation == 6) {
                    matrix.postRotate(90);
                    Log.d("EXIF", "Exif: " + orientation);
                } else if (orientation == 3) {
                    matrix.postRotate(180);
                    Log.d("EXIF", "Exif: " + orientation);
                } else if (orientation == 8) {
                    matrix.postRotate(270);
                    Log.d("EXIF", "Exif: " + orientation);
                }
                scaledBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0,
                        scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix,
                        true);
            } catch (IOException e) {
                e.printStackTrace();
            }

【讨论】:

  • 好吧,这看起来是一个不错的解决方案。据我所知,它不需要加载原始位图,对吗?因为如果这样做可能会给我一个 OutOfMemoryError。
  • 行:scaledBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);有错误,第一个参数不应该是原始位图吗?
  • 好的,谢谢,我会尝试并回复您。
【解决方案2】:

首先你需要创建一个ExifInterface:

ExifInterface exif = new ExifInterface(filename);

然后您可以抓取图像的方向:

orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);

这是方向值的含义:http://sylvana.net/jpegcrop/exif_orientation.html

因此,最重要的值是 3、6 和 8。例如,如果方向是 6,您可以像这样旋转图像:

 Matrix matrix = new Matrix();
matrix.postRotate(90);
rotatedBitmap = Bitmap.createBitmap(sourceBitmap, 0, 0, sourceBitmap.getWidth(),       sourceBitmap.getHeight(), matrix, true);

【讨论】:

  • 您好,感谢您的解决方案。但我试图避免加载两个位图,因为这会给我一个 OutOfMemoryError。
猜你喜欢
  • 2016-05-24
  • 2012-07-02
  • 1970-01-01
  • 1970-01-01
  • 2012-11-26
  • 2013-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多