How to get buffer from Imagemagick image in C ++

I use the ImageMagick library for image processing. I need to upload the image "bmp", convert it to jpeg, upload it to the buffer and send it over the network.

However, I cannot find any helper function in ImageMagick that can convert and store data in a buffer. I can write only in a file. Tried using Magick::Blob , but still useless.

The following code is used to download, convert, and write to a file:

 Magick::Image img("Sample.bmp"); img.magick("jpeg"); img.write("Output.jpeg"); 

EDIT:

Used by Magick :: Blob as:

 Magick::Blob myBlob; img.write(&myBlob); const void *myData = myBlob.data(); 

But here I can’t convert myData to const char* without conversion.

+4
source share
3 answers

Thanks so much for the answers. My existing socket connection accepts a stream of strings from both ends. This is why I needed const char* . Found a base64() conversion function in Magick::Blob that returns a string. This solved my problem. For reference, the final code becomes:

 Magick::Image img("Sample.bmp"); Magick::Blob blob; img.magick( "JPEG" ) img.write( &blob ); std::string myStr = myBlob.base64(); 
+1
source

Have you tried:

 Magick::Image img("Sample.bmp"); Blob blob; img.magick( "JPEG" ) img.write( &blob ); // Then access blob data with blob.data() sendJpegImage(blob.data(), blob.length()) 

With void sendJpegImage(void* data, size_t length) will be your function to send data.

+4
source

Source: https://habr.com/ru/post/1486236/


All Articles