123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- //
- // Created by 阳坤 on 2022/2/23.
- //
- #include "ImageBlurDetection.h"
- #include <sys/stat.h>
- #include <signal.h>
- #include <execinfo.h>
- const int MAX_STACK_FRAMES = 128;
- void sig_crash(int sig) {
- printf("crash---->sig=%d \n", sig);
- FILE *fd;
- struct stat buf;
- const char *crash_path = "./video_similarity_comparison_crash.txt";
- stat(crash_path, &buf);
- if (buf.st_size > 10 * 1000 * 1000) { // 超过10兆则清空内容
- fd = fopen(crash_path, "w");
- } else {
- fd = fopen(crash_path, "at");
- }
- if (NULL == fd) {
- exit(0);
- }
- try {
- char szLine[512] = {0,};
- time_t t = time(NULL);
- tm *now = localtime(&t);
- int nLen1 = sprintf(szLine,
- "#########################################################\n[%04d-%02d-%02d %02d:%02d:%02d][crash signal number:%d]\n",
- now->tm_year + 1900,
- now->tm_mon + 1,
- now->tm_mday,
- now->tm_hour,
- now->tm_min,
- now->tm_sec,
- sig);
- printf("crash---->%s \n", szLine);
- fwrite(szLine, 1, strlen(szLine), fd);
- #ifdef __linux
- void* array[MAX_STACK_FRAMES];
- size_t size = 0;
- char** strings = NULL;
- size_t i, j;
- signal(sig, SIG_DFL);
- size = backtrace(array, MAX_STACK_FRAMES);
- strings = (char**)backtrace_symbols(array, size);
- //fprintf(stderr, "oncrash;\n");
- for (i = 0; i < size; ++i)
- {
- char szLine[512] = {0, };
- sprintf(szLine, "%d %s\n", i, strings[i]);
- fwrite(szLine, 1, strlen(szLine), fd);
- std::string symbol(strings[i]);
- size_t pos1 = symbol.find_first_of("[");
- size_t pos2 = symbol.find_last_of("]");
- std::string address = symbol.substr(pos1 + 1, pos2 - pos1 -1);
- char cmd[128] = {0, };
- sprintf(cmd, "addr2line -e /home/libpiaoquan_java_opencv.so %s", address.c_str()); // test为应用程序名称,需要改为用户自己的应用程序名
- FILE *fPipe = popen(cmd, "r");
- if(fPipe != NULL){
- char buff[1024];
- memset(buff, 0, sizeof(buff));
- char* ret = fgets(buff, sizeof(buff), fPipe);
- pclose(fPipe);
- fwrite(ret, 1, strlen(ret), fd);
- }
- }
- free(strings);
- #endif // __linux
- } catch (...) {
- //
- }
- printf("sig_crash fflush 1\n");
- fflush(fd);
- printf("sig_crash fclose 2\n");
- fclose(fd);
- fd = NULL;
- printf("sig_crash exit 3\n");
- exit(0);
- }
- static jint Java_JNI_OpenCV_ImagePathBlurDetection(JNIEnv *env, jobject obj, jstring filepath) {
- const char *image_path = env->GetStringUTFChars(filepath, 0);
- int ret = image_blur_detection(image_path);
- env->ReleaseStringUTFChars(filepath, image_path);
- return ret;
- }
- static jstring Java_JNI_OpenCV_GetVideoSimilarityLists(JNIEnv *env, jobject obj, jstring filepath) {
- LOGE("Java_JNI_OpenCV_GetVideoSimilarityLists\n ");
- const char *image_path = env->GetStringUTFChars(filepath, 0);
- const char *ret_json = get_video_similarity_list(image_path);
- env->ReleaseStringUTFChars(filepath, image_path);
- LOGE("Java_JNI_OpenCV_GetVideoSimilarityLists\n");
- if (ret_json) {
- LOGE("Java_JNI_OpenCV_GetVideoSimilarityLists NewStringUTF \n");
- jstring str = env->NewStringUTF(ret_json);
- LOGE("Java_JNI_OpenCV_GetVideoSimilarityLists NewStringUTF 1\n");
- free((void *) ret_json);
- LOGE("Java_JNI_OpenCV_GetVideoSimilarityLists NewStringUTF out\n");
- ret_json = NULL;
- return str;
- }
- return NULL;
- }
- static jint
- Java_JNI_OpenCV_ImageBytesBlurDetection(JNIEnv *env, jobject obj, jbyteArray data) {
- jbyte *image_bytes = env->GetByteArrayElements(data, 0);
- int ret = bytes_blur_detection((uint8_t *) image_bytes, env->GetArrayLength(data));
- env->ReleaseByteArrayElements(data, image_bytes, 0);
- return ret;
- }
- static const JNINativeMethod configMethods[] = {
- {"blurDetectionFromImagePath", "(Ljava/lang/String;)I", (void **) Java_JNI_OpenCV_ImagePathBlurDetection},
- {"blurDetectionFromImageBytes", "([B)I", (void **) Java_JNI_OpenCV_ImageBytesBlurDetection},
- {"getVideoSimilarityLists", "(Ljava/lang/String;)Ljava/lang/String;", (void **) Java_JNI_OpenCV_GetVideoSimilarityLists},
- };
- /**
- * System.load(xxx.so) ִ
- * @param vm
- * @param pVoid
- * @return
- */
- int JNI_OnLoad(JavaVM *vm, void *pVoid) {
- JNIEnv *jniEnv;
- if (vm->GetEnv(reinterpret_cast<void **>(&jniEnv), JNI_VERSION_1_6) != JNI_OK) {
- printf(" JNI_OnLoad 1 error.\n");
- return JNI_ERR;
- }
- jclass jcls = jniEnv->FindClass(JAVA_JNI_CLASS_PATH);
- if (jniEnv->RegisterNatives(jcls, configMethods, NELEM(configMethods)) != JNI_OK) {
- printf(" JNI_OnLoad error.\n");
- return JNI_ERR;
- }
- // signal(SIGABRT, sig_crash);
- // signal(SIGSEGV, sig_crash);
- printf(" JNI_OnLoad ok.\n");
- return JNI_VERSION_1_6;
- };
|