QT打开摄像头(自定义取景器)
自建取景器
.h
#ifndef CAMERASURFACE_H
#define CAMERASURFACE_H
#include<QAbstractVideoSurface>
#include <QObject> class CameraSurface : public QAbstractVideoSurface
{
Q_OBJECT
public:
CameraSurface(QObject *parent = Q_NULLPTR);
~CameraSurface();
QList<QVideoFrame::PixelFormat> supportedPixelFormats(QAbstractVideoBuffer::HandleType handleType = QAbstractVideoBuffer::NoHandle) const;
bool present(const QVideoFrame &frame);
signals:
void frameAvailable(QVideoFrame &frame);
}; #endif // CAMERASURFACE_H
.cpp
#include "camerasurface.h"
#include<QVideoSurfaceFormat>
#include <QDateTime>
#include <QPixmap>
#include <QImage>
#include <QPen>
#include <QPainter>
#include <QDebug> CameraSurface::CameraSurface(QObject *parent): QAbstractVideoSurface(parent)
{
} CameraSurface::~CameraSurface()
{
} QList<QVideoFrame::PixelFormat> CameraSurface::supportedPixelFormats(QAbstractVideoBuffer::HandleType handleType) const
{
QList<QVideoFrame::PixelFormat> listPixelFormats;
listPixelFormats << QVideoFrame::Format_ARGB32
<< QVideoFrame::Format_ARGB32_Premultiplied
<< QVideoFrame::Format_RGB32
<< QVideoFrame::Format_RGB24
<< QVideoFrame::Format_RGB565
<< QVideoFrame::Format_RGB555
<< QVideoFrame::Format_ARGB8565_Premultiplied
<< QVideoFrame::Format_BGRA32
<< QVideoFrame::Format_BGRA32_Premultiplied
<< QVideoFrame::Format_BGR32
<< QVideoFrame::Format_BGR24
<< QVideoFrame::Format_BGR565
<< QVideoFrame::Format_BGR555
<< QVideoFrame::Format_BGRA5658_Premultiplied
<< QVideoFrame::Format_AYUV444
<< QVideoFrame::Format_AYUV444_Premultiplied
<< QVideoFrame::Format_YUV444
<< QVideoFrame::Format_YUV420P
<< QVideoFrame::Format_YV12
<< QVideoFrame::Format_UYVY
<< QVideoFrame::Format_YUYV
<< QVideoFrame::Format_NV12
<< QVideoFrame::Format_NV21
<< QVideoFrame::Format_IMC1
<< QVideoFrame::Format_IMC2
<< QVideoFrame::Format_IMC3
<< QVideoFrame::Format_IMC4
<< QVideoFrame::Format_Y8
<< QVideoFrame::Format_Y16
<< QVideoFrame::Format_Jpeg
<< QVideoFrame::Format_CameraRaw
<< QVideoFrame::Format_AdobeDng;
return listPixelFormats;
} bool CameraSurface::present(const QVideoFrame &frame)
{
if (frame.isValid())
{
QVideoFrame cloneFrame(frame);
emit frameAvailable(cloneFrame);
return true;
}
return false;
}
调用
void MainWindow::OpenCamera()//开相机
{
foreach(const QCameraInfo& info, QCameraInfo::availableCameras())
{
m_camera =new QCamera(info);//info.deviceName()
break;//取第一个后跳出
}
cameraSurface=new CameraSurface(this);
m_camera->setViewfinder(cameraSurface);
connect(cameraSurface, SIGNAL(frameAvailable(QVideoFrame &)), this, SLOT(displayImage(QVideoFrame &)));
m_camera->setCaptureMode(QCamera::CaptureStillImage);
m_camera->load();
//Set fbl 1920*1080
QCameraViewfinderSettings set;
#if defined(PLAT_DONG_AARCH64)//根据平台设置分辨率
set.setResolution(QSize(1920,1080));
#else
set.setResolution(QSize(640,480));
#endif set.setPixelFormat(QVideoFrame::Format_RGB32);
m_camera->setViewfinderSettings(set);
m_camera->start();
}
显示图像的槽
void MainWindow::displayImage(QVideoFrame &buffer)
{
QVideoFrame frame(buffer);
frame.map(QAbstractVideoBuffer::ReadOnly);
QImage::Format imageFormat = QVideoFrame::imageFormatFromPixelFormat(frame.pixelFormat());
QImage img;
if (imageFormat != QImage::Format_Invalid)
{
img = QImage(frame.bits(),frame.width(),frame.height(),imageFormat);
}
else
{
int nbytes = frame.mappedBytes();
img = QImage::fromData(frame.bits(), nbytes);
}
ui->CameraView->setPixmap(QPixmap::fromImage(img));
}
QT打开摄像头(自定义取景器)的更多相关文章
- 项目实战:Qt+Ffmpeg+OpenCV相机程序(打开摄像头、支持多种摄像头、分辨率调整、翻转、旋转、亮度调整、拍照、录像、回放图片、回放录像)
若该文为原创文章,未经允许不得转载原博主博客地址:https://blog.csdn.net/qq21497936原博主博客导航:https://blog.csdn.net/qq21497936/ar ...
- 如何使用 OpenCV 打开摄像头获取图像数据?
OpenCV 如何打开摄像头获取图像数据? 代码运行环境:Qt 5.9.1 msvc2015 32bit OpenCV 3.3.0 #include "include/opencv2/ope ...
- 基于opencv和QT的摄像头采集代码( GoQTtemplate3持续更新)
在Linux操作系统上,编写带界面的图像处理程序,选择opencv+QT是一种很好的选择.GoQTtemplate3是我为编写Linux下图像处理程序实现的框架,希望能够为大家解决Linux环境下桌面 ...
- Android CameraX 打开摄像头预览
目标很简单,用CameraX打开摄像头预览,实时显示在界面上.看看CameraX有没有Google说的那么好用.先按最简单的来,把预览显示出来. 引入依赖 模块gradle的一些配置,使用的Andro ...
- Unity打开摄像头占满全屏
Unity打开摄像头占满全屏 AR项目需求,Unity打开摄像头作为背景渲染占满全屏~ Unity对设备硬件操作的API并不是太友好~打开一个摄像头,渲染到屏幕上也都得自己写,虽然步骤少,提取摄像头t ...
- QT打开ROS工作空间时遇到的问题和解决方法
之前一直觉得不用IDE写程序看着好像我很能的样子. 其实就相当于工业时代我还钻木取火并且告诉别人你们用打火机根本不知道火被点燃的过程是怎样的. 因为这个技能并非人人都会,就可以拿出去到处臭屁 好了, ...
- OpenCV Open Camera 打开摄像头
这是一个用OpenCV2.4.10打开摄像头的一个例子,参见代码如下: #include <iostream> #include <stdio.h> #include < ...
- Qt:使用自定义的字体
Qt:使用自定义的字体 1. 下载字体文件 2. 加载字体文件 3. 使用字体 QFontDatabase::addApplicationFont("XENOTRON.TTF" ...
- QT打开网页 QURL
用QT打开一个网页就是先定义一个QUrl对象url,然后利用QDesktopServices::open(url)即可. 例如: const QUrl url(http://www.baidu.com ...
- Qt打开外部程序和文件夹需要注意的细节(Qt调用VC写的动态库,VC需要用C的方式输出函数,否则MinGW32编译过程会报错)
下午写程序中遇到几个小细节,需要在这里记录一下. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 QProcess *process = new QProcess(this ...
随机推荐
- LSP协议被劫持,导致无法上网
QQ无法登录,网页打不开 用火绒的断网修复 说已经修复了 结果屁用没有 然后找的百度经验 管理员打开命令行窗口 输入 netsh winsock reset catalog 重启即生效
- requests的基础使用
爬虫介绍 # 爬虫:又称网络蜘蛛,spider,一堆程序,从互联网中抓取数据---->数据清洗---->入库 # 爬虫需要掌握的知识 -抓取数据:发送网络请求(http),获得响应(htt ...
- 响应式编程:Vert.x官网学习
本文基于 Vert.x 官网 https://vertx.io/ 内容,带领大家学习响应式编程里比较有名的工具包 Vert.x .文章内容取自官网由博主简化总结,希望帮助大家理解响应式编程. Vert ...
- 2023-07-16:讲一讲Kafka与RocketMQ中零拷贝技术的运用?
2023-07-16:讲一讲Kafka与RocketMQ中零拷贝技术的运用? 答案2023-07-16: 什么是零拷贝? 零拷贝(英语: Zero-copy) 技术是指计算机执行操作时,CPU不需要先 ...
- Java与PHP的区别
1.PHP暂时不支持像Java那样的JIT运行时编译的热点代码,但PHP具有opcache机制,能够将脚本对应的opcode缓存在内存中. 补充:JIT与JVM的三种执行模式:解释模式.编译模式.混合 ...
- ubuntu 终端选中黏贴、自带截图
鼠标选中 -- 复制 鼠标中键 -- 粘贴 注意,在tmux中,这个操作需要加上 Shift 键. PrtSc:截图整个桌面保存到Pictures Ctrl + PrtSc:截图整个桌面到剪贴板 Sh ...
- Vue笔记(一)
1. Vue.js是什么? 1). 一位华裔前Google工程师(尤雨溪)开发的前端js库 2). 作用: 动态构建用户界面 3). 特点: * 遵循MVVM模式 * 编码简洁, 体积小, 运行效率高 ...
- 带你走进数仓大集群内幕丨详解关于作业hang及残留问题定位
本文分享自华为云社区<[带你走进DWS大集群内幕]大集群通信:作业hang.残留问题定位>,作者: 雨落天穹丶. 前言: 测试过程中,我们会遇到这样一种情况,我的作业都执行很久了,为啥还不 ...
- 使用lame以多进程方式转码wav为mp3
前言 lame以单进程的方式转码wav文件,之前量少,足够使用.如今每日wav文件数量极多,单进程的效率就不够用了,所以这里使用脚本的方式,启动多个lame进程转码wav文件. code01: aut ...
- 1、MyBatis简介
1.1.MyBatis历史 MyBatis最初是Apache的一个开源项目iBatis, 2010年6月这个项目由Apache Software Foundation迁 移到了Google Code. ...