自建取景器

.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打开摄像头(自定义取景器)的更多相关文章

  1. 项目实战:Qt+Ffmpeg+OpenCV相机程序(打开摄像头、支持多种摄像头、分辨率调整、翻转、旋转、亮度调整、拍照、录像、回放图片、回放录像)

    若该文为原创文章,未经允许不得转载原博主博客地址:https://blog.csdn.net/qq21497936原博主博客导航:https://blog.csdn.net/qq21497936/ar ...

  2. 如何使用 OpenCV 打开摄像头获取图像数据?

    OpenCV 如何打开摄像头获取图像数据? 代码运行环境:Qt 5.9.1 msvc2015 32bit OpenCV 3.3.0 #include "include/opencv2/ope ...

  3. 基于opencv和QT的摄像头采集代码( GoQTtemplate3持续更新)

    在Linux操作系统上,编写带界面的图像处理程序,选择opencv+QT是一种很好的选择.GoQTtemplate3是我为编写Linux下图像处理程序实现的框架,希望能够为大家解决Linux环境下桌面 ...

  4. Android CameraX 打开摄像头预览

    目标很简单,用CameraX打开摄像头预览,实时显示在界面上.看看CameraX有没有Google说的那么好用.先按最简单的来,把预览显示出来. 引入依赖 模块gradle的一些配置,使用的Andro ...

  5. Unity打开摄像头占满全屏

    Unity打开摄像头占满全屏 AR项目需求,Unity打开摄像头作为背景渲染占满全屏~ Unity对设备硬件操作的API并不是太友好~打开一个摄像头,渲染到屏幕上也都得自己写,虽然步骤少,提取摄像头t ...

  6. QT打开ROS工作空间时遇到的问题和解决方法

    之前一直觉得不用IDE写程序看着好像我很能的样子. 其实就相当于工业时代我还钻木取火并且告诉别人你们用打火机根本不知道火被点燃的过程是怎样的. 因为这个技能并非人人都会,就可以拿出去到处臭屁 好了, ...

  7. OpenCV Open Camera 打开摄像头

    这是一个用OpenCV2.4.10打开摄像头的一个例子,参见代码如下: #include <iostream> #include <stdio.h> #include < ...

  8. Qt:使用自定义的字体

    Qt:使用自定义的字体 1. 下载字体文件 2. 加载字体文件 3. 使用字体   QFontDatabase::addApplicationFont("XENOTRON.TTF" ...

  9. QT打开网页 QURL

    用QT打开一个网页就是先定义一个QUrl对象url,然后利用QDesktopServices::open(url)即可. 例如: const QUrl url(http://www.baidu.com ...

  10. 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 ...

随机推荐

  1. Springcloud2021+Nacos2.2+Dubbo3+Seata1.6实现分布式事务

    示例代码地址:https://gitee.com/gtnotgod/Springcloud-alibaba.git 更详细参考Gitee完整的项目:https://gitee.com/gtnotgod ...

  2. Pyinstaller打包 Pytest+Allure成exe文件执行时,报错ERROR: usage: apitest.exe [options] [file_or_dir] [file_or_dir] [...] xxx.exe: error: unrecognized arguments: --alluredir=.\\report\\xml --clean-alluredir

    网上找了很多案例啊  都没解决问题,由本人的多次试验 终于成功解决 1.打包运行 pyinstaller -D  xxx.py 打包成功后  执行exe报错  如下 2.此情况是说明  命令无法正确识 ...

  3. 【技术实战】Vue技术实战【一】

    需求实战一 组件来源 ant-design-vue Button 按钮 Progress 进度条 效果展示 代码展示 <template> <ARow> <ACol> ...

  4. 使用netdata 监控Linux 主机

    在linux主机上,直接使用下面的命令创建容器即可 docker run -d --name=netdata \ -p 19999:19999 \ -v netdataconfig:/etc/netd ...

  5. 使用JMeter连接达梦数据库的步骤和示例

    引言: 本文将介绍如何使用JMeter连接达梦数据库,并提供连接达梦数据库的步骤和示例,帮助您快速开始进行数据库性能测试. 步骤: 1. 下载并安装JMeter:首先,从JMeter官方网站下载并安装 ...

  6. python-gitlab 一个简单demo

    背景 需要收集git仓库信息到数据库供前端展示 包括:仓库信息.仓库所有者.成员列表.提交信息.活跃情况等 需要定时启动.灵活触发 实现简介 使用gitlab v4 restful 接口 使用pyth ...

  7. 牛客小白月赛64 C题 题解

    题目链接 题意描述 这一题的意思其实就是,让你构造一个\(n * k\)的矩阵,使得第 i 列的总和为 i ,同时使得:每一列的任意两个数之间的差不大于1,且任意两行之间的总和差不大于1. \(1 \ ...

  8. Java程序员的MacBookPro(14寸M1)配置备忘录

    欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 本篇概览 欣宸的月俸虽然很低,但还是咬着牙攒够银子,入 ...

  9. Ubuntu安装后续工作

    更新源: sudo gedit /etc/apt/sources.list 清华的源 deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial ma ...

  10. [Pwn之路]根据所给库,获得远程同环境——使用patchelf的正确姿势

    原文:https://www.freebuf.com/sectool/366854.html 存自己这里方便看. 0x00 前言 如何修改本地pwn文件和题目所给环境一致,从而进行调试,这是从学习堆开 ...