使用ExifInterface设置图像方向

时间:2013-11-03 14:15:53

标签: android orientation exif

setRotation method中的

Camera.Parameters无法在所有设备中使用。有人建议手动更改EXIF信息以解决问题。您能否举例说明如何使用exif设置ExifInterface信息,以便将图像方向设置为肖像?

private int savePicture(byte[] data)
{
       File pictureFile = getOutputMediaFile();
       if (pictureFile == null)
           return FILE_CREATION_ERROR;

       try {
           FileOutputStream fos = new FileOutputStream(pictureFile);
           fos.write(data);
           fos.close();
       } catch (FileNotFoundException e) {
           return FILE_NOT_FOUND;
       } catch (IOException e) {
           return ACCESSING_FILE_ERROR;
       }

   return OKAY;
}

我已尝试过这个:

    try {
        ExifInterface exifi = new ExifInterface(pictureFile.getAbsolutePath());
        exifi.setAttribute(ExifInterface.TAG_ORIENTATION, String.valueOf(ExifInterface.ORIENTATION_ROTATE_90));
        exifi.saveAttributes();
    } catch (IOException e) {
        Log.e(TAG, "Exif error");
    }

但是当我从android画廊想象出图片时没有任何改变。

3 个答案:

答案 0 :(得分:9)

对于那些真正想要写出这些EXIF信息的人,这里有一些代码:

ExifInterface exifInterface = new ExifInterface(someFile.getPath());
exifInterface.setAttribute(ExifInterface.TAG_ORIENTATION,
                           String.valueOf(orientation));
exifInterface.saveAttributes();

orientation是标准方向之一,即ExifInterface.ORIENTATION_ROTATE_{90,180,270}

答案 1 :(得分:3)

如果方向已保存到文件中但未显示在库中,则可能是因为方向已缓存在MediaStore中。因此,您还需要尝试更新此信息。

以下是剪辑的代码(未经测试)

/**
 * @param fileUri the media store file uri
 * @param orientation in degrees 0, 90, 180, 270
 * @param context
 * @return
 */
public boolean setOrientation(Uri fileUri, int orientation, Context context) {
    ContentValues values = new ContentValues();
    values.put(MediaStore.Images.Media.ORIENTATION, orientation);
    int rowsUpdated = context.getContentResolver().update(fileUri, values, null, null);
    return rowsUpdated > 0;
}

/**
 * Get content uri for the file path
 * 
 * @param path
 * @param context
 * @return
 */
public Uri getContentUriForFilePath(String path, Context context) {
    String[] projection = {
        MediaStore.Images.Media._ID
    };
    Cursor cursor = context.getContentResolver().query(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection,
            MediaStore.Images.Media.DATA + " = ?", new String[] {
                path
            }, null);
    Uri result = null;
    if (cursor != null) {
        try {
            if (cursor.moveToNext()) {
                long mediaId = cursor.getLong(0);
                result = ContentUris.withAppendedId(
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, mediaId);
            }
        } finally {
            cursor.close();
        }
    }
    return result;
}

答案 2 :(得分:1)

好吧,我遇到了这样的问题,并尝试了你正在处理的解决方案并最终使用了Matrix函数。

无论我拍摄的图像是什么,都是风景,所以在应用程序中我只应用了以下代码,并且效果很好: -

Matrix matrix = new Matrix();
//set image rotation value to 90 degrees in matrix.
matrix.postRotate(90);
//supply the original width and height, if you don't want to change the height and width of //bitmap.
Bitmap bMapRotate = Bitmap.createBitmap(bMap, 0, 0, newWidth, newHeight, matrix, true);
相关问题