日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

qt 提高图片加载速度

發布時間:2025/6/17 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 qt 提高图片加载速度 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一,將圖片在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_OBJECTpublic:explicit Widget(QWidget *parent = 0);~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 3Widget::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); //1for(int i = 0; i < line; i++){uchar* lineData = image.scanLine(i);for(int j = 0; j < sizePerLine; j++){file.write((char*)(lineData + j),1);}}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 = 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);// 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繪制黑白

轉載于:https://www.cnblogs.com/senior-engineer/p/9548271.html

總結

以上是生活随笔為你收集整理的qt 提高图片加载速度的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。