在Android上,我試圖將從相機拍攝的圖像位圖裁剪爲固定的寬度/寬度,但是當我創建裁剪後的圖像時,結果始終會旋轉(即使沒有定義矩陣),請參閱http://i.imgur.com/MppWH69l.png 原始圖像和http://i.imgur.com/C4WVtwZl.png作爲裁剪後的圖像,該圖像被旋轉並顯示不正確。Android位圖從畫布或從BitmapFactory.createBitmap(src,x,y,w,h)繪製不正確
爲什麼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;
}
檢查了這一點http://stackoverflow.com/questions/3647993/android-bitmaps-loaded-from-gallery-are-rotate-in-imageview – JRowan 2014-09-19 03:48:15