首页 > 其他分享 >QT打开摄像头(自定义取景器)

QT打开摄像头(自定义取景器)

时间:2022-12-27 17:14:38浏览次数:62  
标签:set QT 自定义 camera frame 取景器 CameraSurface include QVideoFrame

自建取景器

.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));
}

 

标签:set,QT,自定义,camera,frame,取景器,CameraSurface,include,QVideoFrame
From: https://www.cnblogs.com/RainbowSea/p/17008489.html

相关文章

  • Azure ARM (25) 自定义Role,不允许移动Azure资源
    《WindowsAzurePlatform系列文章目录》 我们把一个资源从资源组A移动到资源组B的时候,如果这时候有其他人对资源组A或者资源组B的时候,会遇到创建失败的错误,并......
  • 自定义elementUI皮肤、色系、主题、主色调
    ​​Element-Theworld'smostpopularVueUIframeworkElement,一套为开发者、设计师和产品经理准备的基于Vue2.0的桌面端组件库https://element.eleme.cn/#/zh-CN/th......
  • DSP+ZYNQ多核例程使用手册-XQTyer【开源】
    【开源资料】XQTyer评估板例程使用手册.pdf链接:​​https://share.weiyun.com/8csewUvh​​密码:8r9by7XQ6657Z35/45-EVM高速数据处理评估板(XQTyer评估板,包含一片TIDSP......
  • python——发送mqtt消息
    (1)创建mqtt连接参考https://www.jianshu.com/p/06d23de47aed文中写的发布消息代码如下:文件名:mypub.py#!/usr/bin/envpython#coding:utf-8importtimeimportjso......
  • 初步实现GoQtTemplate
    #ifndefMAINWINDOW_H#defineMAINWINDOW_H#include<QMainWindow>//新添加#include<opencv2/core/core.hpp>#include<opencv2/highgui/highgui.hpp>#include<opencv2/......
  • Vue按需引入注册UI以及自定义组件的封装
    1.单文件global注册自定义组件的封装importAfrom"../view/A.vue";importBfrom"../view/B.vue";constcomponents={A,B};exportdefault{install(Vue){......
  • 如何自定义小程序页面分享?
    步骤分解界面设置选中页面点击页面右侧图标点击界面设置设置值这样就可以实现自定义小程序页面分享了。......
  • Hive 自定义函数
    Hive自带了一些函数,比如:max/min等,但是数量有限,自己可以通过自定义UDF来方便的扩展。当Hive提供的内置函数无法满足你的业务处理需要时,此时就可以考虑使用用户自定义函数(UD......
  • Java 自定义注解校验字段唯一性
    业务场景在项目中,某些情景下我们需要验证编码是否重复,账号是否重复,身份证号是否重复等...那么有没有办法可以解决这类似的重复代码量呢?我们可以通过自定义注解校验的方......
  • QT实现表单
    /********************************************************************************Copyright(C)2016TheQtCompanyLtd.**Contact:https://www.qt.io/licensing/......