使用JNative对条码打印机进行打印

使用JNative对条码打印机进行打印

因项目需要,对Godex EZ2300条码机进行调用打印, 驱动为Ez2000.dll, 用JNI可以实现调用,但比较麻烦, 需

要用到VC6进行DLL封装,改用JNative组件进行调用, 步骤如下:

  1. 下载 JNative-1.3.2.zip 组件库

  2. 解压出来后有 JNative.jarJNativeCpp.dlllibJNativeCpp.so 三个文件,JNative.jar置于 %JAVA_HOME%/jre/lib/ext目录下, JNativeCpp.dll 置于%SYSTEM%/WINDOWS/SYSTEM32目录下

3.将Ez2000.dll置于%SYSTEM%/WINDOWS/SYSTEM32目录以及%JAVA_HOME%/jdk/bin目录下

具体Java代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package com.athens.test;

import org.xvolks.jnative.JNative;
import org.xvolks.jnative.Type;
import org.xvolks.jnative.exceptions.NativeException;

public class Demo {

private static final String PRINTER_DLL = "Ez2000.dll";

public static void main(String [] args) {
Demo demo = new Demo();
System.loadLibrary("Ez2000");
JNative n = null;
try {
n = demo.getJNativeByFunction("isready");
n.setRetVal(Type.INT);
// n.setParameter(1, Type.VOID, "");
n.invoke();
Integer isready = Integer.parseInt(n.getRetVal());
if(isready == 1){

System.out.println("条码机可以开始接收资料!Starting...");
System.out.println("Starting...");
// 打开端口
n = demo.getJNativeByFunction("openport");
n.setParameter(0, Type.STRING, "0");
n.invoke();

// 设定参数
n = demo.getJNativeByFunction("setup");
n.setRetVal(Type.INT);
n.setParameter(0, Type.INT, "30");
n.setParameter(0, Type.INT, "7");
n.setParameter(0, Type.INT, "2");
n.setParameter(0, Type.INT, "1");
n.setParameter(0, Type.INT, "0");
n.setParameter(0, Type.INT, "0");
n.invoke();

// 发出指令
n = demo.getJNativeByFunction("sendcommand");
n.setRetVal(Type.INT);
n.setParameter(0, Type.STRING, "W70");
n.invoke();

n.setRetVal(Type.INT);
n.setParameter(0, Type.STRING, "^P1");
n.invoke();

n.setRetVal(Type.INT);
n.setParameter(0, Type.STRING, "^L");
n.invoke();

n.setRetVal(Type.INT);
n.setParameter(0, Type.STRING, "AC,20,60,1,1,1,0,TEST");
n.invoke();

// 发送条形码
n = demo.getJNativeByFunction("ecTextOut");
n.setParameter(0, Type.INT, "20");
n.setParameter(1, Type.INT, "10");
n.setParameter(2, Type.INT, "34");
n.setParameter(3, Type.STRING, "标楷体");
n.setParameter(4, Type.STRING, "Athens Testing Successlly!!!");
n.invoke();

// 发出指令
n = demo.getJNativeByFunction("sendcommand");
n.setRetVal(Type.INT);
n.setParameter(0, Type.STRING, "E");
n.invoke();

// 关闭端口
n = demo.getJNativeByFunction("closeport");
n.invoke();
System.out.println("Ending...");
}else{
System.out.println("条码机无法接收资料!");
}
}catch(Exception e){
e.printStackTrace();
}finally{
if (n != null){
try {
n.dispose();
} catch (NativeException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}

// 通过方法名取得JNative对象
public JNative getJNativeByFunction(String functionName) throws NativeException{
return new JNative(PRINTER_DLL, functionName);
}
}