qt 提高图片加载速度

时间:2022-06-05 14:30:41

一,将图片在pc上解析,然后将解析文件放到qrc文件中,读取qrc文件。

1,将图片解析后的二进制文件保存,源码如下,

下载地址:https://files.cnblogs.com/files/senior-engineer/imageTest.rar

main.cpp

#include "widget.h"
#include <QApplication> int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show(); return a.exec();
}

widget.h

#ifndef WIDGET_H
#define WIDGET_H #include <QWidget> namespace Ui {
class Widget;
} class Widget : public QWidget
{
Q_OBJECT public:
explicit Widget(QWidget *parent = );
~Widget(); private slots:
void saveImageToFile();
void refreshImage(); private:
Ui::Widget *ui; private:
QString m_fileName;
int m_imgWidth;
int m_imgHeight;
}; #endif // WIDGET_H

widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include <QImage>
#include <QFile>
#include <QDebug>
#include <QResource> #define IMAG_BIT 3 Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
//m_fileName = "radio_but_eq_p.png";
m_fileName = "bg.png";
ui->imageName->setText(m_fileName); // QResource resource;
// resource.setFileName(":/main_bg.png");
// const uchar* imageData = resource.data();
// QImage desImage = QImage(imageData, 800,480,QImage::Format_RGB32); //RGB32
// QPixmap pixmag;
// pixmag = QPixmap::fromImage(desImage);
// ui->oldImage->setPixmap(pixmag); QPixmap pixmap(":/main_bg.png");
ui->oldImage->setPixmap(pixmap); // QPixmap pixmag;
// pixmag.load(m_fileName);
// ui->oldImage->setPixmap(pixmag); connect(ui->start, SIGNAL(clicked(bool)), this, SLOT(saveImageToFile()));
connect(ui->refresh, SIGNAL(clicked(bool)), this, SLOT(refreshImage()));
} Widget::~Widget()
{
delete ui;
} void Widget::saveImageToFile()
{
QImage image(m_fileName);
// QImage image = QImage(800,480,QImage::Format_RGB888); //RGB32 // image.load(m_fileName);
int size = image.byteCount();
int sizePerLine = image.bytesPerLine();
int line = size / sizePerLine;
m_imgWidth = image.width();
m_imgHeight = image.height(); qDebug() << "size:" <<size;
qDebug() << "sizePerLine:" << sizePerLine;
qDebug() << "line:" << line;
qDebug() << "width:" << m_imgWidth; uchar* imgDataNew = image.bits(); FILE *pf = fopen("newfile.png", "wb");
if(pf == NULL)
return;
QFile file;
file.open(pf, QIODevice::WriteOnly); //
for(int i = ; i < line; i++)
{
uchar* lineData = image.scanLine(i);
for(int j = ; j < sizePerLine; j++)
{
file.write((char*)(lineData + j),);
}
} file.close(); // QImage desImage = QImage(m_imgWidth,m_imgHeight,QImage::Format_RGB32); //RGB32 // //RGB分量值
// int b = 0;
// int g = 0;
// int r = 0;
// int a = 0; // //设置像素
// for (int i=0;i < m_imgHeight;i++)
// {
// for (int j=0; j < m_imgWidth * 4;)
// {
// b = (int)*(imgDataNew + i * m_imgWidth * 4 + j);
// g = (int)*(imgDataNew + i * m_imgWidth * 4 + j + 1);
// r = (int)*(imgDataNew + i * m_imgWidth * 4 + j + 2);
// a = (int)*(imgDataNew + i * m_imgWidth * 4 + j + 3);
// desImage.setPixel(j / 4,i,qRgba(r,g,b,a));
// j = j + 4;
// //desImage.setPixel(j,i,qRgb(r,g,b));
// }
// } // QPixmap pixmag;
// pixmag = QPixmap::fromImage(desImage);
// ui->newImage->setPixmap(pixmag);
} void Widget::refreshImage()
{
QFile file(":/newfile.png");
//if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
if (!file.open(QIODevice::ReadOnly))
return;
QByteArray array = file.readAll();
//file.close();
char* imgDataNew = array.data(); QImage desImage = QImage(m_imgWidth,m_imgHeight,QImage::Format_RGB32); //RGB32 //RGB分量值
int b = ;
int g = ;
int r = ;
int a = ; //设置像素
for (int i=;i < m_imgHeight;i++)
{
for (int j=; j < m_imgWidth * ;)
{
b = (int)*(imgDataNew + i * m_imgWidth * + j);
g = (int)*(imgDataNew + i * m_imgWidth * + j + );
r = (int)*(imgDataNew + i * m_imgWidth * + j + );
a = (int)*(imgDataNew + i * m_imgWidth * + j + );
desImage.setPixel(j / ,i,qRgba(r,g,b,a));
j = j + ;
//desImage.setPixel(j,i,qRgb(r,g,b));
}
} QPixmap pixmag;
pixmag = QPixmap::fromImage(desImage);
ui->newImage->setPixmap(pixmag); // QImage desImage ;
// if(IMAG_BIT == 3)
// desImage = QImage(m_imgWidth,m_imgHeight,QImage::Format_RGB888); //RGB24
// else if(IMAG_BIT == 4)
// desImage = QImage(m_imgWidth,m_imgHeight,QImage::Format_RGB32); //RGB32 // //RGB分量值
// char b = 0;
// char g = 0;
// char r = 0;
// char a = 0; // //设置像素
// for (int i=0;i < m_imgHeight;i++)
// {
// for (int j=0; j < m_imgWidth * IMAG_BIT;)
// {
// b = (int)*(imgDataNew + i * m_imgWidth * IMAG_BIT + j);
// g = (int)*(imgDataNew + i * m_imgWidth * IMAG_BIT + j + 1);
// r = (int)*(imgDataNew + i * m_imgWidth * IMAG_BIT + j + 2);
// if(IMAG_BIT == 4)
// {
// a = (int)*(imgDataNew + i * m_imgWidth * IMAG_BIT + j + 3);
// desImage.setPixel(j / IMAG_BIT,i,qRgba(r,g,b,a));
// }
// else if(IMAG_BIT == 3)
// {
// desImage.setPixel(j / IMAG_BIT, i, qRgb(r,g,b));
// }
// j = j + IMAG_BIT;
// }
// } // QPixmap pixmag;
// pixmag = QPixmap::fromImage(desImage);
// ui->newImage->setPixmap(pixmag);
}

二,此方法已经试过了,但是open文件时候很耗时,大概200~300ms

(!file.open(QIODevice::ReadOnly))

二,直接加图片放到qrc文件中,读取qrc文件

一,qrc文件中添加图片资源

二,再.pro文件中添加qrc文件

例如:  RESOURCES     = res.qrc

三,在程序中使用图片文件时候,通过“:文件名”

例如:image=new QImage(":1.png");

四,此方法原理对应qt官方文档地址:http://doc.qt.io/qt-5/resources.html

三,将图片转换为数组,然后将图片画到界面上(适用绘制字体)

一,用下面工具将图片转换为数组

https://files.cnblogs.com/files/senior-engineer/fileToC.rar

二,更具对应数每个bit对应的是1或者是0绘制黑白