0

I don't know how to transform opencv::Mat to podofo::PdfImage or how to solve the crash. I'm confused.

My code:

// Mat img
// ...do something to init 'img'...
int image_size = img.cols * img.rows;
unsigned char* outBuffer = new unsigned char[image_size];
memcpy(outBuffer, sub_img[0].data, image_size);
//imwrite(".\\71253_0_0.png",img); // can be saved and displayed correctly

PdfImage pdfimg( &document );
pdfimg.LoadFromData(img.data,image_size); **//cause a crash**
barbsan
  • 3,418
  • 11
  • 21
  • 28
pengpeng
  • 13
  • 4
  • Since `LoadFromData` only takes a pointer and length, it's unlikely that it is intended to process raw pixel data -- it wouldn't know how to interpret it, since it doesn't know the width/height or the color format of the image. Rather it is intended to read encoded (PNG, JPEG, etc.) image data directly from a memory buffer (basically contents of an image file, such as the `71253_0_0.png` you created, but already loaded to memory). A brief look at the [source code](https://github.com/svn2github/podofo/blob/master/src/doc/PdfImage.cpp#L277) confirms this. – Dan Mašek May 07 '19 at 12:14
  • To make it work, encode the image using [`imencode`](https://docs.opencv.org/3.4.6/d4/da8/group__imgcodecs.html#ga461f9ac09887e47797a54567df3b8b63) and feed the resulting buffer to `LoadFromData`. – Dan Mašek May 07 '19 at 12:15

0 Answers0