使用ExifInterface设置图像方向 [英] Set image orientation using ExifInterface
问题描述
在
并不适用于所有设备的正常工作。有人建议手动更改 Camera.Parameters setRotation方法可以
EXIF
信息来解决问题。你能给我如何设置 EXIF
信息与 ExifInterface
以这样的方式来设置图像一个简单的例子方向为纵向?
setRotation method
in Camera.Parameters
does not work in all devices. Somebody suggests to manually change the EXIF
information to solve the problem. Can you give me a short example on how to set the exif
information with ExifInterface
in such a way to set the image orientation as portrait?
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;
}
我已经试过这一点:
I've tried with this:
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画廊没有什么变化。
but nothing change when I visualize the pictures from the android gallery.
推荐答案
如果方向是保存到文件中,但不会出现在画廊也可能是定向的,因为缓存在MediaStore。所以,你需要尝试也有更新此信息。
If orientation is saved into file but doesn't appear in the gallery it may be because of orientation is cached in MediaStore. So you need to try to update this information there also.
下面是code剪断(未测试)
Here is the code snipped (untested)
/**
* @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;
}
这篇关于使用ExifInterface设置图像方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!