如何写入和读取从java传递到jni的bytebuffer [英] how to write and read from bytebuffer passing from java to jni

查看:489
本文介绍了如何写入和读取从java传递到jni的bytebuffer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的android项目需要帮助。
我想将缓冲区从java传递给jni,我的C ++代码将填充数据。然后java将在屏幕上显示它们。
我对C ++并不熟悉,也不知道如何写入写入缓冲区。

I need help with my android project. I want to pass a buffer from java to jni, and my C++ code will populate the data. Then java will display them on the screen. I am not familiar much with C++ and have no idea how to write to write to the buffer.

这就是我得到的。
in java

this is what i got. in java

ByteBuffer bb = ByteBuffer.allocateDirect(216);
        IssmJni.processBuffer(bb);

原生方法

public static native void processBuffer(ByteBuffer bb);

我不使用jni_onload,因此C ++中没有javah

I don't use jni_onload, so no javah in C++

static void fillBuffer(JNIEnv *env, jclass clazz, jobject buf)
    {
        double *dBuf = env->GetDirectBufferAddress(env, buf);

    }

我被困在这里,我可以做双重 DBUF?或者它必须是一个字符

I am stuck here, can I do double dbuf? or is it has to be a char?

我想写一下1,2,3,4,5给这个dbuf,我该怎么办做到了吗?
我在考虑dbuf.put(1); ... dbuf.put(5)但它不起作用。
并且在填充之后,我只是在java中调用bb.get(position)吗?

Let said I want to write 1,2,3,4,5 to this dbuf, how do I do it? i am thinking of dbuf.put(1); ... dbuf.put(5) but it is not working. and after populating it, do I just call bb.get(position) in java?

有人为我澄清这个,例如,将会赞赏
谢谢

Someone clarify this for me please, example would be appreciated Thank you

这是我的原生方法表

静态JNINativeMethod method_table [] = {

static JNINativeMethod method_table[] = {

{"fac"      ,     "(J)J" , (void *) factorial},
    {"getBuffer",     "()[D" , (void *) getBufferNative},
    //{"processBuffer", "(Ljava/nio/ByteBuffer)V", (void *) fillBuffer}};

除了最后一个之外,其他两个工作正常。

The other two works fine except the last one.

推荐答案


我不使用jni_onload,所以C ++中没有javah

I don't use jni_onload, so no javah in C++

这可能与您的问题无关,但您必须做其中一个。您需要在onLoad函数中执行Java-to-Native方法映射,或使用javah生成JNI在运行时可以获取的方法签名。否则,永远不会调用你的原生方法。

It may be irrelevant to your question, but you sort of have to do one or the other. You either need to do your Java-to-Native method mapping in the onLoad function or use javah to generate the method signatures that JNI can pick up at runtime. Otherwise your native methods will never be invoked.

如果正确调用你的原生方法,不管你做什么都可能没问题。

If your native method is properly being called, however, whatever you are doing is probably fine.


我被困在这里,我可以做双dbuf吗?或者它必须是一个字符?

I am stuck here, can I do double dbuf? or is it has to be a char?

该函数的返回值实际上是 void * ,因此您可以将它转换为您想要的任何指针类型,因为它只是一个内存地址。但是,直接缓冲区的实际数据类型将是 jbyte ,这是一个 signed char 的typedef,所以以下可能是最好的:

The return value of the function is actually a void*, so you can cast it to whatever pointer type you want since it is just a memory address. However, the actual data type of the direct buffer will be jbyte, which is a typedef of signed char, so the following is probably best:

jbyte *dBuf = env->GetDirectBufferAddress(env, buf);




我想说要写1,2,3,4, 5到这个dbuf,我该怎么做?

Let said I want to write 1,2,3,4,5 to this dbuf, how do I do it?

用C / C ++访问数组中值的符号是[]符号,即:

The notation in C/C++ to access values in an array is with [] notations, i.e.:

jbyte *dBuf = env->GetDirectBufferAddress(env, buf);

dBuf[0] = 63;
dBuf[1] = 127;
// ...and so on...




填充后,我只是在java中调用bb.get(position)吗?

and after populating it, do I just call bb.get(position) in java?

是的,你可以使用访问器方法在 ByteBuffer 上访问您从本机代码中写入的数据。

Yes, you can use the accessor methods on ByteBuffer to access the data you have written from your native code.

这篇关于如何写入和读取从java传递到jni的bytebuffer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆