Java代码示例:
package zfw;
/**
* 别问我为什么有些分隔注释写在C代码里面,有些写在Java里面,如果你问了,恭喜你,小小彩蛋一个
* 我发现...
* 1.如果native方法有返回值的话,会先执行完java线程中的代码,再执行C中的printf打印
* 2.如果native方法没有返回值的话,如果native方法中有printf语句,则会把所有调用了
* printf打印的地方先全部打印出来,再执行java的代码
* 当然,项目中的开发最终这些printf语句一般是不过引入的,方便开发时调试,所以知道下这个小
* tips也好。
* @author zfw
*
*/
public class JniTest {
// 1.1 返回基本数据类型系列
public native static int getInt();
public native static long getLong();
public native static float getFloat();
public native static double getDouble();
public native static String getString();
// 1.2 返回基本数据类型一维数组系列
public native static int[] getIntArray();
public native static long[] getLongArray();
public native static float[] getFloatArray();
public native static double[] getDoubleArray();
public native static String[] getStringArray();
// 1.3 返回基本数组类型二维数组系列
public native static int[][] get2DIntArray();
public native static long[][] get2DLongArray();
public native static float[][] get2DFloatArray();
public native static double[][] get2DDoubleArray();
public native static String[][] get2DStringArray();
// 2.1 传入基本数据类型参数系列
public native static void transferInt(int param);
public native static void transferLong(long param);
public native static void transferFloat(float param);
public native static void transferDouble(double param);
public native static void transferString(String param);
// 2.2 传入基本数据类型数组参数系列
public native static void transferIntArray(int[] param);
public native static void transferLongArray(long[] param);
public native static void transferFloatArray(float[] param);
public native static void transferDoubleArray(double[] param);
public native static void transferStringArray(String[] param);
// 2.3 传入基本数据类型二维数组参数系列
public native static void transfer2DIntArray(int[][] param);
public native static void transfer2DLongArray(long[][] param);
public native static void transfer2DFloatArray(float[][] param);
public native static void transfer2DDoubleArray(double[][] param);
public native static void transfer2DStringArray(String[][] param);
// 2.4 传入的是bitmap类型或者自定义类对象呢?
public static void main(String[] args) {
System.out.println("-------------1.1 返回基本数据类型系列--------------");
int a0 = getInt();
System.out.println("getInt():" + a0);
long b0 = getLong();
System.out.println("getLong():" + b0);
float c0 = getFloat();
System.out.println("getFloat():" + c0);
double d0 = getDouble();
System.out.println("getDouble():" + d0);
String e0 = getString();
System.out.println("getString():" + e0);
System.out.println("------------1.2 返回基本数据类型一维 数组系列-------------");
int[] a1 = getIntArray();
System.out.print("getIntArray():");
new Printor(a1);
long[] b1 = getLongArray();
System.out.print("getLongArray():");
new Printor(b1);
float[] c1 = getFloatArray();
System.out.print("getFloatArray():");
new Printor(c1);
double[] d1 = getDoubleArray();
System.out.print("getDoubleArray():");
new Printor(d1);
String[] e1 = getStringArray();
System.out.print("getStringArray():");
new Printor(e1);
System.out.println("------------1.3 返回基本数据类型二维数组系列-------------");
int[][] a2 = get2DIntArray();
System.out.print("get2DIntArray():");
new Printor(a2);
long[][] b2 = get2DLongArray();
System.out.print("get2DLongArray():");
new Printor(b2);
float[][] c2 = get2DFloatArray();
System.out.print("get2DFloatArray():");
new Printor(c2);
double[][] d2 = get2DDoubleArray();
System.out.print("get2DDoubleArray():");
new Printor(d2);
String[][] e2 = get2DStringArray();
System.out.print("get2DStringArray():");
new Printor(e2);
// 2.1 传入基本数据类型参数系列
transferInt(10);
transferLong(214721222L);
transferFloat(11.22f);
transferDouble(10.1234567);
transferString("transfer a String type for jni.");
// 2.2 传入基本数据类型数组参数系列
transferIntArray(new int[] { 1, 2, 3 });
transferLongArray(new long[] { 214721222L, 214721223L, 214721224 });
transferFloatArray(new float[] { 0.1f, 0.2f, 0.3f });
transferDoubleArray(new double[] { 0.1234567, 1.1234567, 2.1234567 });
transferStringArray(new String[] { "hello", "java", "and", "jni" });
// 2.3 传入基本数据类型二维数组参数系列
transfer2DIntArray(new int[][]{{1,2},{3,4},{5,6}});
transfer2DLongArray(new long[][]{{214721222L, 214721223L},{214721224L, 214721225L},{214721226L, 214721227L}});
transfer2DFloatArray(new float[][]{{1.1f,2.2f},{3.3f,4.4f},{5.5f,6.6f}});
transfer2DDoubleArray(new double[][]{{1.1234567,2.1234567},{3.1234567,4.1234567},{5.1234567,6.1234567}});
transfer2DStringArray(new String[][]{{"hello","zfw"},{"lear","jni"},{"i love","java"}});
}
static {
System.loadLibrary("Project1");
}
}
C代码示例:
#include <stdio.h>
#include "zfw_JniTest.h"
#include <stdlib.h>
// 1.1 返回基本数据类型系列
JNIEXPORT jint JNICALL Java_zfw_JniTest_getInt
(JNIEnv *env, jclass jcls) {
int result = 5;
return result;
}
JNIEXPORT jlong JNICALL Java_zfw_JniTest_getLong
(JNIEnv *env, jclass jcls) {
unsigned long result = 2147483650;
return result;
}
JNIEXPORT jfloat JNICALL Java_zfw_JniTest_getFloat
(JNIEnv *env, jclass jcls) {
float result = 10.123f;
return result;
}
JNIEXPORT jdouble JNICALL Java_zfw_JniTest_getDouble
(JNIEnv *env, jclass jcls) {
double result = 110.5678910;
return result;
}
JNIEXPORT jstring JNICALL Java_zfw_JniTest_getString
(JNIEnv *env, jclass jcls) {
jstring result = (*env)->NewStringUTF(env, "I am String type.");
return result;
}
// 1.2 返回基本数据类型数组系列
JNIEXPORT jintArray JNICALL Java_zfw_JniTest_getIntArray
(JNIEnv *env, jclass jcls) {
int size = 5;
// 创建指定大小的数组
jintArray jint_arr = (*env)->NewIntArray(env, size);
jint *elems = (*env)->GetIntArrayElements(env, jint_arr, NULL);
int i = 0;
for (; i < size; i++) {
elems[i] = i;
}
(*env)->ReleaseIntArrayElements(env, jint_arr, elems, 0);
return jint_arr;
}
JNIEXPORT jlongArray JNICALL Java_zfw_JniTest_getLongArray
(JNIEnv *env, jclass jcls) {
int size = 5;
jlongArray jlong_arr = (*env)->NewLongArray(env, size);
jlong *elems = (*env)->GetLongArrayElements(env, jlong_arr, NULL);
int i = 0;
for (; i < size; i++) {
elems[i] = 2147483650 + i;
}
(*env)->ReleaseLongArrayElements(env, jlong_arr, elems, 0);
return jlong_arr;
}
JNIEXPORT jfloatArray JNICALL Java_zfw_JniTest_getFloatArray
(JNIEnv *env, jclass jcls) {
int size = 5;
jfloatArray jfloat_arr = (*env)->NewFloatArray(env, size);
jfloat *elems = (*env)->GetFloatArrayElements(env, jfloat_arr, NULL);
int i = 0;
for (; i < size; i++) {
elems[i] = (float)(i / 100.0);
}
(*env)->ReleaseFloatArrayElements(env, jfloat_arr, elems, 0);
return jfloat_arr;
}
JNIEXPORT jdoubleArray JNICALL Java_zfw_JniTest_getDoubleArray
(JNIEnv *env, jclass jcls) {
int size = 5;
jdoubleArray jdouble_arr = (*env)->NewDoubleArray(env, size);
jdouble *elems = (*env)->GetDoubleArrayElements(env, jdouble_arr, NULL);
int i = 0;
for (; i < size; i++) {
elems[i] = i+0.1234567;
}
(*env)->ReleaseDoubleArrayElements(env, jdouble_arr, elems, 0);
return jdouble_arr;
}
JNIEXPORT jobjectArray JNICALL Java_zfw_JniTest_getStringArray
(JNIEnv *env, jclass jcls) {
jobjectArray result;
jsize len = 5;
int index;
char* data[5] = { "Jni", "return", "String", "array", "type" };
result = (*env)->NewObjectArray(
env, len, (*env)->FindClass(env, "java/lang/String"), 0);
for(index = 0; index < 5; index++) {
(*env)->SetObjectArrayElement(
env, result, index, (*env)->NewStringUTF(env, data[index]));
}
return result;
}
// 1.3 返回基本数组类型二维数组系列
JNIEXPORT jobjectArray JNICALL Java_zfw_JniTest_get2DIntArray
(JNIEnv *env, jclass jcls) {
jobjectArray ret;
int row = 3;
int column = 2;
jsize len = row * column;
ret = (*env)->NewObjectArray(
env, len, (*env)->FindClass(env, "[I"), 0);
int i;
int j;
for (i = 0; i < row; i++) {
jintArray intArr = (*env)->NewIntArray(env, column);
jint *elems = (*env)->GetIntArrayElements(env, intArr, NULL);
for (j = 0; j < column; j++) {
elems[j] = i+j;
}
(*env)->ReleaseIntArrayElements(env, intArr, elems, 0);
(*env)->SetObjectArrayElement(env, ret, i, intArr);
(*env)->DeleteLocalRef(env, intArr);
}
return ret;
}
JNIEXPORT jobjectArray JNICALL Java_zfw_JniTest_get2DLongArray
(JNIEnv *env, jclass jcls) {
jobjectArray ret;
int row = 3;
int column = 2;
jsize len = row * column;
ret = (*env)->NewObjectArray(
env, len, (*env)->FindClass(env, "[J"), 0);
int i;
int j;
for (i = 0; i < row; i++) {
jlongArray longArr = (*env)->NewLongArray(env, column);
jlong *elems = (*env)->GetLongArrayElements(env, longArr, NULL);
for (j = 0; j < column; j++) {
elems[j] = i + j + 2147483650;
}
(*env)->ReleaseLongArrayElements(env, longArr, elems, 0);
(*env)->SetObjectArrayElement(env, ret, i, longArr);
(*env)->DeleteLocalRef(env, longArr);
}
return ret;
}
JNIEXPORT jobjectArray JNICALL Java_zfw_JniTest_get2DFloatArray
(JNIEnv *env, jclass jcls) {
jobjectArray ret;
int row = 3;
int column = 2;
jsize len = row * column;
ret = (*env)->NewObjectArray(
env, len, (*env)->FindClass(env, "[F"), 0);
int i;
int j;
for (i = 0; i < row; i++) {
jfloatArray floatArr = (*env)->NewFloatArray(env, column);
jfloat *elems = (*env)->GetFloatArrayElements(env, floatArr, NULL);
for (j = 0; j < column; j++) {
elems[j] = (float)((i+j) / 100.0);
}
(*env)->ReleaseFloatArrayElements(env, floatArr, elems, 0);
(*env)->SetObjectArrayElement(env, ret, i, floatArr);
(*env)->DeleteLocalRef(env, floatArr);
}
return ret;
}
JNIEXPORT jobjectArray JNICALL Java_zfw_JniTest_get2DDoubleArray
(JNIEnv *env, jclass jcls) {
jobjectArray ret;
int row = 3;
int column = 2;
jsize len = row * column;
ret = (*env)->NewObjectArray(
env, len, (*env)->FindClass(env, "[D"), 0);
int i;
int j;
for (i = 0; i < row; i++) {
jdoubleArray doubleArr = (*env)->NewDoubleArray(env, column);
jdouble *elems = (*env)->GetDoubleArrayElements(env, doubleArr, NULL);
for (j = 0; j < column; j++) {
elems[j] = i+j+ 0.1234567;;
}
(*env)->ReleaseDoubleArrayElements(env, doubleArr, elems, 0);
(*env)->SetObjectArrayElement(env, ret, i, doubleArr);
(*env)->DeleteLocalRef(env, doubleArr);
}
return ret;
}
JNIEXPORT jobjectArray JNICALL Java_zfw_JniTest_get2DStringArray
(JNIEnv *env, jclass jcls) {
jobjectArray ret = NULL;
const jsize row = 3;
const jsize column = 2;
char* data[3][2] = {
{"hello", "zfw"},
{"learn", "jni"},
{"i love", "java"}
};
jsize i;
jsize j;
jobjectArray *obj = malloc(sizeof(jobjectArray)*row*column);
for (i = 0; i < row; i++) {
jobjectArray objArr = (*env)->NewObjectArray(
env, column, (*env)->FindClass(env, "java/lang/String"), 0);
for (j = 0; j < column; j++) {
(*env)->SetObjectArrayElement(
env, objArr, j, (*env)->NewStringUTF(env, data[i][j]));
}
obj[i] = objArr;
}
for (i = 0; i < row; i++) {
jobjectArray objArr = obj[i];
if (ret == NULL) {
ret = (*env)->NewObjectArray(env, row, (*env)->GetObjectClass(env, objArr), 0);
}
(*env)->SetObjectArrayElement(env, ret, i, objArr);
}
free(obj);
obj = NULL;
return ret;
}
//2.1 传入基本数据类型参数系列
JNIEXPORT void JNICALL Java_zfw_JniTest_transferInt
(JNIEnv *env, jclass jcls, jint param) {
printf("------------2.1 传入基本数据类型参数系列-------------\n");
printf("transferInt():");
printf("%d\n", param);
}
JNIEXPORT void JNICALL Java_zfw_JniTest_transferLong
(JNIEnv *env, jclass jcls, jlong param) {
printf("transferLong():");
printf("%ld\n", param);
}
JNIEXPORT void JNICALL Java_zfw_JniTest_transferFloat
(JNIEnv *env, jclass jcls, jfloat param) {
printf("transferFloat():");
printf("%0.2f\n", param);
}
JNIEXPORT void JNICALL Java_zfw_JniTest_transferDouble
(JNIEnv *env, jclass jcls, jdouble param) {
printf("transferDouble():");
printf("%lf\n", param);
}
JNIEXPORT void JNICALL Java_zfw_JniTest_transferString
(JNIEnv *env, jclass jcls, jstring param) {
printf("transferString():");
char* str = (*env)->GetStringUTFChars(env, param, NULL);
printf("%s\n", str);
}
// 2.2 传入基本数据类型数组参数系列
JNIEXPORT void JNICALL Java_zfw_JniTest_transferIntArray
(JNIEnv *env, jclass jcls, jintArray arr) {
printf("----------2.2 传入基本数据类型数组参数系列------------\n");
printf("transferIntArray():");
jint *elems = (*env)->GetIntArrayElements(env, arr, NULL);
int len = (*env)->GetArrayLength(env, arr);
int index;
for (index = 0; index < len; index++) {
if (index != len - 1) {
printf("%d | ", elems[index]);
}
else {
printf("%d\n", elems[index]);
}
}
(*env)->ReleaseIntArrayElements(env, arr, elems, JNI_COMMIT);
}
JNIEXPORT void JNICALL Java_zfw_JniTest_transferLongArray
(JNIEnv *env, jclass jcls, jlongArray arr) {
printf("transferLongArray():");
jlong *elems = (*env)->GetLongArrayElements(env, arr, NULL);
int len = (*env)->GetArrayLength(env, arr);
int index;
for (index = 0; index < len; index++) {
if (index != len - 1) {
printf("%ld | ", elems[index]);
}
else {
printf("%ld\n", elems[index]);
}
}
(*env)->ReleaseLongArrayElements(env, arr, elems, JNI_COMMIT);
}
JNIEXPORT void JNICALL Java_zfw_JniTest_transferFloatArray
(JNIEnv *env, jclass jcls, jfloatArray arr) {
printf("transferFloatArray():");
jfloat *elems = (*env)->GetFloatArrayElements(env, arr, NULL);
int len = (*env)->GetArrayLength(env, arr);
int index;
for (index = 0; index < len; index++) {
if (index != len - 1) {
printf("%f | ", elems[index]);
}
else {
printf("%f\n", elems[index]);
}
}
(*env)->ReleaseFloatArrayElements(env, arr, elems, JNI_COMMIT);
}
JNIEXPORT void JNICALL Java_zfw_JniTest_transferDoubleArray
(JNIEnv *env, jclass jcls, jdoubleArray arr) {
printf("transferDoubleArray():");
jdouble *elems = (*env)->GetDoubleArrayElements(env, arr, NULL);
int len = (*env)->GetArrayLength(env, arr);
int index;
for (index = 0; index < len; index++) {
if (index != len - 1) {
printf("%lf | ", elems[index]);
}
else {
printf("%lf\n", elems[index]);
}
}
(*env)->ReleaseDoubleArrayElements(env, arr, elems, JNI_COMMIT);
}
JNIEXPORT void JNICALL Java_zfw_JniTest_transferStringArray
(JNIEnv *env, jclass jcls, jobjectArray arr) {
printf("transferStringArray():");
int len = (*env)->GetArrayLength(env, arr);
int index;
for (index = 0; index < len; index++) {
jstring str = (*env)->GetObjectArrayElement(env, arr, index);
if (index != len - 1) {
printf("%s | ", (*env)->GetStringUTFChars(env, str, NULL));
}
else {
printf("%s\n", (*env)->GetStringUTFChars(env, str, NULL));
}
}
}
// 2.3 传入基本数据类型二维数组参数系列
JNIEXPORT void JNICALL Java_zfw_JniTest_transfer2DIntArray
(JNIEnv *env, jclass jcls, jobjectArray arr) {
printf("----------2.3 传入基本数据类型二维数组参数系列------------\n");
printf("transfer2DIntArray(): ");
int size = (*env)->GetArrayLength(env, arr);
int i, j;
for (i = 0; i < size; i++) {
jarray elem_arr = (*env)->GetObjectArrayElement(env, arr, i);
int elem_arr_size = (*env)->GetArrayLength(env, elem_arr);
jint *elem = (*env)->GetIntArrayElements(env, elem_arr, JNI_FALSE);
for (j = 0; j < elem_arr_size; j++) {
if (j != elem_arr_size - 1) {
printf("%d , ", elem[j]);
}
else {
printf("%d", elem[j]);
}
}
printf(" | ");
(*env)->ReleaseIntArrayElements(env, elem_arr, elem, 0);
}
printf("\n");
}
JNIEXPORT void JNICALL Java_zfw_JniTest_transfer2DLongArray
(JNIEnv *env, jclass jcls, jobjectArray arr) {
printf("transfer2DLongArray(): ");
int size = (*env)->GetArrayLength(env, arr);
int i, j;
for (i = 0; i < size; i++) {
jarray elem_arr = (*env)->GetObjectArrayElement(env, arr, i);
int elem_arr_size = (*env)->GetArrayLength(env, elem_arr);
jlong *elem = (*env)->GetLongArrayElements(env, elem_arr, JNI_FALSE);
for (j = 0; j < elem_arr_size; j++) {
if (j != elem_arr_size - 1) {
printf("%ld , ", elem[j]);
}
else {
printf("%ld", elem[j]);
}
}
printf(" | ");
(*env)->ReleaseLongArrayElements(env, elem_arr, elem, 0);
}
printf("\n");
}
JNIEXPORT void JNICALL Java_zfw_JniTest_transfer2DFloatArray
(JNIEnv *env, jclass jcls, jobjectArray arr) {
printf("transfer2DFloatArray(): ");
int size = (*env)->GetArrayLength(env, arr);
int i, j;
for (i = 0; i < size; i++) {
jarray elem_arr = (*env)->GetObjectArrayElement(env, arr, i);
int elem_arr_size = (*env)->GetArrayLength(env, elem_arr);
jfloat *elem = (*env)->GetFloatArrayElements(env, elem_arr, JNI_FALSE);
for (j = 0; j < elem_arr_size; j++) {
if (j != elem_arr_size - 1) {
printf("%.1f , ", elem[j]);
}
else {
printf("%.1f", elem[j]);
}
}
printf(" | ");
(*env)->ReleaseFloatArrayElements(env, elem_arr, elem, 0);
}
printf("\n");
}
JNIEXPORT void JNICALL Java_zfw_JniTest_transfer2DDoubleArray
(JNIEnv *env, jclass jcls, jobjectArray arr) {
printf("transfer2DDoubleArray(): ");
int size = (*env)->GetArrayLength(env, arr);
int i, j;
for (i = 0; i < size; i++) {
jarray elem_arr = (*env)->GetObjectArrayElement(env, arr, i);
int elem_arr_size = (*env)->GetArrayLength(env, elem_arr);
jdouble *elem = (*env)->GetDoubleArrayElements(env, elem_arr, JNI_FALSE);
for (j = 0; j < elem_arr_size; j++) {
if (j != elem_arr_size - 1) {
printf("%lf , ", elem[j]);
}
else {
printf("%lf", elem[j]);
}
}
printf(" | ");
(*env)->ReleaseDoubleArrayElements(env, elem_arr, elem, 0);
}
printf("\n");
}
JNIEXPORT void JNICALL Java_zfw_JniTest_transfer2DStringArray
(JNIEnv *env, jclass jcls, jobjectArray arr) {
printf("transfer2DStringArray(): ");
int size = (*env)->GetArrayLength(env, arr);
int i, j;
for (i = 0; i < size; i++) {
jobjectArray elem_arr = (*env)->GetObjectArrayElement(env, arr, i);
int elem_arr_size = (*env)->GetArrayLength(env, elem_arr);
for (j = 0; j < elem_arr_size; j++) {
jstring str = (*env)->GetObjectArrayElement(env, elem_arr, j);
if (j != elem_arr_size - 1) {
printf("%s , ", (*env)->GetStringUTFChars(env, str, NULL));
} else {
printf("%s", (*env)->GetStringUTFChars(env, str, NULL));
}
}
printf(" | ");
}
printf("\n");
}