使用热敏打印机Android打印条形码 [英] Print barcode using thermal printer Android
问题描述
我能够打印文本,但是涉及条形码时,它不显示或仅显示不规则的文本.
I was able to print text but when it comes to barcode it not showing or just showing irregular text.
这是我的源代码
//barcode 128
byte[] formats = {(byte) 0x1d, (byte) 0x6b, (byte) 0x73,(byte) 0x0d};
byte[] contents = content.getBytes();
byte[] bytes = new byte[formats.length + contents.length];
System.arraycopy(formats, 0, bytes, 0, formats.length );
System.arraycopy(contents, 0, bytes, formats.length, contents.length);
usbCtrl.sendByte(bytes, dev);
usbCtrl.sendByte(LineFeed(), dev);
但结果条码未显示,我是否缺少某些东西
but the result barcode is not showing, am i missing something
请帮助我
编辑
我找到了ESC/POS代码:
I found the ESC/POS code :
GS k m d1 ... dk NUL或GS k m n d1 ... d k
GS k m d1...dk NUL or GS k m n d1...d k
但是当我尝试这样做时,仍然得到相同的结果
But when I try this, still got same result
推荐答案
GS k
POS代码具有两个版本(您已经发现):
The GS k
POS code has two versions (as you already discovered):
GS k - print one dimensional barcode
m - barcode mode selector
[d]k - data bytes
NUL - terminator
此版本仅适用于纯ASCII数据,因为它使用 0x00
(NUL)作为终止符.
This version works only for pure ASCII data since it uses a 0x00
(NUL) as terminator.
GS k - print one dimensional barcode
m - barcode mode selector
n - content length in bytes
[d]k - data bytes
此版本使用一个额外的长度字节 n
来指示数据部分(它也仅适用于某些编码,包括 CODE128
).
This version uses an additional length byte n
to indicate the data part (it's also only suitable for certain codings including CODE128
).
您的代码在命令字节中有一个流浪汉 0x0d
,并且格式可能也错误.
Your code has a stray 0x0d
in the command bytes and may also be using the wrong format.
如果您打算打印纯ASCII数据格式,请使用以下命令:
If you plan to print pure ASCII data format the command like this:
byte[] formats = {(byte) 0x1d, (byte) 0x6b, (byte) 0x49};
byte[] contents = content.getBytes();
byte[] bytes = new byte[formats.length + contents.length + 1];
System.arraycopy(formats, 0, bytes, 0, formats.length );
System.arraycopy(contents, 0, bytes, formats.length, contents.length);
// add a terminating NULL
bytes[formats.length + contents.length] = (byte) 0x00;
或更安全的版本,因为它也具有预期的数据长度:
Or the more secure version since it also has the expected data length:
byte[] contents = content.getBytes();
// include the content length after the mode selector (0x49)
byte[] formats = {(byte) 0x1d, (byte) 0x6b, (byte) 0x49, (byte)content.length};
byte[] bytes = new byte[formats.length + contents.length];
System.arraycopy(formats, 0, bytes, 0, formats.length );
System.arraycopy(contents, 0, bytes, formats.length, contents.length);
如果这两种方法均 都不起作用,则您的打印机可能根本不支持 CODE128
.
If neither of the two work then your printer may simply not support CODE128
.
5890是一个足够普遍的规格,市场上有许多便宜的即插即用"替代产品,它们省去了更复杂的条形码实现,并且仅包含简单的编码,例如 EAN8
, EAN13
等.
The 5890 is a common enough specification and there are lots of cheap "drop-in" replacements on the market which leave out the more complex barcode implementations and only include simple codings like EAN8
, EAN13
etc.
这篇关于使用热敏打印机Android打印条形码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!