700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > java调用i5100-300驱动连接打印机打印标签

java调用i5100-300驱动连接打印机打印标签

时间:2024-06-14 03:31:16

相关推荐

java调用i5100-300驱动连接打印机打印标签

一、前期准备

java jdk为11保证本机已经安装i5100-300的打印机驱动保证本机能拼通打印机的ip地址

相关配置可查看下面的文章

i5100-300打印机驱动的安装与配置

二、pom文件依赖及其配置

<properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><piler.source>11</piler.source><piler.target>11</piler.target></properties><dependencies><dependency><groupId>org.openjfx</groupId><artifactId>javafx-controls</artifactId><version>11</version></dependency><dependency><groupId>org.openjfx</groupId><artifactId>javafx-fxml</artifactId><version>11</version></dependency><dependency><groupId>com.jfoenix</groupId><artifactId>jfoenix</artifactId><version>9.0.9</version></dependency><dependency><groupId>net.java.dev.jna</groupId><artifactId>jna</artifactId><version>5.5.0</version></dependency><dependency><groupId>com.google.zxing</groupId><artifactId>core</artifactId><version>3.4.0</version></dependency></dependencies>

三、i5100-300的模板类

package com.yq.printer;import javax.print.*;import javax.print.attribute.DocAttributeSet;import javax.print.attribute.HashDocAttributeSet;import javax.print.attribute.HashPrintRequestAttributeSet;import javax.print.attribute.PrintRequestAttributeSet;import java.io.ByteArrayInputStream;import java.io.IOException;import java.nio.charset.StandardCharsets;import java.time.LocalDate;import java.util.logging.Level;import java.util.logging.Logger;/*** i5100 打印机模板* @author **/public class TemplateI5100{private static Logger log = Logger.getLogger(TemplateI5100.class.getName());/*** {"module":"printer","value":{"title":"粪便样本普通标签打印(已选10个)","number":1,"printer":"i5100",labels:[{"template":2,contents:["WAOI2T001","张三","北师大","粪便"]}]}}* @return*/public int print(String printer, String... param) {DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;PrintService printService = getPrintService(printer,flavor);if (printService == null) {return 2;}DocPrintJob job = printService.createPrintJob();StringBuilder sb = new StringBuilder();sb.append("m m\n");sb.append("J\n");sb.append("S l1;3,0,12,15,51\n");sb.append("B 23.4, 1.8,0,code128,4,0.15;").append("[U:CODEA]"+ param[0] + "\n");// 打印文字String date = param.length > 5 ? param[5] : LocalDate.now().toString();sb.append("T 29.0,1.4,0,3,2.0;").append(param[0] + "\n");sb.append("T 22.0,7.5,0,1000,pt4;姓名:").append(param[1] + "\n");sb.append("T 22.0,9.5,0,1000,pt4;日期:").append(date+ "\n");sb.append("T 35,7.5,0,1000,pt4;站点:").append(param[2] + "\n");sb.append("T 35,9.5,0,1000,pt4;类型:").append(param[3] + "\n");sb.append("B 10.6,2.2,0,DATAMATRIX+ROWS14+COLS14,0.4;").append(param[0]).append("\n");sb.append("A ");sb.append(param[4] + "\n");ByteArrayInputStream bis = new ByteArrayInputStream(sb.toString().getBytes(StandardCharsets.UTF_8));DocAttributeSet das = new HashDocAttributeSet();Doc doc = new SimpleDoc(bis, flavor, das);try {bis.close();job.print(doc, null);} catch (IOException | PrintException e) {log.log(Level.SEVERE, e.getMessage(),e);return 2;}return 0;}private PrintService getPrintService(String printer,DocFlavor flavor) {PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();PrintService[] printServices = PrintServiceLookup.lookupPrintServices(flavor, pras);for (PrintService ps : printServices) {if (ps.getName().equals(printer)) {return ps;}}return null;}}

四、测试类

package com.yq.controller;import com.yq.printer.TemplateI5100;import javax.print.DocFlavor;import javax.print.PrintService;import javax.print.PrintServiceLookup;import javax.print.attribute.HashPrintRequestAttributeSet;import javax.print.attribute.PrintRequestAttributeSet;/*** @author * @date /9/10 14:31*/public class TestController {/*** 获取打印机的驱动名称* @return*/public static String getPrinter() {PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;PrintService[] printServices = PrintServiceLookup.lookupPrintServices(flavor, pras);// 模糊匹配for (PrintService printService : printServices) {if(printService.getName().startsWith("i5100-300")){return printService.getName();}}return null;}public static void main(String[] args) {String printer = getPrinter();TemplateI5100 templateI5100 = new TemplateI5100();String [] params = new String[5];params[0] = "12E45F7G9";params[1] = "路飞";params[2] = "海贼王大学";params[3] = "EDTA-2";params[4] = "3";templateI5100.print(printer,params);}}

五、成功

六、其他驱动模板(仅供参考,没有相关打印机的条件未测试)

1. I5100Box

package com.yq.printer;import javax.print.*;import javax.print.attribute.DocAttributeSet;import javax.print.attribute.HashDocAttributeSet;import javax.print.attribute.HashPrintRequestAttributeSet;import javax.print.attribute.PrintRequestAttributeSet;import java.io.ByteArrayInputStream;import java.io.IOException;import java.nio.charset.StandardCharsets;import java.util.logging.Level;import java.util.logging.Logger;/*** i5100 打印机模板**/public class TemplateI5100Box {private static Logger log = Logger.getLogger(TemplateI5100Box.class.getName());/*** {"module":"printer","value":{"title":"测试","number":1,"printer":"i5100",labels:[{"template":1,contents:["XF00000001","测试"]}]}}** @return*/public int print(String printer, String... param) {DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;PrintService printService = getPrintService(printer, flavor);if (printService == null) {return 2;}DocPrintJob job = printService.createPrintJob();StringBuilder sb = new StringBuilder();sb.append("m m\n");sb.append("J\n");//S[ptype;]xo,yo,ho,dy,wd[,dx,col][;name]CRsb.append("S l1;3,0,12,15,51\n");// 打印文字sb.append("T 31,1.4,0,1000,pt5;").append(param[1] + "\n");// code小写则不打印编码,大写则打印sb.append("B 26.8, 2.0,0,code128,6,0.2;").append(param[0] + "\n");// 打印编码sb.append("T 31.5,9.5,0,3,pt5;").append(param[0] + "\n");//圆上的 Data matrix 原X6.5 原"B 7.8,1.3,0,DATAMATRIX+ROWS20+COLS20,0.3;"sb.append("B 10.6,2.2,0,DATAMATRIX+ROWS14+COLS14,0.4;").append(param[0]).append("\n");sb.append("A 1\n");ByteArrayInputStream bis = new ByteArrayInputStream(sb.toString().getBytes(StandardCharsets.UTF_8));DocAttributeSet das = new HashDocAttributeSet();Doc doc = new SimpleDoc(bis, flavor, das);try {bis.close();job.print(doc, null);} catch (IOException | PrintException e) {log.log(Level.SEVERE, e.getMessage(),e);return 2;}return 0;}private PrintService getPrintService(String printer, DocFlavor flavor) {PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();PrintService[] printServices = PrintServiceLookup.lookupPrintServices(flavor, pras);for (PrintService ps : printServices) {if (ps.getName().equals(printer)) {return ps;}}return null;}}

2. BBP12 驱动打印机

package com.yq.printer;import com.sun.jna.Library;import com.sun.jna.Native;/****/public interface TscLibDll extends Library {TscLibDll INSTANCE = Native.load("TSCLIB", TscLibDll.class);/*** Display the DLL version on the screen** @return*/int about();/*** Start the Windows printer spool** @param pirnterName* @return*/int openport(String pirnterName);/*** Close Windows printer spool** @return*/int closeport();/*** 发送指令** @param printerCommand 打印指令(TSPL)* @return*/int sendcommand(String printerCommand);/*** Set up label width, label height, print speed, print density, sensor type,* gap/black mark vertical distance、gap/black mark shift distance** @param width sets up label width; unit: mm* @param height sets up label height; unit: mm* @param speed sets up print speed, (selectable print speeds vary on* different printer models)* @param density sets up print density 0-15* @param sensor sets up the sensor type to be used (0 gap/1 black mark)* @param vertical sets up vertical gap height of the gap/black mark; unit: mm* @param offset sets up shift distance of the gap/black mark; unit:: mm; in* the case of the average label, set this parameter to be 0* @return*/int setup(String width, String height, String speed, String density, String sensor, String vertical, String offset);/*** Download pcx file** @param filename* @param imageName* @return*/int downloadpcx(String filename, String imageName);/*** Use built-in bar code formats to print** @param x the starting point of the bar code along the X direction,* given in points* @param y the starting point of the bar code along the Y direction,* given in points* @param typetype of code* @param height sets up bar code height, given in points* @param readable sets up whether to print human recognizable interpretation* (text) or not* @param rotation sets up rotation degrees* @param narrow sets up narrow bar ratio, refer to TSPL user's manual* @param widesets up narrow bar ratio, refer to TSPL user's manual* @param codecontent* @return*/int barcode(String x, String y, String type, String height, String readable, String rotation, String narrow,String wide, String code);/*** Use printer built-in fonts to print* @param x the starting point of the bar code along the X direction,* given in points* @param y the starting point of the bar code along the Y direction,* given in points* @param fonttype* @param rotation* @param xmul* @param ymul* @param text* @return*/int printerfont(String x, String y, String fonttype, String rotation, String xmul, String ymul, String text);/*** Clear** @return*/int clearbuffer();/*** Print label content* @param set sets up the number of label sets* @param copy sets up the number of print copies* @return*/int printlabel(String set, String copy);/*** Skip to next page (of label); this function is to be used after setup** @return*/int formfeed();/*** disable the backfeed function** @return*/int nobackfeed();/*** Use Windows font to print text.** @param x* @param y* @param fontheight* @param rotation* @param fontstyle* @param fontunderline* @param szFaceName* @param content* @return*/int windowsfont(int x, int y, int fontheight, int rotation, int fontstyle, int fontunderline, String szFaceName,String content);/*** Use Windows font to print Unicode text.** @param x* @param y* @param fontheight* @param rotation* @param fontstyle* @param fontunderline* @param szFaceName* @param content* @return*/int windowsfontUnicode(int x, int y, int fontheight, int rotation, int fontstyle, int fontunderline,String szFaceName, byte[] content);}

package com.yq.printer;import java.time.LocalDate;/*** 模板1, 普通标签打印* <p>标签内容包含条码和二维码</p>* <p>使用bbp12打印机</p>**/public class TemplateBbp12{/*** {"module":"printer","value":{"title":"粪便样本普通标签打印(已选10个)","number":1,"printer":"Brady",labels:[{"template":1,contents:["WAOI2T001","张三","北师大","粪便"]}]}}*/public int print(String printer, String... param) {// 调用设备Brady BBP12 是设备的名称TscLibDll.INSTANCE.openport(printer);// 打印前先清理下缓存TscLibDll.INSTANCE.clearbuffer();// 设置标签参数,包括标签纸的大小,纸张间隔大小,打印机的速度,打印机浓度等;// TscLibDll.INSTANCE.setup("41", "9.5", "4", "12", "0", "3", "0");TscLibDll.INSTANCE.setup("35", "15", "4", "12", "0", "3", "0");// 设置jna.encoding 是防止在dll过程中中文乱码System.setProperty("jna.encoding", "GBK");// 如果日期不传的话,默认使用本地日期String date = param.length > 4 ? param[4] : LocalDate.now().toString();// 打印条码 待调试 x原195 "175", "18", "128", "35", "0", "0", "2", "2",//指定为code128A的情况,解决因为编号后面超过2位数字因而使用CODE128C导致条形码缩短的情况TscLibDll.INSTANCE.barcode("60", "40", "128M", "60", "0", "0", "2", "2", "!101"+param[0]);// 打印编号 原228为X位置,大概30==》1mm 待调试252 -2TscLibDll.INSTANCE.printerfont("132", "-8", "3", "0", "1", "1", param[0]);String font = "Arial";TscLibDll.INSTANCE.windowsfont(40, 110, 26, 0, 2, 0, font, "姓名:" + param[1]);TscLibDll.INSTANCE.windowsfont(190, 110, 26, 0, 2, 0, font, "站点:" + param[2]);TscLibDll.INSTANCE.windowsfont(40, 138, 26, 0, 2, 0, font, "日期:" + date);TscLibDll.INSTANCE.windowsfont(220, 138, 26, 0, 2, 0, font, "类型:" + param[3]);// 在圆上打印data matrix码 原"DMATRIX 40,6,80,80,x4,20,20,\""// 设置打印多少张TscLibDll.INSTANCE.printlabel("1", "1");TscLibDll.INSTANCE.closeport();return 0;}}

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