2015-10-29 74 views
0

我從相機捕獲圖像並將圖像顯示到imageview。它會顯示完美,但在某些設備中自動旋轉90度。我搜索了很多,但無法得到適當的解決方案。請告訴我它的解決方案。提前致謝。如何解決在android中自動旋轉90度的圖像捕捉?

+0

的可能的複製([爲什麼圖像捕捉通過相機獲取的意圖在機器人某些設備旋轉] http://stackoverflow.com/questions/14066038/why-image-captured-using-camera-intent-gets - 在Android上旋轉的一些設備) – Dhina

+0

因爲Exif取向與捕獲的相機圖像.. – Meenal

回答

0

試試這個代碼

public Bitmap adjustImageOrientation(Bitmap image, File f) { 
    int rotate = 0; 
    try { 

     ExifInterface exif = new ExifInterface(f.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; 

     case ExifInterface.ORIENTATION_NORMAL: 
      rotate = 0; 
      break; 

     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    if (rotate != 0) { 
     Matrix matrix = new Matrix(); 
     matrix.postRotate(rotate); 

     image = Bitmap.createBitmap(image, 0, 0, image.getWidth(), 
       image.getHeight(), matrix, true); 
    } 


    return image; 
} 
+0

它會爲棉花糖工作嗎? – Pankaj

+0

是的,它會..你試過了嗎? – Meenal

+0

不,我還沒試過。問你。 – Pankaj

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