Background
I'm working with byte arrays in JNI. And I can't get length of jbyteArray. I'm writing code in eclipse in Windows 7.
Java code:
private native int Enroll( byte[] pSeed );
JNI code:
In JNI I have a struct that have two members unsigned long length and unsigned char data[1]
typedef struct blobData_s {
unsigned long length;
unsigned char data[1];
} blobData_t;
Now as my JNI function get as argument jbyteArray jpSeed i want to get the length of jpSeed and set it as length member of struct.
JNIEXPORT jint JNICALL Java_com_Test_Enroll( JNIEnv* env, jobject thiz, jbyteArray jpSeed ){
blobData_t* bd = malloc( sizeof(blobData_t) );
bd->length = **Question 1**
bd->data[1] = jbyteArray;
}
Question 1: How I can get the length of jpSeed in JNI ?
Question 2: Will this code works correct bd.data[1] = jbyteArray; ?
You can use GetArrayLength(JNIEnv* env, jbyteArray array) Read here.
Not sure what you want to do, I assume you want the content of jpSeed in bd.data[1].
Anyways, accessing the contents of a byte array, should be done with GetByteArrayElements(...).
Solution
Answer on Question 1. As jpSeed is jbyteArray it mean that you can get it's length by calling GetByteArrayElements( ... ) functions that declared in JNI ( you can read documentation here ) here the right code will be:
JNIEXPORT jint JNICALL Java_com_Test_Enroll( JNIEnv* env, jobject thiz, jbyteArray jpSeed ){
blobData_t* bd = malloc( sizeof(blobData_t) );
bd->length = (*env)->GetArrayLength( env, jpSeed );
.......
}
Answer on Question 2. This bd->data[1] = jbyteArray; code is not correct as it will not be compiled the right solution for this part is:
JNIEXPORT jint JNICALL Java_com_Test_Enroll( JNIEnv* env, jobject thiz, jbyteArray jpSeed ){
blobData_t* bd = malloc( sizeof(blobData_t) );
.......
jbyte* bytes = (*env)->GetByteArrayElements( env, jpSeed, 0 );
bd->data[1] = bytes[1];
}
And don't forgot to release.
Related
I have a Problem to convert c++ pointer to jlong. Here is my Situation: I use JNI to load pre-trained opencv machine learning model ANN_MLP(since there is no load function in opencv Android SDK) and the load function works well. But the Problem is I donn't know how to convert this c++ pointer to jlong, just as I want to return this pointer for my App. I googled but haven't fund so i just use memcpy to hold this pointer(maybe this is not the perfect method for it). One more wired Thing is, whatever I set the size of memcpy, the result buffer(here jlong resutl) has always a Offset with 8 from src buffer(here is model). Below is the code Piece what i wrote:
JNIEXPORT jlong JNICALL reader_BridgeNativeModel_nativeCreateObject
(JNIEnv *jenv, jclass, jstring jFileName){
const char* jnamestr = jenv->GetStringUTFChars(jFileName, NULL);
string stdFileName(jnamestr);
Ptr<ml::ANN_MLP> model = Algorithm::load<ml::ANN_MLP>(stdFileName)
jlong result = 0;
memcpy(&result, model, 1); // the size seems doesn't work, tried 4, 8, but got the same result as below outputs
LOGD("model address :%p\t", &model);
LOGD("result address :%p\t", &result);
....
}
// the output of log always like this
model address :0xbeed51e8
result address :0xbeed51e0
When I convert this returned jlong object as MLP pointer for prediction, I also have no idea how to convert it as model pointer:
JNIEXPORT jint JNICALL _BridgeNativeModel_nativePredict
(JNIEnv *jenv, jclass, jlong thiz, jstring jFileName,jlong src, jlong dst){
Ptr<ml::ANN_MLP> model;
printf("thiz %p\n", &thiz);
memcpy(&model, thiz, 4); // always crashed here, memcpy failed
....
}
Thanks for any suggestions in advance.
Jian
I tried implementing direct convolution of two audio files using JNI on Android.. So far, I ve done this:
JNIEXPORT jint JNICALL Java_com_example_directconv_MainActivity_convolve (JNIEnv * env, jobject obj, jdoubleArray signal1, jdoubleArray signal2, jdoubleArray output)
{
jdouble *sig1, *sig2, *out;
// jboolean isCopy1, isCopy2, isCopy3;
int i,j;
jsize n,m;
sig1=(*env)->NewDoubleArray(env,n);
sig2=(*env)->NewDoubleArray(env,m);
out=(*env)->NewDoubleArray(env,m);
n=(*env)->GetArrayLength(env, sig1);
m=(*env)->GetArrayLength(env,sig2);
sig1=(*env)->GetDoubleArrayElements(env, signal1,NULL);
sig2=(*env)->GetDoubleArrayElements(env, signal2,NULL);
out=(*env)->GetDoubleArrayElements(env, output, NULL);
if (sig1 != NULL || sig2!=NULL) {
memcpy(signal1,sig1,n);
memcpy(signal2,sig2,m);
(*env)->ReleaseDoubleArrayElements(env,signal1,sig1,JNI_ABORT);
(*env)->ReleaseDoubleArrayElements(env,signal2,sig2,JNI_ABORT);
}
for(i=0; i<n;i++)
{
out[i]=0;
for(j=0;j<m;j++)
{
out[i]+=sig1[i-j]*sig2[j];
}
}
(*env)->ReleaseDoubleArrayElements(env,output,out,0);
return 1;}
And on the java side:
public class MainActivity extends Activity {
static
{
System.loadLibrary("DirectConv");
}
File externalDir1=Environment.getExternalStorageDirectory();
File externalDir2=Environment.getExternalStorageDirectory();
File f1=new File(externalDir1.getAbsolutePath()+"/Test"+File.separator+"wav2.wav");
File f2=new File(externalDir2.getAbsolutePath()+"/Test"+File.separator+"wav2.wav");
int res;
public native int convolve(double[]signal1, double[]signal2, double[]output);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
//---------------------------------------------read files
double[]raw1=read(f1);
double[]raw2=read(f2);
double[]out=new double[raw1.length];
res=convolve(raw1,raw2,out);
for(int i=0;i<out.length;i++)
Log.i("out", "out "+ out[i]);
}
The resulting array (out) is all zeros. I don't see what I did wrong. Any help?
Your handling of JNI arrays is all wrong. First off, there's no reason for you to be creating any new arrays here. YOur inputs and outputs are passed down, what you want to do is convert the inputs to C arrays, work on them in pure C, then convert your output array to a Java array.
Secondly, you don't need the memcpys. Calling getDoubleArrayElements does it for you.
Third, you need to put the values in out back into output via setDoubleArrayRegion
Fourth, you need to clean up your memory usage. If you don't, you're going to memory leak and eventually die as there's a limit of 256 (or is it 512? Been too long I forget) Java objects pinned to C.
Your code should look more like:
JNIEXPORT jint JNICALL Java_com_example_directconv_MainActivity_convolve (JNIEnv * env, jobject obj, jdoubleArray signal1, jdoubleArray signal2, jdoubleArray output)
{
double *sig1, *sig2, *out;
int i,j;
jsize n,m;
n=(*env)->GetArrayLength(env, sig1);
m=(*env)->GetArrayLength(env,sig2);
sig1=(*env)->GetDoubleArrayElements(env, signal1,NULL);
sig2=(*env)->GetDoubleArrayElements(env, signal2,NULL);
for(i=0; i<n;i++)
{
out[i]=0;
for(j=0;j<m;j++)
{
out[i]+=sig1[i-j]*sig2[j];
}
}
(*env)->SetDoubleArrayRegion(env, output, 0, n, out);
(*env)->ReleaseDoubleArrayElements(env,signal1,sig1,JNI_ABORT);
(*env)->ReleaseDoubleArrayElements(env,signal2,sig2,JNI_ABORT);
return 1;}
I'm going to assume your convolution math is right, its way too late for me to start remembering that stuff :)
I figured out the problem. I added files sizes as parameters like this:
JNIEXPORT jint JNICALL Java_com_example_directconv_MainActivity_convolve (JNIEnv * env, jobject obj, jdoubleArray signal1, jdoubleArray signal2, jdoubleArray output, jint size1, jint size2)
{
jdouble *sig1, *sig2, *out;
int i,j,k;
jint n,m;
n=size1;
m=size2;
sig1=(*env)->GetDoubleArrayElements(env, signal1,NULL);
sig2=(*env)->GetDoubleArrayElements(env, signal2,NULL);
out=(*env)->GetDoubleArrayElements(env,output,NULL);
for(i=0; i<n;i++)
{
out[i]=0;
for(j=0;j<m;j++)
{
if(i-j>=0)
out[i]+=sig1[i-j]*sig2[j];
}
}
(*env)->SetDoubleArrayRegion(env, output, 0, n, out);
(*env)->ReleaseDoubleArrayElements(env,signal1,sig1,JNI_ABORT);
(*env)->ReleaseDoubleArrayElements(env,signal2,sig2,JNI_ABORT);
return 1;
}
I need get byte array from jni to Java.
Ex: I have a byte array byte[] a = {1,2,3,4,5,6}
JNIEXPORT jbyteArray JNICALL Java_com_vn_getArray (JNIEnv *env, jobject obj) {
jbyte[] a = {1,2,3,4,5,6};
return a;
}
I do not know how to return a byte array from jni.
Can someone help me? Please!
In Java, an array is an object. So to hand a byte array from C or C++ over to java you will need to instantiate a jbyteArray, and return that. Instead of a C array. To solve that, see the following code:
JNIEXPORT jbyteArray JNICALL Java_Test_returnArray
(JNIEnv *env, jobject This)
{
jbyte a[] = {1,2,3,4,5,6};
jbyteArray ret = env->NewByteArray(6);
env->SetByteArrayRegion (ret, 0, 6, a);
return ret;
}
Based on this link
I do like that and it's working
JNIEXPORT jbyteArray JNICALL Java_com_vn_getArray(JNIEnv *env, jobject obj) {
jbyte byteUrl[] = {1,2,3,3,4};
int sizeByteUrl = 5;
jbyteArray data = (*env)->NewByteArray(env, sizeByteUrl);
if (data == NULL) {
return NULL; // out of memory error thrown
}
// creat bytes from byteUrl
jbyte *bytes = (*env)->GetByteArrayElements(env, data, 0);
int i;
for (i = 0; i < sizeByteUrl; i++) {
bytes[i] = byteUrl[i];
}
// move from the temp structure to the java structure
(*env)->SetByteArrayRegion(env, data, 0, sizeByteUrl, bytes);
return data;
}
Eclipse gives me this error when I try to build:
jni/cyberlevel9.c:17:31: error: request for member 'NewDirectByteBuffer' in something not a structure or union
jni/cyberlevel9.c:18:28: error: request for member 'NewGlobalRef' in something not a structure or union
This is the problem part of the code:
JNIEXPORT jobject JNICALL Java_com_cyberbg_natcamlevel9_NativeCameraLevel9Start_allocNativeBuffer(JNIEnv* env, jobject this, jlong size)
{
void* buffer = malloc(size);
jobject directBuffer = env->NewDirectByteBuffer(buffer, size);
jobject globalRef = env->NewGlobalRef(directBuffer);
return globalRef;
//return (NewDirectByteBuffer*)(*env)->NewDirectByteBuffer(buffer, size);
}
JNI calls from C program look like
(*env)->fun(env, p1, ...)
Your calling style is OK from C++, where a special wrapper class is defined in jni.h:
env->fun(p1, ...)
You can probably resolve your problems by simply renaming jni/cybrrlevel9.c to jni/cyberlevel9.cpp
Or,
jobject directBuffer = (*env)->NewDirectByteBuffer(env, buffer, size);
jobject globalRef = (*env)->NewGlobalRef(env, directBuffer);
I am trying to understand jni, so I started hacking up hellojni, and I ran into this problem.
My java code looks like this:
short[] buf = new short[16];
Log.d("hello", "before!");
write(buf, 0, 16);
and my C code looks like this:
jint
Java_com_example_hellojni_HelloJni_write(JNIEnv* env, jshortArray buf, jint off, jint len)
{
char debug[1024];
int ii = 0;
jsize cbuflen = (*env)->GetArrayLength(env, buf);
sprintf(debug, "array length: %d", cbuflen);
LOGD(debug);
...
...
The output is:
array length: 1079082088
Why is the array length so big?
Could you show your entire JNI file? You are not declaring the target object in your JNI function. Usually the arguments are JNIEnv* env, jobject javaObject, etc. This means that what you believe is the jshortArray is actually the pointer to a Java object, which would explain the weird results you are getting.