如何编辑我的图像 EXIF 数据以进行图像旋转
How can I edit My image EXIF data for image rotation
在我的应用程序中,用户从相机或画廊获取图像,然后将这些图像转换为 PDF 现在我的问题出在某些设备上,相机捕获的图像旋转了 90 度,这些设备是一些三星设备特别是 S7,
我知道我需要编辑图像的 EXIF 数据,但我是 android 的新手,整个 EXIF 数据的事情完全超出了我的理解任何帮助都会得到帮助
这是我调用所选图像并将其转换为 PDF 的地方
PdfDocument document=new PdfDocument();
// crate a page description
PdfDocument.PageInfo pageInfo;
PdfDocument.Page page;
Canvas canvas;
int i;
位图图片;
for (i=0; i < list.size(); i++) {
pageInfo=new PdfDocument.PageInfo.Builder(992, 1432, 1).create();
page=document.startPage(pageInfo);
canvas=page.getCanvas();
image=BitmapFactory.decodeFile(list.get(i));
image = Bitmap.createScaledBitmap(image, 980, 1420, true);
image.setDensity(DisplayMetrics.DENSITY_300);
canvas.setDensity(DisplayMetrics.DENSITY_300);
canvas.drawBitmap(image, 0, 0, null);
document.finishPage(page);
}
@SuppressWarnings("deprecation") String directory_path=Environment.getExternalStorageDirectory().getPath() + "/mypdf/";
File file=new File(directory_path);
if (!file.exists()) {
//noinspection ResultOfMethodCallIgnored
file.mkdirs();
}
@SuppressLint("SimpleDateFormat") String timeStamp=(new SimpleDateFormat("yyyyMMdd_HHmmss")).format(new Date());
String targetPdf=directory_path + timeStamp + ".pdf";
File filePath=new File(targetPdf);
try {
document.writeTo(new FileOutputStream(filePath));
} catch (IOException e) {
Log.e("main", "error " + e.toString());
Toasty.error(this, "Error making PDF" + e.toString(), Toast.LENGTH_LONG).show();
}
// close the document
document.close();
将此添加到您的构建中 gradle:
implementation 'androidx.exifinterface:exifinterface:1.0.0'
获取旋转数据:
Bitmap bitmap = BitmapFactory.decodeFile(path);
ExifInterface exif;
try {
exif = new ExifInterface(path);
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
Bitmap bmRotated = MyUtility.rotateBitmap(bitmap, orientation);
}
} catch (IOException e) {
e.printStackTrace();
}
rotateBitmap() 方法:
public static Bitmap rotateBitmap(Bitmap bitmap, int orientation) {
Matrix matrix = new Matrix();
switch (orientation) {
case ExifInterface.ORIENTATION_NORMAL:
mLog.i(TAG,"normal");
return bitmap;
case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
mLog.i(TAG,"ORIENTATION_FLIP_HORIZONTAL");
matrix.setScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
mLog.i(TAG,"ORIENTATION_ROTATE_180");
matrix.setRotate(180);
break;
case ExifInterface.ORIENTATION_FLIP_VERTICAL:
mLog.i(TAG,"ORIENTATION_FLIP_VERTICAL");
matrix.setRotate(180);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_TRANSPOSE:
matrix.setRotate(90);
matrix.postScale(-1, 1);
mLog.i(TAG,"ORIENTATION_TRANSPOSE");
break;
case ExifInterface.ORIENTATION_ROTATE_90:
matrix.setRotate(90);
mLog.i(TAG,"ORIENTATION_ROTATE_90");
break;
case ExifInterface.ORIENTATION_TRANSVERSE:
mLog.i(TAG,"ORIENTATION_TRANSVERSE");
matrix.setRotate(-90);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
matrix.setRotate(-90);
mLog.i(TAG,"ORIENTATION_ROTATE_270");
break;
default:
mLog.i(TAG,"UNKNOWN");
return bitmap;
}
try {
Bitmap bmRotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
bitmap.recycle();
return bmRotated;
} catch (OutOfMemoryError e) {
e.printStackTrace();
return null;
}
}
虽然我在一些三星设备上遇到过问题,但效果很好。试一试。
在我的应用程序中,用户从相机或画廊获取图像,然后将这些图像转换为 PDF 现在我的问题出在某些设备上,相机捕获的图像旋转了 90 度,这些设备是一些三星设备特别是 S7,
我知道我需要编辑图像的 EXIF 数据,但我是 android 的新手,整个 EXIF 数据的事情完全超出了我的理解任何帮助都会得到帮助
这是我调用所选图像并将其转换为 PDF 的地方
PdfDocument document=new PdfDocument();
// crate a page description
PdfDocument.PageInfo pageInfo;
PdfDocument.Page page;
Canvas canvas;
int i;
位图图片;
for (i=0; i < list.size(); i++) {
pageInfo=new PdfDocument.PageInfo.Builder(992, 1432, 1).create();
page=document.startPage(pageInfo);
canvas=page.getCanvas();
image=BitmapFactory.decodeFile(list.get(i));
image = Bitmap.createScaledBitmap(image, 980, 1420, true);
image.setDensity(DisplayMetrics.DENSITY_300);
canvas.setDensity(DisplayMetrics.DENSITY_300);
canvas.drawBitmap(image, 0, 0, null);
document.finishPage(page);
}
@SuppressWarnings("deprecation") String directory_path=Environment.getExternalStorageDirectory().getPath() + "/mypdf/";
File file=new File(directory_path);
if (!file.exists()) {
//noinspection ResultOfMethodCallIgnored
file.mkdirs();
}
@SuppressLint("SimpleDateFormat") String timeStamp=(new SimpleDateFormat("yyyyMMdd_HHmmss")).format(new Date());
String targetPdf=directory_path + timeStamp + ".pdf";
File filePath=new File(targetPdf);
try {
document.writeTo(new FileOutputStream(filePath));
} catch (IOException e) {
Log.e("main", "error " + e.toString());
Toasty.error(this, "Error making PDF" + e.toString(), Toast.LENGTH_LONG).show();
}
// close the document
document.close();
将此添加到您的构建中 gradle:
implementation 'androidx.exifinterface:exifinterface:1.0.0'
获取旋转数据:
Bitmap bitmap = BitmapFactory.decodeFile(path);
ExifInterface exif;
try {
exif = new ExifInterface(path);
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
Bitmap bmRotated = MyUtility.rotateBitmap(bitmap, orientation);
}
} catch (IOException e) {
e.printStackTrace();
}
rotateBitmap() 方法:
public static Bitmap rotateBitmap(Bitmap bitmap, int orientation) {
Matrix matrix = new Matrix();
switch (orientation) {
case ExifInterface.ORIENTATION_NORMAL:
mLog.i(TAG,"normal");
return bitmap;
case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
mLog.i(TAG,"ORIENTATION_FLIP_HORIZONTAL");
matrix.setScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
mLog.i(TAG,"ORIENTATION_ROTATE_180");
matrix.setRotate(180);
break;
case ExifInterface.ORIENTATION_FLIP_VERTICAL:
mLog.i(TAG,"ORIENTATION_FLIP_VERTICAL");
matrix.setRotate(180);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_TRANSPOSE:
matrix.setRotate(90);
matrix.postScale(-1, 1);
mLog.i(TAG,"ORIENTATION_TRANSPOSE");
break;
case ExifInterface.ORIENTATION_ROTATE_90:
matrix.setRotate(90);
mLog.i(TAG,"ORIENTATION_ROTATE_90");
break;
case ExifInterface.ORIENTATION_TRANSVERSE:
mLog.i(TAG,"ORIENTATION_TRANSVERSE");
matrix.setRotate(-90);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
matrix.setRotate(-90);
mLog.i(TAG,"ORIENTATION_ROTATE_270");
break;
default:
mLog.i(TAG,"UNKNOWN");
return bitmap;
}
try {
Bitmap bmRotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
bitmap.recycle();
return bmRotated;
} catch (OutOfMemoryError e) {
e.printStackTrace();
return null;
}
}
虽然我在一些三星设备上遇到过问题,但效果很好。试一试。