Java – ESC commands to print barcode to a thermal printer

arraysbarcodejavaprintingthermal-printer

When you purchase a thermal receipt printer, you also get a manual that tells you which byte sequence should be used to do what (cutting, finishing job, setting fonts etc). However I do not have an understanding of Java that is sufficient for me to be able to use the ESC/POS commands.

I have a Citizen CT S2000 thermal receipt printer. I have already managed to use the byte sequences for cutting and line feeding and manipulating what fonts the thermal printer is supposed to have. However I can not figure out how to use the ESC commands for generating and printing barcode.

The manual on how to do this is here on page 144. Here a sample code is provided in BASIC.

LPRINT CHR$(&H1D);"k"; CHR$(73); CHR$(10);
LPRINT "{BNo.{C"; CHR$(12); CHR$(34); CHR$(56);
LPRINT CHR$(&HA);
END

Here is my attempt to print the barcode.

public static void barcode() throws Exception{
    byte[] b = {0x7b, 0x43, 0x12, 0x34}; 
    //This is the commands as I interpreted the to print out 12 and 34

    DocPrintJob job = PrintServiceLookup.lookupDefaultPrintService().createPrintJob();

    DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE; 
    /*This is how I send byte sequences. 
    if you got something better then don't hesitate yelling at me :D : ). */

    Doc doc = new SimpleDoc(b, flavor, null);

    job.print(doc, null);                   
}

Yet this does not yield anything at all and the printer doesn't do anything at all.

I really can't find any examples of how to do it.

Any answers/comments would be appreciated.

Best Answer

Try the below command you can print the barcode.

byte[] barCode = {0x1d,0x6b,0x07,0x6e,0x61,0x72};

Here 0x6e,0x61,0x72 are the charters "nar" which converted into barcode.

I'm sure it will work. Please let me know in case of any query.