使用Qt5将OV7725的RGB565图像数据转换为png图像

文章描述了一个使用C++Qt开发的程序,主要功能是处理ov7725摄像头的图像数据,包括读取文本文件中的像素数据,将图像合成并在窗口中显示,并提供保存图片和重新生成的功能。

效果图

源码

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    init();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::init()
{
    this->setWindowTitle("ov7725图像合成");

    ui->label->setStyleSheet("background-color:#d5d5d5;");
    this->setWindowFlags(Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint);
    this->setFixedSize(this->width(),this->height());
    ui->sava->setEnabled(false);
    ui->r->setEnabled(false);
}
char MainWindow::strToNum(QString x)
{
    bool ok;
    return (x.toInt(&ok,16));
}

void MainWindow::create(QString s)
{
    char b1,b2,b3,b4;
    int rgb;
    char r,g,b;
    for(int x = 0; x < 320*240; x++){
        b1 = strToNum(s.at(0+(x*6)));
        b2 = strToNum(s.at(1+(x*6)));
        b3 = strToNum(s.at(3+(x*6)));
        b4 = strToNum(s.at(4+(x*6)));
        rgb = (b1 << 12) + (b2 << 8) + (b3 << 4) + b4;
        r = ((rgb & 0xF800) >> 8) & 0xFF;
        g = ((rgb & 0x7E0 ) >> 3) & 0xFF;
        b = ((rgb & 0x1F) << 3) & 0xFF;
        soure.setPixel(x%320,x/320,(r << 16) + (g << 8) + b);
    }
    ui->label->setPixmap(QPixmap::fromImage(soure));
    ui->sava->setEnabled(true);
    ui->r->setEnabled(true);
}

void MainWindow::on_open_clicked()
{
    soure = QImage(":/new/prefix1/qrc/void.jpg");
    fileaddr = QFileDialog::getOpenFileName(this,"请选择源文件",QDir::currentPath(),"*.txt");
    QFile read(fileaddr);
    if(!read.exists()){
        return;
    }
    if(read.open(QIODevice::ReadOnly | QIODevice::Text)){
        QString Text = read.readAll();
        ui->plainTextEdit->setPlainText(Text);
        create(Text);
    }


}

void MainWindow::on_sava_clicked()
{
    QString fileaddr = QFileDialog::getSaveFileName(this,"*.png",QDir::currentPath(),"*.png");
    soure.save(fileaddr,"png");
}

void MainWindow::on_r_clicked()
{

    create(ui->plainTextEdit->toPlainText());
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QPixmap>
#include <QImage>
#include <QColor>
#include <QFile>
#include <QDir>
#include <QFileDialog>
#include <QDebug>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    void init();
    char strToNum(QString x);
    void create(QString s);

private slots:
    void on_open_clicked();
    void on_sava_clicked();
    void on_r_clicked();

private:
    QImage soure;
    QString fileaddr;

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

mainwindow.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>666</width>
    <height>367</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <layout class="QVBoxLayout" name="verticalLayout">
    <item>
     <layout class="QHBoxLayout" name="horizontalLayout_2">
      <item>
       <widget class="QPlainTextEdit" name="plainTextEdit">
        <property name="minimumSize">
         <size>
          <width>320</width>
          <height>240</height>
         </size>
        </property>
       </widget>
      </item>
      <item>
       <widget class="QLabel" name="label">
        <property name="minimumSize">
         <size>
          <width>320</width>
          <height>240</height>
         </size>
        </property>
        <property name="text">
         <string>图像还没有生成嘞</string>
        </property>
        <property name="scaledContents">
         <bool>false</bool>
        </property>
        <property name="alignment">
         <set>Qt::AlignCenter</set>
        </property>
        <property name="wordWrap">
         <bool>false</bool>
        </property>
       </widget>
      </item>
     </layout>
    </item>
    <item>
     <layout class="QHBoxLayout" name="horizontalLayout_3">
      <item>
       <widget class="QPushButton" name="open">
        <property name="minimumSize">
         <size>
          <width>0</width>
          <height>40</height>
         </size>
        </property>
        <property name="text">
         <string>打开源文件</string>
        </property>
        <property name="autoRepeat">
         <bool>false</bool>
        </property>
       </widget>
      </item>
      <item>
       <widget class="QPushButton" name="r">
        <property name="minimumSize">
         <size>
          <width>0</width>
          <height>40</height>
         </size>
        </property>
        <property name="text">
         <string>重新生成</string>
        </property>
       </widget>
      </item>
      <item>
       <widget class="QPushButton" name="sava">
        <property name="minimumSize">
         <size>
          <width>0</width>
          <height>40</height>
         </size>
        </property>
        <property name="text">
         <string>保存图片</string>
        </property>
       </widget>
      </item>
     </layout>
    </item>
   </layout>
   <zorder></zorder>
   <zorder></zorder>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>666</width>
     <height>20</height>
    </rect>
   </property>
  </widget>
  <widget class="QToolBar" name="mainToolBar">
   <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
   </attribute>
   <attribute name="toolBarBreak">
    <bool>false</bool>
   </attribute>
  </widget>
  <widget class="QStatusBar" name="statusBar"/>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>

模板图片(:/new/prefix1/qrc/void.jpg)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值