2010-02-17 4 views
20

Androids 카메라 앱으로 사진을 찍으면 휴대 전화의 방향을 감지하고 이에 따라 사진을 저장합니다. 그래서 제가 건물의 사진을 찍으면, 제가 가로 위치 또는 세로로 폰을 들고 있든 지붕이 윗면에있을 것입니다.Intent MediaStore.ACTION_IMAGE_CAPTURE로 찍은 사진의 방향을 어떻게 찾을 수 있습니까?

그러나 제가

의도 imageCaptureIntent = 새로운 텐트 (MediaStore.ACTION_IMAGE_CAPTURE)를 사용할 때

사진을 찍기 위해 카메라 앱이 방향에 반응하지 않습니다. 휴대 전화를 세로로 (세로 방향으로) 잡고 있으면 사진이 화면의 왼쪽에있는 건물의 지붕과 함께 회전합니다.

카메라가 방향을 고려하도록 의도를 어떻게 설정할 수 있습니까?

나는 그림을 찍은 방향을 어떤 방식으로 추론하고 나중에 직접 회전시킬 수 있습니까?

또는 다른 의견을 크게 부탁드립니다.

~ 미리 감사드립니다.

+0

.. http://stackoverflow.com/a/7411824/294884 – Fattie

+0

ExifInterface 당신을 위해 작동하지 않은 경우 내 솔루션을 읽어보십시오. http://stackoverflow.com/a/24969432/513413 – Hesam

답변

26

답을 찾았습니다. 이미지의 exif에는 방향 표시가 있습니다. 이런 경우에, EXIF는 다음과 같이 안드로이드에서 볼 수 있습니다 :

ExifInterface exif = new ExifInterface("filepath"); 
exif.getAttribute(ExifInterface.TAG_ORIENTATION); 
+1

API 레벨 10 이상에서만 사용 가능 ...이 메소드를 사용하지만 더 낮은 API 버전에서 작동하는 메소드를 원합니다. –

+4

모든 오리엔테이션 항목이 표시됩니다. 비슷한 API 레벨 5 : http://developer.android.com/reference/android/media/ExifInterface.html#TAG_ORIENTATION Android 2.0 : http://developer.android.com/guide/appendix/api-levels .html 2.1/Api 레벨 7에서 성공적으로 사용하고 있습니다. –

+1

이미지 문제에 대한이 응용 프로그램에서이 기능을 구현했습니다. 정말 좋은 이미지 방향 표시 및 기능 개발 응용 프로그램에서 관리 기능 ........ –

10

읽기 EXIF에서 사용할 수, 그렇지 않으면 이미지의 폭이보다 큰 경우 확인할 수있는 빠른 수정을 위해 미디어 스토어 MediaStore

public static int getImageOrientation(Context context, String imagePath) { 
    int orientation = getOrientationFromExif(imagePath); 
    if(orientation <= 0) { 
     orientation = getOrientationFromMediaStore(context, imagePath); 
    } 

    return orientation; 
} 

private static int getOrientationFromExif(String imagePath) { 
    int orientation = -1; 
    try { 
     ExifInterface exif = new ExifInterface(imagePath); 
     int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 
       ExifInterface.ORIENTATION_NORMAL); 

     switch (exifOrientation) { 
      case ExifInterface.ORIENTATION_ROTATE_270: 
       orientation = 270; 

       break; 
      case ExifInterface.ORIENTATION_ROTATE_180: 
       orientation = 180; 

       break; 
      case ExifInterface.ORIENTATION_ROTATE_90: 
       orientation = 90; 

       break; 

      case ExifInterface.ORIENTATION_NORMAL: 
       orientation = 0; 

       break; 
      default: 
       break; 
     } 
    } catch (IOException e) { 
     Log.e(LOG_TAG, "Unable to get image exif orientation", e); 
    } 

    return orientation; 
} 

private static int getOrientationFromMediaStore(Context context, String imagePath) { 
    Uri imageUri = getImageContentUri(context, imagePath); 
    if(imageUri == null) { 
     return -1; 
    } 

    String[] projection = {MediaStore.Images.ImageColumns.ORIENTATION}; 
    Cursor cursor = context.getContentResolver().query(imageUri, projection, null, null, null); 

    int orientation = -1; 
    if (cursor != null && cursor.moveToFirst()) { 
     orientation = cursor.getInt(0); 
     cursor.close(); 
    } 

    return orientation; 
} 

private static Uri getImageContentUri(Context context, String imagePath) { 
    String[] projection = new String[] {MediaStore.Images.Media._ID}; 
    String selection = MediaStore.Images.Media.DATA + "=? "; 
    String[] selectionArgs = new String[] {imagePath}; 
    Cursor cursor = context.getContentResolver().query(IMAGE_PROVIDER_URI, projection, 
      selection, selectionArgs, null); 

    if (cursor != null && cursor.moveToFirst()) { 
     int imageId = cursor.getInt(0); 
     cursor.close(); 

     return Uri.withAppendedPath(IMAGE_PROVIDER_URI, Integer.toString(imageId)); 
    } 

    if (new File(imagePath).exists()) { 
     ContentValues values = new ContentValues(); 
     values.put(MediaStore.Images.Media.DATA, imagePath); 

     return context.getContentResolver().insert(
        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); 
    } 

    return null; 
} 
+8

IMAGE_PROVIDER_URI 상수는 무엇입니까? – anivaler

4

에서 읽을 경우 이미지의 높이. 그것은 풍경을 의미하고 초상화로 변경할 수 있습니다. 여기

private Bitmap fromGallery(final Uri selectedImageUri) { 
     try { 
      Bitmap bm = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImageUri); 

      ExifInterface exif = new ExifInterface(selectedImageUri.getPath()); 
      int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); 

      int angle = 0; 
      switch (orientation) { 
       case ExifInterface.ORIENTATION_ROTATE_90: 
        angle = 90; 
        break; 
       case ExifInterface.ORIENTATION_ROTATE_180: 
        angle = 180; 
        break; 
       case ExifInterface.ORIENTATION_ROTATE_270: 
        angle = 270; 
        break; 
       default: 
        angle = 0; 
        break; 
      } 
      Matrix mat = new Matrix(); 
      if (angle == 0 && bm.getWidth() > bm.getHeight()) 
       mat.postRotate(90); 
      else 
       mat.postRotate(angle); 

      return Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), mat, true); 

     } catch (IOException e) { 
      Log.e("", "-- Error in setting image"); 
     } catch (OutOfMemoryError oom) { 
      Log.e("", "-- OOM Error in setting image"); 
     } 
     return null; 
    } 
관련 문제