MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QImage img;
if(!img.load("C://Users//Administrator//Pictures//test.jpg")){
return;
}
int pWidth = img.width();
int pHeight = img.height();
qDebug("读取成功%d, %d" , pWidth, pHeight);
// img.copy的四个参数就是切割区域的代码
QImage pimg = img.copy(0, 0, pWidth / 2, pHeight / 2);
pimg.save("C://Users//Administrator//Pictures//temp//test2.jpg");
}
- 将图片读入内存中之后(char*,windth, high),然后将char*保存为另一张图片
QImage img;
if(!img.load("C://Users//Administrator//Pictures//test.jpg")){
return;
}
const uchar * data24 = img.bits();
// 第一种方法: QImage(const uchar *data, int width, int height, Format format, QImageCleanupFunction cleanupFunction = nullptr, void *cleanupInfo = nullptr);
// QImage timg = QImage(data24, img.width(), img.height(), QImage::Format_ARGB32);
// 第二种方法:QImage(const uchar *data, int width, int height, int bytesPerLine, Format format, QImageCleanupFunction cleanupFunction = nullptr, void *cleanupInfo = nullptr);
QImage timg = QImage(data24, img.width(), img.height(), img.bytesPerLine(), QImage::Format_ARGB32);
image.save("C://Users//Administrator//Pictures//ddddd.jpg");
QImage image;
QByteArray pData;
QFile *file=new QFile("C://Users//Administrator//Pictures//test.jpg");
file->open(QIODevice::ReadOnly);
pData=file->readAll();
image.loadFromData(pData);
图片与QByteArray的相互转换
// 读取图片
QImage img;
if(!img.load("C://Users//Administrator//Pictures//test.jpg")){
return;
}
// ------将图片转为QByteArray------------------
QByteArray ByteArray;
QBuffer buffer(&ByteArray);
buffer.open(QIODevice::WriteOnly);
bool bOk = img.save(&buffer ,"jpg", 20);
qDebug("*****:%d", bOk);
// -----------QByteArray转为图片-------------
QImage timg;
// 第一种方法:ok
// inline static QImage fromData(const QByteArray &data, const char *format = nullptr),
//timg = timg.fromData(ByteArray);
// 第二种方法:ok
// static QImage fromData(const uchar *data, int size, const char *format = nullptr);
timg = timg.fromData(reinterpret_cast<const uchar *>(ByteArray.constData()), ByteArray.size());
// 第三种方法:ok
//inline bool loadFromData(const QByteArray &data, const char *aformat = nullptr)
timg.loadFromData(ByteArray);
// 第四种方法
//loadFromData(const uchar *buf, int len, const char *format = nullptr)
timg.loadFromData(reinterpret_cast<const uchar *>(ByteArray.constData()), ByteArray.size());