This article original, reprint please specify address: http://www.cnblogs.com/baokang/p/4982846.html
For basic usage of JNI, see: Java calls C + + (Java call DLL) Brother Hong, teach you.
For debugging of JNI, see: JNI Debugging: Eclipse + Vs Federated debugging
No more nonsense, directly on the code
1.java Native Interface
/** * The square of each element in the three-dimensional array src, DST output @param src input @param DST output * /publicnativestaticvoid calthreedimensional ( int [][][] src,int[][][] DST);
2.Jni Interface Implementation
1 /*2 * Class:com_bk_jnidemo_jnidemo3 * Method:calthreedimensional4 * Signature: ([[[I[[[i) V5 */6JniexportvoidJnicall java_com_bk_jnidemo_jnidemo_calthreedimensional (JNIENV *env, Jclass cls, Jobjectarray src, jobjectarray DST)7 {8 intx, y, z9 Jobjectarray Src_xy,dst_xy;Ten Jintarray src_x,dst_x; One Jint temp; AJint *xsrcvalues; -Jint *xdstvalues; - the /*------------------------Get the three dimensions of x, Y, z-----------------------*/ -Src_xy=static_cast<jobjectarray> (Env->getobjectarrayelement (src,0)); -Dst_xy=static_cast<jobjectarray> (Env->getobjectarrayelement (dst,0)); - +Src_x=static_cast<jintarray> (Env->getobjectarrayelement (src_xy,0)); -Dst_x=static_cast<jintarray> (Env->getobjectarrayelement (dst_xy,0)); + AZ=env->getarraylength (SRC); atY=env->getarraylength (SRC_XY); -X=env->getarraylength (src_x); - - /*------------------------three-layer loop processing-----------------------------*/ - for(inti=0;i<z;i++) - { inSrc_xy=static_cast<jobjectarray> (env->getobjectarrayelement (src,i)); -Dst_xy=static_cast<jobjectarray> (env->getobjectarrayelement (dst,i)); to + for(intj=0;j<y;j++) - { theSrc_x=static_cast<jintarray> (env->getobjectarrayelement (src_xy,j)); *Dst_x=static_cast<jintarray> (env->getobjectarrayelement (dst_xy,j)); $ Panax NotoginsengXsrcvalues=env->getintarrayelements (src_x,null); -Xdstvalues=env->getintarrayelements (dst_x,null); the for(intk=0;k<x;k++) + { A /*--------here to do a single element processing----------*/ thexdstvalues[k]=xsrcvalues[k]*Xsrcvalues[k]; + } - /*-------------------Data writeback: Layer x---------------------*/ $Env->releaseintarrayelements (Src_x,xsrcvalues,jni_abort); $Env->releaseintarrayelements (dst_x,xdstvalues,0); - - /*----------------Data writeback: Y-layer----------*/ theEnv->setobjectarrayelement (dst_xy,j,dst_x); - }Wuyi /*---------------Data writeback: Z-layer---------*/ theEnv->setobjectarrayelement (DST,I,DST_XY); - } Wu}
Jni: three-dimensional array processing method, taking shaping three-dimensional array as an example C + + implementation