java - 在 Java 中打印 BufferedImage 的正确方法

标签 java graphics printing bufferedimage graphics2d

我想知道是否有在 Java 中打印 BufferedImage 的正确方法。 基本上我已经创建了一个工作正常的照片处理程序,我可以保存图像等。 但我的真正目标是将其发送到打印机软件,以便您可以选择要打印的页数和页面类型。

所以我的简短问题是,如何将缓冲图像发送到打印机,以便弹出打印机选择屏幕等然后能够打印?

如果有人能给我举个例子,我将不胜感激。

最佳答案

这是我的一个 Java 项目中的一个。此代码将缩放并在打印机页面上打印一张图像。

你可以这样调用它:

    printButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event) {
            //Start a Thread
            new Thread(new PrintActionListener(image)).start();         
        }
    });

这是可运行的:

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;
            }
        }

    }

}

关于java - 在 Java 中打印 BufferedImage 的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18404553/

相关文章:

java - 在单个 JFrame 中同时处理两个 JPanel

c# - 在矩形周围放置和粘贴控件

c++ - 我如何将堆栈转储到 Arduino 中?

java - 在Java中,我的MouseMotionListener和MouseListener正在检测点击,但不检测移动

java - 为什么这里呈现的模板不正确?

java - Oracle 的 jvm 中的 notify() 实现

python - 如何在 Python 2.7 中打印由空格而不是新行分隔的值

java - Hazelcast 持久消息队列

c - opengl使聚光灯像手电筒一样

使用 Bixolon R200 进行打印的 Android 应用程序