通过jni将2d矩阵从Java传递到C ++文件 [英] passing a 2d matrix from java to C++ file through jni
本文介绍了通过jni将2d矩阵从Java传递到C ++文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在java_method()方法的MyClass.java文件中的Java中有一个二维矩阵,并且我已经声明了一个本机方法c_method().矩阵是浮点型的,例如:
I have a 2d matrix in java say in a file MyClass.java in the method java_method() and I have declared a native method say c_method(). the matrix is float type like:
float[][] pos_matrix;
大小为3by4的
,我已经在Java中初始化了矩阵.现在,我想将此矩阵传递给jni中的cpp文件.该怎么做?
of size 3by4 and I have initialized the matrix in java. Now I want to pass this matrix to my cpp file in the jni. How to do that?
推荐答案
两个选项:
- 将矩阵编码为长度为12的一维数组,作为float []传递.在本机端结果为
jfloatArray
.无法直接使用,请阅读Get/ReleaseFloatArrayElements
- 使用方法
float GetValueAt(int,int)
(或类似方法)将Java矩阵包装在Facade类中,并通过传递实例(本机端jobject
中的结果)然后调用该方法(GetMethodID/CallFloatMethod)在本机端进行访问
- encode the matrix in 1D array of length 12, pass as float[]. Results in
jfloatArray
on native side. Cannot be used directly, read aboutGet/ReleaseFloatArrayElements
- wrap the Java matrix in a facade class with method
float GetValueAt(int,int)
(or similar) and access on native side by passing the instance (results injobject
on native side) and then calling that method (GetMethodID/CallFloatMethod)
选项1更简单(编码更少),选项2以"OO方式"更简洁-关注点分离.使用选项1,您几乎可以在JVM不显示时修改阵列.
Option 1 is simpler (less coding), option 2 is cleaner in "OO way" - separation of concerns. With option 1 you can practically modify the array when JVM is not looking.
这篇关于通过jni将2d矩阵从Java传递到C ++文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文