Previously, opencv was compiled on QT creator. Therefore, the camera information needs to be collected as required. Therefore, I collected some information on the Internet, followed the instructions of the gourd painting, and finally successfully collected the information.
Open qtcreator and create a widget project.
Put two labels on the interface to display the data and photos collected by the camera.
The source code in widget. H is as follows:
# Ifndef widget_h
#define WIDGET_H
#include <QWidget>
#include <QImage>
# Include <qtimer> // set the data collection Interval
#include
#include <cv.h>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private slots:
Void opencamara (); // camera when enabled
Void readfarme (); // read the current frame information
Void closecamara (); // disable the camera
Void takingpictures (); // photograph
private:
Ui::Widget *ui;
QTimer *timer;
QImage *imag;
Cvcapture * Cam; // The structure of the video, which is used as a parameter of the video retrieval function.
Iplimage * frame; // apply for iplimage type pointer, that is, apply for memory space to store each frame of Image
};
#endif // WIDGET_H
The source files in widget. cpp are as follows:
# Include "widget. H"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
cam=NULL;
timer=new QTimer(this);
imag=new QImage();
connect(timer,SIGNAL(timeout()),this,SLOT(readFarme()));
connect(ui->open,SIGNAL(clicked()),this,SLOT(openCamara()));
connect(ui->pic,SIGNAL(clicked()),this,SLOT(takingPictures()));
connect(ui->close,SIGNAL(clicked()),this,SLOT(closeCamara()));
}
Widget::~Widget()
{
delete ui;
}
void Widget::openCamara()
{
cam=cvCreateCameraCapture(0);
timer->start(33);
}
void Widget::readFarme()
{
frame=cvQueryFrame(cam);
// QImage imag((const uchar*)frame->imageData,frame->width,frame->height,QImage::Format_RGB888);
QImage imag=QImage((const uchar*)frame->imageData,frame->width,frame->height,QImage::Format_RGB888).rgbSwapped();
ui->label->setPixmap(QPixmap::fromImage(imag));
}
void Widget::closeCamara()
{
timer->stop();
Cvreleasecapture (& cam); // releases the memory;
}
void Widget::takingPictures()
{
frame = cvQueryFrame(cam);
QImage imag((const uchar*)frame->imageData, frame->width, frame->height, QImage::Format_RGB888);
ui->label_2->setPixmap(QPixmap::fromImage(imag));
}
Okay, all the code is OK.
(The general idea is: Click the open camera button to enable the camera and set a timer to capture the frames captured by the camera at intervals and convert them to qimage, display on the qlabel control at the end)