【问题标题】:Android bitmap drawed incorrectly either from canvas or from BitmapFactory.createBitmap(src,x,y,w,h)从画布或 BitmapFactory.createBitmap(src,x,y,w,h) 错误绘制的 Android 位图
【发布时间】:2014-11-13 13:53:09
【问题描述】:

在 Android 上,我正在尝试将从相机拍摄的图像位图裁剪为小于拍摄的原始照片的固定宽度/宽度,但是当我创建裁剪后的图像时,结果总是会旋转(即使没有矩阵已定义)见http://i.imgur.com/MppWH69l.png 为原始图像,http://i.imgur.com/C4WVtwZl.png 为裁剪后的图像,该图像正在旋转且显示不正确。

createBitmap 方法为什么会在目标位图中旋转要绘制的位图。

相关代码如下

try {       int dstWidth = params[0].intValue();
            int dstHeight = params[1].intValue();
            int targetSize = Math.min(dstWidth, dstHeight);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(getActivity().getContentResolver().openInputStream(mPhotoUri), null, options); 
//calculate min inSampleSize for avoiding memory problems
            options.inSampleSize = calculateImageInSampleSize(options, targetSize, targetSize);
            options.inJustDecodeBounds = false;
            Bitmap originalPhotoBitmap = BitmapFactory.decodeStream(getActivity().getContentResolver()
                    .openInputStream(mPhotoUri), null, options);
            mOriginalBitmap = Bitmap.createBitmap(originalPhotoBitmap, originalPhotoBitmap.getWidth() - targetSize,
                                                        originalPhotoBitmap.getHeight() - targetSize, targetSize, targetSize);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

//codefor inSampleSize calculation from http://developer.android.com/training/displaying-bitmaps/load-bitmap.html#load-bitmap

private int calculateImageInSampleSize(BitmapFactory.Options originalSize, int targetWidth, int targetHeight) {
    final int width = originalSize.outWidth;
    final int height = originalSize.outHeight;
    int inSampleSize = 1;

    if (height > targetHeight || width > targetWidth) {
        final int halfHeight = height /2;
        final int halfWidth = width /2;

        while ((halfHeight /inSampleSize) > targetHeight &&
                (halfWidth / inSampleSize) > targetWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}

【问题讨论】:

标签: android image canvas bitmap


【解决方案1】:

每个设备在相机中遵循不同的方向,所以你按照这个例子解决了你的问题

private Bitmap imageRotating(String filePath){
    try{
        ExifInterface exif = new ExifInterface(filePath);
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);

        return rotateBitmap(decodeSampledBitmapFromResource(filePath,150,224), orientation);
    }
    catch(Exception e){
        e.printStackTrace();
        return null;
    }
}

public static Bitmap rotateBitmap(Bitmap bitmap, int orientation) {

    try{
        Matrix matrix = new Matrix();
        switch (orientation) {
            case ExifInterface.ORIENTATION_NORMAL:
                return bitmap;
            case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
                matrix.setScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                matrix.setRotate(180);
                break;
            case ExifInterface.ORIENTATION_FLIP_VERTICAL:
                matrix.setRotate(180);
                matrix.postScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_TRANSPOSE:
                matrix.setRotate(90);
                matrix.postScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_ROTATE_90:
                matrix.setRotate(90);
                break;
            case ExifInterface.ORIENTATION_TRANSVERSE:
                matrix.setRotate(-90);
                matrix.postScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                matrix.setRotate(-90);
                break;
            default:
                return bitmap;
        }
        try {
            Bitmap bmRotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
            bitmap.recycle();
            return bmRotated;
        }
        catch (OutOfMemoryError e) {
            e.printStackTrace();
            return null;
        }
    }
    catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

【讨论】:

  • 我不知道为什么,但是 ExifOrientation 总是返回 0 Undefined 所以没有应用旋转,我通过使用 BitmapFactory.Options.inJustDecodeOptions 解码来检查 outWidth>outHeight 得到了一个更简单的解决方案,然后是景观和需要旋转,更简单的方法,但它的工作,无论如何@Thirumoorthy Moorthy 的回答指出我正确的方式,所以我接受你的回答
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-18
  • 2012-11-30
  • 2014-04-08
  • 1970-01-01
  • 2020-04-01
  • 2010-10-14
  • 2015-03-24
相关资源
最近更新 更多