tags:

views:

383

answers:

4

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

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.

A: 

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.

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

dirkgently
+2  A: 

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

Jimmy J
A: 

Raw image data to JPEG can be acheived by ImageMagick.

Alphaneo
A: 

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

There are some more Windows API oriented alternatives available on CodeProject, for instance 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:

// 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&amp;revision=1.1&amp;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.

mloskot