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

Canon SDK fails to download picture from camera

使用 Canon 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() 函数中。在最后一行“EdsCloseSession(camera);”之前添加以下代码;比如

    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);

它解决了我的问题。

以上解决方案的灵感来自:Canon sdk internal error at edsDownload