项目中需要利用JNA调用dll文件,dll方法中不能直接接受Mat对象,因此先对Mat对象特别是像素值操作再传递。
1、映射dll到java接口
package jna_test;
import com.sun.jna.Library;
import com.sun.jna.Native;
public interface jna_test_dll extends Library {
jna_test_dll instanceDll = (jna_test_dll)Native.loadLibrary("Detect",jna_test_dll.class);
public String detect(int rows,int cols,int type,byte[] data,int step,int channel);
}
2、调用接口方法
package jna_test;
import jna_test.jna_test_dll;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.videoio.VideoCapture;
public class jna_test{
public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
VideoCapture capture0=new VideoCapture(0);
if(!capture0.isOpened()) {
System.out.println("could not load video data0...");
return;
}
while(true)
{
boolean isread0 = capture0.read(frame0);
if(!isread0) break;
String result0;
long npixels0 = frame0.total() * frame0.elemSize();
byte[] pixels0 = new byte[(int)npixels0];
frame0.get(0,0,pixels0);//获取Mat对象像素值
result0 = jna_test_dll.instanceDll.detect(frame0.rows(),frame0.cols(),frame0.type(),pixels0,(int)frame0.step1(), 1);
System.out.print(result0);
}
}
}