2013-12-22 5 views
2

나는 사진 갤러리를 여는 응용 프로그램을 만들고 갤러리에서 사진을 선택하여 다른 활동에 사진을 표시합니다. 내 문제는 인물 모드에서 촬영 한 사진이 표시 후 회전된다는 것입니다. 그러나 가로 모드로 촬영 한 사진은 올바르게 표시됩니다.android에서 카메라를 사용하여 이미지가 세로 모드 또는 가로 모드로 캡처되었는지 확인하는 방법은 무엇입니까?

그래서 인물 사진을 회전 할 수 있도록 안드로이드 카메라를 사용하여 이미지를 인물 모드 또는 가로 모드로 캡처하는지 확인해야합니다. 누구든지 저에게 어떻게 도와 줄 수 있습니까?

N.B .: 너비와 높이가 세로 캡처 이미지와 가로 캡쳐 이미지에서 동일합니다.

+1

당신이 ExifInterface을 확인 했나 -이 :

아래에이 기능

회전을 가져 오는 데 사용됩니다? http://stackoverflow.com/questions/11026615/captured-photo-orientation-is-changing-in-android – fasteque

+0

아니요, 저는 ... – CrazyLearner

답변

2

Matrix를 사용하여 이미지의 회전을 항상 확인하고 그에 따라 회전 할 수 있습니다. 여기>

BitmapFactory.Options bmOptions = new BitmapFactory.Options(); 
    bmOptions.inJustDecodeBounds = false; 
    bmOptions.inPurgeable = true; 

     Bitmap cameraBitmap = BitmapFactory.decodeFile(filePath);//get file path from intent when you take iamge. 
     ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
     cameraBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos); 


     ExifInterface exif = new ExifInterface(filePath); 
     float rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); 
     System.out.println(rotation); 

     float rotationInDegrees = exifToDegrees(rotation); 
     System.out.println(rotationInDegrees); 

     Matrix matrix = new Matrix(); 
     matrix.postRotate(rotationInDegrees); 

     Bitmap scaledBitmap = Bitmap.createBitmap(cameraBitmap); 
     Bitmap rotatedBitmap = Bitmap.createBitmap(cameraBitmap , 0, 0, scaledBitmap .getWidth(), scaledBitmap .getHeight(), matrix, true); 
     FileOutputStream fos=new FileOutputStream(filePath); 
     rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); 
     fos.flush(); 
     fos.close(); 

onActivityResult를 코드 엔드 -

이 코드는 onActivityResult를 간다. 여기에 설명 된대로

private static float exifToDegrees(float exifOrientation) {   
    if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) { return 90; } 
    else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) { return 180; } 
    else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) { return 270; }    
    return 0;  
}