Java调用标签打印机

标签打印机:TSC  TTP-244 Pro

官方资料下载地址:https://www.chinatsc.cn/zh-CN/downloads

打印机驱动:

 官方给出的Java参考案例:

 

官方文档预览下载: https://fs.chinatsc.cn/system/files/tspl_tspl2_programming_2018_5_17_scjiantizhongwen.pdf

 

官方Java案例

package com.tscsdk;


import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

import com.sun.jna.Library;
import com.sun.jna.Native;
public class JavaTest 
{

    public interface TscLibDll extends Library
    {		
		TscLibDll INSTANCE = (TscLibDll) Native.loadLibrary("\\TSCLIB", TscLibDll.class);
        int about ();
        int openport (String pirnterName);
        int closeport ();
        int sendcommand (String printerCommand);
        int sendBinaryData (byte[] printerCommand, int CommandLength);
        int setup (String width,String height,String speed,String density,String sensor,String vertical,String offset);
        int downloadpcx (String filename,String image_name);
        int barcode (String x,String y,String type,String height,String readable,String rotation,String narrow,String wide,String code);
        int printerfont (String x,String y,String fonttype,String rotation,String xmul,String ymul,String text);
        int clearbuffer ();
        int printlabel (String set, String copy);        
        int windowsfont (int x, int y, int fontheight, int rotation, int fontstyle, int fontunderline, String szFaceName, String content);
        int windowsfontUnicode(int x, int y, int fontheight, int rotation, int fontstyle, int fontunderline, String szFaceName, byte[] content);
        int windowsfontUnicodeLengh(int x, int y, int fontheight, int rotation, int fontstyle, int fontunderline, String szFaceName, byte[] content, int length);
        byte usbportqueryprinter();
        
    }
    // 使用
    public static void main(String[] args)
    {

        String WT1 = "TSC Printers";
        String B1 = "20080101";

    	//unicode format
    	byte[] result_unicode = new byte[1024];
    	String word_unicode = "简体中文测试123123繁體測試";
    	result_unicode = word_unicode.getBytes(StandardCharsets.UTF_16LE);

    	//utf-8 format
    	byte[] result_utf8 = new byte[1024];
    	String word_utf8 = "TEXT 40,620,\"ARIAL.TTF\",0,12,12,\"utf8 test Wörter auf Deutsch\"";
    	result_utf8 = word_utf8.getBytes(StandardCharsets.UTF_8);

    	
        //TSCLIB_DLL.about();
        byte status = TscLibDll.INSTANCE.usbportqueryprinter();//0 = idle, 1 = head open, 16 = pause, following <ESC>!? command of TSPL manual
        TscLibDll.INSTANCE.openport("TSC TE210");
        TscLibDll.INSTANCE.sendcommand("SIZE 100 mm, 120 mm");
        TscLibDll.INSTANCE.sendcommand("SPEED 4");
        TscLibDll.INSTANCE.sendcommand("DENSITY 12");
        TscLibDll.INSTANCE.sendcommand("DIRECTION 1");
        TscLibDll.INSTANCE.sendcommand("SET TEAR ON");
        TscLibDll.INSTANCE.sendcommand("CODEPAGE UTF-8");
        TscLibDll.INSTANCE.clearbuffer();
        TscLibDll.INSTANCE.downloadpcx("\\UL.PCX", "UL.PCX");
        TscLibDll.INSTANCE.windowsfont(40, 490, 48, 0, 0, 0, "Arial", "Windows Font Test");
        TscLibDll.INSTANCE.windowsfontUnicodeLengh(40, 550, 48, 0, 0, 0, "Arial", result_unicode,word_unicode.length());
        TscLibDll.INSTANCE.sendcommand("PUTPCX 40,40,\"UL.PCX\"");
        TscLibDll.INSTANCE.sendBinaryData(result_utf8, result_utf8.length);
        TscLibDll.INSTANCE.barcode("40", "300", "128", "80", "1", "0", "2", "2", B1);
        TscLibDll.INSTANCE.printerfont("40", "440", "0", "0", "15", "15", WT1);
        TscLibDll.INSTANCE.printlabel("1", "1");
        TscLibDll.INSTANCE.closeport();
    	
    }

}

  

使用案例

1、连续打印条码,条码流水号递增

public static void demo() {
        // 支持中文
        System.setProperty("jna.encoding","GBK");
        // 打印机名称
        TscLibDll.INSTANCE.openport("TSC TTP-244");
        TscLibDll.INSTANCE.clearbuffer();
        // 标签宽高
        TscLibDll.INSTANCE.sendcommand("SIZE 70mm,70mm");
        // 标签间距
        TscLibDll.INSTANCE.sendcommand("GAP 3mm, 0mmn");
        TscLibDll.INSTANCE.sendcommand("DIRECTION 1");
        // 打印机有61个计数器(@0 ~ @60),使用@1计数器,递增1
        TscLibDll.INSTANCE.sendcommand("SET COUNTER @1 1");
        // 设置@1数据
        TscLibDll.INSTANCE.sendcommand("@1 =\"01-XT01-2303220001\"");
        TscLibDll.INSTANCE.sendcommand("CLS");
        // 打印条码,内容使用@1数据
        TscLibDll.INSTANCE.sendcommand("BARCODE 100,50,\"128\",100,1,0,2,2,@1");
        // 打印次数
        TscLibDll.INSTANCE.sendcommand("PRINT 1,5);
        TscLibDll.INSTANCE.closeport();
}

打印结果

01-XT01-2303220001

01-XT01-2303220002

01-XT01-2303220003

01-XT01-2303220004

01-XT01-2303220005

2、打印二维码

文档搜索 QRCODE 查看详细说明

TscLibDll.INSTANCE.sendcommand("QRCODE 10,10,H,4,A,0,\"ABCabc123\"");

3、打印条码

文档搜索 BARCODE 查看详细说明

TscLibDll.INSTANCE.sendcommand("BARCODE 10,50,\"128\",100,1,0,2,2,\"123456\"");

4、打印线条

文档搜索 BAR 查看详细说明

TscLibDll.INSTANCE.sendcommand("BAR 80,80,100,100");

 

posted @ 2023-03-22 15:49  孙浩月  阅读(1002)  评论(0编辑  收藏  举报