QImageWriter Class

QImageWriter class provides a format independent interface for writing images to files or other devices. 更多...

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

注意: 此类的所有函数 可重入 .

公共类型

enum ImageWriterError { DeviceError, UnsupportedFormatError, InvalidImageError, UnknownError }

公共函数

QImageWriter ()
QImageWriter (QIODevice * device , const QByteArray & format )
QImageWriter (const QString & fileName , const QByteArray & format = QByteArray())
~QImageWriter ()
bool canWrite () const
int compression () const
QIODevice * device () const
QImageWriter::ImageWriterError error () const
QString errorString () const
QString fileName () const
QByteArray format () const
float gamma () 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 setGamma (float gamma )
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 class provides a format independent interface for writing images to files or other devices.

QImageWriter supports setting format specific options, such as the gamma level, compression level and quality, prior to storing the image. If you do not need such options, you can use QImage::save () 或 QPixmap::save () 代替。

To store an image, you start by constructing a QImageWriter object. Pass either a file name or a device pointer, and the image format to QImageWriter 's constructor. You can then set several options, such as the gamma level (by calling setGamma ()) and quality (by calling setQuality ()). canWrite () 返回 true if QImageWriter can write the image (i.e., the image format is supported and the device is open for writing). Call write () to write the image to the device.

If any error occurs when writing the image, write () will return false. You can then call error () to find the type of error that occurred, or errorString () to get a human readable description of what went wrong.

调用 supportedImageFormats () for a list of formats that QImageWriter can write. QImageWriter supports all built-in image formats, in addition to any image format plugins that support writing.

注意: QImageWriter assumes exclusive control over the file or device that is assigned. Any attempts to modify the assigned file or device during the lifetime of the QImageWriter object will yield undefined results. If immediate access to a resource is desired, the use of a scope is the recommended method.

例如:

    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 .

成员类型文档编制

enum QImageWriter:: ImageWriterError

This enum describes errors that can occur when writing images with 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 An attempt was made to write an invalid QImage . An example of an invalid image would be a 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 ()

构造空 QImageWriter object. Before writing, you must call setFormat () to set an image format, then setDevice () 或 setFileName ().

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

构造 QImageWriter object using the device device and image format format .

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

构造 QImageWriter objects that will write to a file with the name fileName , using the image format format 。若 format is not provided, QImageWriter will detect the image format by inspecting the extension of fileName .

QImageWriter:: ~QImageWriter ()

销毁 QImageWriter 对象。

bool QImageWriter:: canWrite () const

返回 true if QImageWriter can write the image; i.e., the image format is supported and the assigned device is open for reading.

另请参阅 write (), setDevice (),和 setFormat ().

int QImageWriter:: compression () const

Returns the compression of the image.

另请参阅 setCompression ().

QIODevice *QImageWriter:: device () const

Returns the device currently assigned to QImageWriter , or 0 if no device has been assigned.

另请参阅 setDevice ().

QImageWriter::ImageWriterError QImageWriter:: error () const

返回最后发生的错误类型。

另请参阅 ImageWriterError and errorString ().

QString QImageWriter:: errorString () const

返回最近发生错误的人类可读描述。

另请参阅 error ().

QString QImageWriter:: fileName () const

If the currently assigned device is a QFile , or if 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

Returns the format QImageWriter uses for writing images.

另请参阅 setFormat ().

float QImageWriter:: gamma () const

Returns the gamma level of the image.

另请参阅 setGamma ().

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

Returns the list of image formats corresponding to mimeType .

注意: QGuiApplication instance must be created before this function is called.

This function was introduced in Qt 5.12.

另请参阅 supportedImageFormats () 和 supportedMimeTypes ().

bool QImageWriter:: optimizedWrite () const

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

This function was introduced in Qt 5.5.

另请参阅 setOptimizedWrite ().

bool QImageWriter:: progressiveScanWrite () const

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

This function was introduced in 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 's device to 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 will create a QFile and open it in QIODevice::WriteOnly mode, and use this file when writing images.

另请参阅 fileName () 和 setDevice ().

void QImageWriter:: setFormat (const QByteArray & format )

Sets the 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 () for the full list of formats QImageWriter supports.

另请参阅 format ().

void QImageWriter:: setGamma ( float gamma )

This is an image format specific function that sets the gamma level of the image to gamma . For image formats that do not support setting the gamma level, this value is ignored.

The value range of gamma depends on the image format. For example, the "png" format supports a gamma range from 0.0 to 1.0.

另请参阅 gamma () 和 quality ().

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。

This function was introduced in 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。

This function was introduced in 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);
							

This function was introduced in Qt 5.4.

另请参阅 subType ().

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

Sets the image text associated with the key 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 ().

Support for this option is implemented through QImageIOHandler::Description .

This function was introduced in 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.

This function was introduced in Qt 5.5.

另请参阅 transformation () 和 write ().

QByteArray QImageWriter:: subType () const

Returns the subtype of the image.

This function was introduced in Qt 5.4.

另请参阅 setSubType ().

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

Returns the list of image formats supported by QImageWriter .

By default, Qt can write the following formats:

Format MIME type 描述
BMP image/bmp Windows Bitmap
JPG image/jpeg Joint Photographic Experts Group
PNG image/png Portable Network Graphics
PBM image/x-portable-bitmap Portable Bitmap
PGM image/x-portable-graymap Portable Graymap
PPM image/x-portable-pixmap Portable Pixmap
XBM image/x-xbitmap X11 Bitmap
XPM image/x-xpixmap X11 Pixmap

Reading and writing SVG files is supported through the Qt SVG 模块。 Qt Image Formats module provides support for additional image formats.

注意: QApplication instance must be created before this function is called.

另请参阅 setFormat (), QImageReader::supportedImageFormats (),和 QImageIOPlugin .

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

Returns the list of MIME types supported by QImageWriter .

注意: QApplication instance must be created before this function is called.

另请参阅 supportedImageFormats () 和 QImageReader::supportedMimeTypes ().

QList < QByteArray > QImageWriter:: supportedSubTypes () const

Returns the list of subtypes supported by an image.

This function was introduced in Qt 5.4.

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

返回 true if the writer supports 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");
							

Options can be tested after the writer has been associated with a format.

This function was introduced in Qt 4.2.

另请参阅 QImageReader::supportsOption () 和 setFormat ().

QImageIOHandler::Transformations QImageWriter:: transformation () const

Returns the transformation and orientation the image has been set to written with.

This function was introduced in Qt 5.5.

另请参阅 setTransformation ().

bool QImageWriter:: write (const QImage & image )

写入图像 image to the assigned device or file name. Returns true 当成功时;否则返回 false . If the operation fails, you can call error () to find the type of error that occurred, or errorString () to get a human readable description of the error.

另请参阅 canWrite (), error (),和 errorString ().