在我的应用中,用户从相机或图库中捕获图像,然后将这些图像转换为PDF
问题是我试图编辑EXIF数据,因为三星设备会旋转图像,
但是现在PDF是空白的,但是图像显示在ImageView中但仍然旋转, 有什么建议么?
这是将图像转换为PDF的代码,包括exif编辑
private void PDFCreation(){
PdfDocument document=new PdfDocument();
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);
float rotation=0;
try {
ExifInterface exifInterface=new ExifInterface(selectedPhoto);
int orientation=exifInterface.getAttributeInt(TAG_ORIENTATION, ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90: {
rotation=-90f;
break;
}
case ExifInterface.ORIENTATION_ROTATE_180: {
rotation=-180f;
break;
}
case ExifInterface.ORIENTATION_ROTATE_270: {
rotation=90f;
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
Matrix matrix = new Matrix();
matrix.postRotate(rotation);
Bitmap.createBitmap(image, 0, 0,
image.getWidth(), image.getHeight(),
matrix, true);
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";
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();
}
document.close();
}