我有佳能EOS D600相机。需要使用C++拍照。文档没有帮助我。您是否有示例代码可以拍照?这没有帮助:
#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;
}
有错误:
[链接器错误]未定义对`imp_EdsInitializeSDK @ 0'的引用
请帮忙
最佳答案
您好,此代码有效。
#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;
}
关于c++ - 佳能SDK。从相机拍照,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18267636/