开发者

Printing continuously regenerating barcode images via a thermal printer using Java

I have a java code which generates barcode image with the name barcode.jpg in the same folder as the residing jar file. I do this

Image bcode = Toolkit.getDefaultToolkit().getImage("barcode.jpg");
if (bcode != null) {
    oTokenPrinter.setTokenParameters(strTokenNumber, bcode);
}
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(OrientationRequested.PORTRAIT);
aset.add(new MediaPrintableArea((float) 16.0, (float) 20.5, (float) 184.0, (float) 228.5, 1000));
System.out.println(aset);
PrinterJob oJob = PrinterJob.getPrinterJob();
PageFormat pf = oJob.defaultPage();
oJob.setPrintable(oTokenPrinter, pf);
System.out.pri开发者_如何学编程ntln(aset);
System.out.println("Printing Started...\n");


try {
    oJob.print(aset);
} catch (PrinterException ex) {
    Logger.getLogger(EntryHandler.class.getName()).log(Level.SEVERE, null, ex);
}

setTokenParameters function is

public void setTokenParameters(String strTokenId, Image barcode) {
    strTokenID = strTokenId;
    BarcodeImage = barcode;
}

and this resides in the same class which contains the print() method.

public int print(Graphics g, PageFormat pf, int iPage) throws
        PrinterException {
    if (iPage > 0) {
        return NO_SUCH_PAGE;
    }

    /* User (0,0) is typically outside the imageable area, so we must
     * translate by the X and Y values in the PageFormat to avoid clipping
     */
    Graphics2D g2d = (Graphics2D) g;

    g2d.translate(pf.getImageableX(), pf.getImageableY());


    /* Now we perform our rendering */


    g.setFont(new Font("Arial", Font.BOLD, 10));

    /*Printing Begins*/

    if (BarcodeImage != null) {
        System.out.println("Started Printing...\n");

        g.drawImage(BarcodeImage, 16, -10, 130, 60, null);
        g.drawString("Token ID: " + strTokenID, 18, 60);
        g.dispose();
        System.out.println("Finished Printing");

    } else {

        System.out.print("Image not loaded");

    }
    /*Finished Printing.*/

    /* tell the caller that this page is part of the printed document */
    return PAGE_EXISTS;
}

This is what I do. The generation of image is in a thread and it keeps on overwriting and I can see that when I open the image. But the problem is that, while printing it keeps on printing the same image. Only the image is the same everytime, the token ID keeps on varying.

Any solution for this. I have tried deleting the image everytime after printing. But that doesnt work. I have also tried to clear printer spooler by running a .bat file through my java program. No avail.

NOTE: This happens only when the printer is a thermal printer. I tried on a normal HP LaserJet and it works fine. But there the problem is I have to waste A4 sheets. I need this thing in receipt size on a thermal printer.


Sounds like the printer is caching the image. Try appending a random number to the image name each time perhaps?

Edit:

Another possibility is that in the line:

Toolkit.getDefaultToolkit().getImage("barcode.jpg");

The toolkit is caching the image rather than reloading it.

Edit 2:

From the javadoc for getImage:

Returns an image which gets pixel data from the specified file, whose format can be either GIF, JPEG or PNG. The underlying toolkit attempts to resolve multiple requests with the same filename to the same returned Image. Since the mechanism required to facilitate this sharing of Image objects may continue to hold onto images that are no longer of use for an indefinite period of time, developers are encouraged to implement their own caching of images by using the createImage variant wherever available.

To me that sounds like its caching the image - have you tried using createImage() instead?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜