一、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的顺序。代码如下:

//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文件

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的样子。

三、代码:

#include "stdafx.h"

#include <atlbase.h>

#include <afxwin.h>

#include <WINDOWSX.H>

#define JPEG_QUALITY 50     //它的大小决定jpg的质量好坏

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("ok.bmp");

//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);

}

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);

}

from:http://blog.csdn.net/huxiangyang4/article/details/5728888

使用jpeglib库实现bmp转jpg的更多相关文章

  1. Linux 下V4l2摄像头采集图片,实现yuyv转RGB,RGB转BMP,RGB伸缩,jpeglib 库实现压缩RGB到内存中,JPEG经UDP发送功(转)

    ./configure CC=arm-linux-gnueabihf-gcc LD=arm-linux-gnueabihf-ld --host=arm-linux --prefix=/usr/loca ...

  2. 【Linux开发】Linux下jpeglib库的安装详解

    Linux下jpeglib库的安装详解 首先要下载所需的库压缩包:jpegsrc.v6b.tar.gz或 jpegsrc.v8b.tar.gz 然后将下载的压缩包随便放在和解压到你喜欢的地方. # t ...

  3. 几个关于JPEGLIB库的博客

    1.http://blog.csdn.net/huxiangyang4/archive/2010/07/12/5728888.aspx 我认为是最好的 2.http://blog.csdn.net/a ...

  4. Linux jpeglib库的安装

    tar -zxvf jpegsrc.v9.tar.gz cd jpeg9 ./configure --enable-shared  --enable-static 分别对动态链接库和静态链接库的支持 ...

  5. jpeglib使用指南

    您可以到http://www.ijg.org/网站下载libjpeg的源码, IJG JPEG Library就是jpeg压缩库,是以源码的形式提供给软件开发人员的,当然在软件包里也有编译好的库文件, ...

  6. 【Linux开发】jpeglib使用指南

    您可以到www.ijg.org网站下载libjpeg的源码, IJG JPEG Library就是jpeg压缩库,是以源码的形式提供给软件开发人员的,当然在软件包里也有编译好的库文件,我们这里就只用到 ...

  7. 立体匹配:关于Middlebury提供的源码的简化使用

    Middlebury提供的源码,虽然花了不到一个小时就运行起来啦.但说实话,它那循环读取脚本命令来执行算法真是让我费了不少头脑,花了近三天时间,我才弄明白了它的运行机制.你说,我就想提取一下算法,你给 ...

  8. CentOS6.8配置GO语言开发环境

    Go语言是谷歌2009发布的第二款开源编程语言,Go语言专门针对多处理器系统应用程序的编程进行了优化,使用Go编译的程序可以媲美C或C++代码的速度,而且更加安全.支持并行进程. 鉴于原来越多的开源项 ...

  9. 如何编写go代码

    go是一种静态编译型的语言,它的编译速度非常快. go的官方编译器称为gc,包括编译工具5g,6g和8g,连接工具5l,6l和8l.其中的数字表示处理器的架构.我们不必关心如何挑选这些工具,因为go提 ...

随机推荐

  1. xstream对象xml互转

    1.引入jar包 xpp3_min-1.1.4c.jarxstream-1.4.8.jar 2.建立java bean package com.jdw.bean; import java.util.A ...

  2. ServletConfig和ServletContext

    ServletConfig和ServletContext Servlet初始化参数 在Servlet的配置文件web.xml中,可以使用一个或多个<init-param>标签为Servle ...

  3. python学习之day13

    目录 JavaScript Dom jQuery   JavaScript JavaScript 是世界上最流行的编程语言. 这门语言可用于 HTML 和 web,更可广泛用于服务器.PC.笔记本电脑 ...

  4. boost库的安装,使用,介绍,库分类

    1)首先去官网下载boost源码安装包:http://www.boost.org/ 选择下载对应的boost源码包.本次下载使用的是 boost_1_60_0.tar.gz (2)解压文件:tar - ...

  5. cf459C Pashmak and Buses

    C. Pashmak and Buses time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. C++中的动态类型与动态绑定、虚函数、运行时多态的实现

    动态类型与静态类型 静态类型 是指不需要考虑表达式的执行期语义,仅分析程序文本而决定的表达式类型.静态类型仅依赖于包含表达式的程序文本的形式,而在程序运行时不会改变.通俗的讲,就是上下文无关,在编译时 ...

  7. 新手学习.net编程计划-1

    .NET是一个庞大的学习体系,对于新手来说会感觉无从下手.学习知识必须从入门的基础学起,才能更好地掌握.学习.net也是如此,最基础的莫过于了解.net平台,以及掌握.net的基础语法C#. 本计划是 ...

  8. Codeforces Round #258 (Div. 2/B)/Codeforces451B_Sort the Array

    解题报告 http://blog.csdn.net/juncoder/article/details/38102391 对于给定的数组,取对数组中的一段进行翻转,问翻转后是否是递增有序的. 思路: 仅 ...

  9. Android(一)

    Android Activity TextView,Button 1.在fragment_main.xml文件中直接添加控件 2.在MainActivity.java文件中添加TextView控件 在 ...

  10. URL传参中文乱码encodeURI、UrlDecode

    传递参数  encodeURI("url.aspx?str"+"汉字")-----------(是 URi  不是URL) 后台接收参数  Server.Url ...