2014-09-19 27 views
0

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

檢查了這一點http://stackoverflow.com/questions/3647993/android-bitmaps-loaded-from-gallery-are-rotate-in-imageview – JRowan 2014-09-19 03:48:15

回答

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; 
    } 
} 
+0

我不知道爲什麼,但ExifOrientation總是返回0未定義,所以沒有應用旋轉,我用BitmapFactory解碼得到了一個更簡單的解決方案。 Options.inJustDecodeOptions來檢查f或outWidth> outHeight,然後是風景和需要的旋轉,更簡單的方法,但它的工作,無論如何@Thirumoorthy Moorthy的答案指出我在正確的方式,所以我接受你的答案 – DarkRockman 2014-10-03 04:50:24