RGB bmp转jpeg的方法

RGB转jpeg的方法:先对图像进行预处理,然后DCT变换,量化,然后进行编码,huffman编码或其它编码,就可以转换成jpg了。下面主要讲解使用opencv保存jpg图像,或使用IJG库保存jpg图像,使用opencv保存jpg图像的函数如下:

    CVAPI(int) cvSaveImage( const char* filename, const CvArr* image,
                          const int* params CV_DEFAULT(0) );
第三个参数可以设置压缩的质量

int params[3]

params[0] = CV_IMWRITE_JPEG_QUALITY;

params[1] = 85;//设置s压缩度

params[2] = 0;

把params传入就可以了。

 

使用IJG进行压缩的方法如下:

首先下载IJG库,下载的网站是http://www.ijg.org,然后对下载的源码进行编译,编译可以参考它的文档,我下载的为jpegsr8c,按照它的文档,只能编译出vc6.0和vs2010的版本库,我没有安装vs2010的软件,所以使用vc6.0编译出来的库,发现不能使用,原因可能是vc6.0编译的是单线程的东东,但是我使用的是多线程的东西。所以我使用vs2008重新对源码进行编译:编译方法如下:

       一、建立自己的libjpeg工程
       为了修改后编译方便,也为了以后在VC 环境下容易使用jpeg库,我们按以下步骤将libjpeg转换为VC环境下的工程。
        1、在VC环境下重新建立一个空的static library工程,工程名为jpeg,此处注意,新建工程不要包含mfc,不要预编译头文件;
         2、然后将libjpeg下的jcapimin.c jcapistd.c jccoefct.c jccolor.c jcdctmgr.c jchuff.c
        jcinit.c jcmainct.c jcmarker.c jcmaster.c jcomapi.c jcparam.c
        jcphuff.c jcprepct.c jcsample.c jctrans.c jdapimin.c jdapistd.c
        jdatadst.c jdatasrc.c jdcoefct.c jdcolor.c jddctmgr.c jdhuff.c
        jdinput.c jdmainct.c jdmarker.c jdmaster.c jdmerge.c jdphuff.c
        jdpostct.c jdsample.c jdtrans.c jerror.c jfdctflt.c jfdctfst.c
        jfdctint.c jidctflt.c jidctfst.c jidctint.c jidctred.c jquant1.c
        jquant2.c jutils.c jmemmgr.c
       jchuff.h  jconfig.h jdhuff.h jdct.h jerror.h jinclude.h jmemsys.h jmorecfg.h
        jpegint.h jpeglib.h jversion.h 等文件拷贝到新工程的文件夹下,并将.c文件改名为.cpp;
         3、将所有的源文件及头文件添加到新建的工程中;
         4、编译新工程,此时就可以生成jpeg.lib了。

 

编译完库后就可以使用了。

 

如果谁需要我编译好的库,vs2008的,给我留言,呵呵

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
BMP 格式的图片换为 JPEG 格式的图片需要用到图像处理库,例如 libjpeg 库。以下是一个简单的 BMP JPEG 的 C 语言代码示例: ```c #include <stdio.h> #include <stdlib.h> #include <jpeglib.h> int main(int argc, char** argv) { if (argc != 3) { printf("usage: bmp2jpeg input_file output_file\n"); return -1; } char* input_file = argv[1]; char* output_file = argv[2]; FILE* infile = fopen(input_file, "rb"); if (infile == NULL) { printf("error: cannot open input file\n"); return -1; } unsigned char header[54]; fread(header, sizeof(unsigned char), 54, infile); int width = *(int*)&header[18]; int height = *(int*)&header[22]; int bit_depth = *(int*)&header[28]; if (bit_depth != 24) { printf("error: only support 24-bit BMP\n"); return -1; } unsigned char* data = (unsigned char*)malloc(width * height * 3); fread(data, sizeof(unsigned char), width * height * 3, infile); fclose(infile); struct jpeg_compress_struct cinfo; struct jpeg_error_mgr jerr; cinfo.err = jpeg_std_error(&jerr); jpeg_create_compress(&cinfo); FILE* outfile = fopen(output_file, "wb"); if (outfile == NULL) { printf("error: cannot open output file\n"); return -1; } jpeg_stdio_dest(&cinfo, outfile); cinfo.image_width = width; cinfo.image_height = height; cinfo.input_components = 3; cinfo.in_color_space = JCS_RGB; jpeg_set_defaults(&cinfo); jpeg_start_compress(&cinfo, TRUE); JSAMPROW row_pointer[1]; while (cinfo.next_scanline < cinfo.image_height) { row_pointer[0] = &data[cinfo.next_scanline * width * 3]; jpeg_write_scanlines(&cinfo, row_pointer, 1); } jpeg_finish_compress(&cinfo); fclose(outfile); jpeg_destroy_compress(&cinfo); free(data); return 0; } ``` 使用方法: ``` $ gcc -o bmp2jpeg bmp2jpeg.c -ljpeg $ ./bmp2jpeg input.bmp output.jpg ``` 其中,`input.bmp` 是要换的 BMP 图片文件,`output.jpg` 是换后的 JPEG 图片文件。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值