JNA加载DLL库

因为项目需要,要在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吋打印速度       2.0: 每秒2.0吋打印速度     3.0: 每秒3.0吋打印速度  
                    4.0: 每秒4.0吋打印速度        5.0: 每秒5.0吋打印速度      6.0: 每秒6.0吋打印速度                                                  
                d: 字符串型别,设定打印浓度,   0~15,数字愈大打印结果愈黑  
                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)表示。   (200 DPI,1点=1/8 mm, 300 DPI,1点=1/12 mm)  
	         b: 字符串型别,条形码Y方向起始点,以点(point)表示。   (200 DPI,1点=1/8 mm, 300 DPI,1点=1/12 mm)                
             c: 字符串型别  
                128             Code 128, switching code subset A, B, C   
                automatically   
                128M            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






 
  


你可能感兴趣的:(java基础学习)