2012-05-10 115 views
9

以垂直格式拍攝的照片以橫向格式保存,反之亦然。我使用Android攝像頭使用此意圖Android相機方向ISsue

Intent captureImage = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
captureImage.putExtra(MediaStore.EXTRA_OUTPUT, imageFileUri);    
startActivityForResult(captureImage, CAMERA_PIC_REQUEST); 

onActivityResult()我只是保存圖像的URL到我的數據庫,並在列表視圖顯示它。 但有東方的變化。如果我從圖庫中選擇圖片並保存,也會發生同樣的情況。

我想要拍攝照片的方向。我不想改變它。有沒有人有解決這個問題。

+0

這個答案可以幫助你[攝像機方向] [1] [1]:http://stackoverflow.com/questions/3841122/android-camera-preview-is-sideways/5110406#51104 06 –

回答

16

某些設備在拍攝後不會旋轉圖像,而只是將其方向信息寫入Exif數據中。因此,在使用拍攝照片之前,您應該調用如下方法:

private int resolveBitmapOrientation(File bitmapFile) throws IOException { 
     ExifInterface exif = null; 
     exif = new ExifInterface(bitmapFile.getAbsolutePath()); 

     return exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); 
    } 

檢查其方向。然後申請:

private Bitmap applyOrientation(Bitmap bitmap, int orientation) { 
     int rotate = 0; 
     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; 
      default: 
       return bitmap; 
     } 

     int w = bitmap.getWidth(); 
     int h = bitmap.getHeight(); 
     Matrix mtx = new Matrix(); 
     mtx.postRotate(rotate); 
     return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true); 
    } 

並在你的列表視圖中使用這個新的位圖。或者,拍攝照片後立即調用此方法更好,並用新的旋轉方法覆蓋它。

在情況下,如果您收到的位圖數據作爲烏里下面的方法可以用來檢索其文件路徑:

public static String getPathFromURI(Context context, Uri contentUri) { 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && 
      DocumentsContract.isDocumentUri(context, contentUri)) { 
     return getPathForV19AndUp(context, contentUri); 
    } else { 
     return getPathForPreV19(context, contentUri); 
    } 
} 

private static String getPathForPreV19(Context context, Uri contentUri) { 
    String[] projection = { MediaStore.Images.Media.DATA }; 
    Cursor cursor = context.getContentResolver().query(contentUri, projection, null, null, null); 
    if (cursor != null && cursor.moveToFirst()) { 
     try { 
      int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
      return cursor.getString(columnIndex); 
     } finally { 
      cursor.close(); 
     } 
    } 

    return null; 
} 

@TargetApi(Build.VERSION_CODES.KITKAT) 
private static String getPathForV19AndUp(Context context, Uri contentUri) { 
    String documentId = DocumentsContract.getDocumentId(contentUri); 
    String id = documentId.split(":")[1]; 

    String[] column = { MediaStore.Images.Media.DATA }; 
    String sel = MediaStore.Images.Media._ID + "=?"; 
    Cursor cursor = context.getContentResolver(). 
      query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
        column, sel, new String[]{ id }, null); 

    if (cursor != null) { 
     try { 
      int columnIndex = cursor.getColumnIndex(column[0]); 
      if (cursor.moveToFirst()) { 
       return cursor.getString(columnIndex); 
      } 
     } finally { 
      cursor.close(); 
     } 
    } 

    return null; 
} 
+0

嗨myx,這個int方向是什麼?它的價值是什麼?我正在獲得價值6.這是正確的嗎? – sumone

+1

它是來自android.media.ExifInterface的方向常量值。 6 == ORIENTATION_ROTATE_90。我編輯了我的答案 – birdy

+0

太棒了!你救了我下午的小鳥。 – garlicman

1

您還可以通過這種方式如下:

static Uri image_uri; 
static Bitmap taken_image=null; 

       image_uri=fileUri; // file where image has been saved 

      taken_image=BitmapFactory.decodeFile(image_uri.getPath()); 
      try 
      { 
       ExifInterface exif = new ExifInterface(image_uri.getPath()); 
//Since API Level 5 
       int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); 


       switch(orientation) { 
        case ExifInterface.ORIENTATION_ROTATE_90: 
         taken_image=decodeScaledBitmapFromSdCard(image_uri.getPath(), 200, 200); 
         RotateBitmap(taken_image, 90); 
         break; 
        case ExifInterface.ORIENTATION_ROTATE_180: 
         taken_image=decodeScaledBitmapFromSdCard(image_uri.getPath(), 200, 200); 
         RotateBitmap(taken_image, 180); 

         break; 
        case ExifInterface.ORIENTATION_ROTATE_270: 
         taken_image=decodeScaledBitmapFromSdCard(image_uri.getPath(), 200, 200); 
         RotateBitmap(taken_image, 270); 

         break; 
        case ExifInterface.ORIENTATION_NORMAL: 
         taken_image=decodeScaledBitmapFromSdCard(image_uri.getPath(), 200, 200); 
         RotateBitmap(taken_image, 0); 

         break; 
       } 

      } 
      catch (OutOfMemoryError e) 
      { 
       Toast.makeText(getActivity(),e+"\"memory exception occured\"",Toast.LENGTH_LONG).show(); 


      } 



public Bitmap RotateBitmap(Bitmap source, float angle) { 
    Matrix matrix = new Matrix(); 
    matrix.postRotate(angle); 

    round_Image = source; 
    round_Image = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true); 


    return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true); 
}