I have the following code to simply print a QR Code, but the printer releases a blank page. Adding text to the graphics works well and gets printed, but not the image. If i uncomment the ImageIO section, the image also gets saved properly.
private BufferedImage printImg;
public Print(BufferedImage img) {
try {
this.printImg = img;
// ask for print
PrinterJob pjob = PrinterJob.getPrinterJob();
if (pjob.printDialog() == false) return;
pjob.setPrintable(this);
//File f = new File("MyFile.png");
//ImageIO.write(printImg, "PNG", f);
pjob.print();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public int print(Graphics g, PageFormat pf, int page) throws PrinterException {
if (page > 0)
return NO_SUCH_PAGE;
try {
Graphics2D g2 = (Graphics2D) g;
g2.drawImage(this.printImg, 0, 0, null);
} catch (Exception e) {
return NO_SUCH_PAGE;
}
return PAGE_EXISTS;
}
The original image is a PNG in RGBA and i read it like this:
BufferedImage in = ImageIO.read(qr.file());
Print p = new Print(in);
I think the problem might be the high resolution of a printer (as opposed to a screen). As an image is pixel based. That combined with unprintable margins might show nothing.
The following should show something
double x = pf.getImageableX();
double y = pf.getImageableY();
double w = pf.getImageableWidth();
double h = pf.getImageableHeight();
g2.drawImage(printImg, x, y, w, h, null);
And from there to real proportional scaling is a matter of calculation. You might like to set RenderingHints
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.