虽然源未旋转,但Android ImageView显示旋转的图像

时间:2014-07-24 09:05:24

标签: android image image-processing bitmap rotation

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

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

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

感谢。

2 个答案:

答案 0 :(得分:1)

首先,您需要创建一个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);

答案 1 :(得分: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();
            }
相关问题