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));
}
复制代码

 

posted @   迷海  阅读(362)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示