如何在VC ++中将byte *转换为jpeg文件

时间:2009-03-07 08:49:50

标签: visual-c++

如何在VC ++中将byte *转换为jpeg文件

我正在捕获视频样本并将其写为bmp文件,但我想在ATL COM中使用MFC支持将该视频样本写入jpeg文件。

4 个答案:

答案 0 :(得分:2)

使用libjpg。下载地址:http://www.ijg.org/

答案 1 :(得分:0)

从它出现的内容,您将图像数据放在byte对象指向的缓冲区中。请注意,该类型实际上是BYTE(全部大写)。如果数据已经是JPEG格式,为什么不将这些数据写入文件(使用合适的'.jpg'或'.jpeg'扩展名)并尝试使用图像编辑器加载?否则,您需要将其解码为原始格式并以JPEG格式进行编码。

或者,您需要更详细地解释您的问题,最好是使用一些代码。

答案 2 :(得分:0)

可以通过ImageMagick实现原始图像数据到JPEG。

答案 3 :(得分:0)

您也可以尝试使用CxImage C ++类将静止图像保存为JPEG编码文件。

CodeProject上还有一些面向Windows API的替代方案,例如CMiniJpegEncoder

如果使用libjpeg支持编译,甚至可以使用libgd库从Windows位图渲染JPEG文件。这是我前段时间为此目的开发的小扩展函数gdImageTrueColorAttachBuffer的代码:

// libgd ext// libgd extension by Mateusz Loskot <mateusz at loskot dot net>
// Originally developed for Windows CE to enable direct drawing
// on Windows API Device Context using libgd API.
// Complete example available in libgd CVS:
// http://cvs.php.net/viewvc.cgi/gd/libgd/examples/windows.c?diff_format=u&revision=1.1&view=markup
//
gdImagePtr gdImageTrueColorAttachBuffer(int* buffer, int sx, int sy, int stride)
{
    int i;
    int height;
    int* rowptr;
    gdImagePtr im;
    im = (gdImage *) malloc (sizeof (gdImage));
    if (!im) {
        return 0;
    }
    memset (im, 0, sizeof (gdImage));
#if 0
    if (overflow2(sizeof (int *), sy)) {
        return 0;
    }
#endif

    im->tpixels = (int **) malloc (sizeof (int *) * sy);
    if (!im->tpixels) {
        free(im);
        return 0;
    }

    im->polyInts = 0;
    im->polyAllocated = 0;
    im->brush = 0;
    im->tile = 0;
    im->style = 0;

    height = sy;
    rowptr = buffer;
    if (stride < 0) {
        int startoff = (height - 1) * stride;
        rowptr = buffer - startoff;
    }

    i = 0;
    while (height--) {
        im->tpixels[i] = rowptr;
        rowptr += stride;
        i++;
    }

    im->sx = sx;
    im->sy = sy;
    im->transparent = (-1);
    im->interlace = 0;
    im->trueColor = 1;
    im->saveAlphaFlag = 0;
    im->alphaBlendingFlag = 1;
    im->thick = 1;
    im->AA = 0;
    im->cx1 = 0;
    im->cy1 = 0;
    im->cx2 = im->sx - 1;
    im->cy2 = im->sy - 1;
    return im;
}

void gdSaveJPEG(void* bits, int width, int height, const char* filename)
{
    bool success = false;
    int stride = ((width * 1 + 3) >> 2) << 2;
    gdImage* im = gdImageTrueColorAttachBuffer((int*)bits, width, height, -stride);
    if (0 != im)
    {
        FILE* jpegout = fopen(filename, "wb");
        gdImageJpeg(im, jpegout, -1);
        fclose(jpegout);
        success = true;
    }
    gdImageDestroy(im);
    return success;
}

我希望它有所帮助。

相关问题