场景
任务重、时间赶,未使用ZPL语言调用打印机,也未使用斑马的SDK包
扫码枪扫描条码,打印机打印出扫描条码
思路说明
因为业务场景不需要使用web形式,所以使用java swimg 做的界面,jacob调用本地打印程序,从而实现相关打印任务。
详细步骤
1:准备一台打印机、扫码枪
2:使用bartend制作模板
3:模板数据库指向txt格式数据库,并指定相关数据库域
4:编写代码实现功能
备注:
单机,web形式可以试一下ActiveX 插件启动进程,有机会我会试一下
package main.java;import com.jacob.activeX.ActiveXComponent;import .ComThread;import .Dispatch;import javax.swing.*;import javax.swing.event.*;import java.awt.*;import java.awt.event.*;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.nio.charset.StandardCharsets;import java.text.ParseException;import java.time.LocalDate;import java.time.LocalDateTime;import java.time.format.DateTimeFormatter;/*** @ClassName scanner* @author: hzj* @Description TODO* @date /8/23 9:41*/public class scanner {private static JTextField userText = new JTextField();private static JFrame frame = new JFrame("条码打印");private static ActiveXComponent activeXComponent;private static JPanel panel = new JPanel();private static JLabel userLabel = new JLabel("条码:");private static JLabel viewLable = new JLabel("上一条:");private static JTextField viewText = new JTextField();private static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");public static void main(String[] args) throws IOException {Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();int height = dimension.height;int width = dimension.width;frame.setSize(width/2+800, height/2+500);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {super.windowClosing(e);// 关闭Bartender进程 此部分代码可以做到Dispatch.call(activeXComponent, "Quit", 0);//关闭Bartender进程Dispatch.call(activeXComponent, "Quit", 0);if (activeXComponent != null){activeXComponent.invoke("Quit", 0);}ComThread.Release();}});frame.add(panel);frame.setVisible(true);frame.setLocationRelativeTo(null);placeComponents();}private static void placeComponents() {panel.setLayout(null);panel.addComponentListener(new ComponentAdapter() {@Overridepublic void componentResized(ComponentEvent e) {ponentResized(e);}});mainLayout();}private static void mainLayout(){userLabel.setBounds(100,panel.getSize().height/10,400,400);userLabel.setFont(new Font("楷体",Font.BOLD,150));userText.setBounds(500,panel.getSize().height/10+100,panel.getSize().width/2+400,panel.getSize().height/10+50);userText.setFont(new Font("楷体",Font.BOLD,180));userText.getDocument().addDocumentListener(new DocumentListener(){@Overridepublic void insertUpdate(DocumentEvent e) {System.out.println(userText.getText());String text = userText.getText();try {if(text.length()==14){txt(text);System.out.println(text.length());}} catch (IOException | ParseException ioException) {ioException.printStackTrace();}}@Overridepublic void removeUpdate(DocumentEvent e) {System.out.println("remove");}@Overridepublic void changedUpdate(DocumentEvent e) {System.out.println("changedUpdate");}});userText.getDocument().addUndoableEditListener(new UndoableEditListener() {@Overridepublic void undoableEditHappened(UndoableEditEvent e) {if(14==userText.getText().length()){viewText.setText(userText.getText());userText.setText("");}}});viewLable.setBounds(100,panel.getSize().height-600,300,100);viewLable.setFont(new Font("楷体",Font.BOLD,70));viewLable.setForeground(Color.red);viewText.setEditable(false);viewText.setBounds(100,panel.getSize().height-500,panel.getSize().width/2+850,panel.getSize().height/10+50);viewText.setFont(new Font("楷体",Font.BOLD,180));viewText.setForeground(Color.RED);viewText.setHorizontalAlignment(JTextField.CENTER);panel.add(userLabel);panel.add(userText);panel.add(viewLable);panel.add(viewText);}private static void txt(String code) throws IOException, ParseException {File file = new File("C:/print/65x11-BartenderTemplate-bak/model_1.txt");if(!file.exists()){file.mkdirs();}FileOutputStream fileOutputStream = new FileOutputStream(file);fileOutputStream.write(code.getBytes(StandardCharsets.UTF_8));fileOutputStream.flush();fileOutputStream.close();printTask();todayRecord(code);}private static void todayRecord(String code) throws IOException {String today = "C:/print/65x11-BartenderTemplate-bak/"+ LocalDate.now()+".txt";File file = new File(today);String format = LocalDateTime.now().format(dateTimeFormatter);FileOutputStream fileOutputStream = new FileOutputStream(file,true);fileOutputStream.write(format.getBytes(StandardCharsets.UTF_8));fileOutputStream.write("\t".getBytes(StandardCharsets.UTF_8));fileOutputStream.write(code.getBytes(StandardCharsets.UTF_8));fileOutputStream.write("\n".getBytes(StandardCharsets.UTF_8));fileOutputStream.flush();fileOutputStream.close();}private static void printTask(){//要运行COM组件,需要先将jacob-1.19-x64.dll放到jdk的bin目录下,不然会报错。如果是32位jdk放入32位的dll文件ComThread.InitSTA();if(null == activeXComponent){activeXComponent = new ActiveXComponent("BarTender.Application");}//加载Bartender模板控件Dispatch btFormats = activeXComponent.getProperty("Formats").toDispatch();//调用模板Dispatch btFormat = Dispatch.call(btFormats, "Open", "C:/print/65x11-BartenderTemplate-bak/Goya-Barcode65x11.btw", false, "").toDispatch();//传递参数,参数名称要和Bartender中设置的名称一样,不然会失败// Dispatch.call(btFormat, "value1", "", "G00200000AWQYK");//没试过这种传参方式//设置打印份数,默认是1份可以不设置Dispatch printSetup = Dispatch.get(btFormat, "PrintSetup").toDispatch();Dispatch.put(printSetup, "IdenticalCopiesOfLabel", 1);//打印,Bartender会自动选择模板中设置的打印机打印Dispatch.call(btFormat, "PrintOut", false, false);//打印完成,关闭模板控件Dispatch.call(btFormat, "Close", 0);}}