简体   繁体   English

Java图像缩放

[英]Java image scaling

I am outputting images to a PDF file using iText. 我正在使用iText将图像输出到PDF文件。 The images always appear larger than they are supposed to. 图像总是看起来比它们应该的大。 According to the book (iText in Action), this is because iText always displays the images at a resolution of 72 dpi, regardless of what the actual dpi property of the image is. 根据这本书(iText in Action),这是因为iText总是以72 dpi的分辨率显示图像,而不管图像的实际dpi属性是什么。 The book suggests using image.getDpiX() to find the dpi of the image, and then using image.scalePercent(72 / actualDpi * 100) to display the image properly. 本书建议使用image.getDpiX()来查找图像的dpi,然后使用image.scalePercent(72 / actualDpi * 100)正确显示图像。 So far, the getDpiX() property of all my images have returned 0 (I've tried 2 gifs and 1 jpg). 到目前为止,我所有图像的getDpiX()属性都返回0(我已经尝试了2个GIF和1个jpg)。 Is there another way to figure out the actual DPI so that my images scale properly? 有没有其他方法可以找出实际的DPI,以便我的图像正确缩放?

com.lowagie.text.Image graphic = com.lowagie.text.Image.getInstance(imgPath);
float actualDpi = graphic.getDpiX();
if (actualDpi > 0)
  //Never gets here
  graphic.scalePercent(72f / actualDpi * 100);

According to the com.lowagie.text.Image JavaDoc, the method getDpiX gets the dots-per-inch in the X direction. 根据com.lowagie.text.Image JavaDoc,方法getDpiX获取X方向上的每英寸点数。 Returns zero if not available. 如果不可用则返回零。

You're going to have to assume a value when the getDpiX method returns zero. 当getDpiX方法返回零时,您将不得不假设一个值。 100 dpi is as good an assumption as any. 100 dpi是一个很好的假设。

if (actualDpi <= 0) actualDpi = 100f;
graphic.scalePercent(72f / actualDpi * 100f);

For GIF, there is no place to store a "DPI" information in the file, so "actualDpi" has no meaning in that case. 对于GIF,没有地方可以在文件中存储“DPI”信息,因此“actualDpi”在这种情况下没有意义。 For JPEG, the "DPI" information can be stored in the file, but is not mandatory, and if not set: "actualDPI" has no meaning as well. 对于JPEG,“DPI”信息可以存储在文件中,但不是必需的,如果没有设置:“actualDPI”也没有意义。 The real answer is: there is no such thing as "actual DPI", either the information is provided (ie. "in this image I want 1 pixel to be rendered with this specific physical width (or height)"), or it's not. 真正的答案是:没有“实际DPI”这样的东西,要么提供信息(即“在这个图像中我希望用这个特定的物理宽度(或高度)渲染1个像素”),或者它不是。 Another element is in your sentence: "always appear larger than they are supposed to"; 另一个元素在你的句子中:“总是看起来比它们应该的大”; the "supposed to" is the DPI information stored in the image. “应该”是存储在图像中的DPI信息。 So if this information is absent, and you feel that when you open the image it seems right on the screen, then you have to calculate the density of your screen (width in number of pixels divided by the width in inches of your screen), and use that as your "actualDPI" variable. 因此,如果没有此信息,并且您觉得当您打开图像时它似乎正好在屏幕上,那么您必须计算屏幕的密度(像素数的宽度除以屏幕的宽度,以英寸为单位),并将其用作“actualDPI”变量。

The screen-resolution, retrieved by 屏幕分辨率,由。检索

java.awt.Toolkit.getDefaultToolkit().getScreenResolution()

did not help, image was still too big. 没有帮助,图像仍然太大了。 However, one can scale the image to the page width in the following way: 但是,可以通过以下方式将图像缩放到页面宽度:

BufferedImage bufferedImage = null; // your image
Image image = Image.getInstance(bufferedImage,null);
image.scalePercent((document.right()-document.left())/((float)image.getWidth())*100);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM