Java和C ++之间使用JNI的SharedBuffer [英] SharedBuffer between Java and C++ using JNI
问题描述
我正在研究的是使用C ++从相机读取图像数据,并使用JNI让Java读取那些图像数据,而我正在使用Mac OSX.
what I am working on is to read image data from camera using C++ and use JNI to let Java read those image data, and I am using Mac OSX.
我目前拥有的阻止者是,我还没有找到任何在Mac OSX上的两种语言之间创建共享内存的方法.
The blocker I currently have is, I have not found any method to create a shared memory between two language on Mac OSX.
我可以想象我会使用一些缓冲区来做到这一点,有人能对此问题提供一些提示和知识吗?
I could imagine I will be using some buffer to do so, could anyone gives some hints and knowledge towards this problem?
提前谢谢!
推荐答案
You can use ByteBuffer.allocateDirect
to create native accessible memory:
public static native void getCameraData(ByteBuffer bb);
...
ByteBuffer bb = ByteBuffer.allocateDirect(1024);
getCameraData(bb);
// use data
Then on the C++ side, use GetDirectBufferAddress
, to access the block of memory:
JNIEXPORT void JNICALL Java_MyClass_getCameraData(JNIEnv *env, jclass, jobject bb) {
void* data = env->GetDirectBufferAddress(bb);
}
这篇关于Java和C ++之间使用JNI的SharedBuffer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!