gpt4 book ai didi

java - 指向结构指针的指针作为 JNA 中的参数

转载 作者:太空宇宙 更新时间:2023-11-03 23:40:24 25 4
gpt4 key购买 nike

第 3 方 DLL 有一个函数,它需要一个指向结构指针的指针作为参数:

__declspec(dllimport) int __stdcall
SegmentImages( unsigned char* imageData, int imageWidth, int imageHeight,
int* numOfFinger, SlapInfo** slapInfo, const char* outFilename );

它将 numOfFinger 个指纹(通常是 4 个)的“拍打”图像分割成单个指纹(输出文件名 + 手指数)。

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

根据 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)

我也尝试过:

在所有情况下,我都会收到“无效内存访问”错误。

我做错了什么?

最佳答案

如果 SegmentImages 返回 slapInfo 中一个或多个结构的地址,您需要使用 PointerByReference。实际的 native 类型是 struct **,但您在 slapInfo 中返回的值是一个指针。

IntegerByReference iref = new IntegerByReference();
PointerByReference pref = new PointerByReference();
int result = SegmentImages(..., iref, pref, ...);

如果结构在单个 block 中返回,您可以这样做:

SlapInfo s = new SlapInfo(pref.getValue());
SlapInfo[] slaps = s.toArray(iref.getValue());

关于java - 指向结构指针的指针作为 JNA 中的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47420070/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com