2012-01-10 79 views

回答

12

您需要使用嵌入在照片中的EXIF标签:

private int getExifOrientation() { 
    ExifInterface exif; 
    int orientation = 0; 
    try { 
    exif = new ExifInterface(mImagePath); 
    orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1); 
    } catch (IOException e) { 
    e.printStackTrace(); 
    } 
    Log.d(TAG, "got orientation " + orientation); 
    return orientation; 
} 

然而,返回的实际EXIF值几分怪异。它允许各种旋转和镜像。我发现的最佳参考是here。一般情况下,你的方位后,你会希望通过查找功能来运行它,以获得旋转度:

private int getBitmapRotation() { 
    int rotation = 0; 
    switch (getExifOrientation()) { 
    case ExifInterface.ORIENTATION_ROTATE_180: 
     rotation = 180; 
     break; 
    case ExifInterface.ORIENTATION_ROTATE_90: 
     rotation = 90; 
     break; 
    case ExifInterface.ORIENTATION_ROTATE_270: 
     rotation = 270; 
     break; 
    } 

    return rotation; 
} 
+2

我得到错误“01-11 00:51:37.687:ERROR/JHEAD(7055):无法在该行打开'/file:/mnt/sdcard/.temp/photo1151619024.jpg'”exif = new ExifInterface (mImagePath);“ – StoneHeart 2012-01-10 17:52:22

+0

我确定该文件仍然存在,并且我已经在清单 – StoneHeart 2012-01-10 17:53:40

+2

/file中设置了权限android.permission.WRITE_EXTERNAL_STORAGE:/看起来不像路径的适当部分...它可能只是/ mnt/sdcard/... – elijah 2012-01-10 18:05:51

1

您可以根据照片的高度和宽度确定方向吗?如果它比它高,那就是风景。如果它比它宽,它是肖像。

如果它是方形的,那么你将不得不读取EXIF数据,并希望它为这些图像设置。 EXIF数据应该给定方位数据。

+0

没有,它们的宽度和高度相同 – StoneHeart 2012-01-10 17:45:51

+0

尺寸不够大 - 它可能是风景但是是倒置的,这取决于我是将手机向左还是向右转到拍照。 – 2012-03-11 20:42:04

7

我正在寻找一个类似的解决我的问题。我正在从相机或画廊拍摄照片,并将其转换为位图以用于我的应用程序。问题是在PORTRAIT拍摄的照片正在旋转-90度。

正在搜索答案我发现这个帖子和https://stackoverflow.com/a/11081918/3062284非常相似。我实际上使用了后者。但是,像@StoneHeart一样,我正在读取图像路径时发生错误。

我使用这个代码在我的onActivityResult():

case CAMERA_REQUEST_CODE: 
if (resultCode == RESULT_OK); 
uriImage = data.getData(); 
ExifInterface exif = new ExifInterface(uriImage.getPath()); 

由于这个错误不是在以前的评论,我想我会分享我发现使用https://stackoverflow.com/a/10564727/3062284

我需要真正的解决方案解决从文章中使用此方法的文件的路径。

 private String getRealPathFromURI(Uri contentUri) { 
     String[] proj = { MediaStore.Images.Media.DATA }; 
     CursorLoader loader = new CursorLoader(this, contentUri, proj, null, null, null); 
     Cursor cursor = loader.loadInBackground(); 
     int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
     cursor.moveToFirst(); 
     return cursor.getString(column_index); 
    } 

和编辑我的代码如下:

case CAMERA_REQUEST_CODE: 
if (resultCode == RESULT_OK); 
uriImage = data.getData(); 
String imagePath = getRealPathFromURI(uriImage); 
ExifInterface exif = new ExifInterface(imagePath); 

和固定的“jhead的不能打开文件”的错误我得到的。

+0

繁荣,你刚刚结束了我的2小时问题! – 2015-05-10 20:05:01

+0

JHead问题的最佳解决方案 – rookieDeveloper 2017-02-03 09:54:10