将图像下载到PC并更改Canon EDSDK中的图像名称

时间:2014-08-11 10:32:21

标签: visual-c++ edsdk canon-sdk

我正在尝试编写一个程序来通过佳能EOS700D拍照并更改图像名称,并将该图像保存到特定目录。但我不知道它为什么不起作用!错误是EDS_ERR_TAKE_PICTURE_CARD_NG。 这是我的对象事件处理函数:

EdsError EDSCALLBACK SDKObjectEventHandler( EdsObjectEvent event, EdsImageRef object, 

EdsVoid * context) 
{ 
    switch(event) 
    {
    case kEdsObjectEvent_All:
        break;
    case kEdsObjectEvent_DirItemCancelTransferDT:
        break;
    case kEdsObjectEvent_DirItemContentChanged:
        break;
    case kEdsObjectEvent_DirItemCreated:
        break;
    case kEdsObjectEvent_DirItemInfoChanged:
        break;
    case kEdsObjectEvent_DirItemRemoved:
        break;
    case kEdsObjectEvent_DirItemRequestTransfer:
        DownloadImage(object, ImageSaveDirectory);
        break;
    case kEdsObjectEvent_DirItemRequestTransferDT:
        break;
    case kEdsObjectEvent_FolderUpdateItems:
        break;
    case kEdsObjectEvent_VolumeAdded:
        break;
    case kEdsObjectEvent_VolumeInfoChanged:
        break;
    case kEdsObjectEvent_VolumeRemoved:
        break;
    case kEdsObjectEvent_VolumeUpdateItems:
        break;
    } 
    // Object must be released 
    if(object) 
    { 
        EdsRelease(object); 
    } 
    return EDS_ERR_OK;
}

和我的公开会议:

EdsError OpenSession(EdsCameraRef NewCamera)
{
    //CamRef = NewCamera;
    if (CameraSessionOpen) EdsCloseSession(NewCamera);
    if (NewCamera != NULL)
    {
        //open a session
        Error = EdsOpenSession(NewCamera);
        //subscribe to the camera events (this time, in-Camera)
        Error = EdsSetObjectEventHandler(NewCamera, kEdsObjectEvent_All, SDKObjectEventHandler, NULL);
        Error = EdsSetPropertyEventHandler(NewCamera, kEdsPropertyEvent_All, SDKPropertyEventHandler, NULL);
        Error = EdsSetCameraStateEventHandler(NewCamera,kEdsStateEvent_All, SDKStateEventHandler,NULL);
        CameraSessionOpen = true;
    }
    return Error;
}

和下载图片:

 EdsError DownloadImage(EdsDirectoryItemRef DirItemRef,string Sdirectory )
    {
        EdsDirectoryItemInfo dirInfo;
        EdsStreamRef StreamRef;
        //get information about object
        Error = EdsGetDirectoryItemInfo(DirItemRef, &dirInfo);
        if (Error != EDS_ERR_OK)
            cout<< "SDK Error: " << Error << endl;
        System::String^ CurrentPhoto2;

    string extention = ".CR2" ;
    System::String^ extention2 = gcnew System::String(extention.c_str());
    System::Console::WriteLine(extention2);


    System::String^ directory2 = gcnew System::String(Sdirectory.c_str());
    System::Console::WriteLine(directory2);

    if (ImageName!= 0 )
    {
        System::String^ ImageName2 = gcnew System::String(ImageName/*.c_str()*/);
        System::Console::WriteLine(ImageName2);

        CurrentPhoto2 = Path::Combine(directory2 , ImageName2 , extention2);
    }
    else
    {
        System::String^ FileName2 = gcnew System::String(dirInfo.szFileName);
        System::Console::WriteLine(FileName2);

        CurrentPhoto2 = Path::Combine(directory2,FileName2);
    }
    EdsChar* CurrentPhoto = (char*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(CurrentPhoto2).ToPointer();
    System::Runtime::InteropServices::Marshal::FreeHGlobal((System::IntPtr) CurrentPhoto);

    //create filestream to data
    Error = EdsCreateFileStream(CurrentPhoto, kEdsFileCreateDisposition_CreateAlways, kEdsAccess_ReadWrite, &StreamRef);
    //download file
    Error = DownloadData(DirItemRef, StreamRef);
    //release stream
    Error = EdsRelease(StreamRef);
    return Error;

}

最后拍摄照片功能:

EdsError TakePhoto(EdsCameraRef NewCamera)
{
    int BusyCount = 0;
    EdsError mErr = EDS_ERR_OK;
    //sometimes the camera is not ready immediately
    while (BusyCount < 20)
    {
        //send command to camera
        ImageName = "red" ;
        mErr = EdsSendCommand(NewCamera, kEdsCameraCommand_TakePicture, 0);
        //if camera is busy, wait and try again
        if (mErr == EDS_ERR_DEVICE_BUSY)
        { 
            BusyCount++;
        }
        if (mErr != EDS_ERR_OK)
            cout<< "SDK Error: " << mErr << endl;
    }
    Error = mErr;
    return Error;
}

每件事似乎都是正确的!!但它不起作用! :( 请帮我...

1 个答案:

答案 0 :(得分:1)

我已经解决了我的问题。 这是我正确的代码:

EdsError DownloadImage(EdsDirectoryItemRef DirItemRef,string Sdirectory ,string Imgname, string exten)
{
    EdsDirectoryItemInfo dirInfo;
    EdsStreamRef StreamRef;
    //get information about object
    Error = EdsGetDirectoryItemInfo(DirItemRef, &dirInfo);
    if (Error != EDS_ERR_OK)
        cout<< "SDK Error: " << Error << endl;
    string path;


    if ( !ImageName.empty() )
    {
        path = Sdirectory + "\\" + ImageName + exten;
    }
    else
    {
        path = Sdirectory + "\\" + dirInfo.szFileName ;
    }

    const EdsChar* CurrentPhoto = path.c_str() ;

    //create filestream to data
    Error = EdsCreateFileStream(CurrentPhoto, kEdsFileCreateDisposition_CreateAlways, kEdsAccess_ReadWrite, &StreamRef);
    //download file
    Error = DownloadData(DirItemRef, StreamRef);
    //release stream
    Error = EdsRelease(StreamRef);
    return Error;

}

void main(int argc, char **argv)
{

    CamRef = NULL;
    bool isSDKLoaded = false;
    CameraSessionOpen = false;
    string ImgeSvDrctry;
    string exten;
    string Imgname;

    EdsDirectoryItemRef DirectoryItemRef = NULL;
    EdsInt32 SaveTarget = kEdsSaveTo_Host;
    ImageSaveDirectory = "";
    MSG msg;

    //initial sdk
    Error = InitSDK();

    // Get first camera
    if (Error == EDS_ERR_OK)
    {
        Error = getFirstCamera ( &CamRef ); 
        if (Error == EDS_ERR_OK)
        {
            // Open session with camera
            Error = OpenSession(CamRef);    
            if(Error != EDS_ERR_OK)
                cout << "session failed" <<Error << endl;
            else
            {
                cout << "session opened" << endl;
                Error = DeviceInformation(CamRef);

                // Set camera properties
                Error = EdsSetPropertyData(CamRef, kEdsPropID_SaveTo , 0, sizeof(SaveTarget), &SaveTarget );  
                if(Error == EDS_ERR_OK)
                {
                    cout << "camera properties seted!" << endl;
                }else
                    cout << "Unable to set camera properties, SDK Error: " << Error <<endl; 
                //set capacity
                Error = SetCapacity(CamRef);
                if (Error == EDS_ERR_OK)
                {
                    std::cout << "capacity seted ! " << std::endl;
                    cout <<"Enter the image save directory : " ;
                    cin >> ImgeSvDrctry ;
                    ImageSaveDirectory = ImgeSvDrctry;

                    cout <<"Enter the image name : " ;
                    cin >> Imgname ;
                    ImageName = Imgname ;

                    cout <<"Enter the image extention : " ;
                    cin >> exten ;
                    extention = exten;

                    Error = TakePhoto(CamRef);

                }
                while((GetMessage(&msg, NULL, 0, 0)) != 0)
                {
                    if(GetMessage(&msg, NULL, 0, 0) == -1 || bPhotoTaken)
                    {
                        break;
                    }
                    else
                    {
                        TranslateMessage(&msg);
                        DispatchMessage(&msg);
                    }
                }
            }       
            // Close session with camera
            Error = CloseSession(CamRef);
        }else
            cout<< "SDK Error: " << Error << endl;
    }else
        cout<< "SDK Error: " << Error << endl;

    // Release camera
    if(CamRef != NULL) EdsRelease(CamRef);

    // Terminate SDK
    Error = TerminateSDK();
return 0;
    }