在我的自定义相机中,我需要保存拍摄图像的方向。此代码适用于其他Android版本。但它不适用于6.0.1。将属性保存到图像文件后,得到的结果是错误的。
try {
exif = new ExifInterface(pictureFile.getAbsolutePath());
exif.setAttribute(ExifInterface.TAG_ORIENTATION, "" + orientation);
exif.saveAttributes();
} catch (IOException e) {
e.printStackTrace();
}
答案 0 :(得分:1)
尝试此操作以保存捕获图像的不同角度方向: -
Options options = new Options();
// downsizing image as it throws OutOfMemory Exception for larger
// images
options.inSampleSize = 8;
ExifInterface exif;
try {
exif = new ExifInterface(fileUri.getPath());
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION, 0);
Log.d("EXIF", "Exif: " + orientation);
Matrix matrix = new Matrix();
if (orientation == 6) {
matrix.postRotate(90);
Log.d("EXIF", "Exif: " + orientation);
} else if (orientation == 3) {
matrix.postRotate(180);
Log.d("EXIF", "Exif: " + orientation);
} else if (orientation == 8) {
matrix.postRotate(270);
Log.d("EXIF", "Exif: " + orientation);
}
myBitmap = BitmapFactory.decodeFile(path_img, options);
myBitmap = Bitmap.createBitmap(myBitmap, 0, 0,
myBitmap.getWidth(), myBitmap.getHeight(), matrix,
true);
} catch (Exception e) {
}
答案 1 :(得分:1)
由于我遇到了来自不同制造商的EXIF信息的问题和不同行为,我建议您从保存的图像URI中获取方向,然后将其保存到EXIF界面。
public static void getImageOrientationFromUri(@NonNull ContentResolver contentResolver, @NonNull Uri uri) {
if (uri.getPath() == null)
throw new NullPointerException("URI Path should not be null");
float rotationInDegrees = 0;
Cursor cursor = contentResolver.query(uri, new String[]{MediaStore.Images.ImageColumns.ORIENTATION},
null,
null,
null);
if (cursor != null && cursor.moveToFirst()) {
int col = cursor.getColumnIndex(MediaStore.Images.ImageColumns.ORIENTATION);
if (col != -1)
rotationInDegrees = cursor.getInt(col);
cursor.close();
}
// here you can save to the EXIF interface getting the apropriate value from rotationInDegrees
//If you want to display the image create the bitmap using:
//Bitmap sourceBitmap = MediaStore.Images.Media.getBitmap(contentResolver, uri);
//Matrix matrix = new Matrix();
//matrix.preRotate(rotationInDegrees);
//you can change the signature of the method to return a `Bitmap` and return
//Bitmap.createBitmap(sourceBitmap, 0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight(), matrix, false);
}
答案 2 :(得分:1)
其他解决方案是重写图像而不是操纵EXIF信息。我建议像你尝试使用正确的常量一样:
try {
exif = new ExifInterface(pictureFile.getAbsolutePath());
exif.setAttribute(ExifInterface.TAG_ORIENTATION,
Integer.toString(ExifInterface.ORIENTATION_ROTATE_90));
exif.saveAttributes();
} catch (IOException e) {
e.printStackTrace();
}
根据源代码,您需要使用以下值之一:
ExifInterface.ORIENTATION_UNDEFINED
ExifInterface.ORIENTATION_NORMAL
ExifInterface.ORIENTATION_FLIP_HORIZONTAL
ExifInterface.ORIENTATION_ROTATE_180
ExifInterface.ORIENTATION_FLIP_VERTICAL
ExifInterface.ORIENTATION_TRANSPOSE
ExifInterface.ORIENTATION_ROTATE_90
ExifInterface.ORIENTATION_TRANSVERSE
ExifInterface.ORIENTATION_ROTATE_270
答案 3 :(得分:0)
支持图书馆更新:
谷歌昨天在25.1.0发布了ExifInterface版本的支持库,其中包含大量更新,主要关注最近读取和写入图像文件的属性android版本。请查看SOURCE代码并了解此更新。
希望这有助于你