Java呼叫TSC條碼印表機介面列印條碼和二維碼
阿新 • • 發佈:2019-01-26
公司新買了一臺TSC條碼印表機,型號:TSC TTP-244 PRO,讓和現有資產管理系統對接,可以根據系統上的編碼直接列印。
研究了幾天,終於調試出來了,下邊是程式碼,,目測可用:
java後臺除錯程式碼(連線好印表機後可直接列印,用於直接除錯):
import java.io.UnsupportedEncodingException; import com.sun.jna.win32.StdCallLibrary; import com.sun.jna.Native; public class TscMain { public interface TscLibDll extends StdCallLibrary { 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) throws UnsupportedEncodingException { System.setProperty("jna.encoding", "GBK");// 支援中文 // TscLibDll.INSTANCE.about(); TscLibDll.INSTANCE.openport("TSC TTP-244 Pro"); // TscLibDll.INSTANCE.downloadpcx("C:\\UL.PCX", "UL.PCX");// 列印圖片時需要先下載到裝置 // TscLibDll.INSTANCE.sendcommand("REM ***** This is a test by JAVA. *****"); TscLibDll.INSTANCE.setup("60", "40", "5", "15", "0", "2", "0"); TscLibDll.INSTANCE.sendcommand("SET TEAR ON"); TscLibDll.INSTANCE.clearbuffer(); String command = "QRCODE 300,70,L,6,A,0,M2,S3,\"123456\"";// 列印二維碼 TscLibDll.INSTANCE.sendcommand(command); // TscLibDll.INSTANCE.sendcommand("PUTPCX 550,10,\"UL.PCX\"");// 圖片位置 // TscLibDll.INSTANCE.printerfont("100", "50", "TSS24.BF2", "0", "1", "1", "Technology"); TscLibDll.INSTANCE.barcode("70", "140", "128", "90", "0", "0", "2", "2", "A123456789");// 列印內容,引數是位置和字型 TscLibDll.INSTANCE.windowsfont(15, 15, 40, 0, 2, 1, "Arial", "網路科技公司"); TscLibDll.INSTANCE.windowsfont(30, 90, 32, 0, 2, 0, "Arial", "--- 研發部"); TscLibDll.INSTANCE.windowsfont(120, 240, 32, 0, 2, 0, "Arial", "A123456789"); TscLibDll.INSTANCE.printlabel("1", "1"); TscLibDll.INSTANCE.closeport(); } }
如何能夠線上操作印表機呢?還想傳遞引數,同時列印條碼和二維碼?這裡有兩個解決方案。
2、直接在頁面上使用JS列印二維碼。
<script type="text/javascript"> var TSCObj = new ActiveXObject("TSCActiveX.TSCLIB"); TSCObj.ActiveXopenport("TSC TTP-244 Pro"); TSCObj.ActiveXsetup("60", "40", "5", "12", "0", "2", "0"); TSCObj.ActiveXsendcommand("SET TEAR ON"); TSCObj.ActiveXclearbuffer(); TSCObj.ActiveXwindowsfont(230, 310, 48, 180, 2, 0, "Arial", "固定資產標識卡"); TSCObj.ActiveXwindowsfont(500, 305, 48, 180, 2, 1, "Arial", " "); TSCObj.ActiveXwindowsfont(450, 250, 30, 180, 2, 0, "Arial", "資產名稱:測試資產"); TSCObj.ActiveXwindowsfont(450, 220, 30, 180, 2, 0, "Arial", "資產編號:20171012100003"); TSCObj.ActiveXwindowsfont(450, 190, 30, 180, 2, 0, "Arial", "使用部門:研發中心"); TSCObj.ActiveXwindowsfont(450, 160, 30, 180, 2, 0, "Arial", "產品規格:TEST"); TSCObj.ActiveXwindowsfont(450, 130, 30, 180, 2, 0, "Arial", "購置日期:2017-12-05"); TSCObj.ActiveXbarcode("450", "90", "128", "60", "1", "180", "2", "2", "20171012100003"); // TSCObj.ActiveXsendcommand("QRCODE 10,15,L,5,A,0,M2,S3,\"20171012100003\""); TSCObj.ActiveXsendcommand("QRCODE 12,10,L,3,A,0,M2,S3,\"http://qr.xxxxx.cn/20171012100003\""); TSCObj.ActiveXprintlabel("1", "1"); TSCObj.ActiveXcloseport(); </script>
- 注意:只能用IE下列印,使用前必須先註冊驅動;如果不能列印,試試把安全選項全部啟動。
- 以上程式碼本地已除錯成功,有需要的請留言。
- Java後臺直接生成二維碼請參見另一篇文章。
打印出來大概是這樣子:
-- 2017.01.04更新:增加一些註釋和解決方案
-- 2017.12.19更新:增加JS列印條碼的方法