Search code examples
visual-c++edsdkcanon-sdk

download image to the PC and change name of image in Canon EDSDK


I am trying to write a program to take picture by Canon EOS700D and change the image name, and also save that image to the certain directory. but I don know why it doesn't work!the Error is EDS_ERR_TAKE_PICTURE_CARD_NG. this is my object event handler function:

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

and my open session:

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

and the download image:

 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;

}

and finally the take photo function:

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

every thing seems to be correct!! but it doesn't work! :( plz help mee ...


Solution

  • I have solved my problem. this is my correct code:

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