简体   繁体   English

使用Magick Wand API从图像获取二进制数据

[英]Get binary data from image using Magick Wand API

I am trying to get an image in binary data so that I can base64 encode it. 我正在尝试获取二进制数据中的图像,以便可以对它进行base64编码。 I am using the MagickGetImageBlob() of the Magick Wand API but the Blob I am receiving does not contain the entire information. 我正在使用Magick Wand API的MagickGetImageBlob() ,但是我收到的Blob并不包含全部信息。

My code is as follows. 我的代码如下。 opt and enc are two structs containing user provided parameters and encoding information respectively. optenc是分别包含用户提供的参数和编码信息的两个结构。 The library I am using to encode in base64 is this . 我用来在base64中编码的库是this

void WriteImg(UserDefinedOptions *options, MyStruct *enc, char *format){

    MagickWand *wand;
    char *outputPath;
    unsigned char *buffer = malloc(sizeof(char)*1000);
    size_t length;
    int flen;

    MagickWandGenesis();

    wand = NewMagickWand();

    MagickConstituteImage(wand, enc->image->width, enc->image->height,
            "RGB", CharPixel, enc->image->pxl);

    MagickSetImageResolution(wand, (double) options->dpi, (double) options->dpi);
    MagickSetImageUnits(wand, PixelsPerInchResolution);

    MagickSetImageFormat(wand, format);

    outputPath = (options->outputPath == NULL) ? "-" : options->outputPath;

    MagickWriteImage(wand, outputPath);  // This works and generates correct image


    buffer = MagickGetImageBlob(wand, &length); // Incomplete binary data
    /* Encode base64 */
    encbuffer = base64(buffer, strlen((const char *)buffer), &flen);    
    printf("Base64:%s\n", encbuffer);  

    CleanupMagick(&wand, DmtxFalse);

    MagickWandTerminus();

}

What am I doing wrong? 我究竟做错了什么? Is there a better way to get the base64 encoded string from the image using Magick Wand? 是否有更好的方法使用Magick Wand从图像中获取base64编码的字符串?

The second parameter to base64() should be length , not the return value of strlen() . base64()的第二个参数应该是length ,而不是strlen()的返回值。

/* Encode base64 */
encbuffer = base64(buffer, length, &flen); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM