0

I have a problem in my native code because in my c ++ code I can not save the images I receive from the JAVA part.

I tried using cv::imwrite but the image is never saved in the path and in the corresponding folder.

My JNI code is as follows:

extern "C"{
    JNIEXPORT jintArray JNICALL Java_com_example_testNativeCode_DisplayImage_FindFeatures(JNIEnv* env, jclass obj, jintArray bufImg, jint w, jint h){

    jint *cbuf;
    cbuf = env->GetIntArrayElements(bufImg, 0);

    if(cbuf==NULL){
       return 0;
    }

    cv::Mat mImg(h,w,CV_8UC4, (unsigned char*)cbuf);

    cv::imwrite("../SaveImage/img.png", mImg);
    }
}

I use android studio 2.3.3 and OpenCv 3.1.0. I put in the manifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

I want to know if it was possible to record an image with cv::imwrite? How else can I do to save it?

Fruchtzwerg
  • 10,999
  • 12
  • 40
  • 49
  • 1
    Give absolute path to `imwrite` and see if it works. – zindarod Sep 22 '17 at 12:06
  • @Zindarod I tried to set the absolute path but the problem is the same. Furthermore in Logcat there is no error... –  Sep 22 '17 at 12:11
  • Check the return value of `imwrite`. What is it? – zindarod Sep 22 '17 at 12:13
  • imwrite return false ...so it's normal for the image not to be saved. But I do not understand why she does not have it. –  Sep 22 '17 at 12:20
  • Are you writing to SD card while connected to PC? – zindarod Sep 22 '17 at 12:25
  • yes in my Java activity I save my bitmap image on my SD card and it works. –  Sep 22 '17 at 12:29
  • Check this answer for mount types: https://stackoverflow.com/questions/15291504/android-opencv-imwrite-always-returns-false-and-fails-to-write#17044206. Also, are you sure your `Mat` object is created correctly? – zindarod Sep 22 '17 at 12:32
  • ok thanks, I will look at the link. Normally the matrix is ​​created correctly. See the code above. –  Sep 22 '17 at 12:40
  • 1
    That might be the reason [opencv issue](https://github.com/opencv/opencv/issues/11488) – user5081775 Apr 10 '19 at 08:20

1 Answers1

0

I am experiencing the same problem,but i solve it by this steps:

1.request permission

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

2.Manually open the permission switch

3.Close app and clear task

4.Open your project app and try again,it it working to me.

If it is not working for you,uninstall and reinstall app,and repeat the above steps