在 Java 中打印 BufferedImage 的正确方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18404553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Proper way of printing a BufferedImage in Java
提问by Rose Blax
I would like to know if there is a proper way of printing a BufferedImage
in Java.
Basically I have created a photo manipulation program which works fine, I can save images etc.
But my real goal is to send it to the printer software so that you can select the amount of pages you want to print and page type.
我想知道是否有BufferedImage
在 Java中打印 a 的正确方法。基本上我已经创建了一个工作正常的照片处理程序,我可以保存图像等。但我的真正目标是将它发送到打印机软件,以便您可以选择要打印的页数和页面类型。
So my shortened question is, how do I send a buffered image to the printer so that a printer selection screen will popup etc and then be able to print?
所以我简短的问题是,如何将缓冲图像发送到打印机,以便弹出打印机选择屏幕等然后能够打印?
If anyone can show me an example of this, it would be greatly appreciated.
如果有人可以向我展示这方面的示例,将不胜感激。
回答by Gilbert Le Blanc
Here's one from one of my Java projects. This code will scale and print one image on a printer page.
这是我的一个 Java 项目中的一个。此代码将在打印机页面上缩放和打印一个图像。
You call it like this:
你这样称呼它:
printButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
//Start a Thread
new Thread(new PrintActionListener(image)).start();
}
});
Here's the Runnable:
这是可运行的:
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;
}
}
}
}