自建取景器
.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