Embedded QT usb camera library driver camera

Table of contents

Preface

Edit 

1. Cross compile ffmpeg

 1.1 First get the compressed package of ffmpeg

1.2 Copy the ffmpeg source code to the virtual machine and decompress the compressed package 

1.3 After decompression, we need to enter the decompressed file and compile it into a library 

1.5 make 

1.6 make install

2.usbcame library usage 

  2.1 Obtain the usbcame library and decompress it

 2.2 After decompression is completed, we enter the ussbcamer folder and we will see that there are two folders including include and lib.

 2.3 Copy the include under ffmpeg compiled in the previous chapter to the include under usbcamera

2.4 Copy the lib under ffmpeg to the lib under usbcamera

2.5 Add the usbcamera library to our project 

3.Qwidget class improves USBCameraWidget

 3.1 Right-click the Qwidget you want to upgrade and click Upgrade to:

3.2 Then we need to find the class name we want to improve and copy it. USBCameraWidget also inherits the Qwidget parent class:  

4. Compile 

4.1 Compilation

 4.2 The following problems occur 

4.3 Application output errors 

5. Camera use 

  5.1 Camera initialization

5.2 Taking photos 

5.3 Display labels


Preface

   Why not use QT's own camera class instead of using the usbcamera class? Because most of the UVC cameras on the market are now used , because UVC cameras are low-priced and easy to use ; they require no driver installation ; they have less hardware wiring and the USB interface is highly versatile. UVC is a camera protocol . USB cameras basically use UVC protocol. Many merchants say that the camera is "driver-free". It is not really driver-free. It is just that the system comes with the UVC driver , so they dare to say "driver-free". .Because UVC cameras are very, very, very widely used, many systems have integrated UVC drivers.

The usbcamera library requires the support of ffmpeg ; it is very suitable for operating UVC cameras ; it is more convenient than the QCamera class that comes with Qt .

 

1. Cross compile ffmpeg

 1.1 First get the compressed package of ffmpeg

1.2 Copy the ffmpeg source code to the virtual machine and decompress the compressed package 

sudo tar -xf ffmpeg-3.4.5.tar.gz

1.3 After decompression, we need to enter the decompressed file and compile it into a library 

cd ffmpeg-3.4.5/

Then open the document in the screenshot above which contains the compilation method. This is the compilation method for 32- bit systems.

 

1.4 Compile the source code (32-bit system), but the command to configure the source code needs to be modified according to our own compiler. We circle the places that need to be modified:

 

The cross-compiler I use is arm-cortexa9-linux-gnueabihf- . So it needs to be changed to:

./configure --cross-prefix=arm-cortexa9-linux-gnueabihf- --enable-cross-compile --target-os=linux --cc=arm
cortexa9-linux-gnueabihf-gcc --arch=arm --prefix=host --enable-shared --disable-static --disable-doc --disable
x86asm --enable-ffplay

If you are using a 64- bit system, use a 64- bit compiler: The following is the configuration of the Xinyingda A53 development board:

./configure --cross-prefix=aarch64-linux- --cpu=cortex-a53 --disable-asm --enable-cross-compile --target
os=linux --cc=aarch64-linux-gcc --arch=arm --prefix=$PWD/ffmpeg-3.4.5 --enable-shared --disable-static --
disbable-doc --disbale-x86asm

1.5 make 

1.6 make install

 after finishing:


2.usbcame library usage 

  2.1 Obtain the usbcame library and decompress it

tar -xf usbcamera.tar.gz  

 2.2 After decompression is completed, we enter the ussbcamer folder and we will see that there are two folders including include and lib.

,as follows:

 2.3  Copy the include under ffmpeg compiled in the previous chapter to the include under usbcamera

At this time, there is nothing in include, we can go in and take a look.

Include is used to put header files, so we need to copy the include files of ffmpeg ( note the installation path of ffmpeg
diameter );
cp include/lib* /home/xing/work/usbCamera/usbCamera/usbcamera/include/ -rf

2.4   Copy the lib under ffmpeg to the lib under usbcamera

The operation is almost the same as copying the include header file.

2.5 Add the usbcamera library to our project 

Just add ffmpeg.pri to our project. Note, do not add files by right-clicking. Just add it in the xxx.pro file

 

include($$PWD/usbcamera/ffmpeg.pri)
INCLUDEPATH += $$PWD/usbcamera/

3. Qwidget class improves USBCameraWidget

 3.1 Right-click the Qwidget you want to upgrade and click Upgrade to:

3.2  Then we need to find the class name we want to improve and copy it. USBCameraWidget also inherits the Qwidget parent class: 


4. Compile 

4.1 Compilation

Solution: Add the network module to the pro file

 

 4.2 The following problems occur 

The reason is that the currently used qt version is greater than 5.5 , using QtMultimedia and QtMultimediaWidgets , which are only available in Qt5 and not in QT4. Here we use the QT4 method to use usbcame , and the interface changes 5.5.0 to a version greater than the one we use ( 5.8 or 5.9 ), note that all 5.5.0 needs to be changed. In the usbcame.cpp and usbcame.h files .

We change it to:

 

4.3 Application output errors 

This error occurs because there is no library. Copy the library to the development board. Be careful to compress first and then copy.

 After modification:

 

No more errors!


5. Camera use 

  5.1 Camera initialization

void Widget::cameraInit()
{
 ui->widget->setCameraName("/dev/video0"); //设置摄像头设备的驱动节点
 ui->widget->setInterval(120); //设置获取摄像头图像周期(ms),就是每隔多少 ms 获取一帧图像
 ui->widget->open();//打开摄像头
}
The following error occurs because a 64 -bit compiler is used and macros need to be defined.

 Just run it again after modification:

 

5.2 Taking photos 

void Widget::on_takePhotoBtn_clicked()
{
 QImage img = ui->camerawidget->getImage(); //获取摄像头的一帧
QPixmap pix = QPixmap::fromImage(img); //转成 qpixmap
// ui->labVisiterHeader->setPixmap(pix.scaled(ui->labVisiterHeader->size()));//lable 显示
 QString fileName = QDateTime::currentDateTime().toString("yyyy-MM-dd-hh-mm-ss");
fileName += ".jpg";
#if 1
 QString path = QFileDialog::getExistingDirectory(this,tr("选择一个文件夹"),
 "/",
 QFileDialog::ShowDirsOnly |
QFileDialog::DontResolveSymlinks) ;

//qDebug() << "path=" <<path +"/" + fileName;
 /*保存图片*/
 if(pix.save(path + "/" + fileName) == false){
 qDebug( "pic save error" );
 }
#else
 if(pix.save("/root/"+fileName) == false){
 qDebug( "pic save error" );
 }
#endif
}

5.3 Display labels

void Widget::cameraInit()
{
 ui->usbCameraWidget->setOSD1Visible(true);
 ui->usbCameraWidget->setOSD1Format(USBCameraWidget::OSDFormat_Text);
 ui->usbCameraWidget->setOSD1Text("XYD");
 ui->usbCameraWidget->setOSD1Color(QColor(0, 255, 0));
 ui->usbCameraWidget->setOSD1FontSize(40);
 ui->usbCameraWidget->setOSD2Visible(true);
 ui->usbCameraWidget->setOSD2Format(USBCameraWidget::OSDFormat_DateTime);
 
ui->usbCameraWidget->setOSD2Position(USBCameraWidget::OSDPosition_Right_Bottom);
}

Finish!

Guess you like

Origin blog.csdn.net/Lushengshi/article/details/131286212