BMP转JPG(法一)使用jpeglib库实现bmp转jpg (转)

来自:http://blog.csdn.net/chenyujing1234/article/details/7730478



一、vc编译jpeglib库

1、下载源代码

下载地址:http://www.ijg.org/。注意:一定要下载win32 版本

2、编译源代码.

    A、解压源代码,修改源代码中jconfig.vc为jconfig.h;

    B、添加环境变量PATH,C:/Program Files/Microsoft Visual Studio/VC98/Bin ;

    C、修改makefile.vc,将 Pull in standard variable definitions下面的一行换为:!include <C:/Program Files/Microsoft Visual Studio/VC98/Include/win32.mak> ;

    D、进入命令提示环境下,输入:vcvars32 回车,这是一个用来设置VC路径环境的批处理;

    E、编译生成库文件 命令:nmake /f makefile.vc nodebug=1;

这样就OK了

3、jpeglib库VC下使用

对于库的使用只需要有相应的.lib文件和头文件就可以了。Vc中要添加libjpeg.lib库的连接

         将这几个文件拷贝到你的项目中:libjpeg.lib,jconfig.h,jmorecfg.h,jpeglib.h,在你需要进行压缩的文件中加入

extern "C" {

         #include "jpeglib.h"

         #include "jmorecfg.h"

         #include "jconfig.h"

}

参考:

http://blog.csdn.net/xingyu19871124/archive/2009/06/30/4310800.aspx

小知识:bmp文件的前54个字节是头,后面才是像素值。

二、使用jpeg库压缩

在源代码的文件夹下面有一个example.c的文件和libjpeg.txt,很有参考价值。

1、基本思路

首先调用截屏程序,将屏幕的位图信息存放在一个buffer里面,然后调用jpg压缩函数,在当前的目录下生成一个ok.jpg的文件。

效果图:

2、出现的问题:

A、运行是总是报错:

我参考源代码的例子,也用JSAMPLE * image_buffer;来指向位图的像素的首地址,编译可以通过但是运行时就会报错,后来我用BYTE *image_buffer;来定义就可以正常运行了。

B、生成的jpg图像失真:以下是对比图片

 

 

由于window的位图的像素格式是:BGRA,4个字节,jpeglib库使用的是RGB,3个字节的格式,所以需要将源像素去掉其透明字节,同时改变RGB的顺序。代码如下:

  1. //RGB顺序调整   
  2.   
  3. for (int i=0, int j=0; j < 1440*900*4; i+=3, j+=4)  
  4. {  
  5.     *(image_buffer+i)=*(image_buffer+j+2);  
  6.     *(image_buffer+i+1)=*(image_buffer+j+1);  
  7.     *(image_buffer+i+2)=*(image_buffer+j);  
  8. }  
//RGB顺序调整

for (int i=0, int j=0; j < 1440*900*4; i+=3, j+=4)
{
    *(image_buffer+i)=*(image_buffer+j+2);
    *(image_buffer+i+1)=*(image_buffer+j+1);
    *(image_buffer+i+2)=*(image_buffer+j);
}


 

C、生成的jpg文件图像是倒的:

原因没有找到,后来修改了压缩函数的代码,生成了正确的jpg文件

  1. while (cinfo.next_scanline < cinfo.image_height)   
  2.     {  
  3.         //这里我做过修改,由于jpg文件的图像是倒的,所以改了一下读的顺序   
  4.         //这是原代码:   
  5.         //row_pointer[0] = & bits[cinfo.next_scanline * row_stride];   
  6.         row_pointer[0] = & bits[(cinfo.image_height - cinfo.next_scanline - 1) * row_stride];  
  7.         (void) jpeg_write_scanlines(&cinfo, row_pointer, 1);  
  8.     }  
while (cinfo.next_scanline < cinfo.image_height) 
	{
		//这里我做过修改,由于jpg文件的图像是倒的,所以改了一下读的顺序
		//这是原代码:
		//row_pointer[0] = & bits[cinfo.next_scanline * row_stride];
		row_pointer[0] = & bits[(cinfo.image_height - cinfo.next_scanline - 1) * row_stride];
		(void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
	}


 

3、测试结果:

我编写了测试代码,连续截屏并生成jpg文件100次,大约花费7秒左右,也就是说1秒可以截屏13次左右。同时生成的jpg文件有100多K的样子。

三、代码

  1. #define JPEG_QUALITY 50     //它的大小决定jpg的质量好坏   
  2. //#include <atlbase.h>    
  3. #include <afxwin.h>   
  4.   
  5. extern "C" {  
  6. #include "jpeglib.h"   
  7. #include "jmorecfg.h"   
  8. #include "jconfig.h"   
  9. }  
  10. int savejpeg(char *filename, unsigned char *bits, int width, int height, int depth);  
  11. void CapScreen(char filename[]);  
  12.   
  13.   
  14. BYTE *image_buffer; //指向位图buffer的全局指针,window下像素格式: BGRA(4个字节)   
  15.   
  16. int main(int argc, char* argv[])  
  17. {  
  18.     image_buffer = (BYTE *)malloc(1440 * 900 * 4);  
  19.     for(int i = 0; i < 100; i++)  
  20.     {  
  21.         CapScreen("palm.bmp");      
  22.         //RGB顺序调整   
  23.         for (int i=0, j=0; j < 1440*900*4; i+=3, j+=4)  
  24.         {  
  25.             *(image_buffer+i)=*(image_buffer+j+2);  
  26.             *(image_buffer+i+1)=*(image_buffer+j+1);  
  27.             *(image_buffer+i+2)=*(image_buffer+j);  
  28.         }  
  29.         savejpeg("ok.jpg", image_buffer, 1440, 900, 3);  
  30.     }  
  31.     delete [] image_buffer;  
  32.     return 0;  
  33. }  
  34.   
  35. //===================================================================================   
  36. //function:       jpeg压缩   
  37. //input:          1:生成的文件名,2:bmp的指针,3:位图宽度,4:位图高度,5:颜色深度   
  38. //return:         int   
  39. //description:    bmp的像素格式为(RGB)   
  40. //===================================================================================   
  41. int savejpeg(char *filename, unsigned char *bits, int width, int height, int depth)  
  42. {  
  43.     struct jpeg_compress_struct cinfo;  
  44.     struct jpeg_error_mgr jerr;  
  45.     FILE * outfile;                 //target file    
  46.     JSAMPROW row_pointer[1];        //pointer to JSAMPLE row[s]    
  47.     int     row_stride;             //physical row width in image buffer    
  48.     cinfo.err = jpeg_std_error(&jerr);  
  49.     jpeg_create_compress(&cinfo);  
  50.   
  51.     if ((outfile = fopen(filename, "wb")) == NULL)  
  52.     {  
  53.         fprintf(stderr, "can't open %s/n", filename);  
  54.         return -1;  
  55.     }  
  56.     jpeg_stdio_dest(&cinfo, outfile);  
  57.     cinfo.image_width = width;      //image width and height, in pixels    
  58.     cinfo.image_height = height;  
  59.     cinfo.input_components = 3;         //# of color components per pixel    
  60.     cinfo.in_color_space = JCS_RGB;         //colorspace of input image    
  61.     jpeg_set_defaults(&cinfo);  
  62.     jpeg_set_quality(&cinfo, JPEG_QUALITY, TRUE );//limit to baseline-JPEG values    
  63.     jpeg_start_compress(&cinfo, TRUE);  
  64.   
  65.     row_stride = width * depth; // JSAMPLEs per row in image_buffer    
  66.     while (cinfo.next_scanline < cinfo.image_height)   
  67.     {  
  68.         //这里我做过修改,由于jpg文件的图像是倒的,所以改了一下读的顺序   
  69.         //这是原代码:   
  70.         //row_pointer[0] = & bits[cinfo.next_scanline * row_stride];   
  71.         row_pointer[0] = & bits[(cinfo.image_height - cinfo.next_scanline - 1) * row_stride];  
  72.         (void) jpeg_write_scanlines(&cinfo, row_pointer, 1);  
  73.     }  
  74.     jpeg_finish_compress(&cinfo);  
  75.     fclose(outfile);  
  76.     jpeg_destroy_compress(&cinfo);  
  77.     return 0;  
  78. }  
  79.   
  80.   
  81. void CapScreen(char filename[])  
  82. {  
  83.     CDC *pDC;  
  84.     pDC = CDC::FromHandle(GetDC(GetDesktopWindow()));  
  85.     if(pDC == NULL) return;  
  86.     int BitPerPixel = pDC->GetDeviceCaps(BITSPIXEL);  
  87.     int Width = pDC->GetDeviceCaps(HORZRES);  
  88.     int Height = pDC->GetDeviceCaps(VERTRES);  
  89.   
  90.     CDC memDC;  
  91.     if(memDC.CreateCompatibleDC(pDC) == 0) return;  
  92.     CBitmap memBitmap, *oldmemBitmap;  
  93.     if(memBitmap.CreateCompatibleBitmap(pDC, Width, Height) == NULL) return;  
  94.     oldmemBitmap = memDC.SelectObject(&memBitmap);  
  95.     if(oldmemBitmap == NULL) return;  
  96.     if(memDC.BitBlt(0, 0, Width, Height, pDC, 0, 0, SRCCOPY) == 0) return;  
  97.   
  98.     BITMAP bmp;  
  99.   
  100.     memBitmap.GetBitmap(&bmp);  
  101.   
  102.     //fp = fopen(filename, "w+b");   
  103.   
  104.     BITMAPINFOHEADER bih = {0};  
  105.     bih.biBitCount = bmp.bmBitsPixel;  
  106.     bih.biCompression = BI_RGB;  
  107.     bih.biHeight = bmp.bmHeight;  
  108.     bih.biPlanes = 1;  
  109.     bih.biSize = sizeof(BITMAPINFOHEADER);  
  110.     bih.biSizeImage = bmp.bmWidthBytes * bmp.bmHeight;  
  111.     bih.biWidth = bmp.bmWidth;  
  112.   
  113.   
  114.     BITMAPFILEHEADER bfh = {0};  
  115.   
  116.     bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);  
  117.     bfh.bfSize = bfh.bfOffBits + bmp.bmWidthBytes * bmp.bmHeight;  
  118.     bfh.bfType = (WORD)0x4d42;  
  119.   
  120.     //fwrite(&bfh, 1, sizeof(BITMAPFILEHEADER), fp);   
  121.   
  122.     //fwrite(&bih, 1, sizeof(BITMAPINFOHEADER), fp);   
  123.   
  124.     image_buffer = new BYTE[bmp.bmWidthBytes * bmp.bmHeight];  
  125.   
  126.     GetDIBits(memDC.m_hDC,  
  127.         (HBITMAP) memBitmap.m_hObject,  
  128.         0,  
  129.         Height,  
  130.         image_buffer,  
  131.         (LPBITMAPINFO) &bih,  
  132.         DIB_RGB_COLORS);  
  133.     memDC.SelectObject(oldmemBitmap);  
  134.     //fwrite(p, 1, 1280 * 800 * 4, fp);    
  135.     //fclose(fp);   
  136. }  
#define JPEG_QUALITY 50     //它的大小决定jpg的质量好坏
//#include <atlbase.h> 
#include <afxwin.h>

extern "C" {
#include "jpeglib.h"
#include "jmorecfg.h"
#include "jconfig.h"
}
int savejpeg(char *filename, unsigned char *bits, int width, int height, int depth);
void CapScreen(char filename[]);


BYTE *image_buffer; //指向位图buffer的全局指针,window下像素格式: BGRA(4个字节)

int main(int argc, char* argv[])
{
	image_buffer = (BYTE *)malloc(1440 * 900 * 4);
	for(int i = 0; i < 100; i++)
	{
		CapScreen("palm.bmp");    
		//RGB顺序调整
		for (int i=0, j=0; j < 1440*900*4; i+=3, j+=4)
		{
			*(image_buffer+i)=*(image_buffer+j+2);
			*(image_buffer+i+1)=*(image_buffer+j+1);
			*(image_buffer+i+2)=*(image_buffer+j);
		}
		savejpeg("ok.jpg", image_buffer, 1440, 900, 3);
	}
	delete [] image_buffer;
	return 0;
}

//===================================================================================
//function:       jpeg压缩
//input:          1:生成的文件名,2:bmp的指针,3:位图宽度,4:位图高度,5:颜色深度
//return:         int
//description:    bmp的像素格式为(RGB)
//===================================================================================
int savejpeg(char *filename, unsigned char *bits, int width, int height, int depth)
{
	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 
	cinfo.err = jpeg_std_error(&jerr);
	jpeg_create_compress(&cinfo);

	if ((outfile = fopen(filename, "wb")) == NULL)
	{
		fprintf(stderr, "can't open %s/n", filename);
		return -1;
	}
	jpeg_stdio_dest(&cinfo, outfile);
	cinfo.image_width = width;      //image width and height, in pixels 
	cinfo.image_height = 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, JPEG_QUALITY, TRUE );//limit to baseline-JPEG values 
	jpeg_start_compress(&cinfo, TRUE);

	row_stride = width * depth; // JSAMPLEs per row in image_buffer 
	while (cinfo.next_scanline < cinfo.image_height) 
	{
		//这里我做过修改,由于jpg文件的图像是倒的,所以改了一下读的顺序
		//这是原代码:
		//row_pointer[0] = & bits[cinfo.next_scanline * row_stride];
		row_pointer[0] = & bits[(cinfo.image_height - cinfo.next_scanline - 1) * row_stride];
		(void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
	}
	jpeg_finish_compress(&cinfo);
	fclose(outfile);
	jpeg_destroy_compress(&cinfo);
	return 0;
}


void CapScreen(char filename[])
{
	CDC *pDC;
	pDC = CDC::FromHandle(GetDC(GetDesktopWindow()));
	if(pDC == NULL) return;
	int BitPerPixel = pDC->GetDeviceCaps(BITSPIXEL);
	int Width = pDC->GetDeviceCaps(HORZRES);
	int Height = pDC->GetDeviceCaps(VERTRES);

	CDC memDC;
	if(memDC.CreateCompatibleDC(pDC) == 0) return;
	CBitmap memBitmap, *oldmemBitmap;
	if(memBitmap.CreateCompatibleBitmap(pDC, Width, Height) == NULL) return;
	oldmemBitmap = memDC.SelectObject(&memBitmap);
	if(oldmemBitmap == NULL) return;
	if(memDC.BitBlt(0, 0, Width, Height, pDC, 0, 0, SRCCOPY) == 0) return;

	BITMAP bmp;

	memBitmap.GetBitmap(&bmp);

	//fp = fopen(filename, "w+b");

	BITMAPINFOHEADER bih = {0};
	bih.biBitCount = bmp.bmBitsPixel;
	bih.biCompression = BI_RGB;
	bih.biHeight = bmp.bmHeight;
	bih.biPlanes = 1;
	bih.biSize = sizeof(BITMAPINFOHEADER);
	bih.biSizeImage = bmp.bmWidthBytes * bmp.bmHeight;
	bih.biWidth = bmp.bmWidth;


	BITMAPFILEHEADER bfh = {0};

	bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
	bfh.bfSize = bfh.bfOffBits + bmp.bmWidthBytes * bmp.bmHeight;
	bfh.bfType = (WORD)0x4d42;

	//fwrite(&bfh, 1, sizeof(BITMAPFILEHEADER), fp);

	//fwrite(&bih, 1, sizeof(BITMAPINFOHEADER), fp);

	image_buffer = new BYTE[bmp.bmWidthBytes * bmp.bmHeight];

	GetDIBits(memDC.m_hDC,
		(HBITMAP) memBitmap.m_hObject,
		0,
		Height,
		image_buffer,
		(LPBITMAPINFO) &bih,
		DIB_RGB_COLORS);
	memDC.SelectObject(oldmemBitmap);
	//fwrite(p, 1, 1280 * 800 * 4, fp); 
	//fclose(fp);
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值