QImageReader Class

QImageReader class provides a format independent interface for reading images from files or other devices. 更多...

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

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

公共类型

enum ImageReaderError { FileNotFoundError, DeviceError, UnsupportedFormatError, InvalidDataError, UnknownError }

公共函数

QImageReader ()
QImageReader (QIODevice * device , const QByteArray & format = QByteArray())
QImageReader (const QString & fileName , const QByteArray & format = QByteArray())
~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
float gamma () 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 setGamma (float gamma )
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 ()

详细描述

QImageReader class provides a format independent interface for reading images from files or other devices.

The most common way to read images is through QImage and QPixmap 's constructors, or by calling 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 () to find the type of error that occurred, or errorString () to get a human readable description of what went wrong.

注意: 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 device pixels and device independent pixels be in effect.

The high resolution version is marked by the suffix @2x on the base name. The image read will have its device pixel ratio 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 , QImage::devicePixelRatio (), QPixmap::devicePixelRatio (), QIcon , QPainter::drawPixmap (), QPainter::drawImage (),和 Qt::AA_UseHighDpiPixmaps .

成员类型文档编制

enum QImageReader:: ImageReaderError

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:: QImageReader ()

构造空 QImageReader object. Before reading an image, call setDevice () 或 setFileName ().

QImageReader:: QImageReader ( QIODevice * device , const QByteArray & format = QByteArray())

构造 QImageReader object with the device device and the image format format .

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

构造 QImageReader object with the file name fileName and the image format format .

另请参阅 setFileName ().

QImageReader:: ~QImageReader ()

销毁 QImageReader 对象。

bool QImageReader:: autoDetectImageFormat () const

返回 true if image format autodetection is enabled on this image reader; otherwise returns false . By default, autodetection is enabled.

另请参阅 setAutoDetectImageFormat ().

bool QImageReader:: autoTransform () const

返回 true if the image handler will apply transformation metadata on read ().

This function was introduced in Qt 5.5.

另请参阅 setAutoTransform (), transformation (),和 read ().

QColor QImageReader:: backgroundColor () const

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.

This function was introduced in Qt 4.1.

另请参阅 setBackgroundColor () 和 read ().

bool QImageReader:: canRead () const

返回 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 .

QRect QImageReader:: clipRect () const

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

int QImageReader:: currentImageNumber () const

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

QRect QImageReader:: currentImageRect () const

For image formats that support animation, this function returns the rect for the current frame. Otherwise, a null rect is returned.

另请参阅 supportsAnimation () 和 QImageIOHandler::currentImageRect ().

bool QImageReader:: decideFormatFromContent () const

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

QIODevice *QImageReader:: device () const

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

另请参阅 setDevice ().

QImageReader::ImageReaderError QImageReader:: error () const

Returns the type of error that occurred last.

另请参阅 ImageReaderError and errorString ().

QString QImageReader:: errorString () const

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

另请参阅 error ().

QString QImageReader:: fileName () const

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

QByteArray QImageReader:: format () const

Returns the format QImageReader uses for reading images.

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

float QImageReader:: gamma () const

Returns the gamma level of the decoded image. If setGamma () has been called and gamma correction is supported it will return the gamma set. If gamma level is not supported by the image format, 0.0 被返回。

This function was introduced in Qt 5.6.

另请参阅 setGamma ().

int QImageReader:: imageCount () const

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

QImage::Format QImageReader:: imageFormat () const

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.

This function was introduced in 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 instance must be created before this function is called.

This function was introduced in Qt 5.12.

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

bool QImageReader:: jumpToImage ( int imageNumber )

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

bool QImageReader:: jumpToNextImage ()

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

int QImageReader:: loopCount () const

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

int QImageReader:: nextImageDelay () const

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

int QImageReader:: quality () const

返回图像格式的品质设置。

This function was introduced in Qt 4.2.

另请参阅 setQuality ().

QImage QImageReader:: read ()

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 () to find the type of error that occurred, or errorString () to get a human readable description of the error.

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 .

bool QImageReader:: read ( QImage * image )

这是重载函数。

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 .

QRect QImageReader:: scaledClipRect () const

Returns the scaled clip rect of the image.

另请参阅 setScaledClipRect ().

QSize QImageReader:: scaledSize () const

Returns the scaled size of the image.

另请参阅 setScaledSize ().

void QImageReader:: setAutoDetectImageFormat ( bool enabled )

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:

  • Image plugins are queried first, based on either the optional format string, or the file name suffix (if the source device is a file). No content detection is done at this stage. QImageReader will choose the first plugin that supports reading for this format.
  • If no plugin supports the image format, Qt's built-in handlers are checked based on either the optional format string, or the file name suffix.
  • If no capable plugins or built-in handlers are found, each plugin is tested by inspecting the content of the data stream.
  • If no plugins could detect the image format based on data contents, each built-in image handler is tested by inspecting the contents.
  • Finally, if all above approaches fail, QImageReader will report failure when trying to read the image.

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

void QImageReader:: setAutoTransform ( bool enabled )

Determines that images returned by read () should have transformation metadata automatically applied if enabled is true .

This function was introduced in Qt 5.5.

另请参阅 autoTransform (), transformation (),和 read ().

void QImageReader:: setBackgroundColor (const QColor & color )

Sets the background color to color . Image formats that support this operation are expected to initialize the background to color before reading an image.

This function was introduced in Qt 4.1.

另请参阅 backgroundColor () 和 read ().

void QImageReader:: setClipRect (const QRect & rect )

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

void QImageReader:: setDecideFormatFromContent ( bool ignored )

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

void QImageReader:: setDevice ( QIODevice * device )

QImageReader 's device to 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 ().

void QImageReader:: setFileName (const QString & fileName )

Sets the file name of QImageReader to fileName . Internally, QImageReader will create a 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 ().

void QImageReader:: setFormat (const QByteArray & format )

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

另请参阅 format ().

void QImageReader:: setGamma ( float gamma )

This is an image format specific function that forces images with gamma information to be gamma corrected to gamma . For image formats that do not support gamma correction, this value is ignored.

To gamma correct to a standard PC color-space, set gamma to 1/2.2 .

This function was introduced in Qt 5.6.

另请参阅 gamma ().

void QImageReader:: setQuality ( int quality )

将图像格式的品质设置设为 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).

This function was introduced in Qt 4.2.

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

void QImageReader:: setScaledClipRect (const QRect & rect )

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

void QImageReader:: setScaledSize (const QSize & size )

Sets the scaled size of the image to 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 ().

QSize QImageReader:: size () const

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

QByteArray QImageReader:: subType () const

Returns the subtype of the image.

This function was introduced in Qt 5.4.

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

Returns the list of image formats supported by QImageReader .

By default, Qt can read the following formats:

Format MIME type 描述
BMP image/bmp Windows Bitmap
GIF image/gif Graphic Interchange Format (optional)
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
SVG image/svg+xml Scalable Vector Graphics

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 (), QImageWriter::supportedImageFormats (),和 QImageIOPlugin .

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

Returns the list of MIME types supported by QImageReader .

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

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

QList < QByteArray > QImageReader:: supportedSubTypes () const

Returns the list of subtypes supported by an image.

This function was introduced in Qt 5.4.

bool QImageReader:: supportsAnimation () const

返回 true if the image format supports animation; otherwise, false is returned.

This function was introduced in Qt 4.1.

另请参阅 QMovie::supportedFormats ().

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

返回 true if the reader 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 ()), 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();
							

This function was introduced in Qt 4.2.

另请参阅 QImageWriter::supportsOption ().

QString QImageReader:: text (const QString & key ) const

Returns the image text associated with key .

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

This function was introduced in Qt 4.1.

另请参阅 textKeys () 和 QImageWriter::setText ().

QStringList QImageReader:: textKeys () const

Returns the text keys for this image. You can use these keys with text () to list the image text for a certain key.

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

This function was introduced in Qt 4.1.

另请参阅 text (), QImageWriter::setText (),和 QImage::textKeys ().

QImageIOHandler::Transformations QImageReader:: transformation () const

Returns the transformation metadata of the image, including image orientation. If the format does not support transformation metadata, QImageIOHandler::TransformationNone 被返回。

This function was introduced in Qt 5.5.

另请参阅 setAutoTransform () 和 autoTransform ().