Handle Java bindings

This commit is contained in:
Dmitry Kurtaev 2025-06-03 11:37:31 +03:00
parent f4445bbe4a
commit 30fc9ed9ba
3 changed files with 16 additions and 49 deletions

View File

@ -1002,6 +1002,9 @@ class JavaWrapperGenerator(object):
ret = "return (jlong) _retval_;" ret = "return (jlong) _retval_;"
elif type_dict[fi.ctype]["jni_type"] == "jdoubleArray": elif type_dict[fi.ctype]["jni_type"] == "jdoubleArray":
ret = "return _da_retval_;" ret = "return _da_retval_;"
elif "jni_var" in type_dict[ret_type]:
c_epilogue.append(type_dict[ret_type]["jni_var"] % {"n" : '_retval_'})
ret = f"return {type_dict[ret_type]['jni_name'] % {'n' : '_retval_'}};"
# hack: replacing func call with property set/get # hack: replacing func call with property set/get
name = fi.name name = fi.name

View File

@ -78,10 +78,6 @@ public:
CV_WRAP_AS(detectAndDecodeBytes) NativeByteArray detectAndDecode(InputArray img, OutputArray points = noArray(), CV_WRAP_AS(detectAndDecodeBytes) NativeByteArray detectAndDecode(InputArray img, OutputArray points = noArray(),
OutputArray straight_code = noArray()) const; OutputArray straight_code = noArray()) const;
CV_WRAP_AS(decodeBytes) NativeByteArray decode(InputArray img, InputArray points, OutputArray straight_code = noArray()) const; CV_WRAP_AS(decodeBytes) NativeByteArray decode(InputArray img, InputArray points, OutputArray straight_code = noArray()) const;
// CV_WRAP_AS(decodeBytesMulti) bool decodeMulti(InputArray img, InputArray points, CV_OUT std::vector<NativeByteArray>& decoded_info,
// OutputArrayOfArrays straight_code = noArray()) const;
// CV_WRAP_AS(detectAndDecodeBytesMulti) bool detectAndDecodeMulti(InputArray img, CV_OUT std::vector<NativeByteArray>& decoded_info, OutputArray points = noArray(),
// OutputArrayOfArrays straight_code = noArray()) const;
#endif #endif
struct Impl; struct Impl;

View File

@ -4,9 +4,9 @@
"QRCodeEncoder" : { "QRCodeEncoder" : {
"j_code" : [ "j_code" : [
"\n", "\n",
"/**", "/** Generates QR code from input string.",
" * Constructor of streaming callback object with abstract 'read' and 'seek' methods that should be implemented in Java code.<br>", "@param encoded_info Input bytes to encode.",
" * <b>NOTE</b>: Implemented callbacks should be called from the creation thread to avoid JNI performance degradation", "@param qrcode Generated QR code.",
"*/", "*/",
"public void encode(byte[] encoded_info, Mat qrcode) {", "public void encode(byte[] encoded_info, Mat qrcode) {",
" encode_1(nativeObj, encoded_info, qrcode.nativeObj);", " encode_1(nativeObj, encoded_info, qrcode.nativeObj);",
@ -45,48 +45,16 @@
"\n" "\n"
] ]
} }
}, }
"GraphicalCodeDetector" : { },
"GraphicalCodeDetector" : { "type_dict": {
"j_code" : [ "NativeByteArray": {
"\n", "j_type" : "byte[]",
"public byte[] detectAndDecodeBytes(Mat img) {", "jn_type": "byte[]",
" return detectAndDecodeBytes_0(nativeObj, img.nativeObj);", "jni_type": "jbyteArray",
"}", "jni_name": "n_%(n)s",
"\n" "jni_var": "jbyteArray n_%(n)s = env->NewByteArray(static_cast<jsize>(%(n)s.size())); env->SetByteArrayRegion(n_%(n)s, 0, %(n)s.size(), reinterpret_cast<jbyte*>(&%(n)s[0]));",
], "cast_from": "std::string"
"jn_code": [
"\n",
"private static native byte[] detectAndDecodeBytes_0(long nativeObj, long img_nativeObj);",
"\n"
],
"cpp_code": [
"JNIEXPORT jbyteArray JNICALL Java_org_opencv_objdetect_GraphicalCodeDetector_detectAndDecodeBytes_10 (JNIEnv*, jclass, jlong, jlong);",
"\n",
"JNIEXPORT jbyteArray JNICALL Java_org_opencv_objdetect_GraphicalCodeDetector_detectAndDecodeBytes_10",
" (JNIEnv* env, jclass , jlong self, jlong img_nativeObj)",
"{",
" ",
" static const char method_name[] = \"objdetect::detectAndDecodeBytes_10()\";",
" try {",
" LOGD(\"%s\", method_name);",
" cv::GraphicalCodeDetector* me = (cv::GraphicalCodeDetector*) self; //TODO: check for NULL",
" Mat& img = *((Mat*)img_nativeObj);",
" std::string result = me->detectAndDecode( img );",
" jsize sz = result.size();",
" jbyteArray _retval_ = env->NewByteArray(static_cast<jsize>(sz));",
" env->SetByteArrayRegion(_retval_, 0, sz, reinterpret_cast<jbyte*>(&result[0]));",
" return _retval_;",
" } catch(const std::exception &e) {",
" throwJavaException(env, &e, method_name);",
" } catch (...) {",
" throwJavaException(env, 0, method_name);",
" }",
" return 0;",
"}",
"\n"
]
}
} }
} }
} }