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

时间:2010-06-16 22:05:09

标签: java image printing resolution

我写了一个程序,生成一个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上使用带有eclipse的java 1.6。

5 个答案:

答案 0 :(得分:2)

关于在页面边界上截断的图像,您是否检查了图形的剪辑区域?我的意思是尝试:

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

并确保正确设置。

答案 1 :(得分:0)

听起来你的问题是你正在使网格线成为BufferedImage的一部分,并且在缩放时看起来不太好。为什么不在绘制图像后使用drawLine()来生成网格?

答案 2 :(得分:0)

使用Java转换尺寸图像并打印转换后的图像的代码。

类: ConvertImageWithDimensionsAndPrint.java

package com.test.convert;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

public class ConvertImageWithDimensionsAndPrint {
    private static final int IMAGE_WIDTH = 800;
    private static final int IMAGE_HEIGHT = 1000;

public static void main(String[] args) {
    try {
        String sourceDir = "C:/Images/04-Request-Headers_1.png";
        File sourceFile = new File(sourceDir);
        String destinationDir = "C:/Images/ConvertedImages/";//Converted images save here
        File destinationFile = new File(destinationDir);
        if (!destinationFile.exists()) {
            destinationFile.mkdir();
        }
        if (sourceFile.exists()) {
            String fileName = sourceFile.getName().replace(".png", "");
            BufferedImage bufferedImage = ImageIO.read(sourceFile);
            int type = bufferedImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : bufferedImage.getType();

            BufferedImage resizedImage = new BufferedImage(IMAGE_WIDTH, IMAGE_HEIGHT, type);
            Graphics2D graphics2d = resizedImage.createGraphics();
            graphics2d.drawImage(bufferedImage, 0, 0, IMAGE_WIDTH, IMAGE_HEIGHT, null);//resize goes here
            graphics2d.dispose();

            ImageIO.write(resizedImage, "png", new File( destinationDir + fileName +".png" ));

            int oldImageWidth = bufferedImage.getWidth();
            int oldImageHeight = bufferedImage.getHeight();
            System.out.println(sourceFile.getName() +" OldFile with Dimensions: "+ oldImageWidth +"x"+ oldImageHeight);
            System.out.println(sourceFile.getName() +" ConvertedFile converted with Dimensions: "+ IMAGE_WIDTH +"x"+ IMAGE_HEIGHT);

            //Print the image file
            PrintActionListener printActionListener = new PrintActionListener(resizedImage);
            printActionListener.run();
        } else {
            System.err.println(destinationFile.getName() +" File not exists");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}
PrintActionListener.java的

Reference

package com.test.convert;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

public class PrintActionListener implements Runnable {

private BufferedImage image;

public PrintActionListener(BufferedImage image) {
    this.image = image;
}

@Override
public void run() {
    PrinterJob printJob = PrinterJob.getPrinterJob();
    printJob.setPrintable(new ImagePrintable(printJob, image));

    if (printJob.printDialog()) {
        try {
            printJob.print();
        } catch (PrinterException prt) {
            prt.printStackTrace();
        }
    }
}

public class ImagePrintable implements Printable {

    private double x, y, width;

    private int orientation;

    private BufferedImage image;

    public ImagePrintable(PrinterJob printJob, BufferedImage image) {
        PageFormat pageFormat = printJob.defaultPage();
        this.x = pageFormat.getImageableX();
        this.y = pageFormat.getImageableY();
        this.width = pageFormat.getImageableWidth();
        this.orientation = pageFormat.getOrientation();
        this.image = image;
    }

    @Override
    public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException {
        if (pageIndex == 0) {
            int pWidth = 0;
            int pHeight = 0;
            if (orientation == PageFormat.PORTRAIT) {
                pWidth = (int) Math.min(width, (double) image.getWidth());
                pHeight = pWidth * image.getHeight() / image.getWidth();
            } else {
                pHeight = (int) Math.min(width, (double) image.getHeight());
                pWidth = pHeight * image.getWidth() / image.getHeight();
            }
            g.drawImage(image, (int) x, (int) y, pWidth, pHeight, null);
            return PAGE_EXISTS;
        } else {
            return NO_SUCH_PAGE;
        }
    }

}

}

<强>输出:

04-Request-Headers_1.png OldFile with Dimensions: 1224x1584
04-Request-Headers_1.png ConvertedFile converted with Dimensions: 800x1000

转换图像后,将打开 打印窗口 以打印转换后的图像。窗口显示如下,从名称下拉列表中选择打印机,然后单击确定按钮。

Print Window

答案 3 :(得分:0)

我遇到了同样的问题。这是我的解决方案。

首先更改打印作业的分辨率......

    PrinterJob job = PrinterJob.getPrinterJob();
    // Create the paper size of our preference
    double cmPx300 = 300.0 / 2.54;
    Paper paper = new Paper();
    paper.setSize(21.3 * cmPx300, 29.7 * cmPx300);
    paper.setImageableArea(0, 0, 21.3 * cmPx300, 29.7 * cmPx300);
    PageFormat format = new PageFormat();
    format.setPaper(paper);
    // Assign a new print renderer and the paper size of our choice !
    job.setPrintable(new PrintReport(), format);

    if (job.printDialog()) {
        try {
            HashPrintRequestAttributeSet set = new HashPrintRequestAttributeSet();
            PrinterResolution pr = new PrinterResolution((int) (dpi), (int) (dpi), ResolutionSyntax.DPI);
            set.add(pr);
            job.setJobName("Jobname");
            job.print(set);
        } catch (PrinterException e) {
        }
    }

现在你可以把你喜欢的所有东西都画成这样的新高分辨率纸张了!

    public class PrintReport implements Printable {

    @Override
    public int print(Graphics g, PageFormat pf, int page) throws PrinterException {
        // Convert pixels to cm to lay yor page easy on the paper...
        double cmPx = dpi / 2.54;
        Graphics2D g2 = (Graphics2D) g;
        int totalPages = 2; // calculate the total pages you have...
        if (page < totalPages) {

            // Draw Page Header
            try {
                BufferedImage image = ImageIO.read(ClassLoader.getSystemResource(imgFolder + "largeImage.png"));
                g2.drawImage(image.getScaledInstance((int) (4.8 * cmPx), -1, BufferedImage.SCALE_SMOOTH), (int) (cmPx),
                        (int) (cmPx), null);
            } catch (IOException e) {
            }
            // Draw your page as you like...
            // End of Page
            return PAGE_EXISTS;
        } else {
            return NO_SUCH_PAGE;
        }
    }

答案 4 :(得分:-1)

您可以使用以下任一方法来提高缩放质量。我相信BiCubic会提供更好的结果,但比BILINEAR慢。

g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);

我也不会使用Image.getScaledInstance(),因为它非常慢。我不确定印刷,因为我正在努力解决类似的问题。