Multi-thread video capture and playback in QT Linux v4l2

Source: Internet
Author: User

I. Build a QT Project

2. Draw a display window

3. Add a program (because the v4l2 program is too long to be posted, please download it to the resource)

1. Working thread code

video_thread::video_thread():QThread()
{
    quit_flag = false;
}
video_thread::~video_thread()
{
    this->quit();
    quit_flag = true;
    this->wait();
}
void video_thread::run()
{
M_video.init_video (); // enable the video device.
    while(!quit_flag)
    {
M_video.get_data (); // get video data
    unsigned char *rgb;
RGB = new unsigned char [m_video.buf.length * 2]; // allocate RGB based on the ratio of the yuyv pixel to the station memory of the RGB Pixel
M_video.yuvtorgbo (RGB, 640,480); // convert yuyv to RGB
    QImage img = QImage(rgb,640,480,QImage::Format_RGB888);
    emit image_data(img);
    msleep(150);
Delete [] RGB; // It cannot be released before sleep. Otherwise, the displayed image may have problems.
   }
M_video.release_video (); // release the device before the thread is released.
}

2. Main thread Program

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(ui->show_ptn,SIGNAL(clicked()),this,SLOT(start_thread()));
    connect(ui->stop_ptn,SIGNAL(clicked()),this,SLOT(stop_thread()));
    connect(ui->quit_ptn,SIGNAL(clicked()),this,SLOT(close()));
}
void MainWindow::start_thread()
{
    video = new video_thread();
    video->start();
    connect(video,SIGNAL(image_data(const QImage &)),this,SLOT(show_picture(const QImage &)));
}
void MainWindow::show_picture(const QImage &img)
{
     ui->show_label->setPixmap(QPixmap::fromImage(img));
}
void MainWindow::stop_thread()
{
    delete video;
}
MainWindow::~MainWindow()
{
    delete ui;
}

3. Program

: Click to open the link

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.