access violation while filling a Mat with custom uchar* data [closed]

asked 2016-08-17 09:00:40 -0600

orpheelepoete gravatar image

updated 2016-08-17 09:02:02 -0600

I want to test the base64 encoding/decoding about to make an Android application communicate with native Opencv c++ code.

I try to show the result image about to check weather the result string is correct. The code I used looks like this:

cv::Mat img = imread(imgPath);
string imgData = reinterpret_cast<char*>(;
string str = base64_encode(reinterpret_cast<const unsigned char*>(imgData.c_str()), imgData.length()).c_str();
string strOutput = base64_decode(str);
uchar* data = (uchar*)reinterpret_cast<const unsigned char*>(strOutput.c_str());

I have compared the result data and the input image's data using:

int cpr = strcmp((char*), (char*)data);

and the result (0) confirms that the data uchar* is the same as the input image's data

When I try to copy the input image's data into the output image, everything works:

memcpy(,, img.rows * img.cols * img.channels() * sizeof(uchar));

But when I try to copy the result data into the output image, I get an access violation error

memcpy(, data, img.rows * img.cols * img.channels() * sizeof(uchar));

I wonder why the i cannot fill the output image with the "data" uchar* whereas it is strictly identic with the "" uchar*.

Thanks in advance.

edit retag flag offensive reopen merge delete

Closed for the following reason question is not relevant or outdated by sturkmen
close date 2020-10-04 07:10:10.816582


  • why on earth do you think, you need base64 ever ?

  • never put image data into a string . e.g. imgData.length() will terminate at the 1st black pixel.

berak gravatar imageberak ( 2016-08-18 01:05:10 -0600 )edit

Thanks. I tried that because base64 is used for webservices transmissions. I first tried to transfer byte arrays using jByteArray JNI type and trying to store it inside a unsigned char*, but I didn't succeed in making it work. My problem is I cannot debug what's happening in the native code so I don't know where the bug is. That's why I tried something simple to see if the transmission works. It's the very first time I try to create native code. I have experiences in Java, C++ and OpenCV but I've never been formed for NDK/JNI.

Do you know the safest way to transmit a camera preview frame to an opencv Mat? The view information I found on the web didn't work for me.

orpheelepoete gravatar imageorpheelepoete ( 2016-08-18 02:48:23 -0600 )edit

please be more specific, about what you're trying to achieve

berak gravatar imageberak ( 2016-08-18 03:00:46 -0600 )edit

I want to process Android's camera frames, using native C++ Opencv code. I have to get the Android camera preview frame's data, give those data as parameters to my c++ function, store those data inside an OpenCV Mat, process this Mat and give the Mat's data back to Android.

Note: the Android code has been developped by someone else and I cannot modify it. The camera class used is obsolete (import android.hardware.Camera) and I must use its informations.

orpheelepoete gravatar imageorpheelepoete ( 2016-08-18 03:05:24 -0600 )edit

this sounds more like plain jni coding, you won't need base64 for this at all, rather look at the android sdk samples, like this one

berak gravatar imageberak ( 2016-08-18 03:15:56 -0600 )edit

I am not allowed to use Cv4Android and this tutoriel use a special type of Camera included in Opencv4Android. I am forced to use my co-worker's code without modifying it (except for the function which gets the byte[] camera frame)

orpheelepoete gravatar imageorpheelepoete ( 2016-08-18 03:47:15 -0600 )edit

clear case of "X Y problem" here.

please, think it all over again, try to refine your needs, and ask a new question then.

berak gravatar imageberak ( 2016-08-18 04:09:46 -0600 )edit