Untitled
unknown
c_cpp
a year ago
4.8 kB
2
Indexable
Never
// Write C++ code here. // // Do not forget to dynamically load the C++ library into your application. // // For instance, // // In MainActivity.java: // static { // System.loadLibrary("finwhale"); // } // // Or, in MainActivity.kt: // companion object { // init { // System.loadLibrary("finwhale") // } // } #include <jni.h> #include "finwhale.h" #include <opencv2/imgproc.hpp> struct ResultRect { cv::Point2f p1; cv::Point2f p2; cv::Point2f p3; cv::Point2f p4; float width; float height; float angle; }; struct MaskSettingsCpp { int colorSpace; cv::Scalar lowerBound; cv::Scalar upperBound; }; cv::Scalar convertJavaScalarToCpp(JNIEnv *env, jobject scalarObj); MaskSettingsCpp convertJavaMaskSettingsToCpp(JNIEnv *env, jobject maskSettingsObj) { MaskSettingsCpp maskSettingsCpp; jclass maskSettingsClass = env->FindClass("ru/tekhpromtorg/finwhale/ui/scan/ProtectiveStickerContourUtil$MaskSettings"); jfieldID colorSpaceField = env->GetFieldID(maskSettingsClass, "colorSpace", "I"); maskSettingsCpp.colorSpace = env->GetIntField(maskSettingsObj, colorSpaceField); jfieldID lowerBoundField = env->GetFieldID(maskSettingsClass, "lowerBound", "Lorg/opencv/core/Scalar;"); jfieldID upperBoundField = env->GetFieldID(maskSettingsClass, "upperBound", "Lorg/opencv/core/Scalar;"); jobject lowerBoundObj = env->GetObjectField(maskSettingsObj, lowerBoundField); jobject upperBoundObj = env->GetObjectField(maskSettingsObj, upperBoundField); maskSettingsCpp.lowerBound = convertJavaScalarToCpp(env, lowerBoundObj); maskSettingsCpp.upperBound = convertJavaScalarToCpp(env, upperBoundObj); return maskSettingsCpp; } cv::Scalar convertJavaScalarToCpp(JNIEnv *env, jobject scalarObj) { jclass scalarClass = env->FindClass("org/opencv/core/Scalar"); jfieldID valField = env->GetFieldID(scalarClass, "val", "[D"); jdoubleArray valArray = (jdoubleArray) env->GetObjectField(scalarObj, valField); jdouble *vals = env->GetDoubleArrayElements(valArray, 0); cv::Scalar s(vals[0], vals[1], vals[2], vals[3]); env->ReleaseDoubleArrayElements(valArray, vals, 0); return s; } jobject convertCppPointToJava(JNIEnv *env, const cv::Point2f &point) { jclass pointClass = env->FindClass("org/opencv/core/Point"); jmethodID constructor = env->GetMethodID(pointClass, "<init>", "(DD)V"); return env->NewObject(pointClass, constructor, (double)point.x, (double)point.y); } jobject convertCppResultRectToJava(JNIEnv *env, const ResultRect &rect) { jclass resultRectClass = env->FindClass("ru/tekhpromtorg/finwhale/ui/scan/ProtectiveStickerContourUtil$ResultRect"); jmethodID constructor = env->GetMethodID(resultRectClass, "<init>", "(Lorg/opencv/core/Point;Lorg/opencv/core/Point;Lorg/opencv/core/Point;Lorg/opencv/core/Point;FFF)V"); jobject p1Obj = convertCppPointToJava(env, rect.p1); jobject p2Obj = convertCppPointToJava(env, rect.p2); jobject p3Obj = convertCppPointToJava(env, rect.p3); jobject p4Obj = convertCppPointToJava(env, rect.p4); return env->NewObject(resultRectClass, constructor, p1Obj, p2Obj, p3Obj, p4Obj, rect.width, rect.height, rect.angle); } cv::Mat* getNativeMat(JNIEnv *env, jobject matObj) { jclass matClass = env->FindClass("org/opencv/core/Mat"); jfieldID nativeObjField = env->GetFieldID(matClass, "nativeObj", "J"); jlong matAddr = env->GetLongField(matObj, nativeObjField); return reinterpret_cast<cv::Mat*>(matAddr); } void myBlur(cv::Mat src, float sigma) { GaussianBlur(src, src, cv::Size(), sigma); } #include <android/log.h> extern "C" JNIEXPORT jobject JNICALL Java_ru_tekhpromtorg_finwhale_ui_scan_ProtectiveStickerContourUtil_processMat(JNIEnv *env, jobject thiz, jobject mat, jobject maskSettingsObj) { MaskSettingsCpp maskSettingsCpp = convertJavaMaskSettingsToCpp(env, maskSettingsObj); cv::Mat* cppMat = getNativeMat(env, mat); __android_log_print(ANDROID_LOG_DEBUG, "FINWHALE", "processMat %f", maskSettingsCpp.lowerBound[0]); // myBlur(*cppMat, 5.0f); ResultRect cppRect; cppRect.p1 = cv::Point2f(100, 100); cppRect.p2 = cv::Point2f(200, 100); cppRect.p3 = cv::Point2f(200, 200); cppRect.p4 = cv::Point2f(100, 200); cppRect.width = 100; cppRect.height = 100; cppRect.angle = 0; jobject javaRect = convertCppResultRectToJava(env, cppRect); return javaRect; }