QImageReader 類提供用於從文件或其它設備讀取圖像的格式獨立接口。 更多...
| 頭: | #include <QImageReader> |
| qmake: | QT += gui |
注意: 此類的所有函數 可重入 .
| enum | ImageReaderError { FileNotFoundError, DeviceError, UnsupportedFormatError, InvalidDataError, UnknownError } |
| QImageReader (const QString & fileName , const QByteArray & format = QByteArray()) | |
| QImageReader (QIODevice * device , const QByteArray & format = QByteArray()) | |
| QImageReader () | |
| ~QImageReader () | |
| bool | autoDetectImageFormat () const |
| bool | autoTransform () const |
| QColor | backgroundColor () const |
| bool | canRead () const |
| QRect | clipRect () const |
| int | currentImageNumber () const |
| QRect | currentImageRect () const |
| bool | decideFormatFromContent () const |
| QIODevice * | device () const |
| QImageReader::ImageReaderError | error () const |
| QString | errorString () const |
| QString | fileName () const |
| QByteArray | format () const |
| int | imageCount () const |
| QImage::Format | imageFormat () const |
| bool | jumpToImage (int imageNumber ) |
| bool | jumpToNextImage () |
| int | loopCount () const |
| int | nextImageDelay () const |
| int | quality () const |
| QImage | read () |
| bool | read (QImage * image ) |
| QRect | scaledClipRect () const |
| QSize | scaledSize () const |
| void | setAutoDetectImageFormat (bool enabled ) |
| void | setAutoTransform (bool enabled ) |
| void | setBackgroundColor (const QColor & color ) |
| void | setClipRect (const QRect & rect ) |
| void | setDecideFormatFromContent (bool ignored ) |
| void | setDevice (QIODevice * device ) |
| void | setFileName (const QString & fileName ) |
| void | setFormat (const QByteArray & format ) |
| void | setQuality (int quality ) |
| void | setScaledClipRect (const QRect & rect ) |
| void | setScaledSize (const QSize & size ) |
| QSize | size () const |
| QByteArray | subType () const |
| QList<QByteArray> | supportedSubTypes () const |
| bool | supportsAnimation () const |
| bool | supportsOption (QImageIOHandler::ImageOption option ) const |
| QString | text (const QString & key ) const |
| QStringList | textKeys () const |
| QImageIOHandler::Transformations | transformation () const |
| QByteArray | imageFormat (const QString & fileName ) |
| QByteArray | imageFormat (QIODevice * device ) |
| QList<QByteArray> | imageFormatsForMimeType (const QByteArray & mimeType ) |
| QList<QByteArray> | supportedImageFormats () |
| QList<QByteArray> | supportedMimeTypes () |
讀取圖像的最常見方式是透過 QImage and QPixmap 的構造函數,或通過調用 QImage::load () 和 QPixmap::load (). QImageReader is a specialized class which gives you more control when reading images. For example, you can read an image into a specific size by calling setScaledSize (), and you can select a clip rect, effectively loading only parts of an image, by calling setClipRect (). Depending on the underlying support in the image format, this can save memory and speed up loading of images.
To read an image, you start by constructing a QImageReader object. Pass either a file name or a device pointer, and the image format to QImageReader's constructor. You can then set several options, such as the clip rect (by calling setClipRect ()) and scaled size (by calling setScaledSize ()). canRead () returns the image if the QImageReader can read the image (i.e., the image format is supported and the device is open for reading). Call read () to read the image.
若齣現任何錯誤當讀取圖像時, read () 將返迴 null QImage . You can then call error () 以查找齣現的錯誤類型,或 errorString () 以獲得人類可讀的什麼錯誤描述。
注意: QImageReader 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 QImageReader object will yield undefined results.
調用 supportedImageFormats () for a list of formats that QImageReader can read. QImageReader supports all built-in image formats, in addition to any image format plugins that support reading. Call supportedMimeTypes () to obtain a list of supported MIME types, which for example can be passed to QFileDialog::setMimeTypeFilters ().
QImageReader autodetects the image format by default, by looking at the provided (optional) format string, the file name suffix, and the data stream contents. You can enable or disable this feature, by calling setAutoDetectImageFormat ().
It is possible to provide high resolution versions of images should a scaling between 設備像素 and 設備無關像素 be in effect.
The high resolution version is marked by the suffix
@2x
on the base name. The image read will have its
設備像素比率
set to a value of 2.
This can be disabled by setting the environment variable
QT_HIGHDPI_DISABLE_2X_IMAGE_LOADING
.
另請參閱 QImageWriter , QImageIOHandler , QImageIOPlugin , QMimeDatabase , QColorSpace , QImage::devicePixelRatio (), QPixmap::devicePixelRatio (), QIcon , QPainter::drawPixmap (), QPainter::drawImage (),和 Qt::AA_UseHighDpiPixmaps .
This enum describes the different types of errors that can occur when reading images with QImageReader .
| 常量 | 值 | 描述 |
|---|---|---|
QImageReader::FileNotFoundError
|
1
|
QImageReader was used with a file name, but not file was found with that name. This can also happen if the file name contained no extension, and the file with the correct extension is not supported by Qt. |
QImageReader::DeviceError
|
2
|
QImageReader encountered a device error when reading the image. You can consult your particular device for more details on what went wrong. |
QImageReader::UnsupportedFormatError
|
3
|
Qt 不支持請求的圖像格式。 |
QImageReader::InvalidDataError
|
4
|
The image data was invalid, and QImageReader was unable to read an image from it. The can happen if the image file is damaged. |
QImageReader::UnknownError
|
0
|
An unknown error occurred. If you get this value after calling read (), it is most likely caused by a bug in QImageReader . |
構造 QImageReader 對象采用文件名 fileName 和圖像格式 format .
另請參閱 setFileName ().
構造 QImageReader 對象采用設備 device 和圖像格式 format .
構造空的 QImageReader 對象。 在讀取圖像之前,請緻電xx setDevice () 或 setFileName ().
銷毀 QImageReader 對象。
返迴
true
if image format autodetection is enabled on this image reader; otherwise returns
false
. By default, autodetection is enabled.
另請參閱 setAutoDetectImageFormat ().
返迴
true
if the image handler will apply transformation metadata on
read
().
該函數在 Qt 5.5 引入。
另請參閱 setAutoTransform (), transformation (),和 read ().
Returns the background color that's used when reading an image. If the image format does not support setting the background color an invalid color is returned.
該函數在 Qt 4.1 引入。
另請參閱 setBackgroundColor () 和 read ().
返迴
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.
注意: A QMimeDatabase lookup is normally a better approach than this function for identifying potentially non-image files or data.
For images that support animation, canRead() returns
false
when all frames have been read.
另請參閱 read (), supportedImageFormats (),和 QMimeDatabase .
Returns the clip rect (also known as the ROI, or Region Of Interest) of the image. If no clip rect has been set, an invalid QRect 被返迴。
另請參閱 setClipRect ().
For image formats that support animation, this function returns the sequence number of the current frame. If the image format doesn't support animation, 0 is returned.
此函數返迴 -1,若發生錯誤。
另請參閱 supportsAnimation (), QImageIOHandler::currentImageNumber (),和 canRead ().
For image formats that support animation, this function returns the rect for the current frame. Otherwise, a null rect is returned.
另請參閱 supportsAnimation () 和 QImageIOHandler::currentImageRect ().
Returns whether the image reader should decide which plugin to use only based on the contents of the datastream rather than on the file extension.
另請參閱 setDecideFormatFromContent ().
返迴的設備目前被賦值給
QImageReader
,或
nullptr
若沒有設備被賦值。
另請參閱 setDevice ().
返迴最後齣現的錯誤類型。
另請參閱 ImageReaderError and errorString ().
返迴最近發生錯誤的人類可讀描述。
另請參閱 error ().
若目前賦值設備是 QFile ,或者若 setFileName () has been called, this function returns the name of the file QImageReader reads from. Otherwise (i.e., if no device has been assigned or the device is not a QFile ), an empty QString 被返迴。
另請參閱 setFileName () 和 setDevice ().
返迴格式 QImageReader 用於讀取圖像。
You can call this function after assigning a device to the reader to determine the format of the device. For example:
QImageReader reader("image.png"); // reader.format() == "png"
If the reader cannot read any image from the device (e.g., there is no image there, or the image has already been read), or if the format is unsupported, this function returns an empty QByteArray().
另請參閱 setFormat () 和 supportedImageFormats ().
For image formats that support animation, this function returns the total number of images in the animation. If the format does not support animation, 0 is returned.
此函數返迴 -1,若發生錯誤。
另請參閱 supportsAnimation (), QImageIOHandler::imageCount (),和 canRead ().
Returns the format of the image, without actually reading the image contents. The format describes the image format QImageReader::read () returns, not the format of the actual image.
If the image format does not support this feature, this function returns an invalid format.
該函數在 Qt 4.5 引入。
另請參閱 QImageIOHandler::ImageOption , QImageIOHandler::option (),和 QImageIOHandler::supportsOption ().
[static]
QByteArray
QImageReader::
imageFormat
(const
QString
&
fileName
)
If supported, this function returns the image format of the file fileName . Otherwise, an empty string is returned.
[static]
QByteArray
QImageReader::
imageFormat
(
QIODevice
*
device
)
If supported, this function returns the image format of the device device . Otherwise, an empty string is returned.
另請參閱 QImageReader::autoDetectImageFormat ().
[static]
QList
<
QByteArray
> QImageReader::
imageFormatsForMimeType
(const
QByteArray
&
mimeType
)
Returns the list of image formats corresponding to mimeType .
注意, QGuiApplication 實例必須被創建在調用此函數之前。
該函數在 Qt 5.12 引入。
另請參閱 supportedImageFormats () 和 supportedMimeTypes ().
For image formats that support animation, this function skips to the image whose sequence number is imageNumber , returning true if successful or false if the corresponding image cannot be found.
The next call to read () will attempt to read this image.
另請參閱 jumpToNextImage () 和 QImageIOHandler::jumpToImage ().
For image formats that support animation, this function steps over the current image, returning true if successful or false if there is no following image in the animation.
默認實現調用 read (), then discards the resulting image, but the image handler may have a more efficient way of implementing this operation.
另請參閱 jumpToImage () 和 QImageIOHandler::jumpToNextImage ().
For image formats that support animation, this function returns the number of times the animation should loop. If this function returns -1, it can either mean the animation should loop forever, or that an error occurred. If an error occurred, canRead () 將返迴 false。
另請參閱 supportsAnimation (), QImageIOHandler::loopCount (),和 canRead ().
For image formats that support animation, this function returns the number of milliseconds to wait until displaying the next frame in the animation. If the image format doesn't support animation, 0 is returned.
此函數返迴 -1,若發生錯誤。
另請參閱 supportsAnimation (), QImageIOHandler::nextImageDelay (),和 canRead ().
返迴圖像格式的品質設置。
該函數在 Qt 4.2 引入。
另請參閱 setQuality ().
Reads an image from the device. On success, the image that was read is returned; otherwise, a null QImage is returned. You can then call error () 以查找齣現的錯誤類型,或 errorString () 以獲取人類可讀錯誤描述。
For image formats that support animation, calling read() repeatedly will return the next frame. When all frames have been read, a null image will be returned.
另請參閱 canRead (), supportedImageFormats (), supportsAnimation (),和 QMovie .
這是重載函數。
Reads an image from the device into
image
, which must point to a
QImage
。返迴
true
on success; otherwise, returns
false
.
若 image has same format and size as the image data that is about to be read, this function may not need to allocate a new image before reading. Because of this, it can be faster than the other read() overload, which always constructs a new image; especially when reading several images with the same format and size.
QImage icon(64, 64, QImage::Format_RGB32); QImageReader reader("icon_64x64.bmp"); if (reader.read(&icon)) { // Display icon }
For image formats that support animation, calling read() repeatedly will return the next frame. When all frames have been read, a null image will be returned.
另請參閱 canRead (), supportedImageFormats (), supportsAnimation (),和 QMovie .
返迴圖像的比例縮放裁剪矩形。
另請參閱 setScaledClipRect ().
返迴圖像的比例縮放尺寸。
另請參閱 setScaledSize ().
若 enabled is true, image format autodetection is enabled; otherwise, it is disabled. By default, autodetection is enabled.
QImageReader uses an extensive approach to detecting the image format; firstly, if you pass a file name to QImageReader , it will attempt to detect the file extension if the given file name does not point to an existing file, by appending supported default extensions to the given file name, one at a time. It then uses the following approach to detect the image format:
By disabling image format autodetection, QImageReader will only query the plugins and built-in handlers based on the format string (i.e., no file name extensions are tested).
另請參閱 autoDetectImageFormat (), QImageIOHandler::canRead (),和 QImageIOPlugin::capabilities ().
Determines that images returned by
read
() should have transformation metadata automatically applied if
enabled
is
true
.
該函數在 Qt 5.5 引入。
另請參閱 autoTransform (), transformation (),和 read ().
Sets the background color to color . Image formats that support this operation are expected to initialize the background to color before reading an image.
該函數在 Qt 4.1 引入。
另請參閱 backgroundColor () 和 read ().
Sets the image clip rect (also known as the ROI, or Region Of Interest) to rect . The coordinates of rect are relative to the untransformed image size, as returned by size ().
另請參閱 clipRect (), setScaledSize (),和 setScaledClipRect ().
若 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.
這還禁用自動檢測圖像格式。
另請參閱 decideFormatFromContent ().
設置 QImageReader 的設備到 device . If a device has already been set, the old device is removed from QImageReader and is otherwise left unchanged.
若設備尚未打開, QImageReader will attempt to open the device in QIODevice::ReadOnly 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 QImageReader to fileName . Internally, QImageReader 將創建 QFile object and open it in QIODevice::ReadOnly mode, and use this when reading images.
若 fileName does not include a file extension (e.g., .png or .bmp), QImageReader will cycle through all supported extensions until it finds a matching file.
另請參閱 fileName (), setDevice (),和 supportedImageFormats ().
設置格式 QImageReader will use when reading images, to format . format is a case insensitive text string. Example:
QImageReader reader; reader.setFormat("png"); // same as reader.setFormat("PNG");
可以調用 supportedImageFormats () 瞭解完整格式列錶 QImageReader 支持。
另請參閱 format ().
將圖像格式的品質設置設為 quality .
Some image formats, in particular lossy ones, entail a tradeoff between a) visual quality of the resulting image, and b) decoding execution time. This function sets the level of that tradeoff for image formats that support it.
In case of scaled image reading, the quality setting may also influence the tradeoff level between visual quality and execution speed of the scaling algorithm.
The value range of quality depends on the image format. For example, the "jpeg" format supports a quality range from 0 (low visual quality) to 100 (high visual quality).
該函數在 Qt 4.2 引入。
另請參閱 quality () 和 setScaledSize ().
Sets the scaled clip rect to rect . The scaled clip rect is the clip rect (also known as ROI, or Region Of Interest) that is applied after the image has been scaled.
另請參閱 scaledClipRect () 和 setScaledSize ().
將圖像的比例縮放尺寸設為 size . The scaling is performed after the initial clip rect, but before the scaled clip rect is applied. The algorithm used for scaling depends on the image format. By default (i.e., if the image format does not support scaling), QImageReader will use QImage::scale() with Qt::SmoothScaling.
另請參閱 scaledSize (), setClipRect (),和 setScaledClipRect ().
Returns the size of the image, without actually reading the image contents.
If the image format does not support this feature, this function returns an invalid size. Qt's built-in image handlers all support this feature, but custom image format plugins are not required to do so.
另請參閱 QImageIOHandler::ImageOption , QImageIOHandler::option (),和 QImageIOHandler::supportsOption ().
返迴圖像的子類型。
該函數在 Qt 5.4 引入。
[static]
QList
<
QByteArray
> QImageReader::
supportedImageFormats
()
返迴圖像格式列錶支持通過 QImageReader .
默認情況下,Qt 可以讀取以下格式:
| 格式 | MIME 類型 | 描述 |
|---|---|---|
| BMP | image/bmp | Windows 位圖 |
| GIF | image/gif | GIF (圖形互換格式) 可選 |
| 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 | image/svg+xml | SVG (可伸縮嚮量圖形) |
讀寫 SVG 文件的支持是透過 Qt SVG 模塊。 Qt Image Formats 模塊為額外圖像格式提供支持。
注意, QApplication 實例必須被創建在調用此函數之前。
另請參閱 setFormat (), QImageWriter::supportedImageFormats (),和 QImageIOPlugin .
[static]
QList
<
QByteArray
> QImageReader::
supportedMimeTypes
()
返迴 MIME (多用途 Internet 郵件擴展) 類型列錶支持通過 QImageReader .
注意, QApplication 實例必須被創建在調用此函數之前。
另請參閱 supportedImageFormats () 和 QImageWriter::supportedMimeTypes ().
返迴由圖像所支持的子類型列錶。
該函數在 Qt 5.4 引入。
返迴
true
若圖像格式支持動畫;否則,返迴 false。
該函數在 Qt 4.1 引入。
另請參閱 QMovie::supportedFormats ().
返迴
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 ()), and the BMP format allows you to determine the image's size without loading the whole image into memory (see size ()).
QImageReader reader(":/image.png"); if (reader.supportsOption(QImageIOHandler::Size)) qDebug() << "Size:" << reader.size();
該函數在 Qt 4.2 引入。
另請參閱 QImageWriter::supportsOption ().
返迴的圖像文本關聯 key .
支持此選項的實現是透過 QImageIOHandler::Description .
該函數在 Qt 4.1 引入。
另請參閱 textKeys () 和 QImageWriter::setText ().
Returns the text keys for this image. You can use these keys with text () 列錶某個鍵的圖像文本。
支持此選項的實現是透過 QImageIOHandler::Description .
該函數在 Qt 4.1 引入。
另請參閱 text (), QImageWriter::setText (),和 QImage::textKeys ().
Returns the transformation metadata of the image, including image orientation. If the format does not support transformation metadata, QImageIOHandler::TransformationNone 被返迴。
該函數在 Qt 5.5 引入。
另請參閱 setAutoTransform () 和 autoTransform ().