Print barcode using thermal printer Android

落爺英雄遲暮 提交于 2019-12-05 03:40:53

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

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

This version uses an additional length byte n to indicate the data part (it's also only suitable for certain codings including CODE128).

Your code has a stray 0x0d in the command bytes and may also be using the wrong format.

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);

If neither of the two work then your printer may simply not support CODE128.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!