printing QR codes through an ESC/POS thermal printer
ef test_qrcode (printer, text, print_also_text=false, qr_size=6.chr)s = text.size + 3
lsb = (s % 256).chr
msb = (s / 256).chr
# https://code.google.com/p/python-escpos/wiki/Usage
escpos = ""
escpos << "\x1D\x28\x6B\x03\x00\x31\x43#{qr_size}"
escpos << "\x1D\x28\x6B\x03\x00\x31\x45\x33"
escpos << "\x1D\x28\x6B#{lsb}#{msb}\x31\x50\x30"
escpos << text #
escpos << "\x1D\x28\x6B\x03\x00\x31\x51\x30"
# writing byte streams directly to the serial port
printer.write escpos
end in general, on the ESC/POS message format, especially in case I would insert a long text message (> 400 chars) inside a QR code... It seem that printer reject (do not print) QR codes containing more than 400 chars using this code:****example is in Javapublic void print_qr_code(String qrdata)
{
int store_len = qrdata.length() + 3;
byte store_pL = (byte) (store_len % 256);
byte store_pH = (byte) (store_len / 256);
// QR Code: Select the model
// Hex 1D 28 6B 04 00 31 41 n1(x32) n2(x00) - size of model
// set n1
// https://reference.epson-biz.com/modules/ref_escpos/index.php?content_id=140
byte[] modelQR = {(byte)0x1d, (byte)0x28, (byte)0x6b, (byte)0x04, (byte)0x00, (byte)0x31, (byte)0x41, (byte)0x32, (byte)0x00};
// QR Code: Set the size of module
// Hex 1D 28 6B 03 00 31 43 n
// n depends on the printer
// https://reference.epson-biz.com/modules/ref_escpos/index.php?content_id=141
byte[] sizeQR = {(byte)0x1d, (byte)0x28, (byte)0x6b, (byte)0x03, (byte)0x00, (byte)0x31, (byte)0x43, (byte)0x03};
// Hex 1D 28 6B 03 00 31 45 n
// Set n for error correction
// https://reference.epson-biz.com/modules/ref_escpos/index.php?content_id=142
byte[] errorQR = {(byte)0x1d, (byte)0x28, (byte)0x6b, (byte)0x03, (byte)0x00, (byte)0x31, (byte)0x45, (byte)0x31};
// QR Code: Store the data in the symbol storage area
// Hex 1D 28 6B pL pH 31 50 30 d1...dk
// https://reference.epson-biz.com/modules/ref_escpos/index.php?content_id=143
// 1D 28 6B pL pHcn(49->x31) fn(80->x50) m(48->x30) d1…dk
byte[] storeQR = {(byte)0x1d, (byte)0x28, (byte)0x6b, store_pL, store_pH, (byte)0x31, (byte)0x50, (byte)0x30};
// QR Code: Print the symbol data in the symbol storage area
// Hex 1D 28 6B 03 00 31 51 m
// https://reference.epson-biz.com/modules/ref_escpos/index.php?content_id=144
byte[] printQR = {(byte)0x1d, (byte)0x28, (byte)0x6b, (byte)0x03, (byte)0x00, (byte)0x31, (byte)0x51, (byte)0x30};
// flush() runs the print job and clears out the print buffer
flush();
// write() simply appends the data to the buffer
write(modelQR);
write(sizeQR);
write(errorQR);
write(storeQR);
write(qrdata.getBytes());
write(printQR);
flush();
}
頁:
[1]