2012-05-10 8 views
9

縦書きで撮影した画像は横長で保存し、逆も同様です。私はこの目的に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を保存し、リストビューでそれを表示していますを使用してAndroidのカメラを使用しています。 が変更されています。ギャラリーからイメージを選択して保存すると同じことが起こります。

私は写真が撮影された方向を欲しいです。私はそれを変更したくない。誰もがこれにsolutinを持っていますか?

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