Qt如何在控制台项目中使用opencv打开视频?
重要代码:
1、在pro文件中这样设置:
QT -= gui
QT += core widgets serialport
2、不要继承和使用:QCoreApplication
#include
pro文件:
```cpp
QT -= gui
QT += core widgets serialport
CONFIG += c++11 console
CONFIG -= app_bundle
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp
win32{
INCLUDEPATH += D:/opencv4.1.2/build/install/include/ \
C:/Boost/include/boost-1_70/
LIBS += D:/opencv4.1.2/build/install/x86/mingw/bin/libopencv_*.dll \
C:/Boost/lib/libboost_*-mgw53-mt-d-x32-1_70.a
}
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
#include <QApplication>
#include <QSerialPort>
#include <QByteArray>
#include <QImage>
#include <QBuffer>
#include <opencv2/opencv.hpp>
#include <QDebug>
#include <QTimer>
class Server : public QApplication
{
Q_OBJECT
public:
Server(int &argc, char **argv);
~Server();
public slots:
void startCapture();
void stopCapture();
void sendDataToClient();
private:
QSerialPort *serialPort;
cv::VideoCapture videoCapture;
QTimer *timer;
};
Server::Server(int &argc, char **argv)
: QApplication(argc, argv),
serialPort(new QSerialPort(this)),
timer(new QTimer(this))
{
// 设置串口参数
serialPort->setPortName("COM2");
serialPort->setBaudRate(QSerialPort::Baud9600);
serialPort->setDataBits(QSerialPort::Data8);
serialPort->setParity(QSerialPort::NoParity);
serialPort->setStopBits(QSerialPort::OneStop);
// 打开默认摄像头
// 打开网络摄像头的RTSP流
QString rtspUrl = "rtsp://admin:abcd1234@192.168.2.174:554/h264/ch1/sub/av_stream"; // 替换为实际的摄像头IP和端口
videoCapture.open(rtspUrl.toStdString());
// 连接定时器信号
connect(timer, &QTimer::timeout, this, &Server::sendDataToClient);
// 连接OpenCV的读取完成信号
connect(this, &Server::aboutToQuit, this, &Server::stopCapture);
}
Server::~Server()
{
delete serialPort;
delete timer;
}
void Server::startCapture()
{
if (!videoCapture.isOpened())
{
qDebug() << "Error: Unable to open the camera.";
return;
}
// 启动定时器,每隔一段时间发送视频帧到客户端
timer->start(1); // 每100毫秒触发一次定时器
}
void Server::stopCapture()
{
timer->stop();
videoCapture.release();
}
void Server::sendDataToClient()
{
cv::Mat frame;
videoCapture.read(frame);
if (!frame.empty())
{
cv::imshow("123",frame);
// 将 OpenCV Mat 转换为 Qt 图像
QImage image(frame.data, frame.cols, frame.rows, frame.step, QImage::Format_RGB888);
QByteArray imageData;
QBuffer buffer(&imageData);
buffer.open(QIODevice::WriteOnly);
image.save(&buffer, "JPEG"); // 将图像保存为JPEG格式
buffer.close();
// 发送数据到客户端
if (serialPort->isOpen() && serialPort->isWritable())
{
std::cout<<"send.....2!"<<std::endl;
serialPort->write(imageData);
serialPort->waitForBytesWritten();
}
}
}
int main(int argc, char *argv[])
{
Server server(argc, argv);
// 处理命令行参数,启动视频捕获等
server.startCapture(); // 启动视频捕获
return server.exec();
}
#include "main.moc"