QImageWriter 类

The QImageWriter 类提供用于将图像写入文件 (或其它设备) 的格式独立接口。 更多...

头: #include <QImageWriter>

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

公共类型

enum ImageWriterError { DeviceError, UnsupportedFormatError, 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
ImageWriterError error () const
QString errorString () const
QString fileName () const
QByteArray format () const
float gamma () 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 setQuality (int quality )
void setText (const QString & key , const QString & text )
bool supportsOption (QImageIOHandler::ImageOption option ) const
bool write (const QImage & image )

静态公共成员

QList<QByteArray> supportedImageFormats ()

详细描述

The QImageWriter 类提供用于将图像写入文件 (或其它设备) 的格式独立接口。

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 () returns true if QImageWriter can write the image (i.e., the image format is supported and the device is open for writing). Call write () 能将图像写入设备。

若出现任何错误当写入图像时, write () 将返回 false。然后,可以调用 error () 以查找出现的错误类型,或 errorString () 以获得人类可读的什么错误描述。

调用 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.

另请参阅 QImageReader , QImageIOHandler ,和 QImageIOPlugin .

成员类型文档编制

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::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 () 来设置图像格式,然后 setDevice () 或 setFileName ().

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

构造 QImageWriter 对象使用设备 device 和图像格式 format .

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

构造 QImageWriter objects that will write to a file with the name fileName ,使用图像格式 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 若 QImageWriter 可以写入图像;即:图像格式支持且为读取打开赋值设备。

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

int QImageWriter:: compression () const

返回图像的压缩。

另请参阅 setCompression ().

QIODevice * QImageWriter:: device () const

返回的设备目前被赋值给 QImageWriter , or 0 if no device has been assigned.

另请参阅 setDevice ().

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 ().

float QImageWriter:: gamma () const

Returns the gamma level of the image.

另请参阅 setGamma ().

int QImageWriter:: quality () const

Returns the quality level of the image.

另请参阅 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:: 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:: setQuality ( int quality )

This is an image format specific function that sets the quality level of the image to quality . For image formats that do not support setting the quality, 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 quality, high compression) to 100 (high quality, low compression).

另请参阅 quality ().

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 ().

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

返回图像格式列表支持通过 QImageWriter .

默认情况下,Qt 可以写入下列格式:

格式 描述
BMP Windows 位图
JPG JPEG (联合摄影专家组)
JPEG JPEG (联合摄影专家组)
PNG PNG (便携式网络图形)
PPM Portable Pixmap (便携式像素图)
TIFF TIFF (标签化图像文件格式)
XBM X11 Bitmap (X11 位图)
XPM X11 Pixmap (X11 像素图)

Reading and writing SVG files is supported through Qt's SVG Module .

注意, QApplication 实例必须被创建在调用此函数之前。

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

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

Returns 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");
					

可以被测试 option,在写入器已关联格式后。

该函数在 Qt 4.2 引入。

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

bool QImageWriter:: write (const QImage & image )

写入图像 image to the assigned device or file name. Returns true on success; otherwise returns false. If the operation fails, you can call error () 以查找出现的错误类型,或 errorString () 以获取人类可读错误描述。

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