缩放图像模糊PDFBox [英] Scaled image blurry in PDFBox

查看:829
本文介绍了缩放图像模糊PDFBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将尺寸= 2496 x 3512的图像缩放为PDF文档。我正在使用PDFBox生成它,但缩放的图像最终模糊。

I'm trying to scaling an image with size = 2496 x 3512 into a PDF document. I'm using PDFBox to generate it but the scaled image ends up blurred.

以下是一些片段:


  • PDF页面大小(A4)由page.findMediaBox()返回.createDimension():java.awt.Dimension [width = 612,height = 792]

  • PDF Page size (A4) returned by page.findMediaBox().createDimension(): java.awt.Dimension[width=612,height=792]

然后我根据页面大小与图像大小计算缩放维度,返回:java.awt.Dimension [width = 562,height = 792]
我使用下面的代码是为了计算缩放尺寸:

Then I calculate the scaled dimension based on the Page size vs. Image size which returns: java.awt.Dimension[width=562,height=792] I use the code below in order to calculate the scaled dimension:

public static Dimension getScaledDimension(Dimension imgSize, Dimension boundary) {
  int original_width = imgSize.width;
  int original_height = imgSize.height;
  int bound_width = boundary.width;
  int bound_height = boundary.height;
  int new_width = original_width;
  int new_height = original_height;

  // first check if we need to scale width
  if (original_width > bound_width) {
    //scale width to fit
    new_width = bound_width;
    //scale height to maintain aspect ratio
    new_height = (new_width * original_height) / original_width;
  }

  // then check if we need to scale even with the new height
  if (new_height > bound_height) {
    //scale height to fit instead
    new_height = bound_height;
    //scale width to maintain aspect ratio
    new_width = (new_height * original_width) / original_height;
  }

  return new Dimension(new_width, new_height);
}


  • 实际执行图像缩放我正在使用Image Scalr API:

  • And to actually perform the image scaling I'm using Image Scalr API:

    BufferedImage newImg = Scalr.resize(img, Scalr.Method.ULTRA_QUALITY, Scalr.Mode.FIT_EXACT, 
      scaledWidth, scaledHeight, Scalr.OP_ANTIALIAS);
    


  • 我的问题是我做错了什么?缩放到较小尺寸时,不应模糊大图像。这是与PDF页面分辨率/大小相关的内容吗?

    My question is what am I doing wrong? A big image shouldn't be blurred when scaled to a smaller size. Is this something related to the PDF page resolution/size?

    谢谢,

    Gyo

    推荐答案

    好的,我找到了一种在不损失质量的情况下添加图片的方法。

    Ok, I found a way to add images without losing the quality.

    实际上,为了使图像不被模糊,我让PDFBox通过给出所需的大小来调整图像的大小。与下面的代码类似:

    Actually to make the image not be blurred I let PDFBox to resize the image by giving it the desired size. Like the code below:

    PDXObjectImage ximage = new PDJpeg(doc, new FileInputStream(new File("/usr/gyo/my_large_image.jpg")), 1.0f);
    PDPageContentStream contentStream = new PDPageContentStream(doc, page, true, false);
    Dimension scaledDim = getScaledDimension(new Dimension(ximage.getWidth(),  ximage.getHeight()), page.getMediaBox().createDimension());
    contentStream.drawXObject(ximage, 1, 1, scaledDim.width, scaledDim.height);
    contentStream.close();
    

    谢谢,

    Gyo

    这篇关于缩放图像模糊PDFBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆