场景

任务重、时间赶,未使用ZPL语言调用打印机,也未使用斑马的SDK包

扫码枪扫描条码,打印机打印出扫描条码

思路说明

因为业务场景不需要使用web形式,所以使用java swimg 做的界面,jacob调用本地打印程序,从而实现相关打印任务。

详细步骤

1:准备一台打印机、扫码枪

2:使用bartend制作模板

3:模板数据库指向txt格式数据库,并指定相关数据库域

4:编写代码实现功能

备注:

单机,web形式可以试一下ActiveX 插件启动进程,有机会我会试一下

package main.java;import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.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 2022/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) {super.componentResized(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);}
}

JavaSwimg jacob调用模板,斑马打印机打印条码相关推荐

  1. C#调用斑马打印机打印条码标签(支持COM、LPT、USB、TCP连接方式和ZPL、EPL、CPCL指令)

    在批量打印商品标签时一般都要加上条码或图片,而这类应用大多是使用斑马打印机,所以我也遇到了怎么打印的问题. 一种办法是用标签设计软件做好模板,在标签设计软件中打印,这种办法不用写代码,但对我来说觉得不 ...

  2. C#调用斑马打印机打印条码标签(支持COM、LPT、USB、TCP连接方式和ZPL、EPL、CPCL指令)...

    在批量打印商品标签时一般都要加上条码或图片,而这类应用大多是使用斑马打印机,所以我也遇到了怎么打印的问题. 一种办法是用标签设计软件做好模板,在标签设计软件中打印,这种办法不用写代码,但对我来说觉得不 ...

  3. C#调用斑马打印机打印条码标签(支持COM、LPT、USB、TCP连接方式和ZPL、EPL、CPCL指令)【转】...

    原文地址:http://blog.csdn.net/ldljlq/article/details/7338772 在批量打印商品标签时一般都要加上条码或图片,而这类应用大多是使用斑马打印机,所以我也遇 ...

  4. C#调用斑马打印机打印条码标签(含源码)(支持COM、LPT、USB、TCP连接方式和ZPL、EPL、CPCL指令)

    在批量打印商品标签时一般都要加上条码或图片,而这类应用大多是使用斑马打印机,所以我也遇到了怎么打印的问题. 一种办法是用标签设计软件做好模板,在标签设计软件中打印,这种办法不用写代码,但对我来说觉得不 ...

  5. 调用斑马打印机实现标签打印

    Java 调用斑马打印机实现标签打印 代码实现 最近的一个项目中,需要实现java调用斑马的打印机实现标签打印功能,百度了一些资源,找了不少人搞了一台快报废的斑马105SL 300DPI打印机,分分钟 ...

  6. 斑马打印机测试demo,一维码、二维码、字符串。可以保存创建模板以便于后续使用

    利用RawPrinterHelper和Smt.Zebra两种方式进行打印控制 using Smt.Zebra; using System; using System.Collections.Gener ...

  7. 【求助】关于.NET(C#)调用斑马打印机(ZDesigner GK888t (EPL))换页时退纸的问题

    有解决过类似问题的大神请留步,救救我吧. -------分割------- 最近在做一个快递标签打印系统,使用.NET(C#)调用斑马打印机[ZDesigner GK888t (EPL)]进行打印,程 ...

  8. C#控制利用模板文件通过BarTender控制斑马打印机打印

    重点在后面: https://blog.csdn.net/z_344791576/article/details/46328443?ops_request_misc=%257B%2522request ...

  9. Zebra斑马打印机指令编程进阶(语言通用)--利用指令绘制出图像打印

    之前介绍过Zebra的入门打印,由于Zebra指令繁多,并且自带字体不能打印中文,如果打印内容中有中文的话,还是推荐大家先用Graphic类先画好自己想要的image,然后将整张图片进行打印.而且调试 ...

最新文章

  1. linux u8 头文件,2019-12-11 转载TCP/IP编程常用C语言头文件
  2. Oralce日常高效率巡检_数据库性能、安全检查
  3. solr dataimport 数据导入源码分析(二)
  4. 第八天2017/04/17(1、拷贝构造、❤临时对象)
  5. python语法错误概述_Python 错误和异常代码详解
  6. nchar,char,varchar与nvarchar区别
  7. [DeeplearningAI笔记]卷积神经网络2.9-2.10迁移学习与数据增强
  8. linux otl oracle,linux otl 连接数据库
  9. Longest Palindromic Substring Part II
  10. AJAX的 contentType
  11. Power BI Embedded 开发国内版-21V-版本实测
  12. C语言学习笔记----sizeof()的使用
  13. 建筑电气工程设计常用图形和文字符号_建筑电气施工图设计正误案例对比
  14. rp文件,怎么用浏览器预览
  15. 《商务与经济统计》要点回顾笔记
  16. Java8新特性全面
  17. IE8中解决Cell华表插件不显示方法!
  18. 打开桌面计算机投屏到扩展屏,华为智慧屏怎么共享电脑屏幕
  19. 三角形中的欧拉公式证明过程
  20. win10笔记本外接显示器显示输入信号超出范围

热门文章

  1. c语言uint64_t转String,C语言编程 8字节的数据转换为uint64_t类型数据
  2. html网站手机最小字体大小,html在手机浏览器如何设置字体大小
  3. 计算机命令提示符开热点,win7命令行架设无线热点(AP)
  4. [gazebo_gui-2] process has died [pid 4588, exit code 134, cmd /opt/ros/kinetic/lib/gazebo_ros/gzc
  5. 使用 ipmitool 实现 Linux 系统下对服务器的BMC管理
  6. 一文读懂数据治理怎么做
  7. 罗素:如何避免愚蠢的见识
  8. COS系统安装-图片版
  9. 清明时节,思念友人张锦文
  10. 2021年JS解决那些有趣的数学题