Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

在图片不正确的情况下jni库崩溃 #24

Open
hiroyukki opened this issue Aug 26, 2024 · 0 comments
Open

在图片不正确的情况下jni库崩溃 #24

hiroyukki opened this issue Aug 26, 2024 · 0 comments

Comments

@hiroyukki
Copy link

hiroyukki commented Aug 26, 2024

jni导出未 catch 异常,导致了如果传入一个非正确的图片(如 docx 改名为 png),jvm 崩溃,因为在 java 中无法捕获 c++ exception
可在 jni 接口中捕获异常,异常情况视为未检测到文字:
`

extern "C" JNIEXPORT jobject JNICALL
Java_com_benjaminwan_ocrlibrary_OcrEngine_detect(JNIEnv *env, jobject thiz, jstring input, jint padding,
jint maxSideLen,
jfloat boxScoreThresh, jfloat boxThresh, jfloat unClipRatio,
jboolean doAngle, jboolean mostAngle
) {

std::string imgPath = jstringToChar(env, input);
bool hasTargetImgFile = isFileExists(imgPath);
if (!hasTargetImgFile) {
    fprintf(stderr, "Target image not found: %s\n", imgPath.c_str());
    OcrResult result{};
    return OcrResultUtils(env, result).getJObject();
}
std::string imgDir = imgPath.substr(0, imgPath.find_last_of('/') + 1);
std::string imgName = imgPath.substr(imgPath.find_last_of('/') + 1);
printf("imgDir=%s, imgName=%s\n", imgDir.c_str(), imgName.c_str());

try {
	OcrResult result = ocrLite->detect(imgDir.c_str(), imgName.c_str(), padding, maxSideLen,
		boxScoreThresh, boxThresh, unClipRatio, doAngle, mostAngle);
	return OcrResultUtils(env, result).getJObject();
} catch (...) {
	OcrResult result{};
	return OcrResultUtils(env, result).getJObject();
}

}

`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant