自建取景器

.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. Android 自定义view中根据状态修改drawable图片

    原文地址:Android 自定义view中根据状态修改drawable图片 - Stars-One的杂货小窝 本文涉及知识点: Android里的selector图片使用 底部导航栏的使用 自定义vi ...

  2. Istio 入门(三):体验 Istio、微服务部署、可观测性

    本教程已加入 Istio 系列:https://istio.whuanle.cn 目录 3,快速入门 书店微服务 预先准备 details 应用 ratings 应用 reviews v1/v2/v3 ...

  3. Federated Learning004

    联邦学习--笔记004 2023.03.13周一 快中期答辩了(3.20),最近甲流高发期 毕设期间,今天学习了联邦学习的一篇论文---Differentially Private Federated ...

  4. Day04_Java_作业

    A:选择题 1.下列哪个答案与show不是方法重载(b ). class Demo { void show(int a,int b,float c){} } A.void show(int a,flo ...

  5. Hexo博客yilia主题首页添加helper-live2d模型插件

    插件效果 插件的github地址 插件作者提供了较为详细的安装步骤,我结合自己操作和图示,提供大家. 效果展示:红框内为2d模型,可以随鼠标移动而变化 安装模块: hexo博客根目录选择cmd命令窗口 ...

  6. SpringBoot 使用 Sa-Token 实现账号封禁、分类封禁、阶梯封禁

    一.需求分析 之前的章节中,我们学习了 踢人下线 和 强制注销 功能,用于清退违规账号.在部分场景下,我们还需要将其 账号封禁,以防止其再次登录. Sa-Token 是一个轻量级 java 权限认证框 ...

  7. Swift函数调用方式浅析

    函数的调用机制   函数的调用机制是在函数调用时通过那种路径走到最终调用函数地址的机制. 在编程语言中,函数的调用机制有三种 1.静态调用:编译期就确定了函数内存地址,执行效率最高,还可以使用编译器优 ...

  8. Android 架构模式如何选择

    作者:vivo 互联网客户端团队-Xu Jie Android架构模式飞速演进,目前已经有MVC.MVP.MVVM.MVI.到底哪一个才是自己业务场景最需要的,不深入理解的话是无法进行选择的.这篇文章 ...

  9. 9、Mybatis之动态SQL

    9.1.环境搭建 9.1.1.创建新module 创建名为mybatis_dynamicSQL的新module,过程参考5.1节 9.1.2.创建Emp实体类 package org.rain.myb ...

  10. python独立脚本应用Django项目的环境

    一.需求说明 一直用 Django 在开发一个网站项目,其中的注册用户和登录,都是使用Django自带的认证系统.主要是对密码的加密,在注册或者登录的时候,前端传递多来的密码,我会使用Django的s ...