ESC/POS协议打印机工具类(java)

项目要求实现远程打印,目前设备已经到货,打印机为芯烨XP-80X热敏打印机。

一:去http://www.xprinter.net/里下载使用手册和编程手册,注:其他品牌去百度搜索官网

二:去服务与支持菜单找到驱动,下载到本机,方便测试打印机,可以用本机连接打印机,进而设置相关参数,效果如下图

三:点击打印测试是否连得通打印机,如果可以的话,那么说明这台打印机的ip已经设置好,可以从电脑中拔出插口了。

四:打印机原理:实际打印机内部存在一个socekServer在时刻捕捉通过socket连接进来的线程,具体请参考另一篇socket小结,这里就不多做介绍了。

五:重点是:参考官网下载的编程手册,来发送对应格式的流数据来让打印机解决,之前百度了一下,发现并没有好的方法,所以自己只能详细阅读了一下,编程手册,用java写了一个工具类,希望能给大家帮助,直接上代码:

package com.tms.helper.utils;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.Arrays;
import java.util.List;

/**
 * Created by haha on 2017/4/25.
 */
public class EscPosUtil {
    public static final byte ESC = 27;
    public static final byte FS = 28;
    public static final byte GS = 29;
    public static final byte DLE = 16;
    public static final byte EOT = 4;
    public static final byte ENQ = 5;
    public static final byte SP = 32;
    public static final byte HT = 9;
    public static final byte LF = 10;
    public static final byte CR = 13;
    public static final byte FF = 12;
    public static final byte CAN = 24;
    /**
     * CodePage table
     */
    public static class CodePage {
        public static final byte PC437       = 0;
        public static final byte KATAKANA    = 1;
        public static final byte PC850       = 2;
        public static final byte PC860       = 3;
        public static final byte PC863       = 4;
        public static final byte PC865       = 5;
        public static final byte WPC1252     = 16;
        public static final byte PC866       = 17;
        public static final byte PC852       = 18;
        public static final byte PC858       = 19;
    }

    /**
     * BarCode table
     */
    public static class BarCode {
        public static final byte UPC_A       = 0;
        public static final byte UPC_E       = 1;
        public static final byte EAN13       = 2;
        public static final byte EAN8        = 3;
        public static final byte CODE39      = 4;
        public static final byte ITF         = 5;
        public static final byte NW7         = 6;
        //public static final byte CODE93      = 72;
         public static final byte CODE128     = 73;
    }


    /**
     * Print and line feed
     * LF
     * @return bytes for this command
     */
    public static String printLinefeed()
    {
        return "$ESC_10";
    }

    /**
     * Turn underline mode on, set at 1-dot width
     * ESC - n
     * @return bytes for this command
     */
    public static String underline1DotOn()
    {
        return "$ESC_27_45_1";
    }

    /**
     * Turn underline mode on, set at 2-dot width
     * ESC - n
     * @return bytes for this command
     */
    public static String underline2DotOn()
    {
        return "$ESC_27_45_2";
    }

    /**
     * Turn underline mode off
     * ESC - n
     * @return bytes for this command
     */
    public static String underlineOff()
    {
        return "$ESC_27_45_0";
    }


    /**
     * Initialize printer
     * Clears the data in the print buffer and resets the printer modes to the modes that were
     * in effect when the power was turned on.
     * ESC @
     * @return bytes for this command
     */
    public static String initPrinter()
    {
        return "$ESC_27_64";
    }

    /**
     * Turn emphasized mode on
     * ESC E n
     * @return bytes for this command
     */
    public static String emphasizedOn()
    {
        byte[] result = new byte[3];
        result[0] = ESC;
        result[1] = 69;
        result[2] = 0xF;
        return "$ESC_27_69_1";
    }

    /**
     * Turn emphasized mode off
     * ESC E n
     * @return bytes for this command
     */
    public static String emphasizedOff()
    {
        return "$ESC_27_69_0";
    }

    /**
     * double_strike_on
     * ESC G n
     * @return bytes for this command
     */
    public static String doubleStrikeOn()
    {
        return "$ESC_27_71_1";
    }

    /**
     * double_strike_off
     * ESC G n
     * @return bytes for this command
     */
    public static String doubleStrikeOff()
    {
        return "$ESC_27_71_0";
    }

    /**
     * Select Font A
     * ESC M n
     * @return bytes for this command
     */
    public static String selectFontA()
    {
        return "$ESC_27_77_0";
    }

    /**
     * Select Font B
     * ESC M n
     * @return bytes for this command
     */
    public static String selectFontB()
    {
        return "$ESC_27_77_1";
    }

    /**
     * Select Font C ( some printers don't have font C )
     * ESC M n
     * @return bytes for this command
     */
    public static String selectFontC()
    {
        return "$ESC_27_77_2";
    }

    /**
     * double height width mode on Font A
     * ESC ! n
     * @return bytes for this command
     */
//    public static String doubleHeightWidthOn()
//    {
//        return "$ESC_27_33_56";
//    }

    public static List<String> doubleHeightWidthOn()
    {
        return Arrays.asList("$ESC_27_33_56","$ESC_28_33_12");
    }

    /**
     * double height width mode off Font A
     * ESC ! n
     * @return bytes for this command
     */
    public static List<String> doubleHeightWidthOff()
    {
        return Arrays.asList("$ESC_27_33_0","$ESC_28_33_0");
 
  • 1
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在传统的PC应用中,通过直接调用打印机驱动程序的方式可以轻松地实现对蓝牙打印机的调用。但是,在Web应用和移动应用中,这种方式并不适合,所以我们需要寻找一种新的解决方法。 其中一种解决方案是使用JavaScript库或框架,比如原生JavaScript、jQuery和React等。这些工具可以为我们提供跨平台或跨浏览器的API,使得我们可以屏蔽底层的硬件驱动细节,从而更容易地实现对蓝牙打印机的调用。 实现蓝牙打印机的调用需要遵循ESC / POS打印机语言规范。ESC / POS是一种通用的打印机语言,被各种打印机采用,包括热敏和针式打印机。这种语言通过控制位、字符和命令来描述打印机的行为,每个命令都会发送给打印机的控制寄存器。 要实现对蓝牙打印机的调用,首先需要链接蓝牙打印机,这可以通过调用浏览器的Web Bluetooth API来完成。一旦与打印机建立连接,我们就可以通过发送ESC / POS命令来控制打印机,从而实现小票和图片的打印。 对于小票的打印,我们需要设计好小票模板并将其转换为ESC / POS命令。具体来说,需要先设置打印机的一些参数,比如字符大小和行距,然后将文本和表格等元素添加到模板中,最后将整个模板转换为ESC / POS命令并发送给打印机即可。 对于图片的打印,我们需要将图片转换为位图,并将其转换为ESC / POS命令。具体操作可以使用像CW浏览器的Canvas API在浏览器中渲染位图文件,然后将渲染后的位图文件转换为ESC / POS命令并发送给打印机即可。 总之,实现对蓝牙打印机的调用需要理解ESC / POS语言规范,并使用Web Bluetooth API和Canvas API等便利的工具来实现。虽然这种方法需要花费一些精力来学习和开发,但它可以轻松地在Web应用和移动应用中实现对蓝牙打印机的调用,具有很好的可移植性和开发效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值