一、与摄像头建立连接
1.RTSP格式
rtsp:
rtsp:
username | 摄像机用户名 |
---|
password | 摄像机密码 |
| 摄像机ip地址 |
| RTSP端口(554:默认值) |
Channels | 通道号 |
channel=1&subtype=0 | 码流类型(此为主码流,主码流比子码流清晰,子码流适用于低带宽网络的传输) |
2.测试连接可行性(使用VLC media player)
1.打开VLC
2.打开网络串流(Ctrl + N)
3.输入rtsp://admin:admin123456@10.0.10.21:554/cam/realmonitor?channel=1&subtype=0,点击播放
4.观察是否有监控画面显示。如果没有,检查大华摄像头是否正常启动;如果有监控画面,说明连接可正常建立,获取大华监控画面具有可行性。
二、连接、接收视频帧
1.rtsp地址
std::string url = "rtsp://admin:admin123456@10.0.10.21:554/cam/realmonitor?channel=1&subtype=0";
2.声明
cv::VideoCapture m_capture;
cv::Mat m_frame;
cv::Mat m_buffFrame;
3.建立连接
void DefaultCamera::init()
{
try
{
m_capture.open(url);
}
catch (const std::exception &e)
{
std::cerr << e.what() << std::endl;
}
if (m_capture.isOpened())
{
std::cout << "[DefaultCamera] is opened" << std::endl;
m_run = true;
std::thread _t(&DefaultCamera::readFrame, this);
_t.detach();
}
else
{
std::cout << "[DefaultCamera] is not opened" << std::endl;
}
}
4.接收帧
void DefaultCamera::readFrame()
{
bool _error = m_capture.read(m_frame);
if (m_frame.empty())
m_frame = m_buffFrame;
else
m_buffFrame = m_frame;
}
5.提供接口
cv::Mat DefaultCamera::getCurrentFrame()
{
return m_frame.clone();
}
三、将视频帧转成图片
1.调用接口获取最新帧
QImage exit_image = cameraToImage(exitCamera->getCurrentFrame());
2.帧转成图片
QImage EntranceExitCamera::cameraToImage(const cv::Mat& cvImg)
{
QImage qImg;
int _type = cvImg.type();
if (_type == CV_8UC3)
{
cv::cvtColor(cvImg, cvImg, CV_BGR2RGB);
qImg = QImage((const unsigned char*)(cvImg.data), cvImg.cols, cvImg.rows, static_cast<int>(cvImg.step), QImage::Format_RGB888).copy();
}
else if (_type == CV_8UC1)
{
qImg = QImage((const unsigned char*)(cvImg.data), cvImg.cols, cvImg.rows, static_cast<int>(cvImg.step), QImage::Format_Grayscale8).copy();
}
else
{
cv::cvtColor(cvImg, cvImg, CV_BGRA2RGBA);
qImg = QImage((const unsigned char*)(cvImg.data), cvImg.cols, cvImg.rows, static_cast<int>(cvImg.step), QImage::Format_RGBA8888).copy();
}
return qImg;
}