简体   繁体   English

如何使用JNA将指向结构数组的指针传递给Java中的函数?

[英]How to pass a pointer to a struct array to a function in java using JNA?

I need to pass pointer of an array of IplImage ( IplImage extends CvArray extends Structure implements cloneable ) to a function The native code in C is as follows: 我需要将IplImage数组的IplImageIplImage extends CvArray extends Structure implements cloneable )传递给函数C中的本机代码如下:

cvCalcEigenObjects(
  nTrainFaces,
  (void*)faceImgArr,
  (void*)eigenVectArr,
  CV_EIGOBJ_NO_CALLBACK,
  0,
  0,
  &calcLimit,
  pAvgTrainImg,
  eigenValMat->data.fl);

I tried this: 我尝试了这个:

cvCalcEigenObjects(
  nTrainFaces,
  faceImgArr[0].getPointer(),
  eigenVectArr[0].getPointer(),
  CV_EIGOBJ_NO_CALLBACK,
  0,
                null,
  calcLimit,
  pAvgTrainImg,
  eigenValMat.data.getFloatArray(0, Pointer.SIZE));

but it didn't work. 但这没用。 The declaration of this function in Java is like this: Java中此函数的声明如下:

public static void cvCalcEigenObjects(int i, 
  Pointer pntr, 
  Pointer pntr1, 
  int i1, 
  int 2, 
  Pointer pntr2, 
  cxcore.CvTermCriteria ctc, 
  cxcore.IplImage ii, 
  FloatBuffer fb)

Your C prototype is quite unclear but I'll give you something that's not obvious at first glance in JNA but that might be the cause of your troubles. 您的C原型还不太清楚,但是我会给您一些乍一看在JNA中不明显的东西,但这可能是您遇到麻烦的原因。


When dealing with array of structures you need to do something like these : 处理结构数组时,您需要执行以下操作:

// Syntax to get a new empty structure array (4 cells) to pass to a function
// which will populate it
MyStructureClass[] incomingStructArray = new MyStructureClass().toArray(4);

// Syntax to transform a standard java array to an array suitable 
// to be passed to a C function
MyStructureClass[] standardJavaStructArray = ....
MyStructureClass[] outgoingStructArray = new MyStructureClass().toArray(standardJavaStructArray);

Now if you wonder why one would need to do so (which is completly crazy from the java point of view) you need to remember you're not coding Java, you're coding C 现在,如果您想知道为什么需要这样做(从Java的角度来看这完全是疯狂的),您需要记住您不是在编写Java代码,而是在编写C语言

A standard java array is in fact a void* but a standard C array is a MyStructure* 实际上,标准的Java数组是void *,而标准的C数组是MyStructure *

If MyStructure uses 12 Bytes in memory : 如果MyStructure在内存中使用12个字节:

  • a 4 cell Java array of MyStructureClass uses 16 Bytes (= 4 cell x 4 Bytes per pointer) in memory (not entirely true but let say so ; if all cells != null then an additional 48 Bytes will be used for the MyStructureClass themselves ) 一个4单元格的MyStructureClass Java数组在内存中使用16字节(= 4单元x每个指针4字节)(不是完全正确,但是可以这么说;如果所有单元格都等于null,那么MyStructureClass本身将另外使用48字节)
  • a 4 cell C array of MyStructure uses 48 Bytes (= 4 cell x 12 Bytes per MyStructure) MyStructure的4单元C数组使用48字节(= 4单元x 12字节/ MyStructure)

That's why when using JNA and array of structures you need to be very carefull with what you do, beacause an array of structure is very different from an array of pointers to structure 这就是为什么在使用JNA和结构数组时,您需要非常小心自己做的事情,因为结构数组与结构指针数组非常不同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM