我需要从 SD 卡获取图像,创建、旋转并保存更改后的图像。
我尝试使用这段代码
Bitmap original = BitmapFactory.decodeFile(file.getAbsolutePath());
ExifInterface originalExif = new ExifInterface(file.getAbsolutePath());
int orientation = originalExif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
Matrix matrix = new Matrix();
int rotate = 90;
if(orientation == ExifInterface.ORIENTATION_ROTATE_90){
rotate = 180;
}else if(orientation == ExifInterface.ORIENTATION_ROTATE_180){
rotate = 270;
}else if(orientation == ExifInterface.ORIENTATION_ROTATE_270){
rotate = 0;
}
matrix.postRotate(rotate);
Bitmap bitmap = Bitmap.createBitmap(original, 0, 0, original.getWidth(), original.getHeight(), matrix, true);
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
original.recycle();
bitmap.recycle();
}
ExifInterface newExif = new ExifInterface(file.getAbsolutePath());
newExif.setAttribute(ExifInterface.TAG_ORIENTATION, String.valueOf(ExifInterface.ORIENTATION_ROTATE_90));
newExif.saveAttributes();
但我无法在 ExifInterface 中保存更改。这只是清除所有标签。
仅限 saveAttributes 方法 将标签数据保存到 JPEG 文件中。
检查此链接
http://developer.android.com/reference/android/media/ExifInterface.html#saveAttributes() http://developer.android.com/reference/android/media/ExifInterface.html#saveAttributes()
所以如果你改变你的代码
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
to this
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
它会保存你的 exif 标签数据
希望这有帮助
如有任何其他问题请告诉我
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)