2013-08-16 187 views
1

我有佳能EOS D600相機。需要使用C++拍攝照片。文檔沒有幫助我。您是否有示例代碼,需要拍照?這並沒有幫助:Canon SDK。從相機拍照

#include <windows.h> 
#include <iostream> 
#include <conio.h> 
#include <stdio.h> 
#include <EDSDK.h> 
#include <EDSDKTypes.h> 
#include <EDSDKErrors.h> 

using namespace std; 

EdsError getFirstCamera(EdsCameraRef *camera); 
int main(){ 
    EdsError err=EDS_ERR_OK; 
    EdsCameraRef camera=NULL; 
    bool isSDKloaded=false; 
    err=EdsInitializeSDK(); 
    if(err==EDS_ERR_OK) isSDKloaded=true; 
    if(err==EDS_ERR_OK) err=getFirstCamera(&camera); 
    EdsOpenSession(camera); 
    EdsInt32 saveTarget = kEdsSaveTo_Host; 
    err = EdsSetPropertyData(camera, kEdsPropID_SaveTo, 0, 4, &saveTarget); 

    EdsCapacity newCapacity = {0x7FFFFFFF, 0x1000, 1}; 
    err = EdsSetCapacity(camera, newCapacity); 


    const char* ch_dest = "C:\\photo\\Img.jpg"; 
    EdsCreateFileStream(ch_dest ,kEdsFileCreateDisposition_CreateAlways,kEdsAccess_ReadWrite,0); 

    EdsSendCommand(camera, kEdsCameraCommand_TakePicture, 0); 
    EdsCloseSession(camera); 
    EdsTerminateSDK(); 
    return 0; 
} 

EdsError getFirstCamera(EdsCameraRef *camera) 
{ 
EdsError err=EDS_ERR_OK; 
EdsCameraListRef cameraList=NULL; 
EdsUInt32 count=0; 
// Get camera list 
err = EdsGetCameraList(&cameraList); 
// Get number of cameras 
if(err == EDS_ERR_OK) 
{ 
    err = EdsGetChildCount(cameraList, &count); 
    if(count == 0) 
    { 
     err = EDS_ERR_DEVICE_NOT_FOUND; 
    } 
} 
// Get first camera retrieved 
if(err == EDS_ERR_OK) 
{ 
    err = EdsGetChildAtIndex(cameraList , 0 , camera); 
} 
// Release camera list 
if(cameraList != NULL) 
{EdsRelease(cameraList); 
cameraList = NULL; 
} 
return err; 
} 

#include <windows.h> 
#include <iostream> 
#include <conio.h> 
#include <stdio.h> 
#include <EDSDK.h> 
#include <EDSDKTypes.h> 
#include <EDSDKErrors.h> 

using namespace std; 

EdsError getFirstCamera(EdsCameraRef *camera); 
int main(){ 
    EdsError err=EDS_ERR_OK; 
    EdsCameraRef camera=NULL; 
    bool isSDKloaded=false; 
    err=EdsInitializeSDK(); 
    if(err==EDS_ERR_OK) isSDKloaded=true; 
    if(err==EDS_ERR_OK) err=getFirstCamera(&camera); 
    EdsOpenSession(camera); 
    EdsInt32 saveTarget = kEdsSaveTo_Host; 
    err = EdsSetPropertyData(camera, kEdsPropID_SaveTo, 0, 4, &saveTarget); 

    EdsCapacity newCapacity = {0x7FFFFFFF, 0x1000, 1}; 
    err = EdsSetCapacity(camera, newCapacity); 


    const char* ch_dest = "C:\\photo\\Img.jpg"; 
    EdsCreateFileStream(ch_dest ,kEdsFileCreateDisposition_CreateAlways,kEdsAccess_ReadWrite,0); 

    EdsSendCommand(camera, kEdsCameraCommand_TakePicture, 0); 
    EdsCloseSession(camera); 
    EdsTerminateSDK(); 
    return 0; 
} 

EdsError getFirstCamera(EdsCameraRef *camera) 
{ 
EdsError err=EDS_ERR_OK; 
EdsCameraListRef cameraList=NULL; 
EdsUInt32 count=0; 
// Get camera list 
err = EdsGetCameraList(&cameraList); 
// Get number of cameras 
if(err == EDS_ERR_OK) 
{ 
    err = EdsGetChildCount(cameraList, &count); 
    if(count == 0) 
    { 
     err = EDS_ERR_DEVICE_NOT_FOUND; 
    } 
} 
// Get first camera retrieved 
if(err == EDS_ERR_OK) 
{ 
    err = EdsGetChildAtIndex(cameraList , 0 , camera); 
} 
// Release camera list 
if(cameraList != NULL) 
{EdsRelease(cameraList); 
cameraList = NULL; 
} 
return err; 
} 

有錯誤: [鏈接錯誤]未定義參考`小鬼 _EdsInitializeSDK @ 0'

請幫助

+1

java在哪裏呢? –

+4

您鏈接到Canon EOS SDK嗎? – Chris

+4

問題不在於代碼,而在於你需要在庫中進行鏈接。你如何做到這一點取決於你的操作系統和環境。 – dutt

回答

1

你好此代碼的工作。

#include <iostream> 
#include "EDSDK.h" 
#include "EDSDKErrors.h" 
#include "EDSDKTypes.h" 
#include <thread> 
#include <chrono> 

char * directory = "Here is your directory"; 

void download_img(EdsBaseRef & object, EdsVoid * & context) 
{ 
    EdsStreamRef stream = NULL; 
    EdsDirectoryItemInfo dirItemInfo; 
    EdsGetDirectoryItemInfo(object, &dirItemInfo); 
    strcat(directory, file_name); 
    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) 
{ 
    download_img(object, context); 
    return EDS_ERR_OK; 
} 

void init_camera(EdsCameraRef & camera) 
{ 
    EdsError err = EDS_ERR_OK; 
    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); 
    } 
    EdsSetObjectEventHandler(camera, kEdsObjectEvent_DirItemCreated, handleObjectEvent, NULL); 
    EdsSendStatusCommand(camera, kEdsCameraStatusCommand_UIUnLock, 0); 
} 

void start_liveview(EdsCameraRef camera) 
{ 
    EdsOpenSession(camera); 
    EdsUInt32 device = kEdsPropID_Evf_OutputDevice; 
    EdsGetPropertyData(camera, kEdsPropID_Evf_OutputDevice, 0, sizeof(device), &device); 

    device |= kEdsEvfOutputDevice_PC; 
    EdsSetPropertyData(camera, kEdsPropID_Evf_OutputDevice, 0, sizeof(device), &device); 
} 

void stop_liveview(EdsCameraRef camera) 
{ 
    EdsUInt32 device; 
    EdsGetPropertyData(camera, kEdsPropID_Evf_OutputDevice, 0, sizeof(device), &device); 
    device &= ~kEdsEvfOutputDevice_PC; 
    EdsSetPropertyData(camera, kEdsPropID_Evf_OutputDevice, 0, sizeof(device), &device); 
    EdsCloseSession(camera); 
} 

void take_video(EdsCameraRef camera, int length) 
{ 
    EdsOpenSession(camera); 
    EdsUInt32 record_start = 4; // Begin movie shooting 
    EdsSetPropertyData(camera, kEdsPropID_Record, 0, sizeof(record_start), &record_start); 
    this_thread::sleep_for(chrono::milliseconds(length)); 
    EdsUInt32 record_stop = 0; // End movie shooting 
    EdsSetPropertyData(camera, kEdsPropID_Record, 0, sizeof(record_stop), &record_stop); 
    EdsCloseSession(camera); 
} 

void update_data(EdsCameraRef camera) 
{ 
    EdsOpenSession(camera); 
    // here event happens 
    EdsCloseSession(camera); 
} 

void take_photo(EdsCameraRef camera, int count, int interv_millsec) 
{ 
    for (int i = 0; i < count; ++i) { 
     EdsOpenSession(camera); 
     cout << "shoot" << endl; 
     EdsSendCommand(camera, kEdsCameraCommand_TakePicture, 0); 
     this_thread::sleep_for(chrono::milliseconds(interv_millsec)); 
     EdsCloseSession(camera); 
    } 
    update_data(camera); 
} 

void dispose(EdsCameraRef camera) 
{ 
    EdsCloseSession(camera); 
    EdsTerminateSDK(); 
} 

int main(){ 
    EdsCameraRef camera; 
    init_camera(camera); 

    // here you may call functions above 

    // to take video 
    // firstly you have to start liveview 
    // secondly call "take video" 
    // thirdly stop liveview 
    return 0; 
}