首先看一段简单代码,来实现打印文本的功能。
Socket socket = socket = new Socket(); // 设置发送地址 SocketAddress addr = new InetSocketAddress("127.0.0.1", 9100); // 超时1秒,并连接服务器 socket.connect(addr, 1000); OutputStream socketOut = socket.getOutputStream(); socket.isClosed(); OutputStreamWriter writer = new OutputStreamWriter(socketOut, "GBK"); try{ //初始化打印机 writer.write(0x1B); writer.write(0x40); writer.flush(); //打印文本 writer.write("文本内容"); writer.write("\n"); writer.write("\n"); writer.flush(); //切纸 writer.write(0x1D); writer.write(86); writer.write(65); writer.write(0); writer.flush(); }finally { writer.close(); socketOut.close(); socket.close(); }
下面了解几条常见的指令
(1)设置字体居中(居左0、居右2)
writer.write(0x1B); writer.write(97); writer.write(1);//字体居中
(2)调整字体大小
writer.write(0x1D); writer.write(33); //横向放大一倍 要将纵向放大与横向放大求和 writer.write(17);
(3)设置字体大小默认
//设置字体大小默认 writer.write(0x1D); writer.write(33); writer.write(0);
下面是条码相关的指令:
(1)设置条码的位置
writer.write(0x1B); writer.write(97); //设置条码居中 writer.write(1);
(2)设置条码的宽度
writer.write(0x1D); writer.write('w'); writer.write(2);//默认是2 2-6 之间 writer.flush();
(3)设置条码的高度
//设置条形码的高度 writer.write(0x1D); writer.write('h'); writer.write(120);//默认是60 writer.flush();
(3)条码注释打印在条码下方
writer.write(0x1D); writer.write(72); writer.write(2);
(4)打印条码
扫描二维码关注公众号,回复:
303341 查看本文章
![](/qrcode.jpg)
writer.write(0x1D); writer.write('k'); //选择code128 writer.write(73); //设置字符个数 writer.write(14); //使用CODEB来打印 writer.write(123); writer.write(66); //条形码内容 writer.write(value); writer.flush();