jni开发

# include "com_*_common_*.h"
# include "log*.h"
//抛出带参数的自定义异常
void throw_exception(JNIEnv * env,int code){
	jclass exception_class = env->FindClass("com/*/*/*");

	jmethodID method_id = env->GetMethodID(exception_class, "<init>", "(I)V");
	jthrowable obj =(jthrowable) env->NewObject(exception_class, method_id, code);
	env->Throw(obj);

}
JNIEXPORT jbyteArray JNICALL Java_com_*_*_*_getTemplate(
		JNIEnv * env, jclass clazz, jint template_id) {

	int ret = 1;
	LOGD("template_id = %d\n", template_id);
	if((ret = get_template(template_id))!= 1)
	{
		LOGE("Error:ret = %d,lineNumber = %d,descriptor: Get Template failed.",ret,__LINE__);
		throw_exception(env, ret);
		return 0;
	}
	LOGD("template_id = %d is captured.", template_id);
	jbyte *by = (jbyte*)p_image;
	jbyteArray jarray = env->NewByteArray(SIZEOF_TEMPLATE);
	env->SetByteArrayRegion(jarray, 0, SIZEOF_TEMPLATE, by);

	return jarray;
}

JNIEXPORT jint JNICALL Java_com_*_*_*_openDeviceNative(
		JNIEnv * env, jclass clazz, jstring file_path) {
	char* save_file_path;
	int ret = com_*_*_*_EXE_SUCCESS;
	save_file_path = (char*) env->GetStringUTFChars(file_path, 0);
	LOGD("file_path = %s", save_file_path);
	ret = device_open(save_file_path);
	LOGD("device_open:ret = %d", ret);
	return ret;
}

JNIEXPORT jint JNICALL Java_com_*_*_*_closeDeviceNative(
		JNIEnv * env, jclass clazz) {
	int ret = com_*_*_*_EXE_SUCCESS;
	LOGD("execute closeDeviceNative.");
	ret = device_close();
	LOGD("device_close:ret = %d",ret);
	return ret;
}

JNIEXPORT jbyteArray JNICALL Java_com_*_*_*_getDeviceInfoNative(
		JNIEnv * env, jclass clazz) {

	int ret = com_*_*_*_EXE_SUCCESS;
	char p_image[1024] = { 0 };
	LOGD("Get device descriptor start.\n");
	if((ret = get_desc(p_image))!=com_*_*_*_EXE_SUCCESS)
	{
		LOGE("Error:ret = %d,lineNumber = %d,descriptor: Get device descriptor failed.",ret,__LINE__);
		throw_exception(env, ret);		
	}

	LOGD("Get device descriptor success.\n");

	jbyte *by = (jbyte*)p_image;
	jbyteArray jarray = env->NewByteArray(1024);
	env->SetByteArrayRegion(jarray, 0, 1024, by);

	return jarray;
}

JNIEXPORT jbyteArray JNICALL Java_com_*_*_*_captureImageNative(
		JNIEnv * env, jclass clazz, jint type, jint time_out) {
	int ret = com_*_*_*_EXE_SUCCESS;
	unsigned char p_image[IMAGE_Y*IMAGE_X];
	int i=0;
	LOGD("%d Get image data,type = %d, time_out = %d",__LINE__,type,time_out);
	if((ret = get_image(type, p_image, time_out))!=com_*_*_*_EXE_SUCCESS)
	{
		LOGE("Error:ret = %d,lineNumber = %d,descriptor: Get image data failed.",ret,__LINE__);
		throw_exception(env, ret);
		
	}
	LOGD("%d:Get image data success,type = %d, time_out = %d  -----",__LINE__,type,time_out);
	jbyte *by = (jbyte*)p_image;
	jbyteArray myjarray = env->NewByteArray(IMAGE_Y*IMAGE_X);
	env->SetByteArrayRegion(myjarray, 0, IMAGE_Y*IMAGE_X, by);
	LOGD("%d:Get image data success,type = %d, time_out = %d  -----",__LINE__,type,time_out);

	return myjarray;
}

JNIEXPORT jobject JNICALL Java_com_*_*_*_enrollFingerNative(
		JNIEnv * env, jclass clazz, jint template_id, jint inc_press,
		jint time_out) {
	int ret = com_*_*_*_EXE_SUCCESS;
	unsigned char p_image[MAX_WIDTH*MAX_HEIGHT] = { 0 };
	unsigned char p_template[SIZEOF_TEMPLATE] = {0};

	LOGD("enroll finger data start.");
	if((ret= enroll_finger(template_id, inc_press, p_template, p_image, time_out))!=com_*_*_*_EXE_SUCCESS ){
		LOGE("Error:ret = %d,lineNumber = %d,descriptor: enroll finger data failed.",ret,__LINE__);
		throw_exception(env, ret);
	}

	LOGD("enroll finger data success.");

	jclass cls = env->FindClass("com/*/*/FingerInfo");
	jmethodID id = env->GetMethodID(cls, "<init>", "()V");
	jobject paramOut = env->NewObjectA(cls, id, 0);
	
	jbyte *by = (jbyte*)p_template;
	jbyteArray jarray = env->NewByteArray(SIZEOF_TEMPLATE);
	env->SetByteArrayRegion(jarray, 0, SIZEOF_TEMPLATE, by);
	jfieldID  template_data = env->GetFieldID(cls, "template", "[B");
	env->SetObjectField(paramOut, template_data, jarray);

    jbyte *image_data_by = (jbyte *)p_image;
	jbyteArray image_data_array = env->NewByteArray(MAX_WIDTH*MAX_HEIGHT);
	env->SetByteArrayRegion(image_data_array, 0, MAX_WIDTH*MAX_HEIGHT , image_data_by);
	jfieldID  image_data = env->GetFieldID(cls, "imageBitmap", "[B");
	env->SetObjectField(paramOut, image_data, image_data_array);

	return paramOut;
}

JNIEXPORT jint JNICALL Java_com_*_*_*_setTemplateNative(
		JNIEnv * env, jclass clazz, jint template_id,
		jbyteArray template_data) {
	jint ret = com_*_*_*_EXE_SUCCESS;
	jbyte p_template[SIZEOF_TEMPLATE] = {0} ;
	env->GetByteArrayRegion(template_data, 0, SIZEOF_TEMPLATE, p_template);	
	LOGD("set template start.");
	ret = set_template(template_id, (U8 *) p_template);
	LOGD("set template end. ret = %d",ret);
	return ret;
}

JNIEXPORT jint JNICALL Java_com_*_*_*_delTemplateNative(
		JNIEnv * env, jclass clazz, jint template_id) {
	jint ret = com_*_*_*_EXE_SUCCESS;
	LOGD("delete template start.");
	ret = del_template(template_id);
	LOGD("delete template end. ret = %d",ret);
	return ret;
}


JNIEXPORT jobject JNICALL Java_com_*_*_*_getTemplateTableNative(
		JNIEnv * env, jclass clazz) {
	int ret = com_*_*_*_EXE_SUCCESS;
	unsigned char template_table[SIZEOF_TEMPLATE * MAX_FP_COUNT]={0};

	LOGD("get template table start.");
	if((ret = get_template_table(template_table)) < 0){
		LOGE("Error:ret = %d,lineNumber = %d,descriptor: get template table data failed.",ret,__LINE__);
		throw_exception(env, ret);
	}
	LOGD("get template table end.");
	jclass cls = env->FindClass("com/*/*/TemplateList");
	jmethodID id = env->GetMethodID(cls, "<init>", "()V");
	jobject paramOut = env->NewObjectA(cls, id, 0);

	jbyte *by = (jbyte *)template_table;
	jbyteArray jarray = env->NewByteArray(SIZEOF_TEMPLATE * MAX_FP_COUNT);
	env->SetByteArrayRegion(jarray, 0, SIZEOF_TEMPLATE * MAX_FP_COUNT , by);
	jfieldID  template_list = env->GetFieldID(cls, "templateList", "[B");
	env->SetObjectField(paramOut, template_list, jarray);

	jfieldID template_count = env->GetFieldID(cls, "templateCount", "I");
	env->SetIntField(paramOut, template_count, ret);
	
	return paramOut;
}


JNIEXPORT jint JNICALL Java_com_*_*_*_searchWithFingerNative(
		JNIEnv * env, jclass clazz, jint time_out) {
	jint ret = com_*_*_*_EXE_SUCCESS;
	LOGD("search with finger start.");
	ret = search_with_finger(time_out);
	LOGD("search with finger end.");
	return ret;
}


JNIEXPORT jint JNICALL Java_com_*_*_*_searchWithTemplateNative(
		JNIEnv * env, jclass clazz, jbyteArray template_data) {
	jbyte p_template[SIZEOF_TEMPLATE] = {0} ;
	jint ret = com_*_*_*_EXE_SUCCESS;
	env->GetByteArrayRegion(template_data, 0, SIZEOF_TEMPLATE, p_template);	
	LOGD("search with template start.");
	ret = search_with_template((unsigned char *)p_template);
	LOGD("search with template end.");
	return ret;
}



 

你可能感兴趣的:(jni开发)