java的像素与dpi_java设置图像的分辨率和打印尺寸

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.

解决方案

Regarding the image being cut-off on the page boundary, have you checked the clip region of the graphics? I mean try :

System.out.println(graphics.getClipBounds());

and make sure it is correctly set.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 Java 的 ImageIO 类来读取和写入 PNG 和 JPEG 图像,并且可以设置图像DPI。具体实现步骤如下: 1. 读取 PNG 或 JPEG 图像: ```java File imageFile = new File("image.png"); BufferedImage image = ImageIO.read(imageFile); ``` 2. 设置 DPI: ```java IIOMetadata metadata = ImageIO.getMetadata(image); IIOMetadataNode standardTree = (IIOMetadataNode) metadata.getAsTree(metadata.getNativeMetadataFormatName()); IIOMetadataNode physNode = new IIOMetadataNode("javax_imageio_1.0"); physNode.setAttribute("dpiWidth", "300"); // 设置水平 DPI physNode.setAttribute("dpiHeight", "300"); // 设置垂直 DPI standardTree.appendChild(physNode); metadata.mergeTree(metadata.getNativeMetadataFormatName(), standardTree); ``` 3. 写入 PNG 或 JPEG 图像: ```java File outputImageFile = new File("output.png"); ImageWriter writer = ImageIO.getImageWritersByFormatName("png").next(); ImageWriteParam writeParam = writer.getDefaultWriteParam(); ImageTypeSpecifier typeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB); IIOMetadata metadata = writer.getDefaultImageMetadata(typeSpecifier, writeParam); ByteArrayOutputStream baos = new ByteArrayOutputStream(); writer.setOutput(new MemoryCacheImageOutputStream(baos)); IIOImage imageWithMetadata = new IIOImage(image, null, metadata); writer.write(null, imageWithMetadata, writeParam); Files.write(outputImageFile.toPath(), baos.toByteArray()); ``` 注意:在上述代码中,设置 DPI 的实现方式适用于 JPEG 和 PNG 图像。但是对于其他图像格式,可能需要使用不同的方式来设置 DPI

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值