将 PrinterJob 对象拟合到 BufferedImage 的特定打印格式
Posted
技术标签:
【中文标题】将 PrinterJob 对象拟合到 BufferedImage 的特定打印格式【英文标题】:fitting PrinterJob Object to specific print format of BufferedImage 【发布时间】:2013-01-21 10:30:33 【问题描述】:我正在使用 PrinterJob 对象来打印我的 Bufferedimage,我有一个 BufferedImage,我对其进行处理并将其发送到具有纸张格式等的打印机作业,但我无法使其适合我的卡片打印机。当我将它保存到我的硬盘并通过 Windows 打印管理器打印时,它在我的卡片打印机上打印得非常好,但是使用 PrinterJob 它变得太大并且不适合卡片
卡片的大小是86X54mm,我的缓冲图片大小是1300x816px 代码:
PrinterJob printjob = PrinterJob.getPrinterJob();
printjob.setJobName("CardPrint");
Printable printable = new Printable()
public int print(Graphics pg, PageFormat pf, int pageNum)
if (pageNum > 0)
return Printable.NO_SUCH_PAGE;
JLayeredPane j1 = new JLayeredPane();
Dimension size = j1.getSize();
j1.print(bi.getGraphics());
Graphics2D g2 = (Graphics2D) pg;
g2.translate(pf.getImageableX(), pf.getImageableY());
g2.drawImage(bi, 0, 0, (int) pf.getWidth(), (int) pf.getHeight(), null);
return Printable.PAGE_EXISTS;
;
Paper paper = new Paper();
paper.setImageableArea(0, 0, 0, 0);
paper.setSize(1.15, 0.72);
PageFormat format = new PageFormat();
format.setPaper(paper);
printjob.setPrintable(printable, format);
try
printjob.printDialog();
printjob.print();
catch (Exception eee)
System.out.println("NO PAGE FOUND."+eee.toString());
我发现 paper.setSize(1.15, 0.7); 以英寸为单位 (http://docs.oracle.com/javase/1.5.0/docs/api/java/awt/print/Paper.html) paper.setImageableArea(0, 0, 0, 0); 我不知道这个 setImageableArea。
有没有人知道当前的尺寸,我计算错误了吗? 谢谢。
【问题讨论】:
【参考方案1】:第一次打印,“打印很有趣” - 记住,在凌晨 2 点重复一遍...
基本上,您需要先将纸张尺寸从 CM 转换为像素。 Java 的 API 使用 72dpi。
因此,您 8.6x5.4cm 的页面大小计算为 153.0708659856 x 243.7795273104 像素
那么你需要新建一个满足那些页面要求的Paper
...
这一切都需要包装回PageFormat
并交回PrintJob
。
在Printable
中,您需要缩放图像以“适合”可打印区域。图片缩放很有趣...
所以,对于我的测试,我有一个 800x1159 的图像,它被缩小到 166x241
如需更好地讨论图像缩放,请查看this question
public class PrintTest02
private static BufferedImage image;
public static void main(String[] args)
try
image = ImageIO.read(new File("/path/to/image.png"));
System.out.println(image.getWidth() + "x" + image.getHeight());
PrinterJob pj = PrinterJob.getPrinterJob();
if (pj.printDialog())
PageFormat pf = pj.defaultPage();
Paper paper = pf.getPaper();
// 86X54mm
double width = fromCMToPPI(8.6);
double height = fromCMToPPI(5.4);
paper.setSize(width, height);
paper.setImageableArea(
fromCMToPPI(0.1),
fromCMToPPI(0.1),
width - fromCMToPPI(0.1),
height - fromCMToPPI(0.1));
pf.setOrientation(PageFormat.PORTRAIT);
pf.setPaper(paper);
PageFormat validatePage = pj.validatePage(pf);
System.out.println("Valid- " + dump(validatePage));
pj.setPrintable(new MyPrintable(), validatePage);
try
pj.print();
catch (PrinterException ex)
ex.printStackTrace();
catch (IOException exp)
exp.printStackTrace();
protected static double fromPPItoCM(double dpi)
return dpi / 72 / 0.393700787;
protected static double fromCMToPPI(double cm)
return toPPI(cm * 0.393700787);
protected static double toPPI(double inch)
return inch * 72d;
protected static String dump(Paper paper)
StringBuilder sb = new StringBuilder(64);
sb.append(paper.getWidth()).append("x").append(paper.getHeight())
.append("/").append(paper.getImageableX()).append("x").
append(paper.getImageableY()).append(" - ").append(paper
.getImageableWidth()).append("x").append(paper.getImageableHeight());
return sb.toString();
protected static String dump(PageFormat pf)
Paper paper = pf.getPaper();
return dump(paper);
public static class MyPrintable implements Printable
@Override
public int print(Graphics graphics, PageFormat pageFormat,
int pageIndex) throws PrinterException
System.out.println(pageIndex);
int result = NO_SUCH_PAGE;
if (pageIndex < 1)
Graphics2D g2d = (Graphics2D) graphics;
System.out.println("[Print] " + dump(pageFormat));
double width = pageFormat.getImageableWidth();
double height = pageFormat.getImageableHeight();
System.out.println("Print Size = " + fromPPItoCM(width) + "x" + fromPPItoCM(height));
g2d.translate((int) pageFormat.getImageableX(),
(int) pageFormat.getImageableY());
Image scaled = null;
if (width > height)
scaled = image.getScaledInstance((int)Math.round(width), -1, Image.SCALE_SMOOTH);
else
scaled = image.getScaledInstance(-1, (int)Math.round(height), Image.SCALE_SMOOTH);
g2d.drawImage(scaled, 0, 0, null);
result = PAGE_EXISTS;
return result;
【讨论】:
谢谢,这有点奏效,但结果很差。我认为下一步将是检查如何使用 Windows 打印对话框进行打印。顺便说一句,我发布了这个问题:***.com/questions/14729127/… ...再次感谢您以上是关于将 PrinterJob 对象拟合到 BufferedImage 的特定打印格式的主要内容,如果未能解决你的问题,请参考以下文章
将string类对象中的内容格式化到字符串Buffer中时遇到的异常崩溃分析
如何检测“另存为 PDF…”是从 OS X 上的 AWT PrinterJob 中挑选出来的