📄 qimagereader.cpp
字号:
return image.size();}/*! \since 4.1 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. \sa text(), QImageWriter::setText(), QImage::textKeys()*/QStringList QImageReader::textKeys() const{ d->getText(); return d->text.keys();}/*! \since 4.1 Returns the image text associated with \a key. Support for this option is implemented through QImageIOHandler::Description. \sa textKeys(), QImageWriter::setText()*/QString QImageReader::text(const QString &key) const{ d->getText(); return d->text.value(key); }/*! Sets the image clip rect (also known as the ROI, or Region Of Interest) to \a rect. The coordinates of \a rect are relative to the untransformed image size, as returned by size(). \sa clipRect(), setScaledSize(), setScaledClipRect()*/void QImageReader::setClipRect(const QRect &rect){ d->clipRect = rect;}/*! 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 is returned. \sa setClipRect()*/QRect QImageReader::clipRect() const{ return d->clipRect;}/*! Sets the scaled size of the image to \a 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. \sa scaledSize(), setClipRect(), setScaledClipRect()*/void QImageReader::setScaledSize(const QSize &size){ d->scaledSize = size;}/*! Returns the scaled size of the image. \sa setScaledSize()*/QSize QImageReader::scaledSize() const{ return d->scaledSize;}/*! Sets the scaled clip rect to \a 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. \sa scaledClipRect(), setScaledSize()*/void QImageReader::setScaledClipRect(const QRect &rect){ d->scaledClipRect = rect;}/*! Returns the scaled clip rect of the image. \sa setScaledClipRect()*/QRect QImageReader::scaledClipRect() const{ return d->scaledClipRect;}/*! \since 4.1 Sets the background color to \a color. Image formats that support this operation are expected to initialize the background to \a color before reading an image. \sa backgroundColor(), read()*/void QImageReader::setBackgroundColor(const QColor &color){ if (!d->initHandler()) return; if (d->handler->supportsOption(QImageIOHandler::BackgroundColor)) { d->handler->setOption(QImageIOHandler::BackgroundColor, color); }}/*! \since 4.1 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. \sa setBackgroundColor(), read()*/QColor QImageReader::backgroundColor() const{ if (!d->initHandler()) return QColor(); if (d->handler->supportsOption(QImageIOHandler::BackgroundColor)) return qVariantValue<QColor>(d->handler->option(QImageIOHandler::BackgroundColor)); return QColor();}/*! \since 4.1 Returns true if the image format supports animation; otherwise, false is returned. \sa QMovie::supportedFormats()*/bool QImageReader::supportsAnimation() const{ if (!d->initHandler()) return false; if (d->handler->supportsOption(QImageIOHandler::Animation)) return d->handler->option(QImageIOHandler::Animation).toBool(); return false;}/*! Returns 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. For images that support animation, canRead() returns false when all frames have been read. \sa read(), supportedImageFormats()*/bool QImageReader::canRead() const{ if (!d->initHandler()) return false; return d->handler->canRead();}/*! 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. \sa canRead(), supportedImageFormats(), supportsAnimation(), QMovie*/QImage QImageReader::read(){ if (!d->handler && !d->initHandler()) return QImage(); // set the handler specific options. if (d->handler->supportsOption(QImageIOHandler::ClipRect)) d->handler->setOption(QImageIOHandler::ClipRect, d->clipRect); if (d->handler->supportsOption(QImageIOHandler::ScaledSize)) d->handler->setOption(QImageIOHandler::ScaledSize, d->scaledSize); if (d->handler->supportsOption(QImageIOHandler::ScaledClipRect)) d->handler->setOption(QImageIOHandler::ScaledClipRect, d->scaledClipRect); // read the image QImage image; if (!d->handler->read(&image)) { d->imageReaderError = InvalidDataError; d->errorString = QT_TRANSLATE_NOOP(QImageReader, "Unable to read image data"); return QImage(); } // provide default implementations for any unsupported image // options if (d->handler->supportsOption(QImageIOHandler::ClipRect)) { if (d->handler->supportsOption(QImageIOHandler::ScaledSize)) { if (d->handler->supportsOption(QImageIOHandler::ScaledClipRect)) { // all features are supported by the handler; nothing to do. } else { // the image is already scaled, so apply scaled clipping. if (d->scaledClipRect.isValid()) { image = image.copy(d->scaledClipRect); } } } else { if (d->handler->supportsOption(QImageIOHandler::ScaledClipRect)) { // supports scaled clipping but not scaling, most // likely a broken handler. } else { if (d->scaledSize.isValid()) { image = image.scaled(d->scaledSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); } if (d->scaledClipRect.isValid()) { image = image.copy(d->scaledClipRect); } } } } else { if (d->handler->supportsOption(QImageIOHandler::ScaledSize)) { // in this case, there's nothing we can do. if the // plugin supports scaled size but not ClipRect, then // we have to ignore ClipRect." if (d->handler->supportsOption(QImageIOHandler::ScaledClipRect)) { // nothing to do (ClipRect is ignored!) } else { // provide all workarounds. if (d->scaledClipRect.isValid()) { image = image.copy(d->scaledClipRect); } } } else { if (d->handler->supportsOption(QImageIOHandler::ScaledClipRect)) { // this makes no sense; a handler that supports // ScaledClipRect but not ScaledSize is broken, and we // can't work around it. } else { // provide all workarounds. if (d->clipRect.isValid()) image = image.copy(d->clipRect); if (d->scaledSize.isValid()) image = image.scaled(d->scaledSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); if (d->scaledClipRect.isValid()) image = image.copy(d->scaledClipRect); } } } return image;}/*! For image formats that support animation, this function steps over the current image. The default implementation calls read(), and then discards the resulting image, but the image handler may have a more efficient way of implementing this operation. \sa jumpToImage(), QImageIOHandler::jumpToNextImage()*/bool QImageReader::jumpToNextImage(){ if (!d->initHandler()) return false; return d->handler->jumpToNextImage();}/*! For image formats that support animation, this function skips to the image whose sequence number is \a imageNumber. The next call to read() will attempt to read this image. \sa jumpToNextImage(), QImageIOHandler::jumpToImage()*/bool QImageReader::jumpToImage(int imageNumber){ if (!d->initHandler()) return false; return d->handler->jumpToImage(imageNumber);}/*! For image formats that support animation, this function returns the number of times the animation should loop. Otherwise, it returns -1. \sa supportsAnimation(), QImageIOHandler::loopCount()*/int QImageReader::loopCount() const{ if (!d->initHandler()) return -1; return d->handler->loopCount();}/*! For image formats that support animation, this function returns the total number of images in the animation. Certain animation formats do not support this feature, in which case 0 is returned. \sa supportsAnimation(), QImageIOHandler::imageCount()*/int QImageReader::imageCount() const{ if (!d->initHandler()) return -1; return d->handler->imageCount();}/*! For image formats that support animation, this function returns the number of milliseconds to wait until displaying the next frame in the animation. Otherwise, 0 is returned. \sa supportsAnimation(), QImageIOHandler::nextImageDelay()*/int QImageReader::nextImageDelay() const{ if (!d->initHandler()) return -1; return d->handler->nextImageDelay();}/*! For image formats that support animation, this function returns the sequence number of the current frame. Otherwise, -1 is returned. \sa supportsAnimation(), QImageIOHandler::currentImageNumber()*/int QImageReader::currentImageNumber() const{ if (!d->initHandler()) return -1; return d->handler->currentImageNumber();}/*! For image formats that support animation, this function returns the rect for the current frame. Otherwise, a null rect is returned. \sa supportsAnimation(), QImageIOHandler::currentImageRect()*/QRect QImageReader::currentImageRect() const{ if (!d->initHandler()) return QRect(); return d->handler->currentImageRect();}/*! Returns the type of error that occurred last. \sa ImageReaderError, errorString()*/QImageReader::ImageReaderError QImageReader::error() const{ return d->imageReaderError;}/*! Returns a human readable description of the last error that occurred. \sa error()*/QString QImageReader::errorString() const{ return d->errorString;}/*! If supported, this function returns the image format of the file \a fileName. Otherwise, an empty string is returned.*/QByteArray QImageReader::imageFormat(const QString &fileName){ QFile file(fileName); if (!file.open(QFile::ReadOnly)) return QByteArray(); return imageFormat(&file);}/*! If supported, this function returns the image format of the device \a device. Otherwise, an empty string is returned.*/QByteArray QImageReader::imageFormat(QIODevice *device){ QByteArray format; QImageIOHandler *handler = ::createReadHandler(device, format); if (handler) { if (handler->canRead()) format = handler->format(); delete handler; } return format;}/*! Returns the list of image formats supported by QImageReader. By default, Qt can read the following formats: \table \header \o Format \o Description \row \o BMP \o Windows Bitmap \row \o GIF \o Graphic Interchange Format (optional) \row \o JPG \o Joint Photographic Experts Group \row \o JPEG \o Joint Photographic Experts Group \row \o MNG \o Multiple-image Network Graphics \row \o PNG \o Portable Network Graphics \row \o PBM \o Portable Bitmap \row \o PGM \o Portable Graymap \row \o PPM \o Portable Pixmap \row \o XBM \o X11 Bitmap \row \o XPM \o X11 Pixmap \endtable To configure Qt with GIF support, pass \c -qt-gif to the \c configure script or check the appropriate option in the graphical installer. \sa setFormat(), QImageWriter::supportedImageFormats(), QImageIOPlugin*/QList<QByteArray> QImageReader::supportedImageFormats(){ QSet<QByteArray> formats; formats << "bmp";#ifndef QT_NO_IMAGEFORMAT_PPM formats << "ppm" << "pgm" << "pbm";#endif#ifndef QT_NO_IMAGEFORMAT_XBM formats << "xbm";#endif#ifndef QT_NO_IMAGEFORMAT_XPM formats << "xpm";#endif#ifndef QT_NO_IMAGEFORMAT_PNG formats << "png";#endif#ifndef QT_NO_LIBRARY QFactoryLoader *l = loader(); QStringList keys = l->keys(); for (int i = 0; i < keys.count(); ++i) { QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(l->instance(keys.at(i))); if (plugin && plugin->capabilities(0, keys.at(i).toLatin1()) & QImageIOPlugin::CanRead) formats << keys.at(i).toLatin1(); }#endif // QT_NO_LIBRARY QList<QByteArray> sortedFormats; for (QSet<QByteArray>::ConstIterator it = formats.constBegin(); it != formats.constEnd(); ++it) sortedFormats << *it; qSort(sortedFormats); return sortedFormats;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -