0

I have a native C++ method, which I am using to read an image called "hi.jpg". The code below finds the asset, and loads the data into a char* buffer. (I've tried other methods such as imread() and the file is not found). I would then like to change this data into Mat format, so I've followed some instructions to put the char* buffer into std::vector , and then use cv::imdecode to convert the data to Mat.

JNIEXPORT jint JNICALL Java_com_example_user_application_MainActivity_generateAssets(JNIEnv* env,jobject thiz,jobject assetManager) {

AAsset* img;

AAssetManager *mgr = AAssetManager_fromJava(env, assetManager);
AAssetDir* assetDir = AAssetManager_openDir(mgr, "");
const char* filename;

while ((filename = AAssetDir_getNextFileName(assetDir)) != NULL) {
    AAsset *asset = AAssetManager_open(mgr, filename, AASSET_MODE_UNKNOWN);

    if(strcmp(filename, "hi.jpg")==0 ) {
        img = asset;
    }
}

long sizeOfImg = AAsset_getLength(img);
char* buffer = (char*) malloc (sizeof(char)*sizeOfImg);
AAsset_read(img, buffer, sizeOfImg);

std::vector<char> data(buffer, buffer + strlen(buffer));

cv::Mat dataToMat = cv::imdecode(data, IMREAD_UNCHANGED);


return 0;
}

My problem is that I don't know how to test that the data has been successfully converted into Mat. How can I test this? I have ran the debugger and inspected dataToMat, but it isn't making much sense.

Ber12345
  • 89
  • 1
  • 12
  • 2
    How about just writing the Mat to a file using imwrite and comparing the two jpg's? – wolff Dec 16 '17 at 15:17
  • Agree with @wolff, the only thing you need to remember is to add https://developer.android.com/reference/android/Manifest.permission.html#WRITE_EXTERNAL_STORAGE (WRITE_EXTERNAL_STORAGE) permission to your app in order to use imwrite – Dmitrii Z. Dec 16 '17 at 15:39
  • 1
    Note that you can use AAsset_getBuffer() – Alex Cohn Dec 16 '17 at 16:22

0 Answers0