<menu id="w8yyk"><menu id="w8yyk"></menu></menu>
  • <dd id="w8yyk"><nav id="w8yyk"></nav></dd>
    <menu id="w8yyk"></menu>
    <menu id="w8yyk"><code id="w8yyk"></code></menu>
    <menu id="w8yyk"></menu>
    <xmp id="w8yyk">
    <xmp id="w8yyk"><nav id="w8yyk"></nav>
  • 網站首頁 > 物聯資訊 > 技術分享

    使用jpeglib庫實現bmp轉jpg

    2016-09-28 00:00:00 廣州睿豐德信息科技有限公司 閱讀
    睿豐德科技 專注RFID識別技術和條碼識別技術與管理軟件的集成項目。質量追溯系統、MES系統、金蝶與條碼系統對接、用友與條碼系統對接

    一、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

    RFID管理系統集成商 RFID中間件 條碼系統中間層 物聯網軟件集成
    最近免费观看高清韩国日本大全