2015-11-05 99 views
0

在某些設備上,CaptureSession的圖片旋轉爲橫向即可。使用CameraCaptureSession捕獲Android圖片將圖片旋轉到風景

我的問題是,我會檢測,如果圖片保存在橫向,並將其旋轉並保存。因爲現在用戶因爲某些模型而旋轉了輪廓圖像。

現在我已經嘗試了許多解決方案,包括

getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); 

總是返回0。

我就在想,如果在JPEG文件Exif標籤中由於某種原因沒有設置,但我沒有找到它的任何事情。

到目前爲止效果最好的解決方案就是這個。這只是檢查高度和寬度。這不總是工作。許多設備在旋轉圖片時遇到問題。

public static Bitmap rotate90ifneeded(Bitmap bitmap, int width, int height) { 
    Bitmap bitmap2 = bitmap; 
    if(bitmap.getHeight() < bitmap.getWidth()) { 
     Matrix matrix = new Matrix(); 
     matrix.postRotate(90); 
     bitmap2 = Bitmap.createBitmap(bitmap , 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); 
    } 
    return bitmap2; 
} 

回答

0

使用此方法確定圖像旋轉。

public int getCameraPhotoOrientation(Uri imageUri, String imagePath) { 
     int rotate = 0; 
     try { 
      _context.getContentResolver().notifyChange(imageUri, null); 
      File imageFile = new File(imagePath); 
      ExifInterface exif = new ExifInterface(
        imageFile.getAbsolutePath()); 
      int orientation = exif.getAttributeInt(
        ExifInterface.TAG_ORIENTATION, 
        ExifInterface.ORIENTATION_NORMAL); 

      switch (orientation) { 
       case ExifInterface.ORIENTATION_ROTATE_270: 
        rotate = 270; 
        break; 
       case ExifInterface.ORIENTATION_ROTATE_180: 
        rotate = 180; 
        break; 
       case ExifInterface.ORIENTATION_ROTATE_90: 
        rotate = 90; 
        break; 
      } 


     } catch (Exception e) { 
     } 
     return rotate; 
    } 

如果該方法不會返回零旋轉圖像。

int degree=getCameraPhotoOrientation(Uri.fromFile(tempFile), fPath); 
if (degree!= 0) { 
     bitmap = tools.rotateOrientationCall(bitmap, degree); 
} 

旋轉你的位圖。

public Bitmap rotateOrientationCall(Bitmap src, float degree) { 
     Matrix matrix = new Matrix(); 
     matrix.postRotate(degree); 
     return Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true); 
    } 
+0

如果度數爲0,我該怎麼辦?如果度數爲0,我是否應該組合旋轉90° –

+0

如果它爲零,則使用相同的文件而不旋轉。 –

+0

有一個android bugg我認爲在5.0+,這使得它總是返回0.找不到任何修復。 –