第三方DLL有一个函数,它希望指向结构的指针作为参数:
__declspec(dllimport) int __stdcall
SegmentImages( unsigned char* imageData, int imageWidth, int imageHeight,
int* numOfFinger, SlapInfo** slapInfo, const char* outFilename );
它将 numOfFinger 指纹(通常为4)的“拍击”图像分割成单个指纹(outFilename +手指数)。
SlapInfo的定义是:
struct SlapInfo {
int fingerType;
Point fingerPosition[4];
int imageQuality;
int rotation;
int reserved[3];
};
struct Point {
int x;
int y;
};
C中示例的片段:
unsigned char* imageData;
int imageWidth, imageHeight;
//obtain imageData, imageWidth and imageHeight from scanner...
int numOfFinger;
SlapInfo* slapInfo;
int result = SegmentImages( imageData, imageWidth, imageHeight, &numOfFinger,
&slapInfo, FINGER_IMG_PATH);
根据JNA FAQ,在我的情况下,我应该使用“Structure.ByReference []”:
void myfunc(simplestruct** data_array, int count); // use Structure.ByReference[]
所以,我像这样在Java / JNA中映射:
public class Point extends Structure {
public int x;
public int y;
public Point(){
super();
}
@Override
protected List<String> getFieldOrder() {
return Arrays.asList("x", "y");
}
}
public class SlapInfo extends Structure {
public int fingerType;
public Point[] fingerPosition = new Point[4];
public int imageQuality;
public int rotation;
public int[] reserved = new int[3];
public SlapInfo() {
super();
}
public SlapInfo(Pointer pointer){
super(pointer);
}
@Override
protected List<String> getFieldOrder() {
return Arrays.asList("fingerType", "fingerPosition", "imageQuality",
"rotation", "reserved");
}
public static class ByReference extends SlapInfo implements Structure.ByReference { }
}
int SegmentImages(byte[] imageData, int imageWidth, int imageHeight,
IntByReference numOfFinger, SlapInfo.ByReference[] slapInfo,
String outFileName);
//imageData, width and height are with valid values
IntByReference nrOfFingers = new IntByReference();
SlapInfo.ByReference[] slapInfoArray = (SlapInfo.ByReference[])
new SlapInfo.ByReference().toArray(4);
//Usually, there are 4 fingers in a slap, but the correct
//value should be the returned in nrOfFingers
int result = lib.SegmentImages(imageData, width, height,
nrOfFingers, slapInfoArray, FINGER_IMG_PATH);
但是使用这种方法,我得到错误:
java.lang.Error: Invalid memory access
at com.sun.jna.Native.invokeInt(Native Method)
at com.sun.jna.Function.invoke(Function.java:419)
at com.sun.jna.Function.invoke(Function.java:354)
at com.sun.jna.Library$Handler.invoke(Library.java:244)
at com.sun.proxy.$Proxy0.SegmentImages(Unknown Source)
at scanners.JNAScanner.segmentToFile(JNAScanner.java:366)
我也尝试过:
在所有情况下,我都收到错误“内存访问无效”。
我做错了什么?
答案 0 :(得分:1)
如果SegmentImages
返回slapInfo
中一个或多个结构的地址,则需要使用PointerByReference
。实际的原生类型为struct **
,但您在slapInfo
中返回的值是指针。
IntegerByReference iref = new IntegerByReference();
PointerByReference pref = new PointerByReference();
int result = SegmentImages(..., iref, pref, ...);
如果在单个块中返回结构,则可以执行以下操作:
SlapInfo s = new SlapInfo(pref.getValue());
SlapInfo[] slaps = s.toArray(iref.getValue());