How to save XImage as base64 string in PNG format?
I'm developing in Ubuntu Linux and C++.
I captured the desktop image to XImage.
How to save XImage as ba开发者_开发问答se64 string in PNG format?
Here is a C function to convert XImage
data into a jpeg image :
void write_jpeg( FILE *outfile, int width, int height, unsigned char *rgb, int quality)
{
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
JSAMPROW scanline[1];
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
jpeg_stdio_dest(&cinfo, outfile);
cinfo.image_width = width;
cinfo.image_height = height;
cinfo.input_components = 3;
cinfo.in_color_space = JCS_RGB;
jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo, quality, TRUE);
jpeg_start_compress(&cinfo, TRUE);
while (cinfo.next_scanline < (unsigned int) height)
{
scanline[0] = rgb + 3 * width * cinfo.next_scanline;
jpeg_write_scanlines(&cinfo, scanline, 1);
}
jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo);
}
I am sure you can easily fit this into your code. You just need to use appropriate library to save a PNG file (instead of a jpeg file).
I was about to say that the answer should be using libpng. However after a quick look at the documentation I find this library way much more complex than needed (easy things should be easy, hard things should be possible... after looking at the manual seems that easy things with libpng are instead close to impossible). PNG format has a gajillion options, libpng is extremely flexible and pluggable but apparently there is no easy shortcut when you just want to do something simpler (like saving a single opaque image).
I think I would be tempted to just dump the image in a raw format and then use imagemagick (possibly through a pipe).
On the other hand did you consider use Qt instead of bare X? Capturing the screen is easy and you can get it in a much more manageable format (QImage). Saving to almost any file format you like is also trivial (png format is however somewhat hard on CPU with Qt, so not recommended if you wanna do a live video capture).
精彩评论