版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.youkuaiyun.com/u010468553/article/details/79699774 </div>
<link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-3019150162.css">
<div id="content_views" class="markdown_views">
<!-- flowchart 箭头图标 勿删 -->
<svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
<path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path>
</svg>
<h1 id="使用准备"><a name="t0" target="_blank"></a>使用准备</h1>
在使用QCamera
之前需要在pro文件中添加媒体库。
QT += mutimedia mutimediawidgets
类别介绍
QCamera m_pCamera;
摄像头QCameraViewFinder m_pViewfinder;
取景器,用于显示摄像头的数据QCameraImageCapture m_pImageCapture;
获取摄像头当前帧
m_pCamera = new Camera(this);
m_pViewfinder = new QCameraViewFinder(this);
m_pImageCapture = new QCameraImageCapture(m_pCamera);
//设置采集目标
m_pCamera->setCaptureDestination(QCameraImageCapture::CaptureToFile);
//设置采集模式
m_pCamera->setCaptureMode(QCamera::CaptureStillImage);//将其采集为图片
m_pCamera->setCaptureMode(QCamera::CaptureMode::CaptureViewfinder);//将其采集到取景器中
//设置取景器
m_pCamera->setViewfinder(m_pViewfinder);
//开启相机
m_pCamera->start();
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
m_pCamera->searchAndLock()
//如果曝光和白平衡模式不是手动模式,那么就要使用请求的锁来锁定相机设置 //按下对焦按钮,触发camera的动作,锁定相机设置:对焦于单次自动对焦模式,曝光和白平衡。 connect(focus_btn, SIGNAL(clicked(bool)), m_pCamera, SLOT(searchAndLock()));
- 1
- 2
- 3
m_pCamera->supportedViewfinderResolutions()
//返回支持的取景器分辨率列表 for (auto resolution : m_pCamera->supportedViewfinderResolutions()){ //dosomething about the resolution }
- 1
- 2
- 3
- 4
QCameraViewfinderSettings
为控制相机取景器参数提供了一个抽象类。
取景器的参数有:- Resolution 分辨率
- PixelAspectRatio 像素宽高比
- MinimumFrameRate 最大帧率
- MaximumFrameRate 最小帧率
- PixelFormat 像素格式
- UserParameter
QCameraViewfinderSettings VfSettings; VfSettings.setResolution(preferred_resolution); VfSettings.setPixelFormat(QVideoFrame::Format_NV21); VfSettings.setMaximumFrameRate(15); m_pCamera->setViewfinderSettings(VfSettings);//设置当前相机的取景器
- 1
- 2
- 3
- 4
- 5
QVideoProbe
暂时只在android
平台上支持QCamera
,允许你监控正在播放或者记录的视频。auto* probe = new QVideoProbe(camera_); //一旦有探测到有视频,就触发了ProcessVideoFrame函数 connect(probe, SIGNAL(videoFrameProbed(QVideoFrame)), this, SLOT(ProcessVideoFrame(QVideoFrame))); probe->setSource(camera_);
- 1
- 2
- 3
- 4
videoFrameProbed
是自动触发的信号,其结果将被processFrame
截获并处理。信号和槽的方式是可以直接带变量传递的。QVideoFrame
一个QVideoFrame
代表的就是相机的一帧数据。QVideoFrame::bits()
返回的是一帧图像的起始地址。- 在调用
bits()
函数之前还要先判断frame
是否map
了。所谓map
就是将图像数据放到CPU可以寻址的地方。
那么将一帧的数据转化为一个Mat数据的过程如下:
if (!frame.map(QAbstractVideoBuffer::ReadOnly)) return; cv::Mat nv21(frame.height()*3/2, frame.width(), CV_8UC1, frame.bits(), static_cast<size_t>(frame.bytesPerLine()));
- 1
- 2