佳能SDK无法从相机下载图片

Canon SDK fails to download picture from camera

本文关键字:下载 相机 SDK 佳能      更新时间:2023-10-16

低于佳能SDK的C++大约80%的工作。 它可以设置曝光时间,设置ISO,拍照并将其保存到相机的SD卡中。 但是,拍照后,代码无法将图片保存到我的电脑。 代码永远不会到达"EdsError EDSCALLBACK handleObjectEvent..."。 出于某种原因,即使在我的软件中,我的相机似乎也无法触发它。 我的代码可能有什么问题?

我的相机是EOS-80D,SDK版本是"EDSDK-v13-12-1_for_Windows",我的电脑使用的是Windows 7和Visual Studio 2019预览版。

我对VC++很陌生,所以如果可能的话,请直接更正我的代码。 如果有人可以提供一些提示,真的很感激。 谢谢。

#include <iostream>
#include "EDSDK.h"
#include "EDSDKErrors.h"
#include "EDSDKTypes.h"
#include <thread>
#include <chrono>
#include <string>
using namespace std;

void wait_ms(int t) {
this_thread::sleep_for(chrono::milliseconds(t));
}
void download_img(EdsBaseRef& object, EdsVoid*& context)
{
cout << "I am in download_img()" << endl;
const char* directory = "C:/Test.jpg";
EdsStreamRef stream = NULL;
EdsDirectoryItemInfo dirItemInfo;
EdsGetDirectoryItemInfo(object, &dirItemInfo);
EdsCreateFileStream(directory, kEdsFileCreateDisposition_CreateAlways, kEdsAccess_ReadWrite, &stream);
EdsDownload(object, dirItemInfo.size, stream);
EdsDownloadComplete(object);
EdsRelease(stream);
stream = NULL;
if (object)
EdsRelease(object);
}
EdsError EDSCALLBACK handleObjectEvent(EdsObjectEvent event, EdsBaseRef object, EdsVoid* context)
{
cout << "I am in EDSCALLBACK()." << endl;
download_img(object, context);
return EDS_ERR_OK;
}
int init_camera(EdsCameraRef& camera)
{
cout << "I am in init_camera()." << endl;
EdsError err = 0;
EdsCameraListRef cameraList = NULL;
EdsUInt32 count = 0;
camera = NULL;
err = EdsInitializeSDK();
err = EdsGetCameraList(&cameraList);
err = EdsGetChildCount(cameraList, &count);
if (count > 0){
err = EdsGetChildAtIndex(cameraList, 0, &camera);
EdsRelease(cameraList);
}
else {
cout << "Camera is NOT detected during init_camera()!!" << endl;
return 0; //Fail initialization
}
EdsSetObjectEventHandler(camera, kEdsObjectEvent_DirItemCreated, handleObjectEvent, NULL);
EdsSendStatusCommand(camera, kEdsCameraStatusCommand_UIUnLock, 0);
cout << "Camera has been detected during init_camera()!!" << endl;
return 1; //Initialization is completeced     
}
void update_data(EdsCameraRef camera)
{
EdsOpenSession(camera);
EdsCloseSession(camera);
}
void take_one_bulb_photo(EdsCameraRef camera)
{
EdsOpenSession(camera);
cout << endl << "shoot one image" << endl;
int ISO_factor = 98; // ISO=1000    
EdsSetPropertyData(camera, kEdsPropID_ISOSpeed, ISO_factor, sizeof(ISO_factor), &ISO_factor);
int Exp_factor = 110; // 125th
EdsSetPropertyData(camera, kEdsPropID_Tv, Exp_factor, sizeof(Exp_factor), &Exp_factor);
EdsSendCommand(camera, kEdsCameraCommand_TakePicture, 0); //page 40
EdsCloseSession(camera);
}
void dispose(EdsCameraRef camera)
{
EdsCloseSession(camera);
EdsRelease(camera);
EdsTerminateSDK();
}
int main() {
EdsCameraRef camera;
int ini_result = init_camera(camera);
if (ini_result == 1) {take_one_bulb_photo(camera);}
else {cout << endl << "Fail camera initialization!" << endl;}
dispose(camera);
update_data(camera);
return 0;
}

在 take_one_bulb_photo(( 函数中。 在最后一行"编辑关闭会话(相机(;" 添加以下代码;如

MSG msg;
GetMessage(&msg, NULL, NULL, NULL);
TranslateMessage(&msg); 
DispatchMessage(&msg);
GetMessage(&msg, NULL, NULL, NULL);
TranslateMessage(&msg); 
DispatchMessage(&msg);
GetMessage(&msg, NULL, NULL, NULL);
TranslateMessage(&msg); 
DispatchMessage(&msg);
GetMessage(&msg, NULL, NULL, NULL);
TranslateMessage(&msg); 
DispatchMessage(&msg);

它解决了我的问题。

上述解决方案的灵感来自:佳能 sdk 内部错误在 edsDownload