java的基本类型可以强转。例如jint 和 int (c++中的int ) 。强转包含:基本类型的变量,或者指向基本类型变量的指针
ndk可以用代码1调用 , 但例子中是用代码2。代码2可能是c++中的标准语法,代码1是ndk的。引用时要注意
代码1:env->GetStringUTFChars(str)
代码2:(*env)->NewStringUTF(env, str);
#include
#include "com_test_Test.h"
#include
#include
#include
//获取字符串
JNIEXPORT void JNICALL Java_com_test_Test_sayHello(JNIEnv * env, jobject obj,
jstring s) {
char * str = (char *) (*env)->GetStringUTFChars(env, s, 0);
printf("\n c-string: hello - %s", str);
}
//获取,返回int
JNIEXPORT jint JNICALL Java_com_test_Test_add(JNIEnv * env, jobject obj,
jint a, jint b) {
int i = a;
int j = b;
printf("\n c-int: %d - %d", i, j);
char str[256];
sprintf(str, "%d", i + j);
return (jint) i + j;
}
//获取,返回float
JNIEXPORT jfloat JNICALL Java_com_test_Test_getFloat(JNIEnv * env, jobject obj,
jfloat f) {
float fl = f;
printf("\n c-float: %3f", fl);
fl = 200.555;
return (jfloat) fl;
}
//获取,返回 double
JNIEXPORT jdouble JNICALL Java_com_test_Test_getDouble(JNIEnv * env,
jobject obj, jdouble dou) {
double d = dou;
printf("\n c-double: %3f", d);
d = 800.88;
return (jdouble) d;
}
//获取,返回boolean
JNIEXPORT jboolean JNICALL Java_com_test_Test_getBoolean(JNIEnv * env,
jobject obj, jboolean bool) {
unsigned char b = bool;
printf("\n c-boolean: %lu ", b);
if (b) {
printf("true");
} else {
printf("false");
}
b = 1;
return (jboolean) b;
}
//获取,返回 string
JNIEXPORT jstring JNICALL Java_com_test_Test_getString(JNIEnv * env,
jobject obj, jstring s) {
char * st = (char *) (*env)->GetStringUTFChars(env, s, 0);
printf("\n c-string: %s", st);
char * str = "hello wang wu!";
jstring rtn;
rtn = (*env)->NewStringUTF(env, str);
return rtn;
}
//获取,返回 string []
JNIEXPORT jobjectArray JNICALL Java_com_test_Test_getStringArray(JNIEnv * env,
jobject obj, jobjectArray arr) {
int len = (*env)->GetArrayLength(env, arr);
printf("\n c-stringArray :");
int i = 0;
for (i = 0; i < len; i++) {
jobject obj = (*env)->GetObjectArrayElement(env, arr, i);
jstring str = (jstring) obj;
const char * szStr = (*env)->GetStringUTFChars(env, str, 0);
printf(" %d-%s ", i, szStr);
(*env)->ReleaseStringChars(env, str, szStr);
//env->ReleaseStringUTFChars(str, szStr); 应该是这句
}
//-----返回----
jstring str;
jobjectArray args = 0;
jsize size = 5;
char* sa[] = { "Hello,", "world!", "zhang", "san", "yuang" };
int j = 0;
jclass objClass = (*env)->FindClass(env, "java/lang/String");
args = (*env)->NewObjectArray(env, size, objClass, 0);
for (j = 0; j < size; j++) {
str = (*env)->NewStringUTF(env, sa[j]);
(*env)->SetObjectArrayElement(env, args, j, str);
}
return args;
}
package com.test;
public class Test {
private native void sayHello(String str);
private native int add(int a, int b);
private native float getFloat(float f);
private native double getDouble(double d);
private native boolean getBoolean(boolean b);
private native String getString(String str);
private native String[] getStringArray(String[] sa);
static {
System.loadLibrary("Test");
}
public static void main(String[] args) {
Test test = new Test();
test.sayHello("zhang san");
System.out.println("int --> " + test.add(10, 20));
System.out.println("float --> " + test.getFloat((float) 20.123));
System.out.println("double --> " + test.getDouble(100.369));
System.out.println("boolean --> " + test.getBoolean(true));
System.out.println("string --> " + test.getString("wang Wu"));
String[] ss = { "hello", "-", "zhang", "san" };
Object[] obj = test.getStringArray(ss);
System.out.print("string[]--> ");
for (Object object : obj) {
System.out.print(object + " ");
}
}
我用的是jni动态注册,所以方法有点不一样。
注意变量的动态释放,现在自己还有点拿不准
jstring tan_string_From_JNI(JNIEnv *env, jobject obj,jstring jstr,jint age) {
/*, jint a, jint b
int rtn = (int)(a + b);
return (jint)rtn;*/
int temp_age=(int)age;
std::string age_str;
int2str((temp_age+1),age_str);
const char *my_name = env->GetStringUTFChars(jstr, NULL);
std::string str_name(my_name);
str_name=str_name+"123,age="+age_str;
jstring result_str=env->NewStringUTF(str_name.c_str());
jclass cmake_cls = env->GetObjectClass(obj);
if(cmake_cls != NULL)
{
jmethodID mid = env->GetMethodID(cmake_cls, "showResult", "(Ljava/lang/String;)V");
env->CallVoidMethod(obj , mid , result_str);
}
return result_str;
}
jbyteArray tan_test_byte(JNIEnv *env, jobject obj, jbyteArray byarray) {
jsize len = env->GetArrayLength(byarray);
jbyte *jbarray = (jbyte *)malloc(len * sizeof(jbyte));
env->GetByteArrayRegion(byarray,0,len,jbarray);
BYTE *dData=(BYTE*)jbarray;
BYTE aByte[4]={0x10,0x01};
aByte[2]=dData[0];
aByte[3]=dData[1];
jbyte *jy=(jbyte*)aByte;
free(jbarray);
jbarray=NULL;
jbyteArray resultArray = env->NewByteArray(4);//建立jbarray数组
env->SetByteArrayRegion(resultArray, 0, 4, jy);
return resultArray;
}
jobjectArray get_string_array(JNIEnv * env, jobject obj, jobjectArray arr) {
int len = env->GetArrayLength(arr);
printf("\n c-stringArray :");
int i = 0;
for (i = 0; i < len; i++) {
jobject obj = env->GetObjectArrayElement(arr, i);
jstring str = (jstring) obj;
const char * szStr = env->GetStringUTFChars( str, 0);
printf(" %d-%s ", i, szStr);
env->ReleaseStringUTFChars(str, szStr);
}
//-----返回----
jstring str;
jobjectArray args = 0;
jsize size = 5;
char* sa[] = { "Hello,", "world!", "zhang", "san", "yuang" };
int j = 0;
jclass objClass = env->FindClass("java/lang/String");
args = env->NewObjectArray(size, objClass, 0);
for (j = 0; j < size; j++) {
str = env->NewStringUTF(sa[j]);
env->SetObjectArrayElement(args, j, str);
}
return args;
}