QImageWriter 類

QImageWriter 類提供的格式獨立於將圖像寫入文件 (或其它設備) 的接口。 更多...

頭: #include <QImageWriter>
qmake: QT += gui

注意: 此類的所有函數 可重入 .

公共類型

enum ImageWriterError { DeviceError, UnsupportedFormatError, InvalidImageError, UnknownError }

公共函數

QImageWriter (const QString & fileName , const QByteArray & format = QByteArray())
QImageWriter (QIODevice * device , const QByteArray & format )
QImageWriter ()
~QImageWriter ()
bool canWrite () const
int compression () const
QIODevice * device () const
QImageWriter::ImageWriterError error () const
QString errorString () const
QString fileName () const
QByteArray format () const
bool optimizedWrite () const
bool progressiveScanWrite () const
int quality () const
void setCompression (int compression )
void setDevice (QIODevice * device )
void setFileName (const QString & fileName )
void setFormat (const QByteArray & format )
void setOptimizedWrite (bool optimize )
void setProgressiveScanWrite (bool progressive )
void setQuality (int quality )
void setSubType (const QByteArray & type )
void setText (const QString & key , const QString & text )
void setTransformation (QImageIOHandler::Transformations transform )
QByteArray subType () const
QList<QByteArray> supportedSubTypes () const
bool supportsOption (QImageIOHandler::ImageOption option ) const
QImageIOHandler::Transformations transformation () const
bool write (const QImage & image )

靜態公共成員

QList<QByteArray> imageFormatsForMimeType (const QByteArray & mimeType )
QList<QByteArray> supportedImageFormats ()
QList<QByteArray> supportedMimeTypes ()

詳細描述

QImageWriter 支持設置特定格式選項 (譬如:壓縮級彆和品質),在存儲圖像之前。若不需要這種選項,可以使用 QImage::save () 或 QPixmap::save () 代替。

要存儲圖像,從構造 QImageWriter 對象開始。將文件名 (或設備指針) 及圖像格式,傳遞給 QImageWriter 構造函數。然後,可以設置幾個選項,譬如品質 (通過調用 setQuality ()). canWrite () 返迴 true 若 QImageWriter 可以寫入圖像 (即:支持圖像格式,並為寫入打開設備)。調用 write () 能將圖像寫入設備。

若齣現任何錯誤當寫入圖像時, write () 將返迴 false。然後,可以調用 error () 以查找齣現的錯誤類型,或 errorString () 以獲得人類可讀的什麼錯誤描述。

調用 supportedImageFormats () 瞭解 QImageWriter 可以寫入的格式列錶。QImageWriter 支持所有內置圖像格式,除支持寫入的任何圖像格式插件外。

注意: QImageWriter 假定獨占控製要賦值的文件 (或設備)。在 QImageWriter 對象的壽命期間,對賦值文件 (或設備) 的任何修改嘗試都會産生未定義結果。若期望立即訪問資源,推薦方法是使用作用域。

例如:

    QString imagePath(QStringLiteral("path/image.jpeg"));
    QImage image(64, 64, QImage::Format_RGB32);
    image.fill(Qt::red);
    {
        QImageWriter writer(imagePath);
        writer.write(image);
    }
    QFile::rename(imagePath,
                  QStringLiteral("path/other_image.jpeg"));
					

另請參閱 QImageReader , QImageIOHandler , QImageIOPlugin ,和 QColorSpace .

成員類型文檔編製

enum QImageWriter:: ImageWriterError

此枚舉描述可以齣現的錯誤, 當寫入圖像采用 QImageWriter .

常量 描述
QImageWriter::DeviceError 1 QImageWriter encountered a device error when writing the image data. Consult your device for more details on what went wrong.
QImageWriter::UnsupportedFormatError 2 Qt 不支持請求的圖像格式。
QImageWriter::InvalidImageError 3 試圖寫入無效 QImage 。無效圖像的範例是 null QImage .
QImageWriter::UnknownError 0 An unknown error occurred. If you get this value after calling write (), it is most likely caused by a bug in QImageWriter .

成員函數文檔編製

QImageWriter:: QImageWriter (const QString & fileName , const QByteArray & format = QByteArray())

構造將寫入文件的 QImageWriter 對象采用名稱 fileName ,使用圖像格式 format 。若 format 不提供,QImageWriter 將檢測圖像格式通過審查擴展名對於 fileName .

QImageWriter:: QImageWriter ( QIODevice * device , const QByteArray & format )

構造 QImageWriter 對象使用設備 device 和圖像格式 format .

QImageWriter:: QImageWriter ()

構造空 QImageWriter 對象。寫入前,必須調用 setFormat () 來設置圖像格式,然後 setDevice () 或 setFileName ().

QImageWriter:: ~QImageWriter ()

銷毀 QImageWriter 對象。

bool QImageWriter:: canWrite () const

返迴 true if QImageWriter 可以寫入圖像;即:圖像格式支持且為讀取打開賦值設備。

另請參閱 write (), setDevice (),和 setFormat ().

int QImageWriter:: compression () const

返迴圖像的壓縮。

另請參閱 setCompression ().

QIODevice *QImageWriter:: device () const

返迴的設備目前被賦值給 QImageWriter ,或 nullptr 若沒有設備被賦值。

另請參閱 setDevice ().

QImageWriter::ImageWriterError QImageWriter:: error () const

返迴最後發生的錯誤類型。

另請參閱 ImageWriterError and errorString ().

QString QImageWriter:: errorString () const

返迴最近發生錯誤的人類可讀描述。

另請參閱 error ().

QString QImageWriter:: fileName () const

若目前賦值設備是 QFile ,或者若 setFileName () has been called, this function returns the name of the file QImageWriter writes to. Otherwise (i.e., if no device has been assigned or the device is not a QFile ), an empty QString 被返迴。

另請參閱 setFileName () 和 setDevice ().

QByteArray QImageWriter:: format () const

返迴格式 QImageWriter 用於寫入圖像。

另請參閱 setFormat ().

[static] QList < QByteArray > QImageWriter:: imageFormatsForMimeType (const QByteArray & mimeType )

Returns the list of image formats corresponding to mimeType .

注意, QGuiApplication 實例必須被創建在調用此函數之前。

該函數在 Qt 5.12 引入。

另請參閱 supportedImageFormats () 和 supportedMimeTypes ().

bool QImageWriter:: optimizedWrite () const

Returns whether optimization has been turned on for writing the image.

該函數在 Qt 5.5 引入。

另請參閱 setOptimizedWrite ().

bool QImageWriter:: progressiveScanWrite () const

Returns whether the image should be written as a progressive image.

該函數在 Qt 5.5 引入。

另請參閱 setProgressiveScanWrite ().

int QImageWriter:: quality () const

返迴圖像格式的品質設置。

另請參閱 setQuality ().

void QImageWriter:: setCompression ( int compression )

This is an image format specific function that set the compression of an image. For image formats that do not support setting the compression, this value is ignored.

The value range of compression depends on the image format. For example, the "tiff" format supports two values, 0(no compression) and 1(LZW-compression).

另請參閱 compression ().

void QImageWriter:: setDevice ( QIODevice * device )

設置 QImageWriter 的設備到 device . If a device has already been set, the old device is removed from QImageWriter and is otherwise left unchanged.

若設備尚未打開, QImageWriter will attempt to open the device in QIODevice::WriteOnly mode by calling open(). Note that this does not work for certain devices, such as QProcess , QTcpSocket and QUdpSocket , where more logic is required to open the device.

另請參閱 device () 和 setFileName ().

void QImageWriter:: setFileName (const QString & fileName )

Sets the file name of QImageWriter to fileName . Internally, QImageWriter 將創建 QFile and open it in QIODevice::WriteOnly mode, and use this file when writing images.

另請參閱 fileName () 和 setDevice ().

void QImageWriter:: setFormat (const QByteArray & format )

設置格式 QImageWriter will use when writing images, to format . format is a case insensitive text string. Example:

QImageWriter writer;
writer.setFormat("png"); // same as writer.setFormat("PNG");
					

可以調用 supportedImageFormats () 瞭解完整格式列錶 QImageWriter 支持。

另請參閱 format ().

void QImageWriter:: setOptimizedWrite ( bool optimize )

This is an image format-specific function which sets the optimize flags when writing images. For image formats that do not support setting an optimize flag, this value is ignored.

默認為 false。

該函數在 Qt 5.5 引入。

另請參閱 optimizedWrite ().

void QImageWriter:: setProgressiveScanWrite ( bool progressive )

This is an image format-specific function which turns on progressive scanning when writing images. For image formats that do not support setting a progressive scan flag, this value is ignored.

默認為 false。

該函數在 Qt 5.5 引入。

另請參閱 progressiveScanWrite ().

void QImageWriter:: setQuality ( int quality )

將圖像格式的品質設置設為 quality .

Some image formats, in particular lossy ones, entail a tradeoff between a) visual quality of the resulting image, and b) encoding execution time and compression level. This function sets the level of that tradeoff for image formats that support it. For other formats, this value is ignored.

The value range of quality depends on the image format. For example, the "jpeg" format supports a quality range from 0 (low visual quality, high compression) to 100 (high visual quality, low compression).

另請參閱 quality ().

void QImageWriter:: setSubType (const QByteArray & type )

This is an image format specific function that sets the subtype of the image to type . Subtype can be used by a handler to determine which format it should use while saving the image.

For example, saving an image in DDS format with A8R8G8R8 subtype:

QImageWriter writer("some/image.dds");
if (writer.supportsOption(QImageIOHandler::SubType))
    writer.setSubType("A8R8G8B8");
writer.write(image);
					

該函數在 Qt 5.4 引入。

另請參閱 subType ().

void QImageWriter:: setText (const QString & key , const QString & text )

設置圖像文本關聯鍵 key to text . This is useful for storing copyright information or other information about the image. Example:

QImage image("some/image.jpeg");
QImageWriter writer("images/outimage.png", "png");
writer.setText("Author", "John Smith");
writer.write(image);
					

If you want to store a single block of data (e.g., a comment), you can pass an empty key, or use a generic key like "Description".

The key and text will be embedded into the image data after calling write ().

支持此選項的實現是透過 QImageIOHandler::Description .

該函數在 Qt 4.1 引入。

另請參閱 QImage::setText () 和 QImageReader::text ().

void QImageWriter:: setTransformation ( QImageIOHandler::Transformations transform )

Sets the image transformations metadata including orientation to transform .

If transformation metadata is not supported by the image format, the transform is applied before writing.

該函數在 Qt 5.5 引入。

另請參閱 transformation () 和 write ().

QByteArray QImageWriter:: subType () const

返迴圖像的子類型。

該函數在 Qt 5.4 引入。

另請參閱 setSubType ().

[static] QList < QByteArray > QImageWriter:: supportedImageFormats ()

返迴圖像格式列錶支持通過 QImageWriter .

默認情況下,Qt 可以寫入下列格式:

格式 MIME 類型 描述
BMP image/bmp Windows 位圖
JPG image/jpeg JPEG (聯閤攝影專傢組)
PNG image/png PNG (便攜式網絡圖形)
PBM image/x-portable-bitmap PBM (便攜式位圖)
PGM image/x-portable-graymap PGM (便攜式灰度圖)
PPM image/x-portable-pixmap Portable Pixmap (便攜式像素圖)
XBM image/x-xbitmap X11 Bitmap (X11 位圖)
XPM image/x-xpixmap X11 Pixmap (X11 像素圖)

讀寫 SVG 文件的支持是透過 Qt SVG 模塊。 Qt Image Formats 模塊為額外圖像格式提供支持。

注意, QApplication 實例必須被創建在調用此函數之前。

另請參閱 setFormat (), QImageReader::supportedImageFormats (),和 QImageIOPlugin .

[static] QList < QByteArray > QImageWriter:: supportedMimeTypes ()

返迴 MIME (多用途 Internet 郵件擴展) 類型列錶支持通過 QImageWriter .

注意, QApplication 實例必須被創建在調用此函數之前。

另請參閱 supportedImageFormats () 和 QImageReader::supportedMimeTypes ().

QList < QByteArray > QImageWriter:: supportedSubTypes () const

返迴由圖像所支持的子類型列錶。

該函數在 Qt 5.4 引入。

bool QImageWriter:: supportsOption ( QImageIOHandler::ImageOption option ) const

返迴 true 若寫入器支持 option ;否則返迴 false。

Different image formats support different options. Call this function to determine whether a certain option is supported by the current format. For example, the PNG format allows you to embed text into the image's metadata (see text()).

QImageWriter writer(fileName);
if (writer.supportsOption(QImageIOHandler::Description))
    writer.setText("Author", "John Smith");
					

可以被測試 option,在寫入器已關聯格式後。

該函數在 Qt 4.2 引入。

另請參閱 QImageReader::supportsOption () 和 setFormat ().

QImageIOHandler::Transformations QImageWriter:: transformation () const

返迴已設置為寫入圖像的變換和取嚮。

該函數在 Qt 5.5 引入。

另請參閱 setTransformation ().

bool QImageWriter:: write (const QImage & image )

寫入圖像 image 到賦值設備 (或文件名)。返迴 true 當成功時;否則返迴 false 。若操作失敗,可以調用 error () 以查找齣現的錯誤類型,或 errorString () 以獲取人類可讀錯誤描述。

另請參閱 canWrite (), error (),和 errorString ().