Last active
July 10, 2020 12:57
-
-
Save AlphaNext/6dcb652aa0c66f39960949034bd845bc to your computer and use it in GitHub Desktop.
Android_JNI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct PassResult{ | |
| int errorID; | |
| string errorMsg; | |
| float* pointArray; | |
| int width; | |
| int height; | |
| int dataLength; | |
| // imageArray could be seemed as a opencv cv::Mat | |
| unsigned char* imageArray; | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // PassResult: doc_out is the result which will be passed to java part | |
| // https://stackoverflow.com/questions/8541568/c-and-jni-how-to-pass-an-array-into-a-jfloatarray | |
| // http://stackz.ru/en/1610045/how-to-return-an-array-from-jni-to-java | |
| // 转移存储结果 | |
| jobject docResult_array = NULL; | |
| jclass objectClass = env->FindClass("com/image/demo/PassResult"); | |
| jmethodID construction_id = env->GetMethodID(objectClass, "<init>", "()V"); | |
| jfieldID field_PointBoxs = env->GetFieldID(objectClass, "pointArray", "[F"); | |
| jfieldID field_errorMsg = env->GetFieldID(objectClass, "errorMsg", "Ljava/lang/String;"); | |
| jfieldID field_errorID = env->GetFieldID(objectClass, "errorID", "I"); | |
| jfieldID field_width = env->GetFieldID(objectClass, "width", "I"); | |
| jfieldID field_height = env->GetFieldID(objectClass, "width", "I"); | |
| jfieldID field_length = env->GetFieldID(objectClass, "dataLength", "I"); | |
| jfieldID field_image = env->GetFieldID(objectClass, "imageArray", "[B"); | |
| docResult_array = env->NewObject(objectClass, construction_id); | |
| // points part | |
| jfloatArray pointsArray = env->NewFloatArray(8); | |
| env->SetFloatArrayRegion(pointsArray, 0, 8, (const jfloat*) doc_out.pointArray); | |
| env->SetObjectField(docResult_array, field_PointBoxs, pointsArray); | |
| env->DeleteLocalRef(pointsArray); | |
| env->SetIntField(docResult_array, field_errorID, doc_out.errorID); | |
| // https://stackoverflow.com/questions/11621449/send-c-string-to-java-via-jni/24564937 | |
| // https://juejin.im/post/5afd694ef265da0b96727210 | |
| // int byteCount = doc_out.errorMsg.length(); | |
| // const jbyte* pNativeMsg = reinterpret_cast<const jbyte*>(doc_out.errorMsg.c_str()); | |
| // jbyteArray Msgbytes = env->NewByteArray(byteCount); | |
| // env->SetByteArrayRegion(Msgbytes, 0, byteCount, pNativeMsg); | |
| jstring Msg = env->NewStringUTF(doc_out.errorMsg.c_str()); | |
| env->SetObjectField(docResult_array, field_errorMsg, Msg); | |
| env->DeleteLocalRef(Msg); | |
| // return image part | |
| env->SetIntField(docResult_array, field_height, doc_out.height); | |
| env->SetIntField(docResult_array, field_width, doc_out.width); | |
| env->SetIntField(docResult_array, field_length, doc_out.dataLength); | |
| jbyteArray imageArray = env->NewByteArray(doc_out.dataLength); | |
| env->SetByteArrayRegion(imageArray, 0, doc_out.dataLength, (jbyte*)doc_out.pointArray); | |
| env->SetObjectField(docResult_array, field_image, imageArray); | |
| env->DeleteLocalRef(imageArray); | |
| free(buf); | |
| return docResult_array; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| pass cpp struct result to android part using android JNI |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.image.demo; | |
| public class PassResult{ | |
| int errorID; | |
| String errorMsg; | |
| float[] pointArray; | |
| int width; | |
| int height; | |
| int dataLength; | |
| byte[] imageArray; | |
| DetectResult(){} | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment