Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Android PDFDocument.Page中压缩图像_Java_Android_Pdf_Bitmap_Pdf Generation - Fatal编程技术网

Java 在Android PDFDocument.Page中压缩图像

Java 在Android PDFDocument.Page中压缩图像,java,android,pdf,bitmap,pdf-generation,Java,Android,Pdf,Bitmap,Pdf Generation,我在客户端应用程序中内置了文档扫描功能。该应用程序将文档存储为图像存档,文档可以有1到n页。将显示一个菜单项,允许用户将文档导出为PDF格式 问题是生成的PDF太大(6页文档为13.5 Mb)。这会导致许多不允许通过电子邮件发送PDF的电子邮件服务出现问题 因此,在将位图绘制到PDF之前,我一直在压缩和调整位图的大小。但无论我对位图做了什么(将JPEG压缩更改为2 vs 25 vs 100或矩阵的比例),生成的PDF的文件大小始终相同 如何正确压缩位图以实际影响生成的PDF文件大小 File[]

我在客户端应用程序中内置了文档扫描功能。该应用程序将文档存储为图像存档,文档可以有1到n页。将显示一个菜单项,允许用户将文档导出为PDF格式

问题是生成的PDF太大(6页文档为13.5 Mb)。这会导致许多不允许通过电子邮件发送PDF的电子邮件服务出现问题

因此,在将位图绘制到PDF之前,我一直在压缩和调整位图的大小。但无论我对位图做了什么(将JPEG压缩更改为2 vs 25 vs 100或矩阵的比例),生成的PDF的文件大小始终相同

如何正确压缩位图以实际影响生成的PDF文件大小

File[] images = getImages();
PdfDocument doc = new PdfDocument();

for (int i = 0; i < images.length; i++) {
    // Get bitmap from file
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    Bitmap original = BitmapFactory.decodeFile(images[i].getAbsolutePath(), options);

    // Compress bitmap
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    original.compress(Bitmap.CompressFormat.JPEG, 25, stream);
    byte[] bitmapData = stream.toByteArray();
    Bitmap compressed = BitmapFactory.decodeByteArray(bitmapData, 0, bitmapData.length);

    // Calculate page size and bitmap scale
    int width = 768;
    int height = (int) (((float) width / (float) original.getWidth()) * (float) original.getHeight());
    float scaleWidth = ((float) width) / original.getWidth();
    float scaleHeight = ((float) height) / original.getHeight();

    // Create a scale matrix for the bitmap
    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);

    // Draw scaled and compressed bitmap to page
    PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(width, height, i + 1).create();
    PdfDocument.Page page = doc.startPage(pageInfo);
    page.getCanvas().drawBitmap(compressed, matrix, null);
    doc.finishPage(page);
}
File[]images=getImages();
PdfDocument doc=新PdfDocument();
对于(int i=0;i