Ask Your Question

Encode image and copy to char* buffer [closed]

asked 2017-07-06 08:30:46 -0500

minok gravatar image

Hi I would like to copy encoded image as .png to char* buffer. What I am trying is:

CvMat* mat = cvEncodeImage(".png", img, param);
int bufLen = buffer->step;
unsigned char * pData = buffer->data.ptr;
char* buf;
memcpy( buf, pData, bufLen);

However buffer seems to be empty... I am not sure if I am accessing the data the right way, but I am having a hard time finding a proper documentation for this. Also - this has to be done in raw C (it's part of a bigger project).

Has anyone tried to do something like this before?


edit retag flag offensive reopen merge delete

Closed for the following reason question is not relevant or outdated by berak
close date 2017-07-06 23:10:11.140704


this has to be done in raw C -- you're too late for that., this is no more an option in 2017.

berak gravatar imageberak ( 2017-07-06 23:09:52 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2017-07-06 15:53:25 -0500

matman gravatar image
  1. Don't use C-API. It is deprecated. Use imencode instead.
  2. You try to copy the data to a NULL pointer. You have to allocate required memory first.

Solution could be something like this (from the head):

std::vector<uchar> localBuffer;
imencode(".png", img, localBuffer, param);
char *buf = malloc(localBuffer.size());
memcpy(buf,, localBuffer.size());

Don't forget to free buf if you don't need it anymore.

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2017-07-06 08:30:46 -0500

Seen: 1,100 times

Last updated: Jul 06 '17