使用JNative对条码打印机进行打印
Posted royal_coffee
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用JNative对条码打印机进行打印相关的知识,希望对你有一定的参考价值。
因项目需要,对Godex EZ2300条码机进行调用打印, 驱动为Ez2000.dll, 用JNI可以实现调用,但比较麻烦, 需
要用到VC6进行DLL封装,改用JNative组件进行调用, 步骤如下:
1. 下载JNative-1.3.2.zip组件库
2. 解压出来后有JNative.jar / JNativeCpp.dll / libJNativeCpp.so三个文件,JNative.jar置于%JAVA_HOME%/jre/lib/ext目录下, JNativeCpp.dll置于%SYSTEM%/WINDOWS/SYSTEM32目录下
3.将Ez2000.dll置于%SYSTEM%/WINDOWS/SYSTEM32目录以及%JAVA_HOME%/jdk/bin目录下
4.
具体Java代码如下:
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);
}
}
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);
}
}
以上是关于使用JNative对条码打印机进行打印的主要内容,如果未能解决你的问题,请参考以下文章