gpt4 book ai didi

java - 如何将android文件夹中的多个图像转换为单个PDF?

转载 作者:行者123 更新时间:2023-12-02 09:39:17 25 4
gpt4 key购买 nike

我想让 Android 应用程序将单个文件夹中的多个图像合并到单个 pdf 文件中。

例如:

文件夹名称:

-图像

    - 1.jpg
- 2.jpg
- 3.jpg
- 4.jpg
- 5.jpg

名为images的文件夹中有5张图片

如何将这些图像制作为 pdf 格式?

如果有人有可能的解决方案,请评论答案:)

最佳答案

4.4版本后尝试一下,就可以了。

  private void createPDF() {
final File file = new File(uploadFolder, "AnswerSheet_" + queId + ".pdf");

final ProgressDialog dialog = ProgressDialog.show(this, "", "Generating PDF...");
dialog.show();
new Thread(() -> {
Bitmap bitmap;
PdfDocument document = new PdfDocument();
// int height = 842;
//int width = 595;
int height = 1010;
int width = 714;
int reqH, reqW;
reqW = width;

for (int i = 0; i < array.size(); i++) {
// bitmap = BitmapFactory.decodeFile(array.get(i));
bitmap = Utility.getCompressedBitmap(array.get(i), height, width);


reqH = width * bitmap.getHeight() / bitmap.getWidth();
Log.e("reqH", "=" + reqH);
if (reqH < height) {
// bitmap = Bitmap.createScaledBitmap(bitmap, reqW, reqH, true);
} else {
reqH = height;
reqW = height * bitmap.getWidth() / bitmap.getHeight();
Log.e("reqW", "=" + reqW);
// bitmap = Bitmap.createScaledBitmap(bitmap, reqW, reqH, true);
}
// Compress image by decreasing quality
// ByteArrayOutputStream out = new ByteArrayOutputStream();
// bitmap.compress(Bitmap.CompressFormat.WEBP, 50, out);
// bitmap = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));
//bitmap = bitmap.copy(Bitmap.Config.RGB_565, false);
//Create an A4 sized page 595 x 842 in Postscript points.
//PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(595, 842, 1).create();
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(reqW, reqH, 1).create();
PdfDocument.Page page = document.startPage(pageInfo);
Canvas canvas = page.getCanvas();

Log.e("PDF", "pdf = " + bitmap.getWidth() + "x" + bitmap.getHeight());
canvas.drawBitmap(bitmap, 0, 0, null);

document.finishPage(page);
}

FileOutputStream fos;
try {
fos = new FileOutputStream(file);
document.writeTo(fos);
document.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}

runOnUiThread(() -> {
dismissDialog(dialog);

});
}).start();
}

关于java - 如何将android文件夹中的多个图像转换为单个PDF?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59551957/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com