概述

想要应用 canon 的 sdk 进行实时的一个预览,即 LiveView 性能。

后期筹备

后期的一些相机的连贯,能够参考我之前写的文章 QT 应用 canon sdk 拍照并保留到本机

实时预览步骤

StartLiveView

申明一个变量来标记 m_isLiveView 来标识 liveview 是否开启。

将实时预览输入到 PC 上

device |= kEdsEvfOutputDevice_PC;
// -----------------------------void MainWindow::StartLiveView(){    // Change settings because live view cannot be started    // when camera settings are set to "do not perform live view."    // 开启    EdsError err = EDS_ERR_OK;    uint evfMode = 1;    //把 1 写入 enable    err = EdsSetPropertyData(m_camera, kEdsPropID_Evf_Mode, 1, sizeof(uint), &evfMode);    m_isLiveView = true;    // Get the output device for the live view image    EdsUInt32 device;    err = EdsGetPropertyData(m_camera, kEdsPropID_Evf_OutputDevice, 0, sizeof(device), &device);    if(err == EDS_ERR_OK)    {     device |= kEdsEvfOutputDevice_PC;     err = EdsSetPropertyData(m_camera, kEdsPropID_Evf_OutputDevice, 0 , sizeof(device), &device);    }}

将预览的图像流转为 QImage 再转为 Mat

QImage img = QImage::fromData(data, length, "JPG");

将图像流转为 QImage 格局,这个是最重要的,在网上搜寻了十分久,不晓得怎么利用 data 和 length,网上的很多都是用 vb 和 c# 解决的,没有 C++ 的。

// ------------------------------------bool MainWindow::requestLiveViewImage(){    EdsError error = EDS_ERR_OK;    EdsStreamRef stream = NULL;    EdsEvfImageRef evfImage = NULL;    EdsUInt64 length;    if (!m_isLiveView)    {        error = EDS_ERR_INTERNAL_ERROR;        qDebug() << "liveView false";        return false;    }        // 在主机计算机的内存中创立流。 如果写入超出调配的缓冲区大小,则会主动扩大内存。    error = EdsCreateMemoryStream(0, &stream);    if (error != EDS_ERR_OK)    {        qDebug() << ("failed to create memory stream");        return false;    }    // 创立一个用于获取实时取景图像数据集的对象。    error = EdsCreateEvfImageRef(stream, &evfImage);    if (error != EDS_ERR_OK)    {        qDebug() << ("failed to create Evf image");        return false;    }    // 下载以后处于实时取景模式的相机的实时取景图像数据集。    error = EdsDownloadEvfImage(m_camera, evfImage);    if (error != EDS_ERR_OK)    {        // 当相机未筹备好图像数据集或无奈获取图像数据集时        if (error == EDS_ERR_OBJECT_NOTREADY)        {            qDebug() << ("failed to download Evf image, not ready yet");        }        else        {            qDebug() << ("failed to download Evf image");        }        return false;    }    // 获取图像流的大小    error = EdsGetLength(stream, &length);    if (error != EDS_ERR_OK)    {        qDebug() << ("failed to get Evf image length");        return false;    }    if (length == 0)    {        qDebug() << ("failed to get Evf  length is zero");        return false;    }        // 获取图像流的指针    unsigned char* data = NULL;    error = EdsGetPointer(stream, (EdsVoid**)&data);    if (error != EDS_ERR_OK)    {        qDebug() << ("failed to get pointer from stream");        return false;    }        // 将图像流转为 QImage     QImage img = QImage::fromData(data, length, "JPG");    // 将 QImage 转为 Mat 格局    m_image = QImageToMat(img);        // 开释    if (stream != NULL)    {        EdsRelease(stream);        stream = NULL;    }    if (evfImage != NULL)    {        EdsRelease(evfImage);        evfImage = NULL;    }    return true;}// -----------------------------------------cv::Mat MainWindow::QImageToMat(QImage& src){    // 留神这个 CV_8UC4 和你相机拍到的图像格式有关系,如果不合乎,图像会损坏,显示进去就有问题     cv::Mat tmp(src.height(),src.width(),CV_8UC4,(uchar*)src.bits(),src.bytesPerLine());     cv::Mat result = tmp.clone(); // 深拷贝     return result;}

在 Qt 中用 OpenCv 中显示

// -------------------------void MainWindow::ShowVideo(){    namedWindow("yunhu",WINDOW_NORMAL);    while(1)    {        requestLiveViewImage();        // m_image 就是转换生成的 Mat        if(m_image.data != NULL)        {            imshow("yunhu", m_image);            cvWaitKey(50);          }    }}

参考资料

  • QT 应用 canon sdk 拍照并保留到本机-云胡
  • EDSDK_API 3.4