• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            程序讓生活更美好

            半畝方塘 天光云影

              C++博客 ::  :: 新隨筆 :: 聯(lián)系 ::  :: 管理 ::
              55 隨筆 :: 4 文章 :: 202 評(píng)論 :: 0 Trackbacks

            一、vc編譯jpeglib庫(kù)

            1、下載源代碼

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

            2、編譯源代碼.

                A、解壓源代碼,修改源代碼中jconfig.vcjconfig.h

                B、添加環(huán)境變量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、進(jìn)入命令提示環(huán)境下,輸入:vcvars32 回車,這是一個(gè)用來(lái)設(shè)置VC路徑環(huán)境的批處理;

                E、編譯生成庫(kù)文件 命令:nmake /f makefile.vc nodebug=1

            這樣就OK

            3jpeglib庫(kù)VC下使用

            對(duì)于庫(kù)的使用只需要有相應(yīng)的.lib文件和頭文件就可以了。Vc中要添加libjpeg.lib庫(kù)的連接

                     將這幾個(gè)文件拷貝到你的項(xiàng)目中:libjpeg.libjconfig.hjmorecfg.hjpeglib.h,在你需要進(jìn)行壓縮的文件中加入

            extern "C" {

                     #include "jpeglib.h"

                     #include "jmorecfg.h"

                     #include "jconfig.h"

            }

            參考:

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

            小知識(shí):bmp文件的前54個(gè)字節(jié)是頭,后面才是像素值。

            二、使用jpeg庫(kù)壓縮

            在源代碼的文件夾下面有一個(gè)example.c的文件和libjpeg.txt,很有參考價(jià)值。

            1、基本思路

            首先調(diào)用截屏程序,將屏幕的位圖信息存放在一個(gè)buffer里面,然后調(diào)用jpg壓縮函數(shù),在當(dāng)前的目錄下生成一個(gè)ok.jpg的文件。

            2、出現(xiàn)的問(wèn)題:

            A、運(yùn)行是總是報(bào)錯(cuò):

            我參考源代碼的例子,也用JSAMPLE * image_buffer;來(lái)指向位圖的像素的首地址,編譯可以通過(guò)但是運(yùn)行時(shí)就會(huì)報(bào)錯(cuò),后來(lái)我用BYTE *image_buffer;來(lái)定義就可以正常運(yùn)行了。

            B、生成的jpg圖像失真:

            由于window的位圖的像素格式是:BGRA4個(gè)字節(jié),jpeglib庫(kù)使用的是RGB3個(gè)字節(jié)的格式,所以需要將源像素去掉其透明字節(jié),同時(shí)改變RGB的順序。代碼如下:

            //RGB順序調(diào)整

            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文件圖像是倒的:

            原因沒(méi)有找到,后來(lái)修改了壓縮函數(shù)的代碼,生成了正確的jpg文件

            while (cinfo.next_scanline < cinfo.image_height) {

                     //這里我做過(guò)修改,由于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、測(cè)試結(jié)果:

            我編寫了測(cè)試代碼,連續(xù)截屏并生成jpg文件100次,大約花費(fèi)7秒左右,也就是說(shuō)1秒可以截屏13次左右。同時(shí)生成的jpg文件有100K的樣子。

            三、代碼:

             

            #include "stdafx.h"
            #include <atlbase.h>
            #include <afxwin.h>
            #include <WINDOWSX.H>
             
            #define JPEG_QUALITY 50     //它的大小決定jpg的質(zhì)量好壞
             
            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個(gè)字節(jié))
             
            int main(int argc, char* argv[])
            {
             
                image_buffer = (BYTE *)malloc(1440 * 900 * 4);
             
                for(int i = 0; i < 100; i++){
                    CapScreen("ok.bmp");   
                    //RGB順序調(diào)整
                    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) {
                        //這里我做過(guò)修改,由于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);
            }
             

             

            posted on 2012-02-22 11:04 北風(fēng)之神007 閱讀(10418) 評(píng)論(0)  編輯 收藏 引用 所屬分類: c/c++
            国产毛片欧美毛片久久久| 久久精品国产免费一区| 久久免费美女视频| 99久久er这里只有精品18| 国产亚洲美女精品久久久2020| 日韩精品无码久久一区二区三| 久久综合综合久久狠狠狠97色88| 99久久免费国产精品热| 91精品国产高清久久久久久io| 韩国免费A级毛片久久| 久久久精品人妻一区二区三区蜜桃| 精品国产青草久久久久福利| 2021最新久久久视精品爱| 久久中文字幕人妻熟av女| 伊人久久久AV老熟妇色| 久久精品人人做人人爽97| 丁香狠狠色婷婷久久综合| 97超级碰碰碰碰久久久久| 久久一区二区三区免费| 国产精品99久久久精品无码| 亚洲精品乱码久久久久久久久久久久| 久久婷婷五月综合成人D啪| 精品久久久久久国产潘金莲| 久久免费视频观看| 久久久亚洲AV波多野结衣| 9久久9久久精品| 日日狠狠久久偷偷色综合免费| 亚洲人成精品久久久久| 免费国产99久久久香蕉| 亚洲伊人久久成综合人影院 | 久久久久久国产精品无码超碰| 99久久99这里只有免费费精品| 久久精品成人免费观看97| 久久久久se色偷偷亚洲精品av| 久久综合综合久久综合| 久久99精品久久久久久噜噜| 久久亚洲AV成人无码国产| 久久人人爽人人澡人人高潮AV| 久久久久久夜精品精品免费啦| 精品久久人人做人人爽综合| 东京热TOKYO综合久久精品|