📄 qimagewriter.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the QtGui module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************//*! \class QImageWriter \brief The QImageWriter class provides a format independent interface for writing images to files or other devices. \reentrant \ingroup multimedia \ingroup io QImageWriter supports setting format specific options, such as the gamma level, compression level and quality, prior to storing the image. If you do not need such options, you can use QImage::save() or QPixmap::save() instead. To store an image, you start by constructing a QImageWriter object. Pass either a file name or a device pointer, and the image format to QImageWriter's constructor. You can then set several options, such as the gamma level (by calling setGamma()) and quality (by calling setQuality()). canWrite() returns true if QImageWriter can write the image (i.e., the image format is supported and the device is open for writing). Call write() to write the image to the device. If any error occurs when writing the image, write() will return false. 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. Call supportedImageFormats() for a list of formats that QImageWriter can write. QImageWriter supports all built-in image formats, in addition to any image format plugins that support writing. \sa QImageReader, QImageIOHandler, QImageIOPlugin*//*! \enum QImageWriter::ImageWriterError This enum describes errors that can occur when writing images with QImageWriter. \value DeviceError QImageWriter encountered a device error when writing the image data. Consult your device for more details on what went wrong. \value UnsupportedFormatError Qt does not support the requested image format. \value UnknownError An unknown error occurred. If you get this value after calling write(), it is most likely caused by a bug in QImageWriter.*/#include "qimagewriter.h"#include <qbytearray.h>#include <qfile.h>#include <qfileinfo.h>#include <qimageiohandler.h>#include <qset.h>#include <qvariant.h>// factory loader#include <qcoreapplication.h>#include <private/qfactoryloader_p.h>// image handlers#include <private/qbmphandler_p.h>#include <private/qppmhandler_p.h>#include <private/qxbmhandler_p.h>#include <private/qxpmhandler_p.h>#ifndef QT_NO_IMAGEFORMAT_PNG#include <private/qpnghandler_p.h>#endif#ifndef QT_NO_LIBRARYQ_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, loader, (QImageIOHandlerFactoryInterface_iid, QCoreApplication::libraryPaths(), QLatin1String("/imageformats")))#endifstatic QImageIOHandler *createWriteHandler(QIODevice *device, const QByteArray &format){ QByteArray form = format.toLower(); QByteArray suffix; QImageIOHandler *handler = 0;#ifndef QT_NO_LIBRARY // check if any plugins can write the image QFactoryLoader *l = loader(); QStringList keys = l->keys(); int suffixPluginIndex = -1;#endif if (device && format.isEmpty()) { // if there's no format, see if \a device is a file, and if so, find // the file suffix and find support for that format among our plugins. // this allows plugins to override our built-in handlers. if (QFile *file = qobject_cast<QFile *>(device)) { if (!(suffix = QFileInfo(file->fileName()).suffix().toLower().toLatin1()).isEmpty()) {#ifndef QT_NO_LIBRARY int index = keys.indexOf(QString::fromLatin1(suffix)); if (index != -1) suffixPluginIndex = index;#endif } } } QByteArray testFormat = !form.isEmpty() ? form : suffix;#ifndef QT_NO_LIBRARY if (suffixPluginIndex != -1) { // when format is missing, check if we can find a plugin for the // suffix. QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(l->instance(QString::fromLatin1(suffix))); if (plugin && (plugin->capabilities(device, suffix) & QImageIOPlugin::CanWrite)) handler = plugin->create(device, suffix); }#endif // Q_NO_LIBRARY // check if any built-in handlers can write the image if (!handler && !testFormat.isEmpty()) { if (false) {#ifndef QT_NO_IMAGEFORMAT_PNG } else if (testFormat == "png") { handler = new QPngHandler;#endif#ifndef QT_NO_IMAGEFORMAT_BMP } else if (testFormat == "bmp") { handler = new QBmpHandler;#endif#ifndef QT_NO_IMAGEFORMAT_XPM } else if (testFormat == "xpm") { handler = new QXpmHandler;#endif#ifndef QT_NO_IMAGEFORMAT_XBM } else if (testFormat == "xbm") { handler = new QXbmHandler; handler->setOption(QImageIOHandler::SubType, testFormat);#endif#ifndef QT_NO_IMAGEFORMAT_PPM } else if (testFormat == "pbm" || testFormat == "pbmraw" || testFormat == "pgm" || testFormat == "pgmraw" || testFormat == "ppm" || testFormat == "ppmraw") { handler = new QPpmHandler; handler->setOption(QImageIOHandler::SubType, testFormat);#endif } }#ifndef QT_NO_LIBRARY if (!testFormat.isEmpty()) { for (int i = 0; i < keys.size(); ++i) { QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(l->instance(keys.at(i))); if (plugin && (plugin->capabilities(device, testFormat) & QImageIOPlugin::CanWrite)) { handler = plugin->create(device, testFormat); break; } } }#endif if (!handler) return 0; handler->setDevice(device); if (!testFormat.isEmpty()) handler->setFormat(testFormat); return handler;}class QImageWriterPrivate{public: QImageWriterPrivate(QImageWriter *qq); // device QByteArray format; QIODevice *device; bool deleteDevice; QImageIOHandler *handler; // image options int quality; int compression; float gamma; QString description; QString text; // error QImageWriter::ImageWriterError imageWriterError; QString errorString; QImageWriter *q;};/*! \internal*/QImageWriterPrivate::QImageWriterPrivate(QImageWriter *qq){ device = 0; deleteDevice = false; handler = 0; quality = -1; compression = 0; gamma = 0.0; imageWriterError = QImageWriter::UnknownError; errorString = QT_TRANSLATE_NOOP(QImageWriter, QLatin1String("Unknown error")); q = qq;}/*! Constructs an empty QImageWriter object. Before writing, you must call setFormat() to set an image format, then setDevice() or setFileName().*/QImageWriter::QImageWriter() : d(new QImageWriterPrivate(this)){}/*! Constructs a QImageWriter object using the device \a device and image format \a format.*/QImageWriter::QImageWriter(QIODevice *device, const QByteArray &format) : d(new QImageWriterPrivate(this)){ d->device = device; d->format = format;}/*! Constructs a QImageWriter objects that will write to a file with the name \a fileName, using the image format \a format. If \a format is not provided, QImageWriter will detect the image format by inspecting the extension of \a fileName.*/QImageWriter::QImageWriter(const QString &fileName, const QByteArray &format) : d(new QImageWriterPrivate(this)){ QFile *file = new QFile(fileName); d->device = file; d->deleteDevice = true; d->format = format;}/*! Destructs the QImageWriter object.*/QImageWriter::~QImageWriter(){ if (d->deleteDevice) delete d->device; delete d->handler; delete d;}/*! Sets the format QImageWriter will use when writing images, to \a format. \a format is a case insensitive text string. Example: \code QImageWriter writer; writer.setFormat("png"); // same as writer.setFormat("PNG"); \endcode You can call supportedImageFormats() for the full list of formats QImageWriter supports. \sa format()*/void QImageWriter::setFormat(const QByteArray &format){ d->format = format;}/*! Returns the format QImageWriter uses for writing images. \sa setFormat()*/QByteArray QImageWriter::format() const{ return d->format;}/*! Sets QImageWriter's device to \a device. If a device has already been set, the old device is removed from QImageWriter and is otherwise left unchanged. If the device is not already open, QImageWriter will attempt to open the device in \l QIODevice::WriteOnly 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 QImageWriter::setDevice(QIODevice *device){ if (d->device && d->deleteDevice) delete d->device; d->device = device; d->deleteDevice = false; delete d->handler;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -