因为项目需要,必须在那里java调用佳博打印机TSCLIB.dll库:
1.使用绝对路径
TscLibDll INSTANCE = (TscLibDll) Native.loadLibrary ("D:\\TSCLIB",TscLibDll.class);
此时要将TSCLIB.dll和TSCLIB.lib同时放在D盘下,调用打印机没问题。
2:动态路径加载:(参考:http://blog.csdn.net/u011088260/article/details/52005347)
TscLibDll INSTANCE = (TscLibDll) Native.loadLibrary ("TSCLIB",TscLibDll.class); 注意: 此时TSCLIB此时不能添加后缀名,设置跨平台的思想TSCLIB.dll加载顺序为:1:项目路径下2:库文件路径下:可使用库文件路径 System.out.println(System.getProperty("java.library.path"));打印到控制台ps:按照打印出来的路径去找,我的路径里有以前实验放进去的同名TSCLIB.dll文件,会导致报告out of memery,google说是类型转换问题,没有深入研究,换了打印机自带的dll解决问题后,中间可以报告文件:can't load (win32-x86.,,,跟随你的一串路径,根据这条路径找到没有发现问题,所以阅读本地图书馆的路径应该是上述,根据错误的路径没有发现打印机代码的问题:
package PrintTest;import com.sun.jna.Library;import com.sun.jna.Native;import jackingTest.PipeCutingItem;import jackingTest.PipejackingResult;public class PrintTest { 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 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 formfeed (); int nobackfeed (); int windowsfont (int x,int y,int fontheight,int rotation,int fontstyle,int fontunderline,String szFaceName,String content); } public static void main(String[] args) { System.out.println(System.getProperty("java.library.path")); System.setProperty("jna.encoding","GBK");// 支持中文 TscLibDll.INSTANCE.about(); TscLibDll.INSTANCE.openport("TSC TTP-247");//指定电脑输出端 TscLibDll.INSTANCE.downloadpcx("C:\\UL.PCX","UL.PCX"); TscLibDll.INSTANCE.sendcommand("REM ***** This is a test by JAVA. *****");//送内置指令到条形码打印机 TscLibDll.INSTANCE.setup("104", "30", "6", "10", "0", "2", "2"); //TscLibDll.INSTANCE.setup(标签宽度,标签高度,列印速速,打印浓度,感应器类别,垂直间距高度,偏移距离); /*setup(a,b,c,d,e,f,g) =》 说明: 设置卷标的宽度、高度、印刷速度、印刷浓度、传感器类别gap/black mark 垂直间距,gap/black mark a: 字符串型,设置卷标宽度,单位 mm b: 字符串类型,设置卷标高度,单位 mm c: 字符串型,设定打印速度,(打印速度随机型不同而有不同的选项) 1.0: 每秒1.0英寸打印速度 1.5: 每秒1.5英寸打印速度 .0: 每秒2.0寸打印速度 33.0: 每秒3.0英寸打印速度 44.0: 每秒4.0英寸打印速度 5.0: 每秒5.0英寸打印速度 .0: 每秒6. d: 字符串类型,设置打印浓度, 0~数字越大,打印结果越黑, e: 字符串型,设置使用感应器的类别 =》 0|表示使用垂直间距传感器(gap sensor) 1|表示使用黑标传感器(black mark sensor) f: 字符串类别,设置gap/black mark 垂直间距高度,单位: mm g: 字符串类别,设置gap/black mark 偏移距离,单位: mm,如果使用这个参数,一般的卷标设置为0 * */ TscLibDll.INSTANCE.clearbuffer(); TscLibDll.INSTANCE.sendcommand("PUTPCX 550,10,\"UL.PCX\""); TscLibDll.INSTANCE.printerfont ("100","10","3","0","1","1","(JAVA) DLL Test!!"); TscLibDll.INSTANCE.barcode("460", "60", "128", "90", "1", "0","2", "1","03121DY000000170050735"); TscLibDll.INSTANCE.barcode("50", "60", "128", "90", "1", "0","2", "1","A1234SD7800"); /* barcode(X方向起点,Y方向起点,字符串型别 ,条形码高度,是否打码文,旋转角度,窄bar,窄bar, 内容 ) * 5.barcode(a,b,c,d,e,f,g,h,I) 说明: 使用条形码机内建条形码打印 参数: a: 字符串型,条形码X方向起点,以点(point)表示。DPI,1点=1/8 mm,300 DPI,1点=1/12 mm) b: 字符串型,条形码Y方向起点,以点(point)表示。DPI,1点=1/8 mm,300 DPI,1点=1/12 mm) c: Code 128,switching code subset A,B,C automatically M Code 128,switching code subset A,B,C manually. EAN128 Code 128, switching code subset A, B, C d: 字符串型别,设定条形码高度,高度以点来表示 e: 字符串型别,设定是否打印条形码码文 0: 不打印码文 1: 打印码文 f: 字符串型别,设定条形码旋转角度 0: 旋转0度 90: 旋转90度 180: 旋转180度 270: 旋转270度 g: 字符串型别,设定条形码窄bar 比例因子,请参考TSPL使用手册 h: 字符串型别,设定条形码窄bar 比例因子,请参考TSPL使用手册 I: 字符串型别,条形码内容 */ /* TscLibDll.INSTANCE.windowsfont(450, 180, 40, 0, 3, 0, "arial", "SHL2CF890BBL467j"); TscLibDll.INSTANCE.windowsfont(50, 180, 40, 0, 3, 0, "arial", "广东工业大学");*/ /* windowsfont(X起点,Y起点,字体高度,旋转角度,字体外型,底线,字体,文字内容) TscLibDll.INSTANCE.windowsfont(400, 200, 48, 90, 3, 1, "arial", "DEG 90"); TscLibDll.INSTANCE.windowsfont(400, 200, 48, 180, 3, 1, "arial", "DEG 180"); TscLibDll.INSTANCE.windowsfont(400, 200, 48, 270, 3, 1, "arial", "DEG 270");*/ TscLibDll.INSTANCE.printlabel("1", "1");//打印标签式数,份数 TscLibDll.INSTANCE.closeport(); } //打印标板 public static String printMode(PipeCutingItem p1,PipeCutingItem p2,int length,boolean isDouble) { try { System.setProperty("jna.encoding", "GBK");//支持中文 TscLibDll.INSTANCE.openport("TSC TTP-247");//指定电脑输出端 TscLibDll.INSTANCE.setup("104", "30", "6", "10", "0", "2", "2"); TscLibDll.INSTANCE.clearbuffer(); //基数份数和偶数份数不同 if(isDouble) { TscLibDll.INSTANCE.windowsfont(450, 10, 40, 0, 3, 0, "arial", "原料"+length+"/下料"+p1.getCuting_length()); TscLibDll.INSTANCE.windowsfont(50, 10, 40, 0, 3, 0, "arial", "原料长"+length+"/下料"+p2.getCuting_length()); // barcode(X方向起始点,Y方向起始点,字符串型别 ,条形码高度,是否打码文,旋转角度, 窄bar,窄bar, 内容 ) TscLibDll.INSTANCE.barcode("460", "60", "128", "90", "1", "0", "2", "1", p1.toString()); TscLibDll.INSTANCE.barcode("50", "60", "128", "90", "1", "0", "2", "1", p2.toString()); // windowsfont(X起点,Y起点,字体高度,旋转角度,字体外型,底线,字体,文字内容) TscLibDll.INSTANCE.windowsfont(450, 180, 40, 0, 3, 0, "arial", p1.getPrintText()); TscLibDll.INSTANCE.windowsfont(50, 180, 40, 0, 3, 0, "arial", p2.getPrintText()); }else { TscLibDll.INSTANCE.windowsfont(450, 10, 40, 0, 3, 0, "arial", "原料"+length+"/下料"+p1.getCuting_length()); TscLibDll.INSTANCE.barcode("460", "60", "128", "90", "1", "0", "2", "1", p1.toString()); TscLibDll.INSTANCE.windowsfont(450, 180, 40, 0, 3, 0, "arial", p1.getPrintText()); } TscLibDll.INSTANCE.printlabel("1", "1");//打印标签式数,份数 TscLibDll.INSTANCE.closeport(); }catch (Exception e) { e.printStackTrace(); return "打印异常"; } return "打印成功"; } //打印二维码列 public static void printBarCode(PipejackingResult pjr) { for (Integer i = 0; i < pjr.getPipeJackingList().size(); i+=2) { if(pjr.getPipeJackingList().size()%2 == 0) {//偶数 printMode(pjr.getPipeJackingList().get(i),pjr.getPipeJackingList().get(i+1),pjr.getPipeLength(),true); }else {//奇数 if(i<pjr.getPipeJackingList().size()-2) {//奇数双列 printMode(pjr.getPipeJackingList().get(i),pjr.getPipeJackingList().get(i+1),pjr.getPipeLength(),true); }else{//奇数单列 printMode(pjr.getPipeJackingList().get(i),null,pjr.getPipeLength(),false); } } } } }