我在网上找了些资料,不过都是千篇一律,总是缺胳膊少腿的,这是我没有调通的代码:
int jpeg_impress(char dst[], char src[])
{
struct jpeg_compress_struct jcs;
struct jpeg_error_mgr jem;
FILE *f1;
FILE    *f2; f2 = fopen(src, "rb"); jcs.err = jpeg_std_error(&jem);
jpeg_create_compress(&jcs); //´ò¿ªÎļþ
f1=fopen(dst,"wb");
if (f1==NULL) 
{
return -1;
}
jpeg_stdio_dest(&jcs, f1); jcs.image_width = 1024;  
jcs.image_height = 768;
jcs.input_components = 3;  
jcs.in_color_space = JCS_RGB; 
jpeg_set_defaults(&jcs); 
jpeg_set_quality (&jcs, 80, true); jpeg_start_compress(&jcs, TRUE); JSAMPROW row_pointer[1]; 
int row_stride;     
row_stride = jcs.image_width;  while (jcs.next_scanline < jcs.image_height) {
//row_pointer[0] = &pDataConv[jcs.next_scanline * row_stride];
jpeg_write_scanlines(&jcs, row_pointer, 1);
}
jpeg_finish_compress(&jcs);
jpeg_destroy_compress(&jcs);}
这里面的pDataConv我猜应该是指向bmp文件的,但是应该怎么初始化它呢?费解,哪位先生知道,麻烦讲解一二!

解决方案 »

  1.   


    /*
    code by hopy|侯佩 2009
    */
    _cdecl bool Bmp2Jpg(const char *SrcName,const char *DstName)
    {
    BITMAPFILEHEADER bfh; // bmp文件头
    BITMAPINFOHEADER bih; // bmp头信息
    RGBQUAD rq[256]; // 调色板
    int nAdjust; // 用于字节对齐
    BOOL bRet = false;
    FILE *pSrcFile = NULL,*pDstFile = NULL; BYTE *data= NULL;
    int nComponent = 0;

    // 打开图像文件
    PRINT("[%s]msg : src file name is %s\n",__func__,SrcName);
    pSrcFile = fopen(SrcName,"rb");
    if (!pSrcFile)
    {
    PRINT("[%s]err : Open src file error!\n",__func__);
    goto QUIT;
    }

    // 读取文件头
    if(fread(&bfh,sizeof(bfh),1,pSrcFile) != 1)
    {
    PRINT("[%s]err : fread file head failed!\n",__func__);
    goto QUIT;
    }

    // 读取图像信息
    if(fread(&bih,sizeof(bih),1,pSrcFile) != 1)
    {
    PRINT("[%s]err : fread file body failed!\n",__func__);
    goto QUIT;
    } PRINT("[%s]msg : bih.biBitCount == %d\n",__func__,bih.biBitCount);
    switch (bih.biBitCount) 
    {
    case 8:
    if (bfh.bfOffBits-1024<54) 
    {
    PRINT("[%s]err : bfh.bfOffBits-1024<54\n",__func__);
    goto QUIT;
    }
    // 8位字节对齐
    nAdjust = bih.biWidth%4;
    if (nAdjust) nAdjust = 4-nAdjust; //data= new BYTE[(bih.biWidth+nAdjust)*bih.biHeight];
    data = (BYTE*)malloc((bih.biWidth+nAdjust)*bih.biHeight); // 定位调色板,并读取调色板
    fseek(pSrcFile,bfh.bfOffBits-1024,SEEK_SET);
    if(fread(rq,sizeof(RGBQUAD),256,pSrcFile) != 256)
    {
    PRINT("[%s]err : fread RGBQUAD failed!\n",__func__);
    goto QUIT;
    }
    // 读取位图
    if(fread(data,(bih.biWidth+nAdjust)*bih.biHeight,1,pSrcFile) != 1)
    {
    PRINT("[%s]err : fread bitmap file failed![1]\n",__func__);
    goto QUIT;
    }
    nComponent = 1;
    break;
    case 24:
    {
    // 8位字节对齐
    nAdjust = bih.biWidth*3%4;
    if (nAdjust) nAdjust = 4-nAdjust;
    //data= new BYTE[(bih.biWidth*3+nAdjust)*bih.biHeight];
    data=(BYTE*)malloc((bih.biWidth*3+nAdjust)*bih.biHeight);

    fseek(pSrcFile,bfh.bfOffBits,SEEK_SET);
    if(fread(data,(bih.biWidth*3+nAdjust)*bih.biHeight,1,pSrcFile) != 1)
    {
    PRINT("[%s]err : fread bitmap file failed![2]\n",__func__);
    goto QUIT;
    } for (int j=0;j<bih.biHeight;j++){
    for (int i = 0;i<bih.biWidth;i++)
    {
    BYTE red = data[j*(bih.biWidth*3+nAdjust)+i*3];
    data[j*(bih.biWidth*3+nAdjust)+i*3] = data[j*(bih.biWidth*3+nAdjust)+i*3+2];
    data[j*(bih.biWidth*3+nAdjust)+i*3+2] = red;
    }
    }
    nComponent = 3;
    break;
    }
    default:
    PRINT("[%s]err : Unhandlable BitCount %d\n",__func__,\
    bih.biBitCount);
    goto QUIT;
    } struct jpeg_compress_struct jcs;
    struct jpeg_error_mgr jem;
    jcs.err = jpeg_std_error(&jem); jpeg_create_compress(&jcs); pDstFile = fopen(DstName,"w+b");
    if(!pDstFile) 
    {
    PRINT("[%s]err : open dst file failed!\n",__func__);
    goto QUIT;
    }

    jpeg_stdio_dest(&jcs,pDstFile);
    jcs.image_width = bih.biWidth;  // 为图的宽和高,单位为像素 
    jcs.image_height = bih.biHeight;
    jcs.input_components = nComponent; // 1,表示灰度图, 如果是彩色位图,则为3 
    if (nComponent==1)
    jcs.in_color_space = JCS_GRAYSCALE; //JCS_GRAYSCALE表示灰度图,JCS_RGB表示彩色图像 
    else 
    jcs.in_color_space = JCS_RGB; jpeg_set_defaults(&jcs);
    jpeg_set_quality(&jcs, 60, true); jpeg_start_compress(&jcs, true); JSAMPROW row_pointer[1]; // 一行位图
    int row_stride; // 每一行的字节数  row_stride = jcs.image_width * nComponent; // 如果不是索引图,此处需要乘以3 // 对每一行进行压缩
    while (jcs.next_scanline < jcs.image_height) {
        row_pointer[0] = & data[(jcs.image_height-jcs.next_scanline-1) * (row_stride+nAdjust)];
        jpeg_write_scanlines(&jcs, row_pointer, 1);
    } jpeg_finish_compress(&jcs); jpeg_destroy_compress(&jcs);

    PRINT("[%s]msg : transfer to jpg is ok!\n",__func__);
    bRet = true;
    QUIT:
    if(pSrcFile)
    fclose(pSrcFile);
    if(pDstFile)
    fclose(pDstFile);
    if(data)
    //delete [] data;
    free(data);
    return bRet;
    }