Qt 截取摄像头照片

来源:互联网 发布:数码暴龙网络侦探骇客 编辑:程序博客网 时间:2024/05/24 05:15
#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;
}
 
原创粉丝点击