java数据结构默认均为有符号数,而通过jni转换到c/c++层,却不一定是有符号数。
如若在java中存储的即为无符号数,则在jni中可将jbyte直接进行类型转换。
若进行操作,则可在计算时,先将byte&0xff,这样即可转换为32位数据,而后再进行计算。
转换方式如下:
1、jbyteArray转换为unsigned char*
Java
public class example {
public final static native void set_Foo_array(long jarg0, short[] jarg1);
public final static native short[] get_Foo_array(long jarg0);
}
c++ code:
class Foo {
public:
unsigned char array[];
}; extern "C"{
JNIEXPORT void JNICALL Java_example_set_1Foo_1array(JNIEnv *jenv, jclass jcls, jlong jarg0, jshortArray jarg1) {
Foo *arg0 ;
unsigned char *arg1 ;
int i ;
jshort* jarg1_carray ;
jsize jarg1_len = jenv->GetArrayLength(jarg1) ; arg0 = *(Foo **)&jarg0;
jarg1_carray = jenv->GetShortArrayElements(jarg1, );
arg1 = (unsigned char *) malloc(jarg1_len * sizeof(unsigned char ));
for(i=; i<jarg1_len; i++)
arg1[i] = (unsigned char )jarg1_carray;
{
int i;
for (i=; i<; i++)
arg0->array[i] = arg1[i];
}
jenv->ReleaseShortArrayElements(jarg1, jarg1_carray, );
free(arg1);
}} extern "C"{
JNIEXPORT jshortArray JNICALL Java_example_get_1Foo_1array(JNIEnv *jenv, jclass jcls, jlong jarg0) {
jshortArray jresult = ;
Foo *arg0 ;
unsigned char *result ;
jshort* jnitype_ptr = ;
int k ; arg0 = *(Foo **)&jarg0;
result = (unsigned char *)(unsigned char *) (arg0->array);
jresult = jenv->NewShortArray();
jnitype_ptr = jenv->GetShortArrayElements(jresult, );
for (k=; k<; k++)
jnitype_ptr[k] = (jshort)result[k];
jenv->ReleaseShortArrayElements(jresult, jnitype_ptr, );
return jresult;
}} The code comes from the output of swig (www.swig.org) - a tool which generated the JNI and Java code given the C++ class definition above.
2、char*转jbyte
static .. jvm; // ref to jvm
static jobject printAPI; // the static class ref
static jmethodID loadBuffer; // the static method ref void loadImage(int x, int y, int width, int height, char* pixels, int maxWidth, int maxHeight){
JNIEnv* env;
jint result = (*jvm)->GetEnv(jvm, &env, JNI_VERSION_1_2);
if(result < || env == NULL){
fprintf(stderr, "Error finding JNI environment\n");
}else{
int i, size = width*height*;
jbyteArray rgb; // the byte array
jbyte* v; // array of jbytes for transfer operation /* Transfer char* to jbyteArray */
rgb = (*env)->NewByteArray(env, width * height *);
v = malloc( (width * height *) * sizeof(jbyte));
if( !rgb || !v ){
fprintf(stderr, "Error allocating memory in loadImage(..)");
return;
}
for(i=;i<size; ++i) v[i] = (jbyte) pixels; (*env)->SetByteArrayRegion(env, rgb, , size, v); /* send pixel dump to gui */ (*env)->CallStaticIntMethod(env, printAPI, loadBuffer, x, y, width, height, rgb); } } the method on the java side is
static void loadByteArray( int x, int y, int w, int h, byte[] rgb ){..}
Hope it helps.