Qt sample code for real time preview using canon edsdk
- 2020-11-26 18:54:37
- OfStack
An overview of the
You want to use canon's sdk for a real-time preview of the LiveView feature.
preparation
For the connection of some cameras in the early stage, please refer to the article QT I wrote before to take photos with canon sdk and save them to the machine
Real-time preview steps
StartLiveView
Declare a variable to flag m_isLiveView to indicate whether liveview is on or off.
Print live preview to 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."
// open
EdsError err = EDS_ERR_OK;
uint evfMode = 1;
// the 1 write 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);
}
}
Stream the preview image to QImage and then to Mat
QImage img = QImage::fromData(data, length, "JPG");
I have searched the Internet for a long time. I don't know how to use data and length. Many of the images on the Internet are processed with vb and c#, but not with 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;
}
// Creates a stream in the memory of the host computer. If you write more than the allocated buffer size, memory is automatically expanded.
error = EdsCreateMemoryStream(0, &stream);
if (error != EDS_ERR_OK)
{
qDebug() << ("failed to create memory stream");
return false;
}
// create 1 Objects used to obtain a real-time viewfinder image data set.
error = EdsCreateEvfImageRef(stream, &evfImage);
if (error != EDS_ERR_OK)
{
qDebug() << ("failed to create Evf image");
return false;
}
// Download the live view image data set for the camera currently in live view mode.
error = EdsDownloadEvfImage(m_camera, evfImage);
if (error != EDS_ERR_OK)
{
// When the camera is not ready or unable to obtain the image data set
if (error == EDS_ERR_OBJECT_NOTREADY)
{
qDebug() << ("failed to download Evf image, not ready yet");
}
else
{
qDebug() << ("failed to download Evf image");
}
return false;
}
// Gets the size of the image stream
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;
}
// Gets a pointer to the image stream
unsigned char* data = NULL;
error = EdsGetPointer(stream, (EdsVoid**)&data);
if (error != EDS_ERR_OK)
{
qDebug() << ("failed to get pointer from stream");
return false;
}
// Stream the image to QImage
QImage img = QImage::fromData(data, length, "JPG");
// will QImage to Mat format
m_image = QImageToMat(img);
// The release of
if (stream != NULL)
{
EdsRelease(stream);
stream = NULL;
}
if (evfImage != NULL)
{
EdsRelease(evfImage);
evfImage = NULL;
}
return true;
}
// -----------------------------------------
cv::Mat MainWindow::QImageToMat(QImage& src)
{
// Pay attention to this CV_8UC4 It has to do with the format of the image that your camera takes. If it doesn't match, the image will be damaged and there will be a problem with the display
cv::Mat tmp(src.height(),src.width(),CV_8UC4,(uchar*)src.bits(),src.bytesPerLine());
cv::Mat result = tmp.clone(); // Deep copy
return result;
}
Shown in Qt using OpenCv
// -------------------------
void MainWindow::ShowVideo()
{
namedWindow("yunhu",WINDOW_NORMAL);
while(1)
{
requestLiveViewImage();
// m_image That's what the transformation generates Mat
if(m_image.data != NULL)
{
imshow("yunhu", m_image);
cvWaitKey(50);
}
}
}
The resources
QT USES canon sdk to take a photo and save it to the machine - Yunhu
EDSDK_API 3.4