• <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 評論 :: 0 Trackbacks

            一、vc編譯jpeglib庫

            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、進入命令提示環(huán)境下,輸入:vcvars32 回車,這是一個用來設置VC路徑環(huán)境的批處理;

                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個字節(jié)是頭,后面才是像素值。

            二、使用jpeg庫壓縮

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

            1、基本思路

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

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

            A、運行是總是報錯:

            我參考源代碼的例子,也用JSAMPLE * image_buffer;來指向位圖的像素的首地址,編譯可以通過但是運行時就會報錯,后來我用BYTE *image_buffer;來定義就可以正常運行了。

            B、生成的jpg圖像失真:

            由于window的位圖的像素格式是:BGRA4個字節(jié),jpeglib庫使用的是RGB3個字節(jié)的格式,所以需要將源像素去掉其透明字節(jié),同時改變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文件圖像是倒的:

            原因沒有找到,后來修改了壓縮函數(shù)的代碼,生成了正確的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、測試結(jié)果:

            我編寫了測試代碼,連續(xù)截屏并生成jpg文件100次,大約花費7秒左右,也就是說1秒可以截屏13次左右。同時生成的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個字節(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) {
                        //這里我做過修改,由于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 北風之神007 閱讀(10417) 評論(0)  編輯 收藏 引用 所屬分類: c/c++
            久久国产综合精品五月天| 一级A毛片免费观看久久精品| 伊人久久一区二区三区无码| 欧美午夜A∨大片久久| 人妻无码αv中文字幕久久琪琪布| 国产精品久久久久久| 国产精品成人久久久| 久久青青国产| 久久国产成人精品国产成人亚洲| 久久国产精品无码一区二区三区| 国产精品久久久久久久app| 久久精品国产99久久无毒不卡 | 久久99这里只有精品国产| 久久久亚洲裙底偷窥综合| 久久伊人精品青青草原日本| 亚洲国产精品无码久久| 亚洲国产精品无码久久一线| 亚洲一本综合久久| 久久精品国产99久久丝袜| 亚洲va久久久噜噜噜久久狠狠 | 激情五月综合综合久久69| 东方aⅴ免费观看久久av| 精品综合久久久久久98| 国产成人精品久久综合| 狠狠色综合久久久久尤物| 久久精品水蜜桃av综合天堂 | 狠狠88综合久久久久综合网| 久久丫精品国产亚洲av| 怡红院日本一道日本久久| 国产精品久久波多野结衣| 国产成人久久精品一区二区三区| 日本高清无卡码一区二区久久| 精品99久久aaa一级毛片| 日本三级久久网| 性做久久久久久免费观看| 国产AV影片久久久久久| 精品国产婷婷久久久| 精品久久久久久无码人妻蜜桃| 中文字幕亚洲综合久久2| 久久精品国产一区二区三区不卡| 国内精品免费久久影院|