Printing continuously regenerating barcode images via a thermal printer using Java
<p>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 </p>

<pre><code>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.println(aset);
System.out.println("Printing Started...\n");


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

<p><code>setTokenParameters</code> function is </p>

<pre><code>public void setTokenParameters(String strTokenId, Image barcode) {
    strTokenID = strTokenId;
    BarcodeImage = barcode;
}
</code></pre>

<p>and this resides in the same class which contains the print() method.</p>

<pre><code>public int print(Graphics g, PageFormat pf, int iPage) throws
        PrinterException {
    if (iPage &gt; 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;
}
</code></pre>

<p>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.</p>

<p>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. </p>

<p><strong>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.</strong></p>
