QImageWriter Class Reference

该类为写图片到文件或者其他设备,提供一个格式化独立接口。

#include <QImageWriter>

公共类型:

enum

ImageWriterError { DeviceError, UnsupportedFormatError, UnknownError }

公共函数:

QImageWriter ()

 

QImageWriter ( QIODevice * device, const QByteArray & format )

 

QImageWriter ( const QString & fileName, const QByteArray & format = QByteArray() )

 

~QImageWriter ()

bool

canWrite () const

int

compression () const

QIODevice *

device () const

ImageWriterError

error () const

QString

errorString () const

QString

fileName () const

QByteArray

format () const

float

gamma () const

int

quality () const

void

setCompression ( int compression )

void

setDevice ( QIODevice * device )

void

setFileName ( const QString & fileName )

void

setFormat ( const QByteArray & format )

void

setGamma ( float gamma )

void

setQuality ( int quality )

void

setText ( const QString & key, const QString & text )

bool

supportsOption ( QImageIOHandler::ImageOptionoption ) const

bool

write ( const QImage & p_w_picpath )

静态公共函数:

QList<QByteArray>

supportedImageFormats ()

详细描述:

         该类支持在存储图片之前,设置格式化特别选项,例如发差系数水平,压缩水平,质量。如果你不需要这些选项,你能使用QImage::save()或者 QPixmap::save()代替。

         为了存储一个图片,你开始 通过构造一个QImageWriter对象。 传递一个文件名或者一个设备指针,和图片格式给QImageWriter的构造器。你能然后设置一些选项,就像反差系数(通过setGamma())和质量(通过setQuality(). canWirte()函数返回真,如果QImageWriter能写图片(也就是,图片格式是被支持,并且设备是打开能写入)。调用write()函数来写图片到设备中。

         如果任何错误发生,当写图片时。Write()将返回错。你能然后调用error()函数来找到发生的错误类型,或者errorString()函数来获得一个可读的错误描述。

         调用supportedImageFormats()函数为一个QImageWriter能读的格式清单。QImageWriter支持所有内建的图片格式,还有那些支持写入的图片格式插件。

 

成员类型文件:

enum QImageWriter::ImageWriterError

枚举描述错误,那些当使用QImageWriter写入图片时,发生的错误。

Constant

Value

Description

QImageWriter::DeviceError

1

QImageWriter encountered a device error when writing the p_w_picpath data. Consult your device for more details on what went wrong.

QImageWriter::UnsupportedFormatError

2

Qt does not support the requested p_w_picpath format.

QImageWriter::UnknownError

0

An unknown error occurred. If you get this value after callingwrite(), it is most likely caused by a bug in QImageWriter.

成员函数文件:

QImageWriter::QImageWriter ()

构建一个QImageWriter对象。 在写入之前,你必须调用setFormat()函数来设置一个图片格式,然后setDevice()或者setFileName().

QImageWriter::QImageWriter( QIODevice * device,const QByteArray &format )

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

QImageWriter::~QImageWriter ()

bool QImageWriter::canWrite () const

int QImageWriter::compression () const

返回图片的压缩方法。

QIODevice * QImageWriter::device () const

ImageWriterError QImageWriter::error () const

QString QImageWriter::errorString () const

QString QImageWriter::fileName () const

QByteArray QImageWriter::format () const

float QImageWriter::gamma () const

int QImageWriter::quality () const

void QImageWriter::setCompression ( int compression )

compression根据不同的图片格式,此值不同。例如当格式是tiff,你可以设置01

void QImageWriter::setDevice ( QIODevice * device )

void QImageWriter::setFileName ( const QString & fileName )

void QImageWriter::setFormat ( const QByteArray & format )

QImageWriter writer;

 writer.setFormat("png"); // same as writer.setFormat("PNG");

void QImageWriter::setGamma ( float gamma )

gamma根据不同的图片格式,此值不同。例如当格式是png,你可以设置0.00.1

void QImageWriter::setQuality ( int quality )

quality根据不同的图片格式,此值不同。例如当格式是jpeg,你可以设置0100

void QImageWriter::setText ( const QString & key, const QString & text )

这是有用的来存储版权信息或者其他的信息。

QImage p_w_picpath("some/p_w_picpath.jpeg");

 QImageWriter writer("p_w_picpaths/outp_w_picpath.png", "png");

 writer.setText("Author", "John Smith");

 writer.write(p_w_picpath);

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

Format

Description

BMP

Windows Bitmap

JPG

Joint Photographic Experts Group

JPEG

Joint Photographic Experts Group

PNG

Portable Network Graphics

PPM

Portable Pixmap

TIFF

Tagged Image File Format

XBM

X11 Bitmap

XPM

X11 Pixmap

bool QImageWriter::supportsOption ( QImageIOHandler::ImageOptionoption ) const

bool QImageWriter::write ( const QImage & p_w_picpath )