java设置图像的分辨率和打印尺寸
我编写了一个程序,生成一个 BufferedImage 以显示在屏幕上,然后打印。图像的一部分包括 1 像素宽的网格线。即,一行为1个像素,行与行之间大约有10个像素。由于屏幕分辨率的原因,图像显示得比这大得多,每行有几个像素。我想将其绘制得更小,但是当我缩放图像(通过使用 Image.getScaledInstance 或 Graphics2D.scale)时,我会丢失大量细节。
我也想打印图像,并且正在处理同样的问题。在这种情况下,我使用此代码来设置分辨率:
HashPrintRequestAttributeSet set = new HashPrintRequestAttributeSet();
PrinterResolution pr = new PrinterResolution(250, 250, ResolutionSyntax.DPI);
set.add(pr);
job.print(set);
这可以使图像变小而不丢失细节。但问题是图像在同一边界处被切断,就好像我没有设置分辨率一样。我也很困惑,因为我期望更大的 DPI 来制作更小的图像,但它却以相反的方式工作。
我在 Windows 7 上使用 java 1.6 和 eclipse。
I wrote a program that generates a BufferedImage to be displayed on the screen and then printed. Part of the image includes grid lines that are 1 pixel wide. That is, the line is 1 pixel, with about 10 pixels between lines. Because of screen resolution, the image is displayed much bigger than that, with several pixels for each line. I'd like to draw it smaller, but when I scale the image (either by using Image.getScaledInstance or Graphics2D.scale), I lose significant amounts of detail.
I'd like to print the image as well, and am dealing with the same problem. In that case, I am using this code to set the resolution:
HashPrintRequestAttributeSet set = new HashPrintRequestAttributeSet();
PrinterResolution pr = new PrinterResolution(250, 250, ResolutionSyntax.DPI);
set.add(pr);
job.print(set);
which works to make the image smaller without losing detail. But the problem is that the image is cut off at the same boundary as if I hadn't set the resolution. I'm also confused because I expected a larger number of DPI to make a smaller image, but it's working the other way.
I'm using java 1.6 on Windows 7 with eclipse.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
关于图像在页面边界被剪切的问题,您检查过图形的剪切区域吗?我的意思是尝试:
并确保它设置正确。
Regarding the image being cut-off on the page boundary, have you checked the clip region of the graphics? I mean try :
and make sure it is correctly set.
我也有同样的问题。这是我的解决方案。
首先更改打印作业的分辨率...
现在您可以将您喜欢的所有内容绘制到新的高分辨率纸张中,如下所示!
I had the same problem. Here is my solution.
First change the resolution of the print job...
Now you can draw everything you like into the new high resolution paper like this !
听起来你的问题是你正在将网格线作为 BufferedImage 的一部分,并且缩放时看起来不太好。为什么不在绘制图像后使用 drawLine() 来生成网格?
It sounds like your problem is that you are making the grid lines part of the BufferedImage and it doesn't look good when scaled. Why not use drawLine() to produce the grid after your image has been drawn?
使用 Java 转换具有尺寸的图像的代码并打印转换后的图像。
类: ConvertImageWithDimensionsAndPrint.java
PrintActionListener.java 的参考
}
输出:
图像转换后,将打开打印窗口来打印转换后的图像。该窗口显示如下,从名称下拉列表中选择打印机,然后单击确定按钮。
Code for Convert image with dimensions using Java and print the converted image.
Class: ConvertImageWithDimensionsAndPrint.java
Reference of PrintActionListener.java
}
Output:
After conversion of a image a Print window will be open for printing the converted image. The window displays like below, Select the printer from Name dropdown and Click OK button.
您可以使用以下任一方法来提高缩放质量。我相信 BiCubic 能提供更好的结果,但比 BILINEAR 慢。
我也不会使用 Image.getScaledInstance() 因为它非常慢。我不确定打印,因为我正在努力解决类似的问题。
You can use either of the following to improve the quality of the scaling. I believe BiCubic gives better results but is slower than BILINEAR.
I would also not use Image.getScaledInstance() as it is very slow. I'm not sure about the printing as I'm struggling with similar issues.