QT example of a method that USES canon sdk to take a photo and save it to the machine

  • 2020-11-25 07:23:19
  • OfStack

1. An overview of the

Connect canon SLR through usb, take photos through QT, and save the captured images to the machine.

2. Operation steps

1. Initialize SDK


EdsInitializeSDK();

2. Get the camera list object


  EdsCameraListRef cameraList = NULL;
  error = EdsGetCameraList(&cameraList);

3. Get the number of camera list objects


  //  Iterate through the camera list objects to get the number of camera list objects 
  EdsUInt32 cameraCount = 0;
  error = EdsGetChildCount(cameraList, &cameraCount);
  if (error != EDS_ERR_OK)
  {
    qDebug() << "ERROR - failed to get camera count";
    EdsRelease(cameraList);
    return;
  }

4. Get a single camera object instance


  for (uint32_t idx = 0; idx < cameraCount; idx++)
  {
    //  Gets a single camera object 
    EdsCameraRef cam = NULL;
    error = EdsGetChildAtIndex(cameraList, idx, &cam);
    m_camera = cam;
  }

And then what we do with the camera is mainly through this.

5. Set up camera sessions


EdsOpenSession(m_camera);

6. Save the picture to the machine


  EdsUInt32 saveTo = kEdsSaveTo_Both;    //  Save to camera and native 
  //EdsUInt32 saveTo = kEdsSaveTo_Host;  //  Save to local 
  error = EdsSetPropertyData(m_camera, kEdsPropID_SaveTo, 0, sizeof(saveTo), &saveTo);

7. Set a large enough capacity to store pictures on the machine


  EdsCapacity capacity = {0x7FFFFFFF, 0x1000, 1};
  error = EdsSetCapacity(m_camera, capacity);

8. Register a handle to an object event


error = EdsSetObjectEventHandler(m_camera, kEdsObjectEvent_All, objectEventHandler, this);

But a change in the camera object triggers the objectEventHandler callback

9. objectEventHandler callback function


EdsError EDSCALLBACK MainWindow::objectEventHandler(EdsUInt32 inEvent, EdsBaseRef inRef, EdsVoid* inContext)
{
  EdsStreamRef stream = NULL;
  EdsDirectoryItemInfo dirItemInfo;
  QString m("C:/Users/Administrator/Desktop/");
  switch (inEvent)
  {
   //  Notify the object ( Such as a new folder or file ) The creation of 
   case kEdsObjectEvent_DirItemCreated:
      qDebug() << "Directory Item Created";
      break;
   //  Notifies the camera that an object is to be transferred to the computer. 
   case kEdsObjectEvent_DirItemRequestTransfer:
      qDebug() << "Directory Item Requested Transfer";
      err = EdsGetDirectoryItemInfo(inRef, &dirItemInfo);
      m = m + dirItemInfo.szFileName;  //  Splice local image to save location 
      //  Download the picture to the machine 
      err = EdsCreateFileStream(m.toStdString().data(), kEdsFileCreateDisposition_CreateAlways, kEdsAccess_ReadWrite, &stream);
      if (err != EDS_ERR_OK)
      {
       qDebug("failed to create file stream");
      }
      err = EdsDownload(inRef, dirItemInfo.size, stream);
      if (err != EDS_ERR_OK)
      {
       qDebug("failed to download");
      }
      err = EdsDownloadComplete(inRef);
      if (err != EDS_ERR_OK)
      {
        qDebug("failed to mark download as complete");
      }
      EdsRelease(stream);
      stream = NULL;
      break;
    default:
        qDebug() << " unknowns ";
        break;
    }
   return EDS_ERR_OK;
}

10. Send the command to take photos


error = EdsSendCommand(m_camera, kEdsCameraCommand_TakePicture, 0);

11. Close the camera session


  EdsCameraListRef cameraList = NULL;
  error = EdsGetCameraList(&cameraList);
0

12. End SDK


  EdsCameraListRef cameraList = NULL;
  error = EdsGetCameraList(&cameraList);
1

3. References

Canon EDSDK Tutorial in C#
EDSDK-cpp
Cinder-EDSDK


Related articles: