Java发送ZPL指令进行打印

跟物流UPS和FedEx接口集成后发现接口物流单返回的是Base64加密的ZPL指令,因为当时不知道如何使用ABAP发送ZPL指令打印,所以就研究了一下Java

一、使用java.util.Base64进行解码

1、解码代码:

//Base64编码的ZPL指令
String label = "Cl5YQQ0KXkxSTg0KXk1OWQ0KXk1GTixODQpeTEgxMCwxMg0KXk1DWQ0KXlBPSQ0KXlBXODEyDQpeQ0kyNw0KXkZPMTUsN15BME4sMjAsMjReRlZIVU5USU5HVE9OIEJSQVNTXkZTDQpeRk8xNSwyN15BME4sMjAsMjReRlY3MTQzNzMyNzg4XkZTDQpeRk8xNSw0N15BME4sMjAsMjReRlYxMTEwMCBEQU5BIENJUkNMRV5GUw0KXkZPMTUsNjdeQTBOLDIwLDI0XkZWQ1lQUkVTUyAgQ0EgOTA2MzBeRlMNCl5GTzE1LDE0Ml5BME4sMjgsMzJeRlZTSElQIFRPOiBeRlMNCl5GTzYxLDE2Nl5BME4sMjgsMzJeRlYxLTk1NC0zNDUtNDY0Ml5GUw0KXkZPNjEsMTk0XkEwTiwyOCwzMl5GVkpFVFRBIFdISVJMUE9PTCBCQVRIUyBJTkMuXkZTDQpeRk82MSwyMjJeQTBOLDI4LDMyXkZWMTUyNTAgTi4gSEFZREVOIFJELiBTVUlURSAjMl5GUw0KXkZPNjEsMjUxXkEwTiw0NSw0NF5GVlNDT1RUU0RBTEUgIEFaICA4NTI2MF5GUw0KXkZPNDQ2LDleQTBOLDMwLDM0XkZWMyBMQlNeRlMNCl5GTzY4Myw5XkEwTiwyOCwzMl5GVjEgT0YgMV5GUw0KXkZPNTA4LDUxXkEwTiwyMiwyNl5GVkRXVDogMTEsNyw2XkZTDQpeRk81OTksMzY5XkEwTiw1Niw1OF5GVlNBTVBMRV5GUw0KXkZPMTA2LDQ1Ml5BME4sMzAsMzReRlZVUFNeRlMNCl5GTzY0LDQ4Ml5BME4sMzAsMzReRlZNQVhJQ09ERV5GUw0KXkZPMzgsNTEyXkEwTiwzMCwz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QjZDMDANCkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGMDBDMUI2QzAwDQowMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwQzFCNkMwMA0KMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMEMxOUNDMDANCjAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDBDMTlDQzAwDQowMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwQzE5Q0MwMA0KMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMEMxODhDMDANCjAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwDQowMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMA0KMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDANCl5ETg0KXlhaDQo=";
//Base64解码成字节
byte[] decodedBytes = Base64.getDecoder().decode(label);
//Java字节转String
String zplCode = new String(decodedBytes);
//控制台打印
System.out.println(zplCode);

 

2、解码后的得到的ZPL指令:

3、把ZPL指令转成图片进行预览(可以把ZPL指令贴到网站http://labelary.com/viewer.html进行标签的预览)

 

 

二、发送ZPL指令给斑马打印机打印

1、maven引入字符串工具包

<!--Hutool-->
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.2.3</version>
</dependency>

2、使用工具类去掉ZPL指令的换行符

//去掉换行符号
zplCode = StrUtil.removeAllLineBreaks(zplCode);

3、maven引入zebra的api包(https://mvnrepository.com/artifact/com.zebra/zsdk-api

<!-- Zebar -->
<dependency>
    <groupId>com.zebra</groupId>
    <artifactId>zsdk-api</artifactId>
    <version>2.14.5198</version>
</dependency>

4、打印机设置

ps:一般会用2种方式打印,一个是本机USB方式打印,另一个是远程IP方式打印。但是在实际应用中,需要将USB打印机接打印共享服务器,做一些配置后实现网络打印,也就是远程IP方式打印

5、使用zebra的api进行打印(有关如何使用zebra api,请访问https://techdocs.zebra.com/link-os/2-14/pc/

import java.util.Base64;

import com.zebra.sdk.comm.Connection;
import com.zebra.sdk.comm.ConnectionException;
import com.zebra.sdk.comm.TcpConnection;
import com.zebra.sdk.printer.discovery.DiscoveredPrinterDriver;
import com.zebra.sdk.printer.discovery.UsbDiscoverer;

import cn.hutool.core.util.StrUtil;

/**
 * @author Amell
 * 2022年5月8日 上午8:08:12 
 * 
 */

public class ZplPrint {
    
    public static void main(String[] args) throws Exception {
        //Base64编码的ZPL指令
        String label = "Cl5YQQ0KXkxSTg0KXk1OWQ0KXk1GTixODQpeTEgxMCwxMg0KXk1DWQ0KXlBPSQ0KXlBXODEyDQpeQ0kyNw0KXkZPMTUsN15BME4sMjAsMjReRlZIVU5USU5HVE9OIEJSQVNTXkZTDQpeRk8xNSwyN15BME4sMjAsMjReRlY3MTQzNzMyNzg4XkZTDQpeRk8xNSw0N15BME4sMjAsMjReRlYxMTEwMCBEQU5BIENJUkNMRV5GUw0KXkZPMTUsNjdeQTBOLDIwLDI0XkZWQ1lQUkVTUyAgQ0EgOTA2MzBeRlMNCl5GTzE1LDE0Ml5BME4sMjgsMzJeRlZTSElQIFRPOiBeRlMNCl5GTzYxLDE2Nl5BME4sMjgsMzJeRlYxLTk1NC0zNDUtNDY0Ml5GUw0KXkZPNjEsMTk0XkEwTiwyOCwzMl5GVkpFVFRBIFdISVJMUE9PTCBCQVRIUyBJTkMuXkZTDQpeRk82MSwyMjJeQTBOLDI4LDMyXkZWMTUyNTAgTi4gSEFZREVOIFJELiBTVUlURSAjMl5GUw0KXkZPNjEsMjUxXkEwTiw0NSw0NF5GVlNDT1RUU0RBTEUgIEFaICA4NTI2MF5GUw0KXkZPNDQ2LDleQTBOLDMwLDM0XkZWMyBMQlNeRlMNCl5GTzY4Myw5XkEwTiwyOCwzMl5GVjEgT0YgMV5GUw0KXkZPNTA4LDUxXkEwTiwyMiwyNl5GVkRXVDogM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ZGRkYwMEMxRTNDMDANCkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGMDBDMUUzQzAwDQpGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRjAwQzFBMkMwMA0KRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkYwMEMxQjZDMDANCkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGMDBDMUI2QzAwDQowMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwQzFCNkMwMA0KMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMEMxOUNDMDANCjAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDBDMTlDQzAwDQowMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwQzE5Q0MwMA0KMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMEMxODhDMDANCjAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwDQowMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMA0KMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDANCl5ETg0KXlhaDQo=";
        //Base64解码成字节
        byte[] decodedBytes = Base64.getDecoder().decode(label);
        //Java字节转String
        String zplCode = new String(decodedBytes);
        //去掉换行符号
        zplCode = StrUtil.removeAllLineBreaks(zplCode);
        //打印
        new ZplPrint().sendZplToPrint("TCP/IP",zplCode);
        
    }

    private void sendZplToPrint(String printType,String zplCode) throws ConnectionException {
        
        Connection thePrinterConn = null;
        
        if(printType.equals("TCP/IP")) {
            thePrinterConn = new TcpConnection("192.168.xx.xx", 9100);
        }else if (printType.equals("USB")){
            DiscoveredPrinterDriver[] printerConnections = UsbDiscoverer.getZebraDriverPrinters();
            //System.out.println("printerConnections 共有:" + printerConnections.length + "个打印机");
            /*for (DiscoveredPrinterDriver driver : printerConnections) {
                System.out.println("设备有:" + driver.printerName);
                System.out.println("端口:" + Arrays.toString(driver.portNames));
            }*/
            //默认获取第一个打印机
            thePrinterConn = printerConnections[0].getConnection();
        }
        try {
            // Open the connection - physical connection is established here.
            thePrinterConn.open();
            // Send the data to printer as a byte array.
            thePrinterConn.write(zplCode.getBytes());
        } catch (ConnectionException e) {
            // Handle communications error here.
            e.printStackTrace();
        } finally {
            // Close the connection to release resources.
            thePrinterConn.close();
        }
        
    }
}

 

posted @ 2023-05-09 17:48  鲸与海  阅读(418)  评论(2编辑  收藏  举报