java中怎么读写bytebuffer,如何写入和读取从java传递到jni的bytebuffer

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.

this is what i got.

in java

ByteBuffer bb = ByteBuffer.allocateDirect(216);

IssmJni.processBuffer(bb);

native method

public static native void processBuffer(ByteBuffer bb);

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

}

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

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

this is my table of native methods

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.

解决方案

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

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.

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

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

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

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...

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值