引入依赖
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>3.2.5</version>
</dependency>
2、测试代码
import com.sun.jna.Library;
import com.sun.jna.Native;
public class Gprinter {
//加载库文件
private static final String LOAD_LIBRARY = "TSCLIB";
//打印机型号
private static final String print_model = "Gprinter GP-3150TN (副本 1)";
public interface TscLibDll extends Library {
TscLibDll INSTANCE = (TscLibDll) Native.loadLibrary("TSCLIB", TscLibDll.class);
int about();
int openport(String pirnterName);
int closeport();
int sendcommand(String printerCommand);
int sendBinaryData(byte[] printerCommand, int CommandLength);
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 windowsfont(int x, int y, int fontheight, int rotation, int fontstyle, int fontunderline, String szFaceName, String content);
int windowsfontUnicode(int x, int y, int fontheight, int rotation, int fontstyle, int fontunderline, String szFaceName, byte[] content);
int windowsfontUnicodeLengh(int x, int y, int fontheight, int rotation, int fontstyle, int fontunderline, String szFaceName, byte[] content, int length);
int QRCODE(String x, String y, String ECC, String cell, String mode, String rotation, String data);
byte usbportqueryprinter();
}
/**
* 打印条码
*/
public static void BARCODE(String printerName, String barCode, String quantity) {
//加载驱动
System.loadLibrary(LOAD_LIBRARY);
//解决中文乱码
System.setProperty("jna.encoding", "GBK");
TscLibDll.INSTANCE.openport(printerName);
TscLibDll.INSTANCE.sendcommand("SIZE 40 mm, 15 mm");
//该指令用于控制打印速度
TscLibDll.INSTANCE.sendcommand("SPEED 2");
//该指令用于控制打印时的浓度
TscLibDll.INSTANCE.sendcommand("DENSITY 8");
//该指令用于定义打印时出纸和打印字体的方向
TscLibDll.INSTANCE.sendcommand("DIRECTION 1");
//此命令是用来启用/禁用撕纸位置走到撕纸处,此设置关掉电源后将保存在打印机内
TscLibDll.INSTANCE.sendcommand("SET TEAR ON");
//该指令用于选择对应的国际代码页
TscLibDll.INSTANCE.sendcommand("CODEPAGE UTF-8");
TscLibDll.INSTANCE.clearbuffer();
//BARCODE x 左上角水平坐标起点,y 左上角垂直坐标起点,"code type" 加密类型,height 条形码高度,human readable 0 表示人眼不可识,1 表示人眼可识,
//rotation 条形码旋转角度,顺时针方向,narrow 窄 bar 宽度,以点(dot)表示,wide 宽 bar 宽度,以点(dot)表示,"content"
TscLibDll.INSTANCE.barcode("80", "60", "128", "75", "0", "0", "2", "2", barCode);
//打印条形码下方字体
TscLibDll.INSTANCE.windowsfont(130, 140, 35, 0, 0, 0, "Arial", barCode);
//设置打印三张一列
TscLibDll.INSTANCE.printlabel(quantity, "1");
TscLibDll.INSTANCE.closeport();
}
/**
* 绘制二维码指令
* 功能:繪製QRCODE二維條碼
* 語法:
* QRCODE X, Y, ECC Level, cell width, mode, rotation, [model, mask,]"Data string”
* 參數說明
* X QRCODE條碼左上角X座標
* Y QRCODE條碼左上角Y座標
* ECC level 錯誤糾正能力等級
* L 7%
* M 15%
* Q 25%
* H 30%
* cell width 1~10
* mode 自動生成編碼/手動生成編碼
* A Auto
* M Manual
* rotation 順時針旋轉角度
* 0 不旋轉
* 90 順時針旋轉90度
* 180 順時針旋轉180度
* 270 順時針旋轉270度
* model 條碼生成樣式
* 1 (預設), 原始版本
* 2 擴大版本
* mask 範圍:0~8,預設7
* Data string 條碼資料內容
*/
public static String CMD_QRCODE_FROMT(String barCode) {
StringBuffer sb = new StringBuffer("QRCODE");
sb.append(" ");
sb.append("50,");//X QRCODE條碼左上角X座標
sb.append("0,");//Y QRCODE條碼左上角Y座標
sb.append("L,");//ECC level 錯誤糾正能力等級 L 7% M 15% Q 25% H 30%
sb.append("7,");//cell width 1~10 二维码宽度 1-10
sb.append("A,");//mode 自動生成編碼/手動生成編碼 A Auto M Manual
sb.append("0,");//rotation 順時針旋轉角度 0 不旋轉 90 順時針旋轉90度 180 順時針旋轉180度 270 順時針旋轉270度
sb.append("1,");//model 條碼生成樣式 1 (預設), 原始版本 2 擴大版本
sb.append("7,");//mask 範圍:0~8,預設7
sb.append("\"");
sb.append(barCode);//Data string 二维码內容
sb.append("\"");
return sb.toString();
}
/**
* 调用GP-1134T打印二维码
* Author
*
* @param barCode 二维码内容
*/
public static void QRCODE(String printerName, String barCode, String quantity) {
//加载驱动
System.loadLibrary(LOAD_LIBRARY);
//解决中文乱码
System.setProperty("jna.encoding", "GBK");
TscLibDll.INSTANCE.openport(printerName);
//打印二维码
TscLibDll.INSTANCE.setup("30", "20", "3", "10", "0", "3", "0");
TscLibDll.INSTANCE.clearbuffer();// 清除缓冲信息
TscLibDll.INSTANCE.sendcommand("GAP 2 mm,0");// 设置 打印的方向.
TscLibDll.INSTANCE.sendcommand("DIRECTION 1");// 设置 打印的方向.
TscLibDll.INSTANCE.sendcommand("CODEPAGE UTF-8");
TscLibDll.INSTANCE.sendcommand(CMD_QRCODE_FROMT(barCode));
//打印二维码下方字体
TscLibDll.INSTANCE.windowsfont(210, 150, 20, 90, 0, 0, "Arial", barCode);
TscLibDll.INSTANCE.printlabel(quantity, "1");
TscLibDll.INSTANCE.clearbuffer();// 清除缓冲信息
TscLibDll.INSTANCE.closeport();
}
//测试
public static void main(String[] args) {
QRCODE("Gprinter GP-3150TN (副本 1)", "202208231501254", "1");
}
}
3、引入dll函数库到JAVA_HOME/bin 下
32位电脑引入放入32位的dll
64位的电脑放入64位的dll
dll提前链接
链接: https://pan.baidu.com/s/1OfvzKp1rl3xLsEkVcjjcuA?pwd=7gg5 提取码: 7gg5 复制这段内容后打开百度网盘手机App,操作更方便哦
4、最恶心的是 调x、y 轴
![在这里插入图片描述](https://img-blog.csdnimg.cn/bc61e65222c74bea973bd2f705cf479b.jpeg
5、安装驱动
链接: https://pan.baidu.com/s/1R8RvQu-_sneBQUQJh0VWBA?pwd=uj64 提取码: uj64 复制这段内容后打开百度网盘手机App,操作更方便哦
6、官网地址
http://cn.gainscha.com/qudong.html