QImageWriter 类提供的格式独立于将图像写入文件 (或其它设备) 的接口。 更多...
头: | #include <QImageWriter> |
CMake: |
find_package(Qt6 REQUIRED COMPONENTS Gui)
target_link_libraries(mytarget PRIVATE Qt6::Gui) |
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 |
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 .
此枚举描述可以出现的错误, 当写入图像采用 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 对象。写入前,必须调用 setFormat () 来设置图像格式,然后 setDevice () 或 setFileName ().
[explicit]
QImageWriter::
QImageWriter
(
QIODevice
*
device
, const
QByteArray
&
format
)
构造 QImageWriter 对象使用设备 device 和图像格式 format .
[explicit]
QImageWriter::
QImageWriter
(const
QString
&
fileName
, const
QByteArray
&
format
= QByteArray())
构造将写入文件的 QImageWriter 对象采用名称 fileName ,使用图像格式 format 。若 format 不提供,QImageWriter 将检测图像格式通过审查扩展名对于 fileName .
销毁 QImageWriter 对象。
返回
true
if
QImageWriter
可以写入图像;即:图像格式支持且为读取打开赋值设备。
另请参阅 write (), setDevice (),和 setFormat ().
返回图像的压缩。
另请参阅 setCompression ().
返回的设备目前被赋值给
QImageWriter
,或
nullptr
若没有设备被赋值。
另请参阅 setDevice ().
返回最后发生的错误类型。
另请参阅 ImageWriterError and errorString ().
返回最近发生错误的人类可读描述。
另请参阅 error ().
If the currently assigned device is a file, 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 file), an empty QString 被返回。
另请参阅 setFileName () 和 setDevice ().
返回格式 QImageWriter 用于写入图像。
另请参阅 setFormat ().
[static]
QList
<
QByteArray
> QImageWriter::
imageFormatsForMimeType
(const
QByteArray
&
mimeType
)
Returns the list of image formats corresponding to mimeType .
注意, QGuiApplication 实例必须被创建在调用此函数之前。
另请参阅 supportedImageFormats () 和 supportedMimeTypes ().
Returns whether optimization has been turned on for writing the image.
另请参阅 setOptimizedWrite ().
Returns whether the image should be written as a progressive image.
另请参阅 setProgressiveScanWrite ().
返回图像格式的品质设置。
另请参阅 setQuality ().
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 ().
设置 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 ().
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 ().
设置格式 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 ().
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。
另请参阅 optimizedWrite ().
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。
另请参阅 progressiveScanWrite ().
将图像格式的品质设置设为 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 ().
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);
另请参阅 subType ().
设置图像文本关联键 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 .
另请参阅 QImage::setText () 和 QImageReader::text ().
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.
另请参阅 transformation () 和 write ().
返回图像的子类型。
另请参阅 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 ().
返回由图像所支持的子类型列表。
返回
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,在写入器已关联格式后。
另请参阅 QImageReader::supportsOption () 和 setFormat ().
返回已设置为写入图像的变换和取向。
另请参阅 setTransformation ().
写入图像
image
到赋值设备 (或文件名)。返回
true
当成功时;否则返回
false
。若操作失败,可以调用
error
() 以查找出现的错误类型,或
errorString
() 以获取人类可读错误描述。
另请参阅 canWrite (), error (),和 errorString ().