java调用条码打印机指令进行打印

因项目需要,对Argox OS-214 plus series PPLA条码机进行调用打印, 驱动为BCLW32.DLL、WinPort.dll、Winppla.dll, 用JNI可以实现调用,但比较麻烦, 需 要对DLL封装,改用JNative组件进行调用。

整个实现步骤为:

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

2. 解压出来后有JNative.jar  /  JNativeCpp.dll  /  libJNativeCpp.so三个文件,JNative.jar引用于工程中, JNativeCpp.dll置于C:/WINDOWS/SYSTEM32目录下 。

3.将BCLW32.DLL、WinPort.dll、Winppla.dll, 放一固定文件夹下,(有一种调用是放在C:/WINDOWS/SYSTEM32下 但是我测试以后发现如果放在这里以后就找不到dll文件了 具体原因还不清楚!

4.具体代码如下:

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


/**
 * 条形码打印工具类
 * 
 * @author
 * 
 */
public class NativeDllFacadeUtil_new {


private static String szSaveFile = "C:\\Argox\\PPLA_Example.Prn";
private static final String PRINTER_DLL = "Winppla.dll";


/**
* 加载dll文件
*/
static {

// System.load("G:\\kd_work\\dll\\BCLW32.dll");
// System.load("G:\\kd_work\\dll\\WinPort.dll");
// System.load("G:\\kd_work\\dll\\Winppla.dll");
System.load("C:\\dll\\BCLW32.dll");
System.load("C:\\dll\\WinPort.dll");
System.load("C:\\dll\\Winppla.dll");
}


/**
* 获取dll的指令

* @param functionName
*            指令名称
* @return
* @throws NativeException
*/
private static JNative getJNativeByFunction(String functionName)
throws NativeException {
return new JNative(PRINTER_DLL, functionName);
}


/**
* 执行指令

* @param functionName
* @param params
*            参数集合
* @return
*/
public static int executeFunction(String functionName, Object[] params) {
JNative func = null;
try {
func = getJNativeByFunction(functionName);
func.setRetVal(Type.INT);
if (params != null && params.length > 0) {
for (int i = 0; i < params.length; i++) {
if (params[i] instanceof String)
func.setParameter(i, (String) params[i]);
else if (params[i] instanceof Integer)
func.setParameter(i, ((Integer) params[i]).intValue());
else if (params[i] instanceof Pointer)
func.setParameter(i, (Pointer) params[i]);
else if (params[i] instanceof Character)
func.setParameter(i,  Type.PSTRUCT,(String)params[i].toString());
else if (params[i] instanceof Boolean)
func.setParameter(i, Type.INT, params[i].toString());
else if (params[i] instanceof byte[])
func.setParameter(i, Type.PSTRUCT, (byte[]) params[i]);
}
}
func.invoke();
//System.out.println(functionName +"  ************--> "+func.getRetValAsInt());
return func.getRetValAsInt();
} catch (Exception e) {
throw new RuntimeException(e);


}finally{
if(func!=null){
//func.dispose();
}
}


}


public static void main(String[] args) throws NativeException,
IllegalAccessException {
int nLen, ret;
byte[] pbuf = new byte[128];
String ver, strmsg;
int var = executeFunction("A_Get_DLL_Version", new Object[] { 0 });
ver = var + "";
nLen = executeFunction("A_GetUSBBufferLen", null) + 1;
strmsg = "DLL ";
strmsg += ver;
strmsg += "\r\n";
if (nLen > 1) {
executeFunction("A_EnumUSB", new Object[] { new String(pbuf) });
executeFunction("A_CreateUSBPort", new Object[] { 1 });
} else {
ret = executeFunction("A_CreatePrn", new Object[] { 1, "" });
strmsg += "Open ";
strmsg += szSaveFile;
if (0 != ret) {
strmsg += " file fail!";
} else {
strmsg += " file succeed!";
}
}

executeFunction("A_Set_DebugDialog", new Object[] { 1 });
        executeFunction("A_Set_Unit", new Object[] { 'n' });
        executeFunction("A_Set_Syssetting", new Object[] {1, 0, 0, 0, 0});
executeFunction("A_Set_Darkness", new Object[] { 6 });
        executeFunction("A_Del_Graphic", new Object[] { 1,"*" });
        executeFunction("A_Clear_Memory",null);
        //String path = NativeDllFacadeUtil_new.class.getClassLoader().getResource("/").getPath(); 
        //画一条线
//executeFunction("A_Draw_Line", new Object[] { 'A', 0, 60, 700, 2 });
//打印2维条码
        // A_Bar2d_QR_M(100, 100, 4, '6', 10, 2, 'M', 1, 'A', 'A', 4, "N123456");
        //A_Bar2d_DataMatrix(100, 100, 1, 8, 8, 200, 0, 24, 24, 'N', 2, "DATAMATRIX");
executeFunction("A_Bar2d_DataMatrix", new Object[] { 5,35, 1, 2, 2 ,200,0,24,24,'N',2,"abc_2345671234567890123456789012345678901234567890" });

String outString="00Cr17Ni12Mo2";
int width = 0;
if (outString.length() <= 6) {
       width = 28;
     }
     else {
       width = 144 / outString.length();
}
executeFunction("A_Prn_Text_TrueType_W", new Object[] { 35,48, 20, width, "Times New Roman",1,400,0,0,0,"AB",outString,1 });

outString = "100*2500*10000";
if (outString.length() <= 6) {
       width = 28;
     }
     else {
       width = 144 / outString.length();
}
executeFunction("A_Prn_Text_TrueType_W", new Object[] { 135,48, 20, width, "Times New Roman",1,400,0,0,0,"AC",outString,1 });

outString = "CCCHA943373-1-1";
if (outString.length() <= 6) {
       width = 28;
     }
     else {
       width = 144 / outString.length();
}
executeFunction("A_Prn_Text_TrueType_W", new Object[] { 35,35, 20, width, "Times New Roman",1,400,0,0,0,"AD",outString,1 });
 
outString = "902A-11-999-1";
if (outString.length() <= 6) {
       width = 28;
     }
     else {
       width = 144 / outString.length();
}
executeFunction("A_Prn_Text_TrueType_W", new Object[] { 135,35, 20, width, "Times New Roman",1,400,0,0,0,"AE",outString,1 });

executeFunction("A_Print_Out", new Object[] { 1, 1, 1, 1 });
executeFunction("A_ClosePrn", null);

}
}

5 .需要注意在jnative中对数据类型的转换。

你可能感兴趣的:(java)