Qt 圖片處理QImageReader QImageWriter QPixmap QImage
Qt中的圖片相關,常用的幾個類就是QImageReader QImageWriter QPixmap QImage。
一 QImageReader
1、QImageReader提供了獨立介面用來從檔案或者裝置讀取圖片。
2、有一些實用的控制介面,例如setScaledSize()、setClipRect()等。
3、可以通過supportedImageFormats()檢視QImageReader支援的圖片格式。
4、在讀取之前常用的兩個函式
setDecideFormatFromContent(bool ignored)
文件說明如下:
If ignored is set to true, then the image reader will ignore specified formats or file extensions and decide which plugin to use only based on the contents in the datastream.
Setting this flag means that all image plugins gets loaded. Each plugin will read the first bytes in the image data and decide if the plugin is compatible or not.
This also disables auto detecting the image format.
解釋:如果設定為真,則忽略指定格式或檔案字尾,根據內容決定使用哪個圖片外掛,也意味著所有的圖片外掛就要載入進來,此設定會使自動探測圖片格式設定失效。
canRead()
文件說明如下:
Returns true if an image can be read for the device (i.e., the image format is supported, and the device seems to contain valid data); otherwise returns false.
canRead() is a lightweight function that only does a quick test to see if the image data is valid. read() may still return false after canRead() returns true, if the image data is corrupt.
For images that support animation, canRead() returns false when all frames have been read.
注意返回false的場景。
5、QImageReader有兩個過載的read()函式可以得到QImage
二 QImage
1、可以從檔案讀取圖片,可以從建構函式或者load()函式傳入檔案路徑。
2、有畫素、灰度操作、映象、位深、縮放、顏色等操作,圖片處理函式比較齊全。
三 QPixmap
1、可以從檔案讀取圖片,可以從建構函式或者load()函式傳入檔案路徑。
2、也有圖片處理操作例如縮放等,但一般處理圖片QImage用的更多些,QPixmap更多用於展示。
3、有兩個靜態函式,可以從上面轉換而來
QPixmap::fromImageReader
QPixmap::fromImage
4、還有 toImage() 函式。
四 QImageWriter
寫圖片,QImage 和 QPixmap save()函式也可以儲存圖片。
五、顯示
例如QLabel有setPixmap(const QPixmap &)函式。
六、處理圖片資料主要類
主要有Image, QPixmap, QBitmap and QPicture四個,可以根據需求不同靈活選擇。
文件說明如下:
Qt provides four classes for handling image data: QImage, QPixmap, QBitmap and QPicture. QImage is designed and optimized for I/O, and for direct pixel access and manipulation, while QPixmap is designed and optimized for showing images on screen. QBitmap is only a convenience class that inherits QPixmap, ensuring a depth of 1. The isQBitmap() function returns true if a QPixmap object is really a bitmap, otherwise returns false. Finally, the QPicture class is a paint device that records and replays QPainter commands.