2014-06-17 2 views
2

내 라이브러리에서 이미지를 선택할 때 방향을 가져올 수 없습니다. 이미지 세부 정보로 이동하면 이미지 방향이 90 도로 설정되어 있습니다. 하지만, 내 취향이 사용 ExitInterface 0라이브러리에서 이미지를 업로드 할 때 방향을 가져올 수 없습니다.

String[] orientationColumn = { MediaStore.Images.ImageColumns.ORIENTATION }; 
Cursor cur = managedQuery(data.getData(), orientationColumn, null, null, null); 
int orientation = -1; 

if (cur != null && cur.moveToFirst()) { 
    orientation = cur.getInt(cur.getColumnIndex(orientationColumn[0])); 
} 

항상 : 두 가지 방법이 0을 반환

ExifInterface exif = new ExifInterface(data.getData().getPath()); 
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION); 

그래서 같은 라이브러리 활동에서 선택을 실행 :

protected void selectFromLibrary() { 
    Intent intent = new Intent(Intent.ACTION_PICK); 
    intent.setAction(Intent.ACTION_GET_CONTENT); 
    intent.setData(MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
    intent.setType("image/*"); 
    startActivityForResult(intent, 
      REQUEST_SELECT_IMAGE_FILE); 
} 

이이 켜져 LG G2 실행 4.4.2

+0

모바일의 exif 데이터가 exifInterface에서 인식 할 수없는 다른 형식 일 수 있습니다. 가끔씩 발생합니다. –

+0

그게 무슨 뜻입니까? 나는 운이 없니? – ono

+0

메모장에서 이미지를 열어 사람이 읽을 수있는 문장을 검색하면 의미를 알 수 있습니다. 힌트 : Exif 데이터가 xml 태그처럼 보입니다. –

답변

5

나는 몇 가지 해결책 (여기서는 Images taken with ACTION_IMAGE_CAPTURE always returns 1 for ExifInterface.TAG_ORIENTATION on some newer devices과 같은) , 그러나 그들 중의 누구도 나를 위해 일하지 않습니다. http://androidxref.com/4.0.4/xref/packages/apps/Gallery2/src/com/android/gallery3d/data/Exif.java

그래서 처음에 나는 기본 EXIF ​​방법을 사용하여 방향을 얻기 위해 노력하고 있어요 : 마지막으로 나를 구원 무엇

이 코드 조각이었다. 실패하면 짐승을 풀어 놓습니다. 내 전체 코드 :

protected Matrix getRotationMatrix(String path, String mimeType, Context ctx, Uri imgUri) 
{ 
    Matrix mtx = new Matrix(); 
    try { 

     ExifInterface exif = new ExifInterface(path); 

     if (mimeType.contains("jpeg") && exif != null) { 
      int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); 
      if (orientation != ExifInterface.ORIENTATION_UNDEFINED) { 
       switch (orientation) { 
        case ExifInterface.ORIENTATION_NORMAL: 
         break; 
        case ExifInterface.ORIENTATION_FLIP_HORIZONTAL: 
         mtx.setScale(-1, 1); 
         break; 
        case ExifInterface.ORIENTATION_ROTATE_180: 
         mtx.setRotate(180); 
         break; 
        case ExifInterface.ORIENTATION_FLIP_VERTICAL: 
         mtx.setRotate(180); 
         mtx.postScale(-1, 1); 
         break; 
        case ExifInterface.ORIENTATION_TRANSPOSE: 
         mtx.setRotate(90); 
         mtx.postScale(-1, 1); 
         break; 
        case ExifInterface.ORIENTATION_ROTATE_90: 
         mtx.setRotate(90); 
         break; 
        case ExifInterface.ORIENTATION_TRANSVERSE: 
         mtx.setRotate(-90); 
         mtx.postScale(-1, 1); 
         break; 
        case ExifInterface.ORIENTATION_ROTATE_270: 
         mtx.setRotate(-90); 
         break; 
       } 
      } 
      else 
      { 
       if (ctx != null && imgUri != null) 
       { 
        Cursor cursor = ctx.getContentResolver().query(imgUri, 
          new String[]{MediaStore.Images.ImageColumns.ORIENTATION}, 
          null, null, null); 

        try { 
         if (cursor.moveToFirst()) { 
          orientation = cursor.getInt(0); 
          if (orientation != ExifInterface.ORIENTATION_UNDEFINED) 
           mtx.postRotate(cursor.getInt(0)); 
          else { 
           // last try... 
           mtx.postRotate(Exif.getOrientation(ctx.getContentResolver().openInputStream(imgUri))); 
          } 
         } 
        } finally { 
         cursor.close(); 
        } 

       } 
      } 
     } 
    } 
    catch(Exception ex) 
    { 
     return mtx; 
    } 

    return mtx; 
} 

가 그럼 난 Bitmap.createBitmap 방법 내에서이 행렬을 사용하여 이미지를 얻을 수는 올바른 방법으로 회전.

+1

내게 감사합니다. – ytll21

+0

Exif.java 파일은 정확한 true 값을 반환합니다. 이것은 모든 samsungs와 가진 나의 문제를 해결했다. 고마워. – asozcan

관련 문제