diff --git a/test/videotest5/libv4l2capture.cpp b/test/videotest5/libv4l2capture.cpp index 0fc49488..e5dfbf05 100644 --- a/test/videotest5/libv4l2capture.cpp +++ b/test/videotest5/libv4l2capture.cpp @@ -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" ); diff --git a/test/videotest5/mainwindow.cpp b/test/videotest5/mainwindow.cpp index 12d07c01..2e7305dd 100644 --- a/test/videotest5/mainwindow.cpp +++ b/test/videotest5/mainwindow.cpp @@ -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 ); +} diff --git a/test/videotest5/mainwindow.h b/test/videotest5/mainwindow.h index 29bb970d..8bd02367 100644 --- a/test/videotest5/mainwindow.h +++ b/test/videotest5/mainwindow.h @@ -2,6 +2,7 @@ #define MAINWINDOW_H #include +#include 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 diff --git a/test/videotest5/mainwindow.ui b/test/videotest5/mainwindow.ui index 44243445..ee27b0b6 100644 --- a/test/videotest5/mainwindow.ui +++ b/test/videotest5/mainwindow.ui @@ -6,20 +6,52 @@ 0 0 - 400 - 300 + 176 + 328 videotest5 - + + + + + + + + + + 0 + 0 + + + + Start + + + + + + + + 0 + 0 + + + + Stop + + + + + 0 0 - 400 + 176 17 @@ -35,6 +67,14 @@ + + + QQtWidget + QWidget +
qqtwidget.h
+ 1 +
+
diff --git a/test/videotest5/qqtv4l2videomanager.cpp b/test/videotest5/qqtv4l2videomanager.cpp index 32ade0f4..1fc0b98b 100644 --- a/test/videotest5/qqtv4l2videomanager.cpp +++ b/test/videotest5/qqtv4l2videomanager.cpp @@ -1,19 +1,22 @@ #include +#include 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 ); }