Java调用TSC条码打印机接口打印条码和二维码 |
您所在的位置:网站首页 › tsc打印机亮红灯ttp_244pro › Java调用TSC条码打印机接口打印条码和二维码 |
一、背景简介
公司新买了一台TSC条码打印机,型号:TSC TTP-244 PRO,让和现有资产管理系统对接,可以根据系统上的编码直接打印。 研究了几天,终于调试出来了,下边是代码,目测可用: -- 2017.01.04:增加一些注释和解决方案 -- 2017.03.07:经调试,有两套方案可用,js直接调用和使用java applet,后续会更上。 -- 2018.12.23:js实现较简单,需要代码或有问题可邮件至 [email protected] -- 2018.12.25:增加js调试方式。 -- 2019.01.10:增加TSC applet和js打印测试包(包含打印机驱动、打印插件、applet和js调试代码),文末下载 -- 2020.05.16:原网站内容已迁移至本站,所有内容可从本站查找,有问题请留言或发邮件。 PS:评论区有本站相关文章网址,推荐使用JS实现方式,相对比较简单。 二、js实现方式 var TSCObj; TSCObj = new ActiveXObject("TSCActiveX.TSCLIB"); function tsc_print(name, code, department, model, receiveDate) { 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", "资产名称:" + name); TSCObj.ActiveXwindowsfont(450, 220, 30, 180, 2, 0, "Arial", "资产编号:" + code); TSCObj.ActiveXwindowsfont(450, 190, 30, 180, 2, 0, "Arial", "使用部门:" + department); TSCObj.ActiveXwindowsfont(450, 160, 30, 180, 2, 0, "Arial", "产品规格:" + model); TSCObj.ActiveXwindowsfont(450, 130, 30, 180, 2, 0, "Arial", "领用日期:" + receiveDate); TSCObj.ActiveXbarcode("450", "90", "128", "60", "1", "180", "2", "2", code); TSCObj.ActiveXsendcommand("QRCODE 10,15,L,5,A,0,M2,S3,\"" + code + "\""); TSCObj.ActiveXprintlabel("1", "1"); TSCObj.ActiveXcloseport(); } 三、applet实现方式 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(); } } 如何能够在线操作打印机呢? 还想传递参数,同时打印条码和二维码? 这里有一个解决方案。 使用applet,再通过java调用dll即可。 applet虽然很古老了,但用在这里只需要很少的一些代码即可,整体思路是: applet接收页面传递的参数,调用条码打印机接口打印。 JButton button; String image_path = "/images/img.jpg"; private String title = "xxxxxxxxxxxxxx"; public void init() { ImageIcon image = new ImageIcon(this.getClass().getResource(this.image_path)); button = new JButton(image); button.addActionListener(this); this.getContentPane().add(button); } @Override public void actionPerformed(ActionEvent arg0) { String code = getParameter("bar_code"); if (code != null && code != "") { this.print(code); } } public void print(String code) { // TscLibDll.INSTANCE.about(); TscLibDll.INSTANCE.openport("TSC TTP-244 Pro"); TscLibDll.INSTANCE.setup("60", "40", "5", "10", "0", "2", "0"); TscLibDll.INSTANCE.sendcommand("SET TEAR ON"); TscLibDll.INSTANCE.clearbuffer(); TscLibDll.INSTANCE.sendcommand("QRCODE 100,200,L,7,M,0,[2,7],'N123456' "); // TscLibDll.INSTANCE.sendcommand("PUTPCX 550,10,\"UL.PCX\""); TscLibDll.INSTANCE.barcode("70", "140", "128", "90", "0", "0", "2", "2", code); TscLibDll.INSTANCE.windowsfont(55, 20, 48, 0, 2, 1, "Arial", title); TscLibDll.INSTANCE.windowsfont(120, 240, 32, 0, 2, 0, "Arial", code); // 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(); }这样做呢,代码是没有问题的,而且用java直接执行打印也是可以的,但是在页面上点击打印却不行。为什么呢? 调用打印机涉及到打印驱动,由于安全问题在页面上调用代码不能被执行,有个方法是这样: SwingUtilities.invokeLater(new Runnable() { public void run() { TscLibDll.INSTANCE.openport("TSC TTP-244 Pro"); // ...... } });你会发现这样还是不行,还需要修改一个文件,位置在: D:\Program Files (x86)\Java\jre7\lib\security\java.security(这个文件是在用户端需要修改的,因为想要打印的人是用户端) 在grant中加一行: permission java.security.AllPermission;这样基本上就行了。 四、效果展示 打印出来大概是这样子: 五、相关文件 打印插件包:TSCActiveX.zip 打印测试包(包含打印机驱动、打印插件、applet和js调试代码):TSC applet和js打印测试包.rar
本文作者为新逸网络,转载请注明。 Java tsc 条码打印 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |