使用jpeglib库实现bmp转jpg

简介: 一、vc编译jpeglib库 1、下载源代码 下载地址:http://www.ijg.org/。注意:一定要下载win32 版本 2、编译源代码.     A、解压源代码,修改源代码中jconfig.

一、vc编译jpeglib

1、下载源代码

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

2、编译源代码.

    A、解压源代码,修改源代码中jconfig.vcjconfig.h

    B、添加环境变量PATHC:/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

3jpeglibVC下使用

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

         将这几个文件拷贝到你的项目中:libjpeg.libjconfig.hjmorecfg.hjpeglib.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的位图的像素格式是:BGRA4个字节,jpeglib库使用的是RGB3个字节的格式,所以需要将源像素去掉其透明字节,同时改变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文件有100K的样子。

三、代码:

#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

目录
相关文章
|
监控 API C++
利用C++构建PC端监控软件:实时屏幕录制
在今天的数字化世界中,监控软件变得越来越重要。无论是家庭用户需要监控他们的孩子,还是企业需要监控员工的电脑活动,实时屏幕录制是一种有效的方法。本文将向您介绍如何使用C++构建PC端监控软件,实现实时屏幕录制功能。此外,我们还将讨论如何自动提交监控到的数据到一个网站上。
750 0
|
存储 机器学习/深度学习 Python
NumPy 高级教程——存储和加载数据
NumPy 高级教程——存储和加载数据 【1月更文挑战第1篇】
368 3
NumPy 高级教程——存储和加载数据
|
7月前
|
存储 监控 算法
员工屏幕监控系统之 C++ 图像差分算法
在现代企业管理中,员工屏幕监控系统至关重要。本文探讨了其中常用的图像差分算法,该算法通过比较相邻两帧图像的像素差异,检测屏幕内容变化,如应用程序切换等。文中提供了C++实现代码,并介绍了其在实时监控、异常行为检测和数据压缩等方面的应用,展示了其实现简单、效率高的特点。
168 15
|
11月前
|
存储 自然语言处理 关系型数据库
MySQL 自定义变量并声明字符编码
MySQL 自定义变量并声明字符编码
367 1
|
Unix Linux 测试技术
Linux系统之wget命令的基本使用
【8月更文挑战第4天】Linux系统之wget命令的基本使用
534 5
|
关系型数据库 MySQL 数据库
【MySQL】MySQL数据库的delete from table和truncate table之间的区别
【MySQL】MySQL数据库的delete from table和truncate table之间的区别
1295 1
|
11月前
|
Linux Docker 容器
docker启动完美容器的过程
本文详细介绍了使用Docker创建和管理容器的过程,包括拉取镜像、搜索镜像、创建容器、启动、停止、删除容器,以及查看容器日志和进程信息的常用命令。
538 2
|
算法 C++ 容器
【C++进阶(四)】STL大法--list深度剖析&list迭代器问题探讨
【C++进阶(四)】STL大法--list深度剖析&list迭代器问题探讨
|
安全 PHP
【攻防世界】file_include (PHP伪协议+过滤器)
【攻防世界】file_include (PHP伪协议+过滤器)
|
Windows
windows 使用自带的cmd终端进行文件MD5校验
windows 使用自带的cmd终端进行文件MD5校验
1500 0