斑马射频打印机同时操作打印、写入EPC、读TID

所需资源下载:ts24.lib和斑马打印机API的jar包-Java文档类资源-CSDN下载

1.首先引入ts24.lib和斑马打印机API的jar包(ZSDK_API.jar),确保打包后能将ts24.lib打进去,如果用Idea,引入外部jar需要点击左上角 File -> Project Structure -> Modules -> Dependencies然后点击加号加进去

2.将dll文件以同样的方式引入,视系统选择32/64的dll(File -> Project Structure -> Modules -> Dependencies然后点击加号加进去)

3.下面就是调用斑马打印机并发送ZPL命令代码

import com.modules.business.dto.PrintDataBackDTO;
import com.zebra.sdk.comm.Connection;
import com.zebra.sdk.comm.ConnectionException;
import com.zebra.sdk.comm.DriverPrinterConnection;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;

import javax.print.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

/**
 * 连接斑马打印机 发送zpl命令
 */
@Slf4j
public class ZplPrinter {
    private PrintService printService = null;
    private byte[] dotFont;

    /**
     * 标签模板
     */
    public static String printModel(String printerName, String caseName, String physicalCode, String physicalName, String sendMsg, String tidMsg) {
        ZplPrinter p = new ZplPrinter(printerName);
        StringBuilder begin = new StringBuilder();
        StringBuilder content = new StringBuilder();

        // 二维条形码
        content.append("^XA^FO160,160^BY2,3.0,100^BCN,,Y,N,N,^FD").append(physicalCode).append("^FS");
        // 事件名称
        PrintDataBackDTO d = p.getText("事件名称:" + caseName, 160, 70);
        begin.append(d.getBegin());
        content.append(d.getContent());
        // 检材名称
        PrintDataBackDTO n = p.getText("检材名称:" + physicalName, 160, 320);
        begin.append(n.getBegin());
        content.append(n.getContent());
        // 写入EPC值
        content.append(sendMsg);
        // 读取TID值
        content.append(tidMsg);

        return begin.toString() + content.toString() + "^XZ";
    }

    /**
     * 获取TID
     */
    public static String getTID(String printerName,String send){
        Connection conn = null;
        try {
            conn = new DriverPrinterConnection(printerName);
            conn.open();
            byte[] zpl = send.getBytes();
            byte[] tid = conn.sendAndWaitForResponse(zpl, 500, 500, null);
            return new String(tid, StandardCharsets.UTF_8);
        } catch (ConnectionException e) {
            log.error("获取TID出错:", e);
        } finally {
            try {
                if (conn != null)
                    conn.close();
            } catch (ConnectionException e) {
                log.error("关闭流出错:", e);
            }
        }

        return null;
    }

    /**
     * 构造方法
     *
     * @param printerURI 打印机路径/名称
     */
    ZplPrinter(String printerURI) {
        try {
            InputStream fis = new ClassPathResource("lib/ts24.lib").getInputStream();
            dotFont = new byte[fis.available()];
            fis.read(dotFont);
            fis.close();
        } catch (IOException e) {
            log.error("连接打印机出错:", e);
            return;
        }

        //初始化打印机
        PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
        if (services != null && services.length > 0) {
            for (PrintService service : services) {
                if (printerURI.equals(service.getName())) {
                    printService = service;
                    break;
                }
            }
        }
    }

    /**
     * 打印内容
     *
     * @param str 内容
     * @param x   x坐标
     * @param y   y坐标
     */
    private PrintDataBackDTO getText(String str, int x, int y) {
        PrintDataBackDTO result = new PrintDataBackDTO();
        StringBuilder begin = new StringBuilder();
        begin.append("^XA~SD22^PW800");
        StringBuilder content = new StringBuilder();
        byte[] ch = str2bytes(str);
        for (int off = 0; off < ch.length; ) {
            if (((int) ch[off] & 0x00ff) >= 0xA0) {
                try {
                    //中文字符
                    int qcode = ch[off] & 0xff;
                    int wcode = ch[off + 1] & 0xff;
                    content.append(String.format("^FO%d,%d^XG0000%01X%01X,%d,%d^FS\n", x, y, qcode, wcode, 1, 1));
                    begin.append(String.format("~DG0000%02X%02X,00072,003,\n", qcode, wcode));
                    qcode = (qcode + 128 - 32) & 0x00ff;
                    wcode = (wcode + 128 - 32) & 0x00ff;
                    int offset = (qcode - 16) * 94 * 72 + (wcode - 1) * 72;
                    for (int j = 0; j < 72; j += 3) {
                        qcode = (int) dotFont[j + offset] & 0x00ff;
                        wcode = (int) dotFont[j + offset + 1] & 0x00ff;
                        int qcode1 = (int) dotFont[j + offset + 2] & 0x00ff;
                        begin.append(String.format("%02X%02X%02X\n", qcode, wcode, qcode1));
                    }
                    x = x + 24;
                    off = off + 2;
                } catch (Exception e) {
                    //替换成X号
                    content.append(getChar("X", x, y, 30, 20));
                    x = x + 15;
                    off = off + 2;
                }
            } else if (((int) ch[off] & 0x00FF) < 0xA0) {
                content.append(getChar(String.format("%c", ch[off]), x, y, 30, 20));
                x = x + 15;
                off++;
            }
        }

        result.setBegin(begin);
        result.setContent(content);
        return result;
    }

    /**
     * 英文字符串(包含数字)
     *
     * @param str 英文字符串
     * @param x   x坐标
     * @param y   y坐标
     * @param h   高度
     * @param w   宽度
     */
    private String getChar(String str, int x, int y, int h, int w) {
        return "^FO" + x + "," + y + "^A0," + h + "," + w + "^FD" + str + "^FS";
    }

    /**
     * 打印
     */
    private boolean print(String zpl) {
        boolean result = false;
        if (printService != null) {
            try {
                DocPrintJob job = printService.createPrintJob();
                byte[] by = zpl.getBytes();
                DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
                Doc doc = new SimpleDoc(by, flavor, null);
                job.print(doc, null);
                result = true;
            } catch (PrintException e) {
                log.error("---打印出错---", e);
            }
        }

        return result;
    }

    /**
     * 字符串转byte[]
     */
    private byte[] str2bytes(String s) {
        if (null == s || "".equals(s)) {
            return null;
        }
        byte[] abytes = null;
        try {
            abytes = s.getBytes("gb2312");
        } catch (UnsupportedEncodingException ex) {
            log.error("字符串转byte出错");
        }

        return abytes;
    }

    public static void main(String[] args) {
        String printerName = "ZDesigner ZD500R-203dpi ZPL";
        String code = "020226-1-JC2";
        // 写EPC命令
        String epcMsg = "^RB24,4,4,4,4,4,4^RQE^FD" + code;
        // 读TID命令
        String tidMsg = "^FN1^RFR,H,0,12,2^FS^HV1,64^FS";
        // 打印标签、向打印的标签中写EPC值
        String send = ZplPrinter.printModel(printerName, "神奇的发现", code, "5光年宽大气团", epcMsg, tidMsg);
        String tid = ZplPrinter.getTID(printerName, send);
        System.out.println(tid);
    }
}

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
@Data
@ApiModel(value = "打印标签返回参数")
public class PrintDataBackDTO implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "头部信息")
    private StringBuilder begin;

    @ApiModelProperty(value = "内容")
    private StringBuilder content;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值