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

时间:2021-10-31 21:03:11

how to convert byte* into jpeg file in VC++

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

i am capturing Video samples and writing it as bmp files, but i want to write that video samples into jpeg file using MFC support in ATL COM.

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

4 个解决方案

#1


Use libjpg. Download from: http://www.ijg.org/

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

#2


From what it appears, you have the image data in a buffer pointed to by a byte object. Note, that the type actually is BYTE (all uppercase). If the data is in JPEG format already why don't you write that data out to a file (with a suitable '.jpg' or '.jpeg' extension) and try loading it with an image editor? Otherwise, you will need to decode that to raw format and encode in the JPEG format.

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

Or, you need to explain you problem in more detail, preferably with some code.

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

#3


Raw image data to JPEG can be acheived by ImageMagick.

ImageMagick可以实现JPEG的原始图像数据。

#4


You may also try to use CxImage C++ class to save your stills to JPEG-encoded file.

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

There are some more Windows API oriented alternatives available on CodeProject, for instance CMiniJpegEncoder

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

It is even possible to render JPEG to file from Windows bitmap using libgd library if compiled with libjpeg support. Here is code of small extension function gdImageTrueColorAttachBuffer I developed for this purpose some time ago:

如果使用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;
}

I hope it helps.

我希望它有所帮助。

#1


Use libjpg. Download from: http://www.ijg.org/

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

#2


From what it appears, you have the image data in a buffer pointed to by a byte object. Note, that the type actually is BYTE (all uppercase). If the data is in JPEG format already why don't you write that data out to a file (with a suitable '.jpg' or '.jpeg' extension) and try loading it with an image editor? Otherwise, you will need to decode that to raw format and encode in the JPEG format.

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

Or, you need to explain you problem in more detail, preferably with some code.

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

#3


Raw image data to JPEG can be acheived by ImageMagick.

ImageMagick可以实现JPEG的原始图像数据。

#4


You may also try to use CxImage C++ class to save your stills to JPEG-encoded file.

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

There are some more Windows API oriented alternatives available on CodeProject, for instance CMiniJpegEncoder

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

It is even possible to render JPEG to file from Windows bitmap using libgd library if compiled with libjpeg support. Here is code of small extension function gdImageTrueColorAttachBuffer I developed for this purpose some time ago:

如果使用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;
}

I hope it helps.

我希望它有所帮助。