2010-04-06 98 views
3

有誰知道如何打開一個圖像,特別是一個JPG,以C或C + +字節數組?任何形式的幫助表示讚賞。在C中將圖像轉換爲可用的字節數組?

謝謝!

+1

什麼樣的字節陣列?像素顏色的字節數組? – glebm 2010-04-06 00:07:14

+0

有什麼意義?一個圖像只在某個地方繪製像素時纔有意思。字節很無聊。如果必須使用FileStream + byte [(int)fs.Length]。 – 2010-04-06 00:18:08

回答

1

你可以試試DevIL Image Library我只使用它與OpenGL相關的東西,但它也只是一個普通的圖像加載庫。

1

查看wxWidgets GUI Framework中wxImage的源代碼。你很可能會對* nix發行版感興趣。

另一種選擇是GNU Jpeg庫。

2

ImageMagick庫也可以做到這一點,雖然它經常提供足夠的圖像處理函數,您可以做很多事情,而無需將圖像轉換爲字節數組並自行處理。

+0

從簡介:「僅向精靈級程序員推薦」o_O – DerMike 2011-09-14 12:12:47

+1

@DerMike:這只是爲了讓初學者對自己感覺更好,實在不那麼難:-) – Malvineous 2011-09-16 02:33:07

0

我讓我的學生使用netpbm來表示圖像,因爲它帶有一個方便的C庫,但您也可以將圖像轉換爲文本形式,手動創建它們等等。這裏的好處在於,您可以使用命令行工具Unix方式將各種圖像(不只是JPEG)轉換爲PBM格式。 djpeg工具可用於許多地方,包括JPEG Club。經驗相對較少的學生可以使用這種格式編寫一些相當複雜的程序。

0

這是我將如何使用GDIPlus在頭GdiPlusBitmap.h定義Bitmap.LockBits方法做到這一點:

Gdiplus::BitmapData bitmapData; 
    Gdiplus::Rect rect(0, 0, bitmap.GetWidth(), bitmap.GetHeight()); 

    //get the bitmap data 
    if(Gdiplus::Ok == bitmap.LockBits(
         &rect, //A rectangle structure that specifies the portion of the Bitmap to lock. 
         Gdiplus::ImageLockModeRead | Gdiplus::ImageLockModeWrite, //ImageLockMode values that specifies the access level (read/write) for the Bitmap.    
         bitmap.GetPixelFormat(),// PixelFormat values that specifies the data format of the Bitmap. 
         &bitmapData //BitmapData that will contain the information about the lock operation. 
         )) 
    { 
     //get the lenght of the bitmap data in bytes 
     int len = bitmapData.Height * std::abs(bitmapData.Stride); 

     BYTE* buffer = new BYTE[len]; 
     memcpy(bitmapData.Scan0, buffer, len);//copy it to an array of BYTEs 

     //... 

     //cleanup 
     pBitmapImageRot.UnlockBits(&bitmapData);  
     delete []buffer; 
    } 
相關問題