这时我的代码:
#include "stdafx.h"
#include <atlbase.h> 
#include <afxwin.h>
#include <WINDOWSX.H>extern "C" {
#include "jpeglib.h"
//#include "jmorecfg.h"
#include "jconfig.h"
}GLOBAL(void) jpeg_compress(char *filename, int quality);
void CapScreen(char filename[]);BYTE *p;
JSAMPLE * image_buffer; /* Points to large array of R,G,B-order data */
int image_height = 900; /* Number of rows in image */
int image_width = 1440; /* Number of columns in image */int main(int argc, char* argv[])
{
image_buffer = p; p = (BYTE *)malloc(1440 * 900 * 4);
//CapScreen("ok.bmp");

jpeg_compress("ok.jpg", 1);
delete [] p;
return 0;
}
/*
 * IMAGE DATA FORMATS:
 *
 * The standard input image format is a rectangular array of pixels, with
 * each pixel having the same number of "component" values (color channels).
 * Each pixel row is an array of JSAMPLEs (which typically are unsigned chars).
 * If you are working with color data, then the color values for each pixel
 * must be adjacent in the row; for example, R,G,B,R,G,B,R,G,B,... for 24-bit
 * RGB color.
 *
 * For this example, we'll assume that this data structure matches the way
 * our application has stored the image in memory, so we can just pass a
 * pointer to our image buffer.  In particular, let's say that the image is
 * RGB color and is described by:
 */
GLOBAL(void) jpeg_compress(char *filename, int quality)
{
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
FILE * outfile; /* target file */
JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */
int row_stride; /* physical row width in image buffer */ /* Step 1: allocate and initialize JPEG compression object */
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
/* Step 2: specify data destination (eg, a file) */
if ((outfile = fopen(filename, "wb")) == NULL) {
fprintf(stderr, "can't open %s\n", filename);
return;
}
jpeg_stdio_dest(&cinfo, outfile);
/* Step 3: set parameters for compression */
cinfo.image_width = image_width;  /* image width and height, in pixels */
cinfo.image_height = image_height;
cinfo.input_components = 3; /* # of color components per pixel */
cinfo.in_color_space = JCS_RGB;  /* colorspace of input image */ jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */); /* Step 4: Start compressor */
jpeg_start_compress(&cinfo, TRUE);
/* Step 5: while (scan lines remain to be written) */
row_stride = image_width * 3; /* JSAMPLEs per row in image_buffer */

while (cinfo.next_scanline < cinfo.image_height) {
    /* jpeg_write_scanlines expects an array of pointers to scanlines.
     * Here the array is only one element long, but you could pass
     * more than one scanline at a time if that's more convenient.
     */
row_pointer[0] = & image_buffer[cinfo.next_scanline * row_stride];
(void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
}
/* Step 6: Finish compression */
jpeg_finish_compress(&cinfo);
/* After finish_compress, we can close the output file. */
fclose(outfile);
/* Step 7: release JPEG compression object */
jpeg_destroy_compress(&cinfo);
}

解决方案 »

  1.   


    #include "stdafx.h"
    #include <atlbase.h> 
    #include <afxwin.h>
    #include <WINDOWSX.H>extern "C" {
    #include "jpeglib.h"
    //#include "jmorecfg.h"
    #include "jconfig.h"
    }GLOBAL(void) jpeg_compress(char *filename, int quality);
    void CapScreen(char filename[]);BYTE *p;
    JSAMPLE * image_buffer; /* Points to large array of R,G,B-order data */
    int image_height = 900; /* Number of rows in image */
    int image_width = 1440; /* Number of columns in image */int main(int argc, char* argv[])
    {
    image_buffer = p; p = (BYTE *)malloc(1440 * 900 * 4);
    //CapScreen("ok.bmp");

    jpeg_compress("ok.jpg", 1);
    delete [] p;
    return 0;
    }
    /*
     * IMAGE DATA FORMATS:
     *
     * The standard input image format is a rectangular array of pixels, with
     * each pixel having the same number of "component" values (color channels).
     * Each pixel row is an array of JSAMPLEs (which typically are unsigned chars).
     * If you are working with color data, then the color values for each pixel
     * must be adjacent in the row; for example, R,G,B,R,G,B,R,G,B,... for 24-bit
     * RGB color.
     *
     * For this example, we'll assume that this data structure matches the way
     * our application has stored the image in memory, so we can just pass a
     * pointer to our image buffer.  In particular, let's say that the image is
     * RGB color and is described by:
     */
    GLOBAL(void) jpeg_compress(char *filename, int quality)
    {
    struct jpeg_compress_struct cinfo;
    struct jpeg_error_mgr jerr;
    FILE * outfile; /* target file */
    JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */
    int row_stride; /* physical row width in image buffer */ /* Step 1: allocate and initialize JPEG compression object */
    cinfo.err = jpeg_std_error(&jerr);
    jpeg_create_compress(&cinfo);
    /* Step 2: specify data destination (eg, a file) */
    if ((outfile = fopen(filename, "wb")) == NULL) {
    fprintf(stderr, "can't open %s\n", filename);
    return;
    }
    jpeg_stdio_dest(&cinfo, outfile);
    /* Step 3: set parameters for compression */
    cinfo.image_width = image_width;  /* image width and height, in pixels */
    cinfo.image_height = image_height;
    cinfo.input_components = 3; /* # of color components per pixel */
    cinfo.in_color_space = JCS_RGB;  /* colorspace of input image */ jpeg_set_defaults(&cinfo);
    jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */); /* Step 4: Start compressor */
    jpeg_start_compress(&cinfo, TRUE);
    /* Step 5: while (scan lines remain to be written) */
    row_stride = image_width * 3; /* JSAMPLEs per row in image_buffer */

    while (cinfo.next_scanline < cinfo.image_height) {
        /* jpeg_write_scanlines expects an array of pointers to scanlines.
         * Here the array is only one element long, but you could pass
         * more than one scanline at a time if that's more convenient.
         */
    row_pointer[0] = & image_buffer[cinfo.next_scanline * row_stride];
    (void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
    }
    /* Step 6: Finish compression */
    jpeg_finish_compress(&cinfo);
    /* After finish_compress, we can close the output file. */
    fclose(outfile);
    /* Step 7: release JPEG compression object */
    jpeg_destroy_compress(&cinfo);
    }
      

  2.   

    把JSAMPLE * image_buffer;改成BYTE * image_buffer就可以了