【发布时间】: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