1
0
mirror of https://gitee.com/drabel/LibQQt.git synced 2025-01-04 10:18:44 +08:00

update videotest5 code.

This commit is contained in:
tianduanrui 2019-09-08 09:15:01 +08:00
parent 248bd8397e
commit 5ebd3df8bd
5 changed files with 108 additions and 31 deletions

View File

@ -74,7 +74,7 @@ static int init_mmap ( struct cap_handle* handle )
return -1;
}
handle->buffers = (buffer_t*)calloc ( req.count, sizeof ( struct buffer_t ) );
handle->buffers = ( buffer_t* ) calloc ( req.count, sizeof ( struct buffer_t ) );
if ( !handle->buffers )
{
printf ( "--- Calloc memory failed\n" );
@ -250,7 +250,7 @@ static int init_device ( struct cap_handle* handle )
struct cap_handle* capture_open ( struct cap_param param )
{
int ret;
struct cap_handle* handle = (struct cap_handle*)malloc ( sizeof ( struct cap_handle ) );
struct cap_handle* handle = ( struct cap_handle* ) malloc ( sizeof ( struct cap_handle ) );
if ( !handle )
{
printf ( "--- malloc capture handle failed\n" );

View File

@ -1,14 +1,32 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
MainWindow::MainWindow ( QWidget* parent ) :
QMainWindow ( parent ),
ui ( new Ui::MainWindow )
{
ui->setupUi(this);
ui->setupUi ( this );
manager = new QQtV4L2VideoManager ( this );
connect ( manager, SIGNAL ( readyRead ( QImage ) ), this, SLOT ( slotCaptured ( QImage ) ) );
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
manager->startCapture();
}
void MainWindow::on_pushButton_2_clicked()
{
manager->stopCapture();
}
void MainWindow::slotCaptured ( const QImage& img )
{
ui->widget->setPixmap ( img );
}

View File

@ -2,6 +2,7 @@
#define MAINWINDOW_H
#include <QMainWindow>
#include <qqtv4l2videomanager.h>
namespace Ui {
class MainWindow;
@ -12,11 +13,18 @@ class MainWindow : public QMainWindow
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
explicit MainWindow ( QWidget* parent = 0 );
~MainWindow();
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
void slotCaptured ( const QImage& img );
private:
Ui::MainWindow *ui;
Ui::MainWindow* ui;
QQtV4L2VideoManager* manager;
};
#endif // MAINWINDOW_H

View File

@ -6,20 +6,52 @@
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
<width>176</width>
<height>328</height>
</rect>
</property>
<property name="windowTitle">
<string>videotest5</string>
</property>
<widget class="QWidget" name="centralWidget"/>
<widget class="QWidget" name="centralWidget">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0" colspan="2">
<widget class="QQtWidget" name="widget" native="true"/>
</item>
<item row="1" column="0">
<widget class="QPushButton" name="pushButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Start</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QPushButton" name="pushButton_2">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Stop</string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<width>176</width>
<height>17</height>
</rect>
</property>
@ -35,6 +67,14 @@
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<customwidgets>
<customwidget>
<class>QQtWidget</class>
<extends>QWidget</extends>
<header>qqtwidget.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>

View File

@ -1,19 +1,22 @@
#include <qqtv4l2videomanager.h>
#include <linux/videodev2.h>
QQtV4L2VideoManager::QQtV4L2VideoManager ( QObject* parent ) : QObject ( parent )
{
handler = 0;
pp = 0;
frame = 0;
mDevName = "/dev/video0";
mSize = QSize ( 720, 480 );
mFormat = 0; //NV21? YUV? RGB?
mRate = 30;
mFormat = V4L2_PIX_FMT_NV21; //NV21? YUV? RGB?
mRate = 10;
timer = new QTimer(this);
timer->setSingleShot(false);
timer->setInterval(200);
connect(timer, SIGNAL(timeout()), this, SLOT(slotGetCapture()));
timer = new QTimer ( this );
timer->setSingleShot ( false );
timer->setInterval ( 100 );
connect ( timer, SIGNAL ( timeout() ), this, SLOT ( slotGetCapture() ) );
}
QQtV4L2VideoManager::~QQtV4L2VideoManager() {}
@ -40,14 +43,16 @@ void QQtV4L2VideoManager::setVideoRate ( int rate )
void QQtV4L2VideoManager::startCapture()
{
if(handler)
capture_close(handler);
if ( handler )
capture_close ( handler );
struct cap_param param;
param.dev_name = mDevName.toLocal8Bit().data();
param.width = mSize.width();
param.height = mSize.height();
param.rate = mRate;
handler = capture_open(param);
param.pixfmt = mFormat;
handler = capture_open ( param );
pp = ( unsigned char* ) malloc ( param.width * param.height * 3 * sizeof ( char ) );
frame = new QImage ( pp, param.width, param.height, QImage::Format_RGB888 );
@ -56,11 +61,17 @@ void QQtV4L2VideoManager::startCapture()
void QQtV4L2VideoManager::stopCapture()
{
if(handler)
capture_close(handler);
timer->stop();
if ( handler )
capture_close ( handler );
if ( pp )
free ( pp );
if ( frame )
delete frame;
}
int QQtV4L2VideoManager::convert_yuv_to_rgb_pixel(int y, int u, int v)
int QQtV4L2VideoManager::convert_yuv_to_rgb_pixel ( int y, int u, int v )
{
unsigned int pixel32 = 0;
unsigned char* pixel = ( unsigned char* ) &pixel32;
@ -77,7 +88,7 @@ int QQtV4L2VideoManager::convert_yuv_to_rgb_pixel(int y, int u, int v)
return pixel32;
}
int QQtV4L2VideoManager::convert_yuv_to_rgb_buffer(unsigned char *yuv, unsigned char *rgb, unsigned int width, unsigned int height)
int QQtV4L2VideoManager::convert_yuv_to_rgb_buffer ( unsigned char* yuv, unsigned char* rgb, unsigned int width, unsigned int height )
{
unsigned int in, out = 0;
unsigned int pixel_16;
@ -88,10 +99,10 @@ int QQtV4L2VideoManager::convert_yuv_to_rgb_buffer(unsigned char *yuv, unsigned
for ( in = 0; in < width * height * 2; in += 4 )
{
pixel_16 =
yuv[in + 3] << 24 |
yuv[in + 2] << 16 |
yuv[in + 1] << 8 |
yuv[in + 0];
yuv[in + 3] << 24 |
yuv[in + 2] << 16 |
yuv[in + 1] << 8 |
yuv[in + 0];
y0 = ( pixel_16 & 0x000000ff );
u = ( pixel_16 & 0x0000ff00 ) >> 8;
y1 = ( pixel_16 & 0x00ff0000 ) >> 16;
@ -118,7 +129,7 @@ int QQtV4L2VideoManager::convert_yuv_to_rgb_buffer(unsigned char *yuv, unsigned
void QQtV4L2VideoManager::slotGetCapture()
{
int len = 0;
capture_get_data(handler, (void**)&p, &len);
convert_yuv_to_rgb_buffer(p, pp, mSize.width(), mSize.height());
capture_get_data ( handler, ( void** ) &p, &len );
convert_yuv_to_rgb_buffer ( p, pp, mSize.width(), mSize.height() );
emit readyRead ( *frame );
}