Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
imagemagick:保存MagickExportImagePixels';是否将blob输出到灰度图像文件?_Imagemagick_Zbar - Fatal编程技术网

imagemagick:保存MagickExportImagePixels';是否将blob输出到灰度图像文件?

imagemagick:保存MagickExportImagePixels';是否将blob输出到灰度图像文件?,imagemagick,zbar,Imagemagick,Zbar,zbar发动机样本源(zbarimg.c)显示了以下内容 https://github.com/ZBar/ZBar/blob/master/zbarimg/zbarimg.c size_t bloblen = width * height; unsigned char *blobdata = malloc(bloblen); MagickExportImagePixels(images, 0, 0, width, height, "I", CharPixel, blobdata); 我想看看

zbar发动机样本源(zbarimg.c)显示了以下内容

https://github.com/ZBar/ZBar/blob/master/zbarimg/zbarimg.c

size_t bloblen = width * height;
unsigned char *blobdata = malloc(bloblen);
MagickExportImagePixels(images, 0, 0, width, height, "I", CharPixel, blobdata);
我想看看blobdata。 如何将blobdata保存到文件

我使用save_imgdata函数来保存blobdata

int save_imgdata(char* imgf, int width, int height, char *raw)
{
    PixelWand *p_wand = NULL;
    PixelIterator *iterator = NULL;
    PixelWand **pixels = NULL;
    unsigned long x, y;
    char hex[128];

    //MagickWandGenesis();
    p_wand = NewPixelWand();
    PixelSetColor(p_wand, "gray");
    //PixelSetColor(p_wand, "white");
    MagickWand *m_wand = NewMagickWand(); //CORE_RL_wand_.lib;
    MagickSetImageDepth(m_wand, 8);
    MagickNewImage(m_wand, width, height, p_wand);
    // Get a new pixel iterator 
    iterator = NewPixelIterator(m_wand);
    for (y = 0; y<height; y++) {
        // Get the next row of the image as an array of PixelWands
        pixels = PixelGetNextIteratorRow(iterator, &x);
        // Set the row of wands to a simple gray scale gradient
        for (x = 0; x<width; x++) {
            sprintf(hex, "#%02x", *raw++);
            //sprintf(hex, "#%02%x02%x02x", *raw, *raw, *raw); raw++;
            PixelSetColor(pixels[x], hex);
        }
        // Sync writes the pixels back to the m_wand
        PixelSyncIterator(iterator);
    }
    MagickWriteImage(m_wand, imgf);
    DestroyMagickWand(m_wand);
    return 0;
}
int save\u imgdata(char*imgf,int width,int height,char*raw)
{
PixelWand*p_wand=NULL;
像素迭代器*迭代器=空;
像素**像素=空;
无符号长x,y;
字符十六进制[128];
//MagickWandGenesis();
p_wand=新像素wand();
像素设置颜色(p_棒,“灰色”);
//像素设置颜色(p_棒,“白色”);
MagickWand*m_wand=NewMagickWand();//CORE_RL_wand_.lib;
MagickSetImageDepth(m_wand,8);
MagickNewImage(木棒、宽度、高度、木棒);
//获取一个新的像素迭代器
迭代器=新像素迭代器(m_wand);

对于(y=0;y不必费心迭代和构建动态颜色/像素值——这既慢又占用资源。如果数据来自导出方法,请使用导入方法进行恢复

int save\u imgdata(char*imgf,int width,int height,void*raw)
{
魔杖*魔杖;
像素棒*bgcolor;
bgcolor=NewPixelWand();
像素设置颜色(bgcolor,“白色”);
魔杖=新魔杖();
MagickNewImage(魔杖、宽度、高度、颜色);
bgcolor=像素棒(bgcolor);
MagickSetImageDepth(魔杖,8);
MagickSetImageColorspace(魔杖,灰色色彩空间);
MagickImportImagePixels(魔杖、0、0、宽度、高度、“I”、CharPixel、raw);
MagickQuantizeImage(魔杖,
256,//减少到8bpp
GRAYColorspace,//匹配颜色空间
0,//计算最佳树深度
MagickTrue,//使用抖动?这会在IM-7中发生变化
MagickFalse);//测量错误
MagickWriteImage(魔杖,imgf);
魔杖=魔杖(魔杖);
返回0;
}

非常感谢。我添加了save\u imgdata(“mgf.bmp”,宽度、高度、斑点)在MagickExportImagePixels之后保存imgdata。它保存24bpp位图文件。我不明白为什么imagemagick保存24bpp?当输入图像为24bpp或8bpp或1bpp时,它保存24bpp。使用
magickequantizeimage
将颜色从24减少到8bpp。这与使用文档中建议的
-colors 256
相同在上。我将更新答案中的示例。很好,效果很好。MagickExportImagePixels是否始终导出24bp?否。24bp是
bmp
编码格式的默认值。中有一些概述。