【发布时间】:2012-08-29 08:15:06
【问题描述】:
我需要用 Java 打印图像。所以我实现了Printable接口的print()方法。但是打印机总是只打印一些原始图像。如何使图像适合打印区域(A4),以便打印机能够打印整个图像,而不是其中的一部分?现在我的代码如下所示:
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
if (pageIndex >= images.size()) {
return Printable.NO_SUCH_PAGE;
}
RenderedImage image;
Graphics2D graphics2D = (Graphics2D) graphics;
image = new NullOpImage((RenderedImage) images.get(pageIndex), null, null, OpImage.OP_IO_BOUND);
double x = (pageFormat.getImageableWidth() - image.getWidth())/2 + pageFormat.getImageableX();
double y = (pageFormat.getImageableHeight() - image.getHeight())/2 + pageFormat.getImageableY();
graphics2D.drawRenderedImage(image, AffineTransform.getTranslateInstance(x, y));
return PAGE_EXISTS;
}
【问题讨论】: