summary
Want to use canon Of sdk Make a preview in real time .
Preparation
Some of the previous camera connections , You can refer to the article I wrote before QT Use canon sdk Take a picture and save it to this machine
Preview steps in real time
StartLiveView
Declare a variable to mark m_isLiveView
To mark liveview Open or not .
Output live preview to PC On
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."
// Turn on
EdsError err = EDS_ERR_OK;
uint evfMode = 1;
// hold 1 write in 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);
}
}
Flow the preview image to QImage And then to Mat
QImage img = QImage::fromData(data, length, "JPG");
Turn the image into QImage
Format , This is the most important , I've been searching the Internet for a long time , I don't know how to use data and length, A lot of it is on the Internet vb and c# To deal with the , No, C++ Of .
// ------------------------------------
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;
}
// Create a stream in the host computer's memory . If the write exceeds the allocated buffer size , The memory will be expanded automatically .
error = EdsCreateMemoryStream(0, &stream);
if (error != EDS_ERR_OK)
{
qDebug() << ("failed to create memory stream");
return false;
}
// Create an object to get a real-time viewfinder image dataset .
error = EdsCreateEvfImageRef(stream, &evfImage);
if (error != EDS_ERR_OK)
{
qDebug() << ("failed to create Evf image");
return false;
}
// Download the live view image dataset of the camera currently in live view mode .
error = EdsDownloadEvfImage(m_camera, evfImage);
if (error != EDS_ERR_OK)
{
// When the camera is not ready for the image data set or cannot 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;
}
// Get 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;
}
// Get 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;
}
// Turn the image into QImage
QImage img = QImage::fromData(data, length, "JPG");
// take QImage To Mat Format
m_image = QImageToMat(img);
// Release
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 something to do with the format of the image you take with your camera , If it doesn't meet , Images can be corrupted , There's something wrong with it
cv::Mat tmp(src.height(),src.width(),CV_8UC4,(uchar*)src.bits(),src.bytesPerLine());
cv::Mat result = tmp.clone(); // Deep copy
return result;
}
stay OpenCv It shows that
// -------------------------
void MainWindow::ShowVideo()
{
namedWindow("yunhu",WINDOW_NORMAL);
while(1)
{
requestLiveViewImage();
// m_image It's generated by transformation Mat
if(m_image.data != NULL)
{
imshow("yunhu", m_image);
cvWaitKey(50);
}
}
}