gpt4 book ai didi

java - 将 PrinterJob 对象调整为 BufferedImage 的特定打印格式

转载 作者:行者123 更新时间:2023-12-01 17:26:22 24 4
gpt4 key购买 nike

我使用 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());

}

我发现纸张.setSize(1.15, 0.7);单位为英寸 ( http://docs.oracle.com/javase/1.5.0/docs/api/java/awt/print/Paper.html ) 纸.setImageableArea(0, 0, 0, 0);我不知道这个 setImageableArea。

有人知道当前的尺寸吗?我计算错误了吗?谢谢。

最佳答案

首先,“打印很有趣” - 请记住,凌晨 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;
}

}

}

关于java - 将 PrinterJob 对象调整为 BufferedImage 的特定打印格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14725456/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com