我尝试使用原生awt print API从java项目使用pos80热敏打印机在标签上打印二维码,结果如下。
请帮助我解决这个问题,并在标签中间一个接一个地打印二维码。
我使用javac 1.8.0_101,netbeans11.0,qrgen4来生成二维码。

我用来从dir读取二维码图像并通过awt打印的代码如下:
public class PrinterService implements Printable {
private PrinterJob printerJob;
private PageFormat pageFormat;
private Paper paper;
private final int MARGIN = 1;
public PrinterService() {
printerJob = PrinterJob.getPrinterJob();
pageFormat = printerJob.defaultPage(); // Getting the page format.
paper = new Paper(); // Create a new paper...
paper.setImageableArea(MARGIN, MARGIN, pageFormat.getWidth(), pageFormat.getHeight());
pageFormat.setPaper(paper);
pageFormat.setOrientation(PageFormat.PORTRAIT);
printerJob.setPrintable(this, pageFormat);
try {
printerJob.print();
} catch (PrinterException ex) {
JOptionPane.showMessageDialog(null, "Printing Failed, Error: " + ex.toString());
}
}
@Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
if (pageIndex > 0) {
return NO_SUCH_PAGE;
}
float marginTop = (float) 70.83;
Graphics2D g2d = (Graphics2D) graphics;
ImageIcon imageIcon = new ImageIcon("qrcode75b75.gif");
Image image = imageIcon.getImage();
g2d.drawImage(image, 20, (int) (marginTop + 10), imageIcon.getImageObserver());
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
return PAGE_EXISTS;
}
}发布于 2021-02-09 00:41:03
一般的解决办法是用打印机在纸上印上一个停止标记,它是纸卷上的一个黑点,打印机读取黑点并了解对齐情况。
https://stackoverflow.com/questions/61273765
复制相似问题