Qt5.7+opencv对摄像头的一些简单操作

 通过获取到视频流的一帧, 可以实现对摄像头获取到的视频进行放大,缩小,左右旋转以及镜像功能。。。。。

代码很少,所以就不写注释了,欢迎大家交流学习,写的不好,求轻喷。。。。。

项目下载地址:http://download.csdn.net/detail/phr_nick/9583812

Qt自带MinGW编译的opencv3.1的库文件:http://download.csdn.net/detail/phr_nick/9694167

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 
#include 
#include 
#include 
#include 
#define MIRROR 1
#define UNMIRROR 0


namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    void openCamara();
    void keyPressEvent(QKeyEvent  *event) ;

private slots:
    void readFarme();

private:
    Ui::MainWindow *ui;
    CvCapture *cam;// 视频获取结构, 用来作为视频获取函数的一个参数
    QTimer *timer;
    IplImage  *frame;//申请IplImage类型指针,就是申请内存空间来存放每一帧图像
    QImage image;
    int Current_Angle;
    float Current_Size;
    int status;
    int counts;
};

#endif // MAINWINDOW_H



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

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::FramelessWindowHint);
    counts=0;
    Current_Angle=0;
    Current_Size=1.0;
    status=UNMIRROR;
    
    timer=new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(readFarme()));
    openCamara();
}
MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::openCamara()
{
    cam = cvCreateCameraCapture(0);
    timer->start(33);
}
void MainWindow::keyPressEvent(QKeyEvent *event)
{
    if(event->key()==Qt::Key_Left)
    {
        Current_Angle = Current_Angle+90;
    }
    else if(event->key()==Qt::Key_Right)
    {
        Current_Angle =Current_Angle-90;
    }
    else if(event->key()==Qt::Key_Space)
    {
        Current_Angle=0;
        Current_Size=1;
    }
    else if(event->key()== Qt::Key_Up)
    {
        Current_Size = Current_Size*1.1;
        //        if(event->modifiers() & Qt::ControlModifier)
        
    }
    else if(event->key()== Qt::Key_Down)
    {
        Current_Size = Current_Size/1.1;
    }
    else if(event->key()==Qt::Key_Escape)
    {
        timer->stop();         // 停止读取数据。
        cvReleaseCapture(&cam);
        this->close();
    }
    else if(event->key()==Qt::Key_Tab)
    {
        counts++;
        if(counts%2==1)
            status=MIRROR;
        else
            status=UNMIRROR;
    }
    
}


void MainWindow::readFarme()
{
    frame = cvQueryFrame(cam);// 从摄像头中抓取并返回每一帧// 将抓取到的帧,转换为QImage格式。QImage::Format_RGB888不同的摄像头用不同的格式。
    image = QImage((const uchar*)frame->imageData, frame->width, frame->height, QImage::Format_RGB888).rgbSwapped();
    if(status==MIRROR)
    {
        image=image.mirrored(true,false);
    }
    
    else if(status==UNMIRROR)
    {
        image=image.mirrored(false,false);
    }
    image = image.scaled((frame->width)*Current_Size,(frame->height)*Current_Size,Qt::KeepAspectRatio);
    this->resize((frame->width)*Current_Size,(frame->height)*Current_Size);
    QMatrix matrix;
    matrix.rotate(Current_Angle);
    ui->label->setPixmap((QPixmap::fromImage(image)).transformed(matrix,Qt::SmoothTransformation));
    if(Current_Angle%180==0 )
    {
        this->resize(image.width(),image.height());
        this->move((QApplication::desktop()->width() - image.width())/2,(QApplication::desktop()->height() - image.height())/2);
    }
    else
    {
        this->resize(image.height(),image.width());
        this->move((QApplication::desktop()->width() - image.height())/2,(QApplication::desktop()->height() - image.width())/2);
    }
}

你可能感兴趣的:(Qt初体验,Qt5,摄像头,OpenCV)