这篇文章主要介绍“Qt中如何使用canon edsdk实现实时预览”,在日常操作中,相信很多人在Qt中如何使用canon edsdk实现实时预览问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Qt中如何使用canon edsdk实现实时预览”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
声明一个变量来标志 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 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;
}
// -------------------------
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 edsdk实现实时预览”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/u/4706611/blog/4706889