700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Java TSC 打印机调用java 实例 打印标签(文字 图片)

Java TSC 打印机调用java 实例 打印标签(文字 图片)

时间:2022-04-27 06:49:01

相关推荐

Java  TSC 打印机调用java 实例  打印标签(文字 图片)

本文 打印机型号:TSC TTP-243E Pro选配USB分辨率200 DPI,1 点=1/8 mm (300 DPI,1点 =1/12 mm)

一、 打印机安装

1、安装说明:官方文档

2、驱动下载:驱动

二、程序调用

1、下载官方提供的 Java 调用 范例

解压 文件,如下:

其中提供了调用实例(但会报错,找不到TSCLIB),如下

import com.sun.jna.Library;import com.sun.jna.Native;public class Main {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 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 formfeed ();int nobackfeed ();int windowsfont (int x, int y, int fontheight, int rotation, int fontstyle, int fontunderline, String szFaceName, String content);}public static void main(String[] args) {TscLibDll.INSTANCE.openport("TSC TTP-243E Pro");TscLibDll.INSTANCE.sendcommand("REM ***** This is a test by JAVA. *****");TscLibDll.INSTANCE.setup("100", "100", "5", "8", "0", "0", "0");TscLibDll.INSTANCE.clearbuffer();TscLibDll.INSTANCE.printerfont ("100", "10", "3", "0", "1", "1", "(JAVA) DLL Test!!");TscLibDll.INSTANCE.barcode("100", "40", "128", "50", "1", "0", "2", "2", "123456789");TscLibDll.INSTANCE.windowsfont(400, 200, 48, 0, 3, 1, "arial", "DEG 0");TscLibDll.INSTANCE.windowsfont(400, 200, 48, 90, 3, 1, "arial", "DEG 90");TscLibDll.INSTANCE.windowsfont(400, 200, 48, 180, 3, 1, "arial", "DEG 180");TscLibDll.INSTANCE.windowsfont(400, 200, 48, 270, 3, 1, "arial", "DEG 270");TscLibDll.INSTANCE.printlabel("1", "1");TscLibDll.INSTANCE.closeport();}}

但在,运行时会找不到.dll文件:

Exception in thread "main" java.lang.UnsatisfiedLinkError: Unable to load library 'TSCLIB': ???????¨at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:164)at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.java:237)at com.sun.jna.Library$Handler.<init>(Library.java:140)at com.sun.jna.Native.loadLibrary(Native.java:375)at com.sun.jna.Native.loadLibrary(Native.java:360)at lunch.Test$TscLibDll.<clinit>(Test.java:16)at lunch.Test.main(Test.java:39)

即使如 文档中要求Copy the “TscLib.Dll”to the folder\\WINDOWS\system32. 也是一样的效果

无奈只好另求他法,究极原因还是因为这个TscLib.Dll 找不到

2、DLL文件使用

2.1、把dll文件放到lib文件夹中,也可以放在外面,后面加载它能找到就行,如下:

2.2、 加载lib 时 build path 是找不到 .dll 文件的,需要如下的配置

快捷键 Alt + Enter 进入属性配置

2.3、新建 JniDll 类实例化接口,加载TSCLIB

package lunch;import com.sun.jna.Library;import com.sun.jna.Native;public class JniDll {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 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 formfeed ();int nobackfeed ();int windowsfont (int x, int y, int fontheight, int rotation, int fontstyle, int fontunderline, String szFaceName, String content);}}

2.4、调用 jni方法

package lunch;import lunch.JniDll.TscLibDll;public class Tester {// 加载 dll 库文件static {System.loadLibrary("TSCLIB");}public static void main(String[] args) {// TODO Auto-generated method stubSystem.out.println("------test----------");//设置打印机型号TscLibDll.INSTANCE.openport("TSC TTP-243E Pro");TscLibDll.INSTANCE.sendcommand("REM ***** This is a test by JAVA. *****");//设置标签的寬度、高度、列印速度、列印浓度、感应器类别、gap/black mark 垂直间距(标签纸的间距)、gap/black mark 偏移距离)TscLibDll.INSTANCE.setup("60", "40", "5", "8", "0", "3", "0");TscLibDll.INSTANCE.clearbuffer();//打印文本TscLibDll.INSTANCE.printerfont ("20", "10", "3", "0", "1", "1", "(JAVA) DLL Test!!");//设置条形码(不是二维码) 位置、大小、内容TscLibDll.INSTANCE.barcode("40", "30", "128", "50", "1", "0", "2", "2", "123456789");//设置文本(位置(x,y)、大小、旋转角度、文字X方向放大倍率,1~8 、文字Y方向放大倍率,1~8 字体arial、内容)TscLibDll.INSTANCE.windowsfont(40, 20, 18, 0, 3, 1, "arial", "DEG 0");TscLibDll.INSTANCE.windowsfont(400, 200, 48, 90, 3, 1, "arial", "DEG 90");TscLibDll.INSTANCE.windowsfont(400, 200, 48, 180, 3, 1, "arial", "DEG 180");TscLibDll.INSTANCE.windowsfont(400, 200, 48, 270, 3, 1, "arial", "DEG 270");//画框String command = "BOX 16,24,296,168,3"; TscLibDll.INSTANCE.sendcommand(command); //打印的页数TscLibDll.INSTANCE.printlabel("1", "1");TscLibDll.INSTANCE.closeport();}}

注:文档的接口中提供的接口barcode(a,b,c,d,e,f,g,h,I)打印的是条形码,若想实现打印二维码需要自定义命令, 接口说明可去参考提供的 Android 接口文档

详细指令请参考TSPL

public static void TscPrinter(String qrCode,String mT,String mS,String mD) {// TODO Auto-generated method stubSystem.setProperty("jna.encoding", "GBK");// 支持中文String mP ="P:15869423";String mV ="V:45821";//初始化 :设备 型号TscLibDll.INSTANCE.openport("TSC TTP-243E Pro");//參數:// a: 字串型別,設定標籤寬度,單位 mm// b: 字串型別,設定標籤高度,單位 mm// c: 字串型別,設定列印速度,(列印速度隨機型不同而有不同的選項)//1.0: 每秒1.0吋列印速度//1.5: 每秒1.5吋列印速度//2.0: 每秒2.0吋列印速度//3.0: 每秒3.0吋列印速度//4.0: 每秒4.0吋列印速度//6.0: 每秒6.0吋列印速度//8.0: 每秒8.0吋列印速度//10.0: 每秒10.0吋列印速度// d: 字串型別,設定列印濃度,//0~15,數字愈大列印結果愈黑// e: 字串型別,設定使用感應器類別//0 表示使用垂直間距感測器(gap sensor)//1 表示使用黑標感測器(black mark sensor)// f: 字串型別,設定gap/black mark 垂直間距高度,單位: mm// g: 字串型別,設定gap/black mark 偏移距離,單位: mm,此參數若使用一般標籤時均設為0 TscLibDll.INSTANCE.setup("60","40","4","12","0","3","0");TscLibDll.INSTANCE.clearbuffer();//二维码的 位置 、大小、内容//80 80(x,y位置,8:大小)String command = "QRCODE 70,80,Q,5,A,0,M2,S7,\"" + qrCode+"\""; //String command = "QRCODE 80,80,Q,8,A,0,M2,S7,\"" + qrCode+"\""; //传送指令TscLibDll.INSTANCE.sendcommand(command); TscLibDll.INSTANCE.windowsfont(70, 10, 80, 0, 2, 0, "Segoe UI Black", "Title");TscLibDll.INSTANCE.windowsfont (360, 30, 32,0, 2, 0, "arial", "Logo");//說明: 使用 Windows TTF 字型列印文字//參數://a: 整數型別,文字 X 方向起始點,以點(point)表示。//b: 整數型別,文字 Y 方向起始點,以點(point)表示。//c: 整數型別,字體高度,以點(point)表示。//d: 整數型別,旋轉角度,逆時鐘方向旋轉//0 -> 0 degree//90-> 90 degree//180-> 180 degree//270-> 270 degree//e: 整數型別,字體外形//0-> 標準(Normal)//1-> 斜體(Italic)//2-> 粗體(Bold)//3-> 粗斜體(Bold and Italic)//f: 整數型別, 底線//0-> 無底線//1-> 加底線//g: 字串型別,字體名稱。如: Arial, Times new Roman, 細名體, 標楷體//h: 字串型別,列印文字內容。TscLibDll.INSTANCE.windowsfont(250, 80, 32, 0, 2, 0, "arial", mP);TscLibDll.INSTANCE.windowsfont(250, 110, 32, 0, 2, 0, "arial", mT);TscLibDll.INSTANCE.windowsfont(250, 140, 31, 0, 2, 0, "arial", mV);TscLibDll.INSTANCE.windowsfont(245, 170, 31, 0, 2, 0, "arial", mS);TscLibDll.INSTANCE.windowsfont(250, 210, 31, 0, 2, 0, "arial", mD);TscLibDll.INSTANCE.windowsfont (120, 247, 35,0, 2, 0, "Microsoft YaHei UI", "MADE IN CHINA");//打印的页数 3:三页TscLibDll.INSTANCE.printlabel("1", "3");TscLibDll.INSTANCE.closeport();}

***关于打印图片

根据文档指令

需先将 想要打印的图片,转换成 .pcx 格式,放在本地路径下

然后根据 命令,打印

/* * C:\\11.PCX图片路径* 11.PCX图片名* */TscLibDll.INSTANCE.downloadpcx("C:\\11.PCX", "11.PCX");/*** * (x,y)* 70 ,15* */TscLibDll.INSTANCE.sendcommand("PUTPCX 70,15,\"11.PCX\"");

**注意:运行后,若是报以下错误 ,说明你所依赖的 .dll 文件是 32位的

Exception in thread "main" java.lang.UnsatisfiedLinkError: D:\AecplseWorksp\Tsc\lib\TSCLIB.dll: Can't load IA 32-bit .dll on a AMD 64-bit platformat java.lang.ClassLoader$NativeLibrary.load(Native Method)at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1929)at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1847)at java.lang.Runtime.loadLibrary0(Runtime.java:870)at java.lang.System.loadLibrary(System.java:1119)at com.tsc.lunch.Test.<clinit>(Test.java:15)

若是你的环境是64位的,请将你的JDK 环境 换成 32位的 同样 eclipse也是32位的才行

亦或者可以去下载 对应的 .dll 文件,附上地址:Windows dall 下载

解压如下:

注意:打包JRE时,若是加载不到 dll文件,改为如下

// 加载 dll 库static {// System.loadLibrary("TSCLIB");//打包时用,否则加载不到 dll库System.loadLibrary("lib/TSCLIB");}

结束:奉上测试时打印的 标签

若想Android实现 可参考上一篇文文章 :Android 连接 TSC打印机, 打印标签(文字、图片)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。