📄 qimagewriter.cpp
字号:
d->handler = 0;}/*! Returns the device currently assigned to QImageWriter, or 0 if no device has been assigned.*/QIODevice *QImageWriter::device() const{ return d->device;}/*! Sets the file name of QImageWriter to \a fileName. Internally, QImageWriter will create a QFile and open it in \l QIODevice::WriteOnly mode, and use this file when writing images. \sa fileName(), setDevice()*/void QImageWriter::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 QImageWriter writes to. 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 QImageWriter::fileName() const{ QFile *file = qobject_cast<QFile *>(d->device); return file ? file->fileName() : QString();}/*! 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 QImageWriter::setQuality(int quality){ d->quality = quality;}/*! Returns the quality level of the image. \sa setQuality()*/int QImageWriter::quality() const{ return d->quality;}/*! 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 \a compression depends on the image format. For example, the "tiff" format supports two values, 0(no compression) and 1(LZW-compression). \sa compression()*/void QImageWriter::setCompression(int compression){ d->compression = compression;}/*! Returns the compression of the image. \sa setCompression()*/int QImageWriter::compression() const{ return d->compression;}/*! This is an image format specific function that sets the gamma level of the image to \a gamma. For image formats that do not support setting the gamma level, this value is ignored. The value range of \a gamma depends on the image format. For example, the "png" format supports a gamma range from 0.0 to 1.0. \sa quality()*/void QImageWriter::setGamma(float gamma){ d->gamma = gamma;}/*! Returns the gamma level of the image. \sa setGamma()*/float QImageWriter::gamma() const{ return d->gamma;}/*! \obsolete Use setText() instead. This is an image format specific function that sets the description of the image to \a description. For image formats that do not support setting the description, this value is ignored. The contents of \a description depends on the image format. \sa description()*/void QImageWriter::setDescription(const QString &description){ d->description = description;}/*! \obsolete Use QImageReader::text() instead. Returns the description of the image. \sa setDescription()*/QString QImageWriter::description() const{ return d->description;}/*! \since 4.1 Sets the image text associated with the key \a key to \a text. This is useful for storing copyright information or other information about the image. Example: \code QImage image("some/image.jpeg"); QImageWriter writer("images/outimage.png", "png"); writer.setText("Author", "John Smith"); writer.write(image); \endcode 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(). Support for this option is implemented through QImageIOHandler::Description. \sa QImage::setText(), QImageReader::text()*/void QImageWriter::setText(const QString &key, const QString &text){ if (!d->description.isEmpty()) d->description += QLatin1String("\n\n"); d->description += key.simplified() + QLatin1String(": ") + text.simplified();}/*! Returns true if QImageWriter can write the image (i.e., the image format is supported and the assigned device is open for reading. \sa write(), setDevice(), setFormat()*/bool QImageWriter::canWrite() const{ if (d->device && !d->device->isOpen()) d->device->open(QIODevice::WriteOnly); if (!d->device || !d->device->isWritable()) { d->imageWriterError = QImageWriter::DeviceError; d->errorString = QT_TRANSLATE_NOOP(QImageWriter, QLatin1String("Device not writable")); return false; } if (!d->handler && (d->handler = ::createWriteHandler(d->device, d->format)) == 0) { d->imageWriterError = QImageWriter::UnsupportedFormatError; d->errorString = QT_TRANSLATE_NOOP(QImageWriter, QLatin1String("Unsupported image format")); return false; } return true;}/*! Writes the image \a image to the assigned device or file name. Returns true on success; otherwise returns false. If the operation fails, you can call error() to find the type of error that occurred, or errorString() to get a human readable description of the error. \sa canWrite(), error(), errorString()*/bool QImageWriter::write(const QImage &image){ if (!canWrite()) return false; if (d->handler->supportsOption(QImageIOHandler::Quality)) d->handler->setOption(QImageIOHandler::Quality, d->quality); if (d->handler->supportsOption(QImageIOHandler::CompressionRatio)) d->handler->setOption(QImageIOHandler::CompressionRatio, d->compression); if (d->handler->supportsOption(QImageIOHandler::Gamma)) d->handler->setOption(QImageIOHandler::Gamma, d->gamma); if (!d->description.isEmpty() && d->handler->supportsOption(QImageIOHandler::Description)) d->handler->setOption(QImageIOHandler::Description, d->description); if (!d->handler->write(image)) return false; if (QFile *file = qobject_cast<QFile *>(d->device)) file->flush(); return true;}/*! Returns the type of error that last occurred. \sa ImageWriterError, errorString()*/QImageWriter::ImageWriterError QImageWriter::error() const{ return d->imageWriterError;}/*! Returns a human readable description of the last error that occurred. \sa error()*/QString QImageWriter::errorString() const{ return d->errorString;}/*! \since 4.2 Returns true if the writer supports \a option; otherwise returns 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()). \code QImageWriter writer(fileName); if (writer.supportsOption(QImageIOHandler::Description)) writer.setText("Author", "John Smith"); \endcode Options can be tested after the writer has been associated with a format. \sa QImageReader::supportsOption(), setFormat()*/bool QImageWriter::supportsOption(QImageIOHandler::ImageOption option) const{ if (!d->handler && (d->handler = ::createWriteHandler(d->device, d->format)) == 0) { d->imageWriterError = QImageWriter::UnsupportedFormatError; d->errorString = QT_TRANSLATE_NOOP(QImageWriter, QLatin1String("Unsupported image format")); return false; } return d->handler->supportsOption(option);}/*! Returns the list of image formats supported by QImageWriter. By default, Qt can write the following formats: \table \header \o Format \o Description \row \o BMP \o Windows Bitmap \row \o JPG \o Joint Photographic Experts Group \row \o JPEG \o Joint Photographic Experts Group \row \o PNG \o Portable Network Graphics \row \o PPM \o Portable Pixmap \row \o TIFF \o Tagged Image File Format \row \o XBM \o X11 Bitmap \row \o XPM \o X11 Pixmap \endtable \sa setFormat(), QImageReader::supportedImageFormats(), QImageIOPlugin*/QList<QByteArray> QImageWriter::supportedImageFormats(){ QSet<QByteArray> formats; formats << "bmp";#ifndef QT_NO_IMAGEFORMAT_PPM formats << "ppm";#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::CanWrite) != 0) 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 + -