Qt:摄像头拍照

#include "cameraimagepool.h"

 
CameraImagePool::CameraImagePool(QObject *parent):QAbstractVideoSurface(parent)
{
//    m_time=0;
    handling=false;
}
 
QImage CameraImagePool::capture()
{
    if(handling)
        return m_lastBefore;
    else
        return m_last;
}
 
QList<QVideoFrame::PixelFormat> CameraImagePool::supportedPixelFormats(QAbstractVideoBuffer::HandleType handleType) const
{
    Q_UNUSED(handleType);
    QList<QVideoFrame::PixelFormat> list;
    list << QVideoFrame::Format_RGB32;
    return list;
}
 
void CameraImagePool::storeVideoFrame(const QVideoFrame &frame)
{
//    //保存照片的时间间隔
//    if((QDateTime::currentMSecsSinceEpoch() - m_time)/1000 < 2)
//        return;
//    m_time=QDateTime::currentMSecsSinceEpoch();
    const QImage image(frame.bits(),
                       frame.width(),
                       frame.height(),
                       QVideoFrame::imageFormatFromPixelFormat(frame.pixelFormat()));
    if(handling)
        m_last=image.mirrored(false,true);
    else
        m_lastBefore=image.mirrored(false,true);
    handling=!handling;
}
 
bool CameraImagePool::present(const QVideoFrame &frame)
{
    // 处理捕获的帧,测试结果每秒处理13帧
    if(frame.isMapped())
    {
        storeVideoFrame(frame);
    }
    else
    {
        QVideoFrame f(frame);
        f.map(QAbstractVideoBuffer::ReadOnly);
        storeVideoFrame(f);
        f.unmap();
    }
    return true;
}
 
 
#include "cameraimageengine.h"
 
CameraImageEngine::CameraImageEngine(QObject *parent) :
    QObject(parent)
{
    //启用特定摄像头
//    QList<QCameraInfo> cameras = QCameraInfo::availableCameras();
//    foreach (const QCameraInfo &cameraInfo, cameras) {
//        if (cameraInfo.description() == "Sirius USB2.0 Camera"){
//            m_pCamera = new QCamera(cameraInfo);
//        }
//    }
//    m_pCameraInfo        =   new QCameraInfo(*m_pCamera);      //摄像头信息(根据设备有可能获得传感器定位)
    m_pCamera           =   new QCamera(this);                   //启用系统默认的摄像头
    m_pPool             =   new CameraImagePool(this);           //过滤图像每帧
    CameraState         =   false;
    m_pCamera->setViewfinder(m_pPool);
    m_pCamera->setCaptureMode(QCamera::CaptureStillImage);       //设置摄像头的模式,可以抓取静态图像
    m_pCamera->load();
    installCameraDIRSet();                                       //设置摄像头自定义参数(曝光/变焦/图片处理)
    connect(m_pCamera,&QCamera::stateChanged,this,&CameraImageEngine::slotStateChanged);
 
    m_pCamera->start();//摄像头启动
}
CameraImageEngine::~CameraImageEngine()
{
 
}
 
void CameraImageEngine::installCameraDIRSet()
{
    QCameraExposure *exposure=m_pCamera->exposure();
    QCameraFocus *focus=m_pCamera->focus();
    QCameraImageProcessing *imgPro=m_pCamera->imageProcessing();
 
    qDebug()<<"---Camera Start Setting---";
//    qDebug()<<"lockstatus:"<<m_pCamera->lockStatus();
    qDebug()<<"exposure->isAvailable():"<<exposure->isAvailable();
    qDebug()<<"focus->isAvalibale():"<<focus->isAvailable();
    qDebug()<<"imgPro->isAvalibale():"<<imgPro->isAvailable();
 
    installExposureSet(exposure);
    installFocusSet(focus);
    installImageProcessingSet(imgPro);
 
    qDebug()<<"--Camera End Setting---";
 
    qDebug()<<"****Camera resolution setting****";
    //    qDebug()<<m_pCamera->supportedViewfinderFrameRateRanges();
    //    qDebug()<<m_pCamera->supportedViewfinderSettings();
    qDebug()<<m_pCamera->supportedLocks();
    qDebug()<<m_pCamera->supportedViewfinderPixelFormats();
    qDebug()<<m_pCamera->supportedViewfinderResolutions();
    QCameraViewfinderSettings cameraSetting=m_pCamera->viewfinderSettings();
    qDebug()<<"cameraSetting.maximumFrameRate()"<<cameraSetting.maximumFrameRate();
    qDebug()<<"cameraSetting.minimumFrameRate()"<<cameraSetting.minimumFrameRate();
    qDebug()<<"cameraSetting.pixelAspectRatio()"<<cameraSetting.pixelAspectRatio();
    qDebug()<<"cameraSetting.pixelFormat()"<<cameraSetting.pixelFormat();
//    cameraSetting.setPixelFormat(QVideoFrame::Format_YUYV);   //只能设置支持参数
//    cameraSetting.setPixelAspectRatio(640,480);   //不能设置
//    qDebug()<<cameraSetting.resolution();
    cameraSetting.setResolution(640,480);   //只能设置支持比率
//    m_pCamera->setViewfinderSettings(cameraSetting);
//    qDebug()<<cameraSetting.resolution();
    qDebug()<<"****";
}
//曝光
void CameraImageEngine::installExposureSet(QCameraExposure *exposure)
{
    qDebug()<<"<<installExposureSet()>>";
    //支持的标准
    qDebug()<<exposure->supportedApertures();
    qDebug()<<exposure->supportedIsoSensitivities();
    qDebug()<<exposure->supportedShutterSpeeds();
 
    //exposureCompensation
    qDebug()<<"exposure->exposureCompensation():"<<exposure->exposureCompensation();
    qDebug()<<"exposure->setExposureCompensation(5)";
    exposure->setExposureCompensation(5);
    qDebug()<<"exposure->exposureCompensation():"<<exposure->exposureCompensation();
 
    //exposureMode
    qDebug()<<"exposure->isExposureModeSupported(QCameraExposure::ExposureAuto):"\
           <<exposure->isExposureModeSupported(QCameraExposure::ExposureAuto);
    qDebug()<<"exposure->isExposureModeSupported(QCameraExposure::ExposureManual):"\
           <<exposure->isExposureModeSupported(QCameraExposure::ExposureManual);
    qDebug()<<"exposure->isExposureModeSupported(QCameraExposure::ExposurePortrait):"\
           <<exposure->isExposureModeSupported(QCameraExposure::ExposurePortrait);
    qDebug()<<"exposure->isExposureModeSupported(QCameraExposure::ExposureNight):"\
           <<exposure->isExposureModeSupported(QCameraExposure::ExposureNight);
    qDebug()<<"exposure->isExposureModeSupported(QCameraExposure::ExposureBacklight):"\
           <<exposure->isExposureModeSupported(QCameraExposure::ExposureBacklight);
    qDebug()<<"exposure->isExposureModeSupported(QCameraExposure::ExposureSpotlight):"\
           <<exposure->isExposureModeSupported(QCameraExposure::ExposureSpotlight);
    qDebug()<<"exposure->isExposureModeSupported(QCameraExposure::ExposureSports):"\
           <<exposure->isExposureModeSupported(QCameraExposure::ExposureSports);
    qDebug()<<"exposure->isExposureModeSupported(QCameraExposure::ExposureSnow):"\
           <<exposure->isExposureModeSupported(QCameraExposure::ExposureSnow);
    qDebug()<<"exposure->isExposureModeSupported(QCameraExposure::ExposureModeVendor):"\
           <<exposure->isExposureModeSupported(QCameraExposure::ExposureModeVendor);
    //flashMode
    qDebug()<<"exposure->isFlashModeSupported(QCameraExposure::FlashManual):"<<\
              exposure->isFlashModeSupported(QCameraExposure::FlashManual);
 
}
//变焦
void CameraImageEngine::installFocusSet(QCameraFocus *focus)
{
    qDebug()<<"<<installFocusSet()>>";
//...
}
//图像处理
void CameraImageEngine::installImageProcessingSet(QCameraImageProcessing *imgPro)
{
    qDebug()<<"<<installImageProcessingSet()>>";
    //whiteBalance
            //定制的白平衡枚举类型是否支持
    qDebug()<<"imgPro->isWhiteBalanceModeSupported(QCameraImageProcessing::WhiteBalanceAuto):"\
           <<imgPro->isWhiteBalanceModeSupported(QCameraImageProcessing::WhiteBalanceAuto);
    qDebug()<<"imgPro->isWhiteBalanceModeSupported(QCameraImageProcessing::WhiteBalanceManual):"\
           <<imgPro->isWhiteBalanceModeSupported(QCameraImageProcessing::WhiteBalanceManual);
    qDebug()<<"imgPro->isWhiteBalanceModeSupported(QCameraImageProcessing::WhiteBalanceSunlight):"\
           <<imgPro->isWhiteBalanceModeSupported(QCameraImageProcessing::WhiteBalanceSunlight);
    qDebug()<<"imgPro->isWhiteBalanceModeSupported(QCameraImageProcessing::WhiteBalanceCloudy):"\
           <<imgPro->isWhiteBalanceModeSupported(QCameraImageProcessing::WhiteBalanceCloudy);
    qDebug()<<"imgPro->isWhiteBalanceModeSupported(QCameraImageProcessing::WhiteBalanceShade):"\
           <<imgPro->isWhiteBalanceModeSupported(QCameraImageProcessing::WhiteBalanceShade);
    qDebug()<<"imgPro->isWhiteBalanceModeSupported(QCameraImageProcessing::WhiteBalanceVendor):"\
           <<imgPro->isWhiteBalanceModeSupported(QCameraImageProcessing::WhiteBalanceVendor);
            //...
            //设置手动白平衡模式
    qDebug()<<"imgPro->whiteBalanceMode:"<<imgPro->whiteBalanceMode();
    qDebug()<<"imgPro->setWhiteBalanceMode(QCameraImageProcessing::WhiteBalanceAuto)";
    imgPro->setWhiteBalanceMode(QCameraImageProcessing::WhiteBalanceAuto);
    qDebug()<<"imgPro->whiteBalanceMode:"<<imgPro->whiteBalanceMode();
            //如果是手动白平衡模式则可以调整数值
//    qDebug()<<"imgPro->manualWhiteBalance():"<<imgPro->manualWhiteBalance();
//    qDebug()<<"imgPro->setManualWhiteBalance(4600)";
//    imgPro->setManualWhiteBalance(4600);    //数值越大色温越暖
//    qDebug()<<"imgPro->manualWhiteBalance():"<<imgPro->manualWhiteBalance();
 
    //colorFilter
    qDebug()<<"imgPro->colorFilter():"<<imgPro->colorFilter();
    qDebug()<<"imgPro->isColorFilterSupported(QCameraImageProcessing::ColorFilterNone):"\
           <<imgPro->isColorFilterSupported(QCameraImageProcessing::ColorFilterNone);
 
    //denosingLevel
    qDebug()<<"imgPro->denosingLevel():"<<imgPro->denoisingLevel();
    qDebug()<<"imgPro->setDenoisingLevel(5)";
    imgPro->setDenoisingLevel(5);
    qDebug()<<"imgPro->denoisingLevel():"<<imgPro->denoisingLevel();
 
    //constrast
    qDebug()<<"imgPro->constrast():"<<imgPro->contrast();
    qDebug()<<"imgPro->setConstrast(2)";
    imgPro->setContrast(2);
    qDebug()<<"imgPro->constrast():"<<imgPro->contrast();
 
    //sharpeningLevel
    qDebug()<<"imgPro->sharpeningLevel:"<<imgPro->sharpeningLevel();
    qDebug()<<"imgPro->setsharpeningLevel(95)";
    imgPro->setSharpeningLevel(95);
    qDebug()<<"imgPro->sharpeningLevel():"<<imgPro->sharpeningLevel();
}
 
void CameraImageEngine::slotStateChanged(QCamera::State state)
{
    qDebug()<<state;
    if(state==QCamera::ActiveState)
        CameraState=true;
}
 
bool CameraImageEngine::RecvCamera(char *szFile)       //RecvCamera("fileName")
{
    if(!CameraState)
        return false;
    QImage img=m_pPool->capture();
    if(img.save(szFile,"jpg"))
        return true;
    return false;
}
 
bool CameraImageEngine::GetState()
{
    return CameraState;
}
 
 
 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在Linux平台下使用QT框架来控制摄像头进行拍照的实现,需要使用V4L2(Video for Linux 2)框架实现视频采集、视频显示以及图片保存。V4L2提供了一套底层的驱动程序接口,可以访问系统中的视频设备并获取视频数据。在应用程序中,我们可以通过QT的QCamera类和QCameraViewfinder类来调用V4L2驱动程序操作视频设备,并将视频数据显示在GUI界面上。此外,还可以使用QImage类将视频帧转换为图像数据,并保存为图像文件。实现这一过程需要以下步骤: 1. 创建QCamera和QCameraViewfinder对象,设置视频设备。 2. 创建QImage对象,并将视频帧数据转换为图像数据。 3. 将图像保存为文件。 代码示例: QCamera *camera = new QCamera; QCameraViewfinder *viewFinder = new QCameraViewfinder; camera->setViewfinder(viewFinder); QImage *image = new QImage; connect(camera, SIGNAL(frameCaptured(QImage)), this, SLOT(saveImage(QImage))); void MainWindow::saveImage(QImage image) { image.save("/home/user/image.jpg"); } 通过以上代码,我们可以实现在Linux平台下使用QT框架控制摄像头进行拍照的功能。 ### 回答2: 在Linux平台上使用Qt开发程序时,想要通过摄像头拍照需要先安装v4l-utils(Video 4 Linux Utilities)工具包,它包括了对多种摄像头的支持。安装完成后,需要用v4l-info命令检测设备是否被识别,如果识别出来了就可以使用v4l2-ctl命令来调节视频流参数。要实现拍照功能,可以使用Qt的Multimedia模块来捕获摄像头图像,并调用QImage类来处理图片数据。具体实现中,需要用到QCamera和QCameraViewfinder类以及QImageEncoderSettings、QPixelFormat、QVideoFrame、QMediaPlayer等相关类实现。其中,QCameraViewfinder类可以在GUI界面中显示摄像头捕获的实时图像,而QImageEncoderSettings和QPixelFormat可以控制图片格式和编码方式。使用QVideoFrame类来读取摄像头捕获的图像数据,将其转换成QImage型的数据并保存即可完成拍照操作。总之,通过使用Qt的Multimedia模块配合v4l-utils工具包,可以方便地在Linux平台上实现摄像头拍照功能。 ### 回答3: Qt是一个跨平台的C++应用程序开发框架,能够在各种操作系统上运行,而Linux是其中一种操作系统。在Linux环境下,Qt可以很方便地实现摄像头拍照的功能。 Qt提供了QCamera类来调用摄像头,可以通过指定设备名称和设备类型创建QCamera对象。使用QCameraViewfinder作为摄像头预览的窗口,可以显示实时图像。我们可以使用QImageCapture类来实现拍照操作,设置拍照保存路径和文件名,调用capture()函数就可以完成一次拍照。 在Linux系统下,我们需要先安装Linux摄像头驱动,Linux系统会自动识别并加载摄像头驱动,此后摄像头就可以正常工作。驱动成功安装后,就可以通过Qt编写程序来调用摄像头并进行拍照操作。 总的来说,使用Qt在Linux环境下实现摄像头拍照的功能是非常便捷的。需要注意的是,Linux系统对摄像头支持的情况基本上是看硬件,因此在选购摄像头设备时需要注意,确保摄像头和Linux系统兼容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值