There is dedicated ESC/POS command for Bar Code printing to Thermal Printer on Thermal Paper. Here I am showing the full code how to print Bar Code-128.
Here I’am using String variable-conId to print BarCode.
public byte[] barCodeString (String conId) {
int[] c = new int[9];
try {
int num = Integer.parseInt( conId );
for (int i = 0; i < 9; i++) {
c[i] = ((num % 10) + 48);
num = num / 10;
}
} catch (Exception e) {
System.out.Println(e);
}
return new byte[]{0x1D,0x68,0x50,0x1D, 0x6B, 73, 9, (byte)c[8], (byte)c[7], (byte)c[6], (byte)c[5], (byte)c[4], (byte)c[3], (byte)c[2], (byte)c[1], (byte)c[0]};
}
Since conId is consisting of 9 characters, ‘9’ is being used in above return byte array.
Now to print Barcode following commands should be used—
mmOutputStream.write(INIT);
mmOutputStream.write(SELECT_FONT_B);
mmOutputStream.write(SET_EMPHASIZED_MODE_ON);
mmOutputStream.write(CENTER_JUSTIFICATION);
mmOutputStream.write(barCodeString(“123456789”));