当前位置:网站首页>Introduction to QT: video player

Introduction to QT: video player

2022-07-08 02:16:00 Strawberry sauce toast

Recently, due to engineering needs , Begin to learn QT Programming .

Two points learned in this process :

  1. The program is not simply stacked , Be sure to understand the role of every sentence you write ;
  2. Careful and patient , look for bug It's a great waste of patience , So think about it before you start .

This article introduces you to use QT The process of implementing a simple video player , Introduce some entry-level knowledge points encountered in this process , And the solution .


introduction

1. QT Of “ Cross platform ” characteristic

I only knew QT It's cross platform , But yes. “ platform ” Your understanding is not accurate .

When we talk about developing applications , Platform may have many different meanings , Including operating system type 、 Operating system version 、 Compiler type 、 Compiler version and processor architecture (32 position 、64 position 、ARM etc. ).

QT frame : Open source application development framework widely used to create rich visual effects , You can build and compile between different platforms , And hardly need to change any line of code , But the premise is that no non QT Or platform specific libraries .(e.g. stay Windows The next program , If used windows The library of , You cannot migrate directly to ARM The use of .) 

2. QT Supported compilers

MSVC , MinGW, GCC

3. OpenCV brief introduction

OpenCV It's open source 、 Cross platform library , For developing computer vision applications , Focus on speed and performance , It can be divided into main module and additional module .

OpenCV The main module :

  • Core functions (core modular ), Included by all others OpenCV All basic structures used by the module 、 Constants and functions ,(e.g. Mat class );
  • The image processing (imgproc modular ), Including image filtering 、 Different algorithms of image transformation ,(e.g. median, mean,......);
  • Two dimensional feature frame module (feature2d), Contains classes and methods for feature extraction and matching ;(e.g. SURF, SIFT,......)
  • Video module , Contains for motion estimation 、 Algorithms for background subtraction, tracking and other topics .

4. How to be in QT Of .pro Configuration in file OpenCV?

First , You need to select any folder ( Usually choose to build OpenCV Folder , To ensure that all are related to OpenCV All relevant files are in the same folder ), Create a folder named “opencv.pri” Text file for ;

Windows user

(1) stay opencv.pri Enter the following in the file :

INCLUDEPATH += C:/opencv/build/install/include
Debug:{
LIBS += -LC:/opencv/build/install/x64/vc16/lib/opencv_world330d
}
Release:{
LIBS += -LC:/opencv/build/install/x64/vc16/lib/opencv_world330
}

(2) take OpenCV Of DLL Add folder to system Path route ,DLL Folder location :C:/opencv/build/install/x64/vc16/bin

Macos/Linux user  

stay opencv.pri Add the following to the file , There is no need to configure the path

INCLUDEPATH += /usr/local/include
LIBS += -L/usr/local/lib \
        -lopencv_world

To configure .pro file  

stay .pro Add at the end of the file :

include(C:/opencv/opencv.pri)

One 、 Interface layout

At first, I was in a state of confusion , Several projects have been established , Decide to build the interface first and then write the code . This process really breaks through itself “ be anxious for success ” Mentality and habits , I feel that learning programming is really like wild fungus , Learning in scattered ways is not enough to get started , So this semester always falls into ” How can I do this “ In self doubt ( I must have written too little to make this kind of critical hit more violent )

The final interface is shown in the figure 1

  • Use QLabel Control displays an image
  • Contains the selection file , Start 、 Pause play , Forward 、 back off , Display current progress and total length of video ;
  • Save function : Save a single frame , Save a video ;
  • Display adjustment : brightness 、 Contrast adjustment ; Enlarge the display and display the original size .

                                                                    chart 1 Video player layout and functions to be realized

Two 、 Open file

The mode selected here is : Click on “ Open file ”, eject “ File selection dialog ”, The function used is :

https://blog.csdn.net/liyj127/article/details/54090705

QString filename = QFileDialog::getOpenFileName(this, "Open Input File",             
                    QDir::currentPath(), "FileType (*.jpg *png *.bmp)");
if (QFile::exists(filename))
{
    ……
}                

QString turn char*

QString  str;
char*  ch;
QByteArray ba = str.toLatin1(); // must
ch=ba.data();

3、 ... and 、QLabel Display images

The trouble with this step is that the author reads pixel values byte by byte from the file , And the value needs to be grayscale mapped and displayed as 8bit grayscale .

  1. Used to store 16-bit And 8-bit The data types of pixel values of are selected as unsigned short And unsigned char;
  2. To use QLable The display image needs data type conversion :unsigned char---> QImage---> QPixmap;

unsigned char Array rotation QImage

QImage qimg = QImage(&ch[0], img_width, img_height, m_width, QImage::Format_Grayscale8);

The first parameter is zero char Array address ; second 、 The three parameters are the width and height of the image ; The fourth parameter is the number of bytes per line of the image ; The fifth parameter refers to QImage Format Introduction .

QLable Display images

//  Display image size and QLabel Same size 
ui->ImageLabel->setPixmap(QPixmap::fromImage(qimg).scaled(ui->ImageLabel->size()));

//  Display the size of the original image 
ui->ImageLabel->setPixmap(QPixmap::fromImage(qimg));

// QImage  turn  QPixmap
QPixmap pixmap = QPixmap::fromImage(qimg);

Four 、 Refresh the interface

Interface refresh use QTimer class

//  establish QTimer object 
QTimer* qtimer = new QTimer(this);
 
//  Set refresh rate 
qtimer->start(200);  //  Every time 200ms Refresh once 

//  perhaps 
qTimer->setInterval(200);
qTimer->start();

//  Judge timer The state of 
qtimer->isActive();  // true --  Start refreshing ;false --  Stop refreshing 

//  Stop refreshing 
qtimer->stop(); 

//  binding update() function 
connect(qtimer, SIGNAL(timerout()), this, SLOT(update()));

update() The function can be replaced by a custom slot function , But this function should include update() function . 

5、 ... and 、 some Bug

1. Right click the control to automatically create a slot function , After the slot function is deleted ,moc_..... File compilation error

Solution : Not only in .cpp Delete the definition of slot function in the file , Also delete .h File slot function declaration and rebuild the project , Otherwise, an error will be reported when building the project .

2. Custom slot function

The custom slot function must be declared in private slots: below , For example, customized and timer Bound slot function

Otherwise connect It won't work !

3. Use pointer with caution

 

原网站

版权声明
本文为[Strawberry sauce toast]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130540433509.html