📄 qimagereader.cpp
字号:
void getText(); // error QImageReader::ImageReaderError imageReaderError; QString errorString; QImageReader *q;};/*! \internal*/QImageReaderPrivate::QImageReaderPrivate(QImageReader *qq){ device = 0; deleteDevice = false; handler = 0; quality = -1; imageReaderError = QImageReader::UnknownError; errorString = QLatin1String(QT_TRANSLATE_NOOP(QImageReader, "Unknown error")); q = qq;}/*! \internal*/QImageReaderPrivate::~QImageReaderPrivate(){ if (deleteDevice) delete device; delete handler;}/*! \internal*/bool QImageReaderPrivate::initHandler(){ // check some preconditions if (!device || (!deleteDevice && !device->isOpen())) { imageReaderError = QImageReader::DeviceError; errorString = QLatin1String(QT_TRANSLATE_NOOP(QImageReader, "Invalid device")); return false; } // probe the file extension if (deleteDevice && !device->isOpen() && !device->open(QIODevice::ReadOnly)) { QList<QByteArray> extensions = QImageReader::supportedImageFormats(); if (!format.isEmpty()) { // Try the most probable extension first int currentFormatIndex = extensions.indexOf(format.toLower()); if (currentFormatIndex > 0) extensions.swap(0, currentFormatIndex); } int currentExtension = 0; QFile *file = static_cast<QFile *>(device); QString fileName = file->fileName(); do { file->setFileName(fileName + QLatin1Char('.') + QString::fromLatin1(extensions.at(currentExtension++).constData())); file->open(QIODevice::ReadOnly); } while (!file->isOpen() && currentExtension < extensions.size()); if (!device->isOpen()) { imageReaderError = QImageReader::FileNotFoundError; errorString = QLatin1String(QT_TRANSLATE_NOOP(QImageReader, "File not found")); return false; } } // assign a handler if (!handler && (handler = ::createReadHandler(device, format)) == 0) { imageReaderError = QImageReader::UnsupportedFormatError; errorString = QLatin1String(QT_TRANSLATE_NOOP(QImageReader, "Unsupported image format")); return false; } return true;}/*! \internal*/void QImageReaderPrivate::getText(){ if (!text.isEmpty() || (!handler && !initHandler()) || !handler->supportsOption(QImageIOHandler::Description)) return; foreach (QString pair, handler->option(QImageIOHandler::Description).toString().split( QLatin1String("\n\n"))) { int index = pair.indexOf(QLatin1Char(':')); if (index >= 0 && pair.indexOf(QLatin1Char(' ')) < index) { text.insert(QLatin1String("Description"), pair.simplified()); } else { QString key = pair.left(index); text.insert(key, pair.mid(index + 2).simplified()); } }}/*! Constructs an empty QImageReader object. Before reading an image, call setDevice() or setFileName().*/QImageReader::QImageReader() : d(new QImageReaderPrivate(this)){}/*! Constructs a QImageReader object with the device \a device and the image format \a format.*/QImageReader::QImageReader(QIODevice *device, const QByteArray &format) : d(new QImageReaderPrivate(this)){ d->device = device; d->format = format;}/*! Constructs a QImageReader object with the file name \a fileName and the image format \a format. \sa setFileName()*/QImageReader::QImageReader(const QString &fileName, const QByteArray &format) : d(new QImageReaderPrivate(this)){ QFile *file = new QFile(fileName); d->device = file; d->deleteDevice = true; d->format = format;}/*! Destructs the QImageReader object.*/QImageReader::~QImageReader(){ delete d;}/*! Sets the format QImageReader will use when reading images, to \a format. \a format is a case insensitive text string. Example: \code QImageReader reader; reader.setFormat("png"); // same as reader.setFormat("PNG"); \endcode You can call supportedImageFormats() for the full list of formats QImageReader supports. \sa format()*/void QImageReader::setFormat(const QByteArray &format){ d->format = format;}/*! 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: \code QImageReader reader("image.png"); // reader.format() == "png" \endcode 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(). \sa setFormat(), supportedImageFormats()*/QByteArray QImageReader::format() const{ if (d->format.isEmpty()) { if (!d->initHandler()) return QByteArray(); return d->handler->canRead() ? d->handler->format() : QByteArray(); } return d->format;}/*! Sets QImageReader's device to \a device. If a device has already been set, the old device is removed from QImageReader and is otherwise left unchanged. If the device is not already open, QImageReader will attempt to open the device in \l 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. \sa device(), setFileName()*/void QImageReader::setDevice(QIODevice *device){ if (d->device && d->deleteDevice) delete d->device; d->device = device; d->deleteDevice = false; delete d->handler; d->handler = 0; d->text.clear();}/*! Returns the device currently assigned to QImageReader, or 0 if no device has been assigned.*/QIODevice *QImageReader::device() const{ return d->device;}/*! Sets the file name of QImageReader to \a fileName. Internally, QImageReader will create a QFile object and open it in \l QIODevice::ReadOnly mode, and use this when reading images. If \a 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. \sa fileName(), setDevice(), supportedImageFormats()*/void QImageReader::setFileName(const QString &fileName){ setDevice(new QFile(fileName)); d->deleteDevice = true;}/*! 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 is returned. \sa setFileName(), setDevice()*/QString QImageReader::fileName() const{ QFile *file = qobject_cast<QFile *>(d->device); return file ? file->fileName() : QString();}/*! \since 4.2 This is an image format specific function that sets the quality level of the image to \a quality. For image formats that do not support setting the quality, this value is ignored. The value range of \a 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). \sa quality()*/void QImageReader::setQuality(int quality){ d->quality = quality;}/*! \since 4.2 Returns the quality level of the image. \sa setQuality()*/int QImageReader::quality() const{ return d->quality;}/*! 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. \sa QImageIOHandler::ImageOption, QImageIOHandler::option(), QImageIOHandler::supportsOption()*/QSize QImageReader::size() const{ if (!d->initHandler()) return QSize(); if (d->handler->supportsOption(QImageIOHandler::Size)) return d->handler->option(QImageIOHandler::Size).toSize(); return QSize();}/*! \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);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -