0

IplImage 我需要将( )数组的指针传递IplImage extends CvArray extends Structure implements cloneable给函数 C 中的本机代码如下:

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

我试过这个:

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

但它没有用。这个函数在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)
4

1 回答 1

1

您的 C 原型非常不清楚,但我会给您一些在 JNA 中乍一看并不明显的东西,但这可能是您遇到麻烦的原因。


在处理结构数组时,您需要执行以下操作:

// 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);

现在,如果您想知道为什么需要这样做(从 java 的角度来看这完全是疯狂的),您需要记住您不是在编写 Java 代码,而是在编写 C

标准 java 数组实际上是 void*,但标准 C 数组是 MyStructure*

如果 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 )
  • a 4 cell C array of MyStructure uses 48 Bytes (= 4 cell x 12 Bytes per 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

于 2011-01-21T14:59:19.087 回答