最近项目需要,打印订单信息,携带二维码,并且要做二维码识别。打印机使用芯烨xp58系列的打印机,以前只打印文字,今天研究了一下他的栅格图像协议,实现了二维码打印,理论上打印二值化图像也是可以。
协议解析:
官方文档给的协议,描述有些太过地方不是很详细,整体协议是这样,16进制表示
1D 76 30 0 这4个字节,前3个是固定的,第4个0,表示正常模式200DPI,具体参考上图
xL xH 表示宽度,是字节数,等于图像的宽度/8,如果除不尽,增加一个,于是x=(width+7)/8。
xL是宽度的低位,xH是宽度高位,所以宽度=xh*256+xl
yL yH是高度,这个就等于图像高度,就是像素行数
总字节数=8(4个头,x,y)+x*y
另外:图最后特别强调了,每个字节的位,是从高到低排列。
实现思路:生成二维码,宽度最好是8的整数倍,然后遍历二维码,将黑色部分映射到字节数组某个位上,设置为1。
映射规则,p=8+y*n+x/8;//当前操作的是第几个字节,前面有8个头,每行n个,所以是y*n,当前行x/8,因为按bit算点,
bit=7-x%8;//bit位置0-7,高位在前,低位在后
具体代码
public class XPrinter58 {
final static byte[] cut=new byte[] {0x1D,0x56,0x42,0x00,0x0A,0x0A,0x00};//切纸命令
final byte[] charwidth2=new byte[] {0x1C,0x57,0x01};//汉字宽度2倍
final byte[] charwidth1=new byte[] {0x1C,0x57,0x02};//宽度默认
final byte[] charsize2=new byte[] {0x1B,0x21,0x18};//字体变大加粗
final byte[] charsize1=new byte[] {0x1B,0x21,0x01};//正常字体
final byte[] alienLeft=new byte[] {0x1B,0x61,0x00};//左对齐
final byte[] alienCenter=new byte[] {0x1B,0x61,0x01};//居中对齐
final byte[] lineHeight=new byte[] {0x1B,0x33,0x4f};//行高指令
private String ip;
private Socket client;
public XPrinter58(String ip) {
this.ip=ip;
client=new Socket();
}
private boolean connect() {
if(client.isConnected())return true;//已经链接了
try {
client.connect(new InetSocketAddress("192.168.0.100" , 9100),1000);//链接打印机
return client.isConnected();
}
catch (Exception e) {
// TODO: handle exception
return false;
}
}
private void sendData(byte [] dat) {
try {
if(connect()) {
client.getOutputStream().write(dat);//发送切纸指令
Thread.sleep(20);
}
}
catch (Exception e) {
// TODO: handle exception
}
}
public void cut() {
sendData(cut);
try {
client.close();
}
catch (Exception e) {
// TODO: handle exception
}
}
public void printString(String con) {
sendData(charsize1);//小字体
sendData(alienLeft);//左对齐
sendData(lineHeight);//行高加高
sendData(charwidth1);//小宽度
sendData(con.getBytes());//内容
}
public void printTitle(String title) {
sendData(charsize2);//大字体
sendData(charwidth2);//大宽度
sendData(alienCenter);//居中
sendData(title.getBytes());//标题
sendData(charsize1);//小字体
sendData("rn rn".getBytes());//增加一个空行
}
public void printQrCode(String code,int width,int height) {
sendData(alienCenter);//居中
sendData(getQrCodeData(code,width,height));//打印二维码
}
private byte[] getQrCodeData(String con,int width,int height) {
byte[] dat=null;
Hashtable hints = new Hashtable();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.MARGIN, 1);
try {
BitMatrix bts = new MultiFormatWriter().encode(con, BarcodeFormat.QR_CODE, width, width, hints);
int n=(width+7)/8;//一行多少个字节,不足8个字节,补一个
dat=new byte[8+n*height];//前8个字节是二维码打印头 1d 76 30 0 xL xH yL yH
dat[0]=0x1d;
dat[1]=0x76;
dat[2]=0x30;
dat[3]=0x0;//打印模式正常 200 dpi
dat[4]=(byte)(n&0x000000ff);//xL低位
dat[5]=(byte)((n>>8)&0x000000ff);//高位
dat[6]=(byte)(height&0x000000ff);//xL低位
dat[7]=(byte)((height>>8)&0x000000ff);//高位
//数据头准备完毕
int p,bit;//当前字节位置和bit位置
for (int y = 0; y < height; y++) {//循环行,y方向
for (int x = 0; x < width; x++) {//循环列,x方向
p=8+y*n+x/8;//当前操作的是第几个字节,前面有8个头,每行n个,所以是y*n,当前行x/8,因为按bit算点,
bit=7-x%8;//bit位置0-7,高位在前,低位在后
if (bts.get(x, y)) {//如果为true表示这个是一个黑色点,黑色点要打印
dat[p]|=0x01<
调用示例:
public class main {
public static void main(String[] args) {
String con="供应商: 乐乐rn订单号:123123123123rn提货单号:2324234234rn产品类别:10-16MMrn数量:70Trn单价:125¥rn车牌:豫C22312Arn卡号:AB23EDF323rn";
XPrinter58 printer=new XPrinter58("192.168.0.100");
printer.printTitle("中信自助装车系统");//标题
printer.printString(con);//内存
printer.printQrCode("CITIC202203150010", 256, 256);//打印二维码
printer.cut();//切纸
}
}



