ImageBlurDetection.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // Created by 阳坤 on 2022/2/23.
  3. //
  4. #include "ImageBlurDetection.h"
  5. #include <sys/stat.h>
  6. #include <signal.h>
  7. #include <execinfo.h>
  8. const int MAX_STACK_FRAMES = 128;
  9. void sig_crash(int sig) {
  10. printf("crash---->sig=%d \n", sig);
  11. FILE *fd;
  12. struct stat buf;
  13. const char *crash_path = "./video_similarity_comparison_crash.txt";
  14. stat(crash_path, &buf);
  15. if (buf.st_size > 10 * 1000 * 1000) { // 超过10兆则清空内容
  16. fd = fopen(crash_path, "w");
  17. } else {
  18. fd = fopen(crash_path, "at");
  19. }
  20. if (NULL == fd) {
  21. exit(0);
  22. }
  23. try {
  24. char szLine[512] = {0,};
  25. time_t t = time(NULL);
  26. tm *now = localtime(&t);
  27. int nLen1 = sprintf(szLine,
  28. "#########################################################\n[%04d-%02d-%02d %02d:%02d:%02d][crash signal number:%d]\n",
  29. now->tm_year + 1900,
  30. now->tm_mon + 1,
  31. now->tm_mday,
  32. now->tm_hour,
  33. now->tm_min,
  34. now->tm_sec,
  35. sig);
  36. printf("crash---->%s \n", szLine);
  37. fwrite(szLine, 1, strlen(szLine), fd);
  38. #ifdef __linux
  39. void* array[MAX_STACK_FRAMES];
  40. size_t size = 0;
  41. char** strings = NULL;
  42. size_t i, j;
  43. signal(sig, SIG_DFL);
  44. size = backtrace(array, MAX_STACK_FRAMES);
  45. strings = (char**)backtrace_symbols(array, size);
  46. //fprintf(stderr, "oncrash;\n");
  47. for (i = 0; i < size; ++i)
  48. {
  49. char szLine[512] = {0, };
  50. sprintf(szLine, "%d %s\n", i, strings[i]);
  51. fwrite(szLine, 1, strlen(szLine), fd);
  52. std::string symbol(strings[i]);
  53. size_t pos1 = symbol.find_first_of("[");
  54. size_t pos2 = symbol.find_last_of("]");
  55. std::string address = symbol.substr(pos1 + 1, pos2 - pos1 -1);
  56. char cmd[128] = {0, };
  57. sprintf(cmd, "addr2line -e /home/libpiaoquan_java_opencv.so %s", address.c_str()); // test为应用程序名称,需要改为用户自己的应用程序名
  58. FILE *fPipe = popen(cmd, "r");
  59. if(fPipe != NULL){
  60. char buff[1024];
  61. memset(buff, 0, sizeof(buff));
  62. char* ret = fgets(buff, sizeof(buff), fPipe);
  63. pclose(fPipe);
  64. fwrite(ret, 1, strlen(ret), fd);
  65. }
  66. }
  67. free(strings);
  68. #endif // __linux
  69. } catch (...) {
  70. //
  71. }
  72. printf("sig_crash fflush \n");
  73. fflush(fd);
  74. printf("sig_crash fclose \n");
  75. fclose(fd);
  76. fd = NULL;
  77. printf("sig_crash exit \n");
  78. exit(0);
  79. }
  80. static jint Java_JNI_OpenCV_ImagePathBlurDetection(JNIEnv *env, jobject obj, jstring filepath) {
  81. const char *image_path = env->GetStringUTFChars(filepath, 0);
  82. int ret = image_blur_detection(image_path);
  83. env->ReleaseStringUTFChars(filepath, image_path);
  84. return ret;
  85. }
  86. static jstring Java_JNI_OpenCV_GetVideoSimilarityLists(JNIEnv *env, jobject obj, jstring filepath) {
  87. LOGE("Java_JNI_OpenCV_GetVideoSimilarityLists\n ");
  88. const char *image_path = env->GetStringUTFChars(filepath, 0);
  89. const char *ret_json = get_video_similarity_list(image_path);
  90. env->ReleaseStringUTFChars(filepath, image_path);
  91. LOGE("Java_JNI_OpenCV_GetVideoSimilarityLists\n");
  92. if (ret_json) {
  93. LOGE("Java_JNI_OpenCV_GetVideoSimilarityLists NewStringUTF \n");
  94. jstring str = env->NewStringUTF(ret_json);
  95. LOGE("Java_JNI_OpenCV_GetVideoSimilarityLists NewStringUTF 1\n");
  96. free((void *) ret_json);
  97. LOGE("Java_JNI_OpenCV_GetVideoSimilarityLists NewStringUTF out\n");
  98. ret_json = NULL;
  99. return str;
  100. }
  101. return NULL;
  102. }
  103. static jint
  104. Java_JNI_OpenCV_ImageBytesBlurDetection(JNIEnv *env, jobject obj, jbyteArray data) {
  105. jbyte *image_bytes = env->GetByteArrayElements(data, 0);
  106. int ret = bytes_blur_detection((uint8_t *) image_bytes, env->GetArrayLength(data));
  107. env->ReleaseByteArrayElements(data, image_bytes, 0);
  108. return ret;
  109. }
  110. static const JNINativeMethod configMethods[] = {
  111. {"blurDetectionFromImagePath", "(Ljava/lang/String;)I", (void **) Java_JNI_OpenCV_ImagePathBlurDetection},
  112. {"blurDetectionFromImageBytes", "([B)I", (void **) Java_JNI_OpenCV_ImageBytesBlurDetection},
  113. {"getVideoSimilarityLists", "(Ljava/lang/String;)Ljava/lang/String;", (void **) Java_JNI_OpenCV_GetVideoSimilarityLists},
  114. };
  115. /**
  116. * System.load(xxx.so) ִ
  117. * @param vm
  118. * @param pVoid
  119. * @return
  120. */
  121. int JNI_OnLoad(JavaVM *vm, void *pVoid) {
  122. JNIEnv *jniEnv;
  123. if (vm->GetEnv(reinterpret_cast<void **>(&jniEnv), JNI_VERSION_1_6) != JNI_OK) {
  124. printf(" JNI_OnLoad 1 error.\n");
  125. return JNI_ERR;
  126. }
  127. jclass jcls = jniEnv->FindClass(JAVA_JNI_CLASS_PATH);
  128. if (jniEnv->RegisterNatives(jcls, configMethods, NELEM(configMethods)) != JNI_OK) {
  129. printf(" JNI_OnLoad error.\n");
  130. return JNI_ERR;
  131. }
  132. signal(SIGABRT, sig_crash);
  133. signal(SIGSEGV, sig_crash);
  134. printf(" JNI_OnLoad ok.\n");
  135. return JNI_VERSION_1_6;
  136. };