java调用博思得打印机的心得

转载:http://www.zuidai和ma.com/blog/2793091475983360.htm

www.zuidai ma.com居然是违禁字符,我也是醉了,所以在www.zuidai后面加一个和字。

曾经公司的项目 让我弄个一个二维码的打印 客户要求选择博思得品牌打印机 我试过调用java里面的打印机的api 可惜打印机可以运行了 但是打印不出图片 我也研究很多关于打印的东西 发现还是不行 后来找到他们技术 他们推荐我使用c/s 调用他们提供的dll

可是我发现还是出现问题 到最后我才发现原来他们的打印机只支持单色的bmp格式图片 然后我只能把图片先转成bmp 终于成功了 。 我使用的是JNative的包

下面提供下代码给大家参考下:

import org.xvolks.jnative.JNative;

import org.xvolks.jnative.Type;

import org.xvolks.jnative.exceptions.NativeException;

public class javaDll {

/**

* @param args

*对于各个方法没做进一步的处理,只是抛异常而已。

*执行Main函数可正常打印一张图片

*/

static final String DLL_FILE = "D:\\CDFPSK.dll";

public String OpenPort(int xx) throws NativeException,

IllegalAccessException {

JNative jn_handler = null;

jn_handler = new JNative(DLL_FILE, "OpenPort");

jn_handler.setRetVal(Type.INT); // 指定返回参数的类型

jn_handler.setParameter(0, xx);

jn_handler.invoke(); // 调用方法

return jn_handler.getRetVal();

}

public String SetPCComPort(int BaudRate,boolean HandShake) throws NativeException,

IllegalAccessException {

JNative jn_handler = null;

jn_handler = new JNative(DLL_FILE, "SetPCComPort");

jn_handler.setRetVal(Type.INT); // 指定返回参数的类型

jn_handler.invoke(); // 调用方法

return jn_handler.getRetVal();

}

public String GetErrState() throws NativeException,

IllegalAccessException {

JNative jn_handler = null;

jn_handler = new JNative(DLL_FILE, "GetErrState");

jn_handler.setRetVal(Type.VOID); // 指定返回参数的类型

jn_handler.invoke(); // 调用方法

return jn_handler.getRetVal();

}

public String PTK_GetInfo() throws NativeException,

IllegalAccessException {

JNative jn_handler = null;

jn_handler = new JNative(DLL_FILE, "PTK_GetInfo");

jn_handler.setRetVal(Type.INT); // 指定返回参数的类型

jn_handler.invoke(); // 调用方法

return jn_handler.getRetVal();

}

public String PTK_DrawTextTrueTypeW(int x,int y,int FHeight,int FWidth,String FType,int Fspin,int FWeight,int FLtatlic, int FUnline, int FStrikeOut,String id_name, String data ) throws NativeException,

IllegalAccessException {

JNative jn_handler = null;

jn_handler = new JNative(DLL_FILE, "PTK_DrawTextTrueTypeW");

jn_handler.setRetVal(Type.INT); // 指定返回参数的类型

jn_handler.setParameter(0, x);

jn_handler.setParameter(1, y);

jn_handler.setParameter(2, FHeight);

jn_handler.setParameter(3, FWidth);

jn_handler.setParameter(4, FType);

jn_handler.setParameter(5, Fspin);

jn_handler.setParameter(6, FWeight);

jn_handler.setParameter(7, FLtatlic);

jn_handler.setParameter(8, FUnline);

jn_handler.setParameter(9, FStrikeOut);

jn_handler.setParameter(10, id_name);

jn_handler.setParameter(11, data);

jn_handler.invoke(); // 调用方法

return jn_handler.getRetVal();

}

public String PTK_PrintLabel(int number,int cpnumber) throws NativeException,

IllegalAccessException {

JNative jn_handler = null;

jn_handler = new JNative(DLL_FILE, "PTK_PrintLabel");

jn_handler.setRetVal(Type.INT); // 指定返回参数的类型

jn_handler.setParameter(0, number);

jn_handler.setParameter(1, cpnumber);

jn_handler.invoke(); // 调用方法

return jn_handler.getRetVal();

}

public String PTK_BmpGraphicsDownload(String pxname,String pypath,int iDire) throws NativeException,

IllegalAccessException {

JNative jn_handler = null;

jn_handler = new JNative(DLL_FILE, "PTK_BmpGraphicsDownload");

jn_handler.setRetVal(Type.INT); // 指定返回参数的类型

jn_handler.setParameter(0, pxname);

jn_handler.setParameter(1, pypath);

jn_handler.setParameter(2, iDire);

jn_handler.invoke(); // 调用方法

return jn_handler.getRetVal();

}

public String PTK_BinGraphicsDownload(String pxname,String pbyte ,int iDire,char[] buff) throws NativeException,

IllegalAccessException {

JNative jn_handler = null;

jn_handler = new JNative(DLL_FILE, "PTK_BmpGraphicsDownload");

jn_handler.setRetVal(Type.INT); // 指定返回参数的类型

jn_handler.setParameter(0, pxname);

jn_handler.setParameter(1, pbyte);

jn_handler.setParameter(2, iDire);

jn_handler.invoke(); // 调用方法

return jn_handler.getRetVal();

}

public String PTK_DrawPcxGraphics(int px,int py,String name) throws NativeException,

IllegalAccessException {

JNative jn_handler = null;

jn_handler = new JNative(DLL_FILE, "PTK_DrawPcxGraphics");

jn_handler.setRetVal(Type.INT); // 指定返回参数的类型

jn_handler.setParameter(0, px);

jn_handler.setParameter(1, py);

jn_handler.setParameter(2, name);

jn_handler.invoke(); // 调用方法

return jn_handler.getRetVal();

}

public String PTK_PcxGraphicsDel(String pid) throws NativeException,

IllegalAccessException {

JNative jn_handler = null;

jn_handler = new JNative(DLL_FILE, "PTK_PcxGraphicsDel");

jn_handler.setRetVal(Type.INT); // 指定返回参数的类型

jn_handler.setParameter(0, pid);

jn_handler.invoke(); // 调用方法

return jn_handler.getRetVal();

}

public String PTK_RWRFIDLabel(int a,int b,int c,int d,int f,String ff) throws NativeException,

IllegalAccessException {

JNative jn_handler = null;

jn_handler = new JNative(DLL_FILE, "PTK_RWRFIDLabel");

jn_handler.setRetVal(Type.INT); // 指定返回参数的类型

jn_handler.setParameter(0, a);

jn_handler.setParameter(1, b);

jn_handler.setParameter(2, c);

jn_handler.setParameter(3, d);

jn_handler.setParameter(4, f);

jn_handler.setParameter(5, ff);

jn_handler.invoke(); // 调用方法

return jn_handler.getRetVal();

}

public String PTK_SetLabelHeight(int height,int gapH) throws NativeException,

IllegalAccessException {

JNative jn_handler = null;

jn_handler = new JNative(DLL_FILE, "PTK_SetLabelHeight");

jn_handler.setRetVal(Type.INT); // 指定返回参数的类型

jn_handler.setParameter(0, height);

jn_handler.setParameter(1,gapH);

jn_handler.invoke(); // 调用方法

return jn_handler.getRetVal();

}

public String PTK_SetLabelWidth(int width) throws NativeException,

IllegalAccessException {

JNative jn_handler = null;

jn_handler = new JNative(DLL_FILE, "PTK_SetLabelWidth");

jn_handler.setRetVal(Type.INT); // 指定返回参数的类型

jn_handler.setParameter(0, width);

jn_handler.invoke(); // 调用方法

return jn_handler.getRetVal();

}

public String ClosePort() throws NativeException,

IllegalAccessException {

JNative jn_handler = null;

jn_handler = new JNative(DLL_FILE, "ClosePort");

jn_handler.setRetVal(Type.INT); // 指定返回参数的类型

jn_handler.invoke(); // 调用方法

return jn_handler.getRetVal();

}

public static void main(String[] args) throws NativeException, IllegalAccessException {

// TODO Auto-generated method stub

System.out.println(new javaDll().OpenPort(255));

// System.out.println(new javaDll().PTK_DrawTextTrueTypeW (30,35,24,0,"宋体",4,400,0,0,0,"A1","机要绝密"));

System.out.println(new javaDll().PTK_SetLabelHeight(-300,24));

System.out.println(new javaDll().PTK_SetLabelWidth(0));

//System.out.println(new javaDll().PTK_RWRFIDLabel(1,0,2,12,1,"123131312312"));

System.out.println(new javaDll().PTK_PcxGraphicsDel("Bag"));

System.out.println(new javaDll().PTK_BmpGraphicsDownload("Bag","D:\\001.bmp",0));//将一张图片打印

System.out.println(new javaDll().PTK_DrawPcxGraphics(0,30,"Bag"));

System.out.println(new javaDll().PTK_PrintLabel(1,1));

// System.out.println(new javaDll().SetPCComPort(9600, true));

// System.out.println(new javaDll().PTK_GetInfo());

}

}