服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - Qt 使用 canon edsdk 实现实时预览的示例代码

Qt 使用 canon edsdk 实现实时预览的示例代码

2021-10-03 22:39云胡 C/C++

这篇文章主要介绍了Qt 使用 canon edsdk 实现实时预览的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

概述

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

前期准备

前期的一些相机的连接,可以参考我之前写的文章QT 使用 canon sdk 拍照并保存到本机

实时预览步骤

StartLiveView

声明一个变量来标志 m_isLiveView 来标识 liveview 是否开启。

将实时预览输出到 PC 上

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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

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

将图像流转为 QImage 格式,这个是最重要的,在网上搜索了非常久,不知道怎么利用 data 和 length,网上的很多都是用 vb 和 c# 处理的,没有 C++ 的。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// ------------------------------------
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 中显示

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// -------------------------
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

到此这篇关于Qt 使用 canon edsdk 实现实时预览的文章就介绍到这了,更多相关Qt 使用 canon edsdk 实现实时预览内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://segmentfault.com/a/1190000037751691

延伸 · 阅读

精彩推荐