Android位图从canvas或BitmapFactory.createBitmap(src,x,y,w,h)绘制错误

时间:2014-09-19 03:34:48

标签: android image canvas bitmap

在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;
}

1 个答案:

答案 0 :(得分: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;
    }
}