#include "stdafx.h"
#include "CameraControl.h"
#include "CameraControlDlg.h"
#include "EDSDK.h"
#include "EDSDKTypes.h"
#include "CameraModel.h"
#include "CameraModelLegacy.h"
#include "CameraController.h"
#include "CameraEventListener.h"
#include "TakePictureCommand.h"
#include "DownloadCommand.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
/* Function prototype. Displays the results of an SDK call. */
void showResult(char* sdkFunctionName, EdsError result);
/* Function prototype. Displays a message, pauses and exits the program. */
void exitProgram(char* message);
/* Function prototype. Camera added callback function. */
EdsError EDSCALLBACK cameraAddedHandler(EdsVoid* inContext);
/* Function prototype. Property event callback function. */
EdsError EDSCALLBACK propertyEventHandler(EdsPropertyEvent inEvent, EdsPropertyID inPropertyID, EdsUInt32 inParam, EdsVoid* inContext);
/* Function prototype. Object event callback function. */
EdsError EDSCALLBACK objectEventHandler(EdsObjectEvent inEvent, EdsBaseRef inRef, EdsVoid* inContext);
/* Function prototype. State event callback function. */
EdsError EDSCALLBACK stateEventHandler(EdsStateEvent inEvent, EdsUInt32 inEventData, EdsVoid* inContext);
/* Function prototype. Progress callback function. */
EdsError EDSCALLBACK progressCallback(EdsUInt32 inPercent, EdsVoid* inContext, EdsBool* outCancel);
/* Function prototype. Downloads the latest directory item. */
void downloadDirectoryItem();
//reference to lastest directory item
EdsBaseRef dirItemRef = NULL;
//the number of downloaded images
int numDownloads = 0;
CameraModel* cameraModelFactory(EdsCameraRef camera, EdsDeviceInfo deviceInfo);
// CCameraControlApp
BEGIN_MESSAGE_MAP(CCameraControlApp, CWinApp)
ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()
// CCameraControlApp construction
CCameraControlApp::CCameraControlApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
// The one and only CCameraControlApp object
CCameraControlApp theApp;
// CCameraControlApp initialization
BOOL CCameraControlApp::InitInstance()
{
InitCommonControls();
CWinApp::InitInstance();
// Init log file
FILE *fp;
fp=fopen("camera.txt", "w");
fprintf(fp, "Camera Run Log:\n");
fprintf(fp, "---------------\n");
fprintf(fp, "\n");
fclose(fp);
//initialize the SDK
showResult("EdsInitializeSDK", EdsInitializeSDK());
//list of connected cameras
EdsCameraListRef cameraListRef;
//get the list of connected cameras
showResult("EdsGetCameraList", EdsGetCameraList(&cameraListRef));
//the number of connected cameras
EdsUInt32 numCameras;
//get the number of connected cameras
showResult("EdsGetChildCount", EdsGetChildCount(cameraListRef, &numCameras));
//if no cameras connected, exit the program
if(numCameras == 0)
exitProgram("No cameras connected to host");
//the camera reference
EdsBaseRef cameraRef;
//get the camera at index 0
showResult("EdsGetChildAtIndex", EdsGetChildAtIndex(cameraListRef, 0, &cameraRef));
//open a session with the camera
showResult("EdsOpenSession", EdsOpenSession(cameraRef));
//********************************* Register callback functions
//set camera added callback
showResult("EdsSetCameraAddedHandler", EdsSetCameraAddedHandler(&cameraAddedHandler, 0));
//set property event callback
showResult("EdsSetPropertyEventHandler", EdsSetPropertyEventHandler(cameraRef, kEdsPropertyEvent_All, &propertyEventHandler, 0));
//set object event callback
showResult("EdsSetObjectEventHandler", EdsSetObjectEventHandler(cameraRef, kEdsObjectEvent_All, &objectEventHandler, 0));
//set camera state event callback
showResult("EdsSetCameraStateEventHandler", EdsSetCameraStateEventHandler(cameraRef, kEdsStateEvent_All, &stateEventHandler, 0));
//*********************************
//inform the SDK to save to host
EdsUInt32 saveTo = kEdsSaveTo_Host;
showResult("EdsSetPropertyData.kEdsPropID_SaveTo", EdsSetPropertyData(cameraRef, kEdsPropID_SaveTo, 0, sizeof(saveTo), &saveTo));
//set the capacity of the host (this hard-coded value comes from Canon's VC example)
EdsCapacity capacity = {0x7FFFFFFF, 0x1000, 1};
showResult("EdsSetCapacity", EdsSetCapacity(cameraRef, capacity));
//start a message pump
MSG Msg;
while(GetMessage(&Msg, NULL, 0, 0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
//wait for message 275 (WM_TIMER) which appears to indicate no new messages
if(Msg.message == 275)
{
//take a picture if there isn't one outstanding
if(dirItemRef == NULL)
{
//take a picture
showResult("EdsSendCommand.kEdsCameraCommand_TakePicture", EdsSendCommand(cameraRef, kEdsCameraCommand_TakePicture, 0));
//showResult("EdsSendCommand.kEdsCameraCommand_BulbStart", EdsSendCommand(cameraRef, kEdsCameraCommand_BulbStart, 0));
//Sleep(500);
//showResult("EdsSendCommand.kEdsCameraCommand_BulbEnd", EdsSendCommand(cameraRef, kEdsCameraCommand_BulbEnd, 0));
}
else
{
//download the latest directory item reported by the callback
downloadDirectoryItem();
//the number of images to take and download before exiting the message pump and the program
if(numDownloads == 25)
break;
}
//give the camera a moment to process the command or multiple shots may occur
Sleep(2000);
}
}
//close the session with the camera
showResult("EdsCloseSession", EdsCloseSession(cameraRef));
//terminate the SDK
showResult("EdsTerminateSDK", EdsTerminateSDK());
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
/* Displays the results of an SDK call. */
void showResult(char* sdkFunctionName, EdsError result)
{
//printf("%s: %u\n", sdkFunctionName, result);
//::MessageBox(NULL,"Begin",NULL,MB_OK);
FILE *fp;
fp=fopen("camera.txt", "a");
fprintf(fp, "%s: %u\n", sdkFunctionName, result);
fclose(fp);
//if the result is an error, exit the program
if(result != EDS_ERR_OK)
exitProgram("Terminating");
}
/* Displays a message, pauses and exits the program. */
void exitProgram(char* message)
{
//puts(message);
FILE *fp;
fp=fopen("camera.txt", "a");
fprintf(fp, "Program exit:%s:\n", message);
fclose(fp);
exit(1);
}
/* Camera added handler. */
EdsError EDSCALLBACK cameraAddedHandler(EdsVoid* inContext)
{
//this callback is not needed for this example program
return EDS_ERR_OK;
}
/* Property event callback function. */
EdsError EDSCALLBACK propertyEventHandler(EdsPropertyEvent inEvent, EdsPropertyID inPropertyID, EdsUInt32 inParam, EdsVoid* inContext)
{
//this callback is not needed for this example program
return EDS_ERR_OK;
}
/* Object event callback function. */
EdsError EDSCALLBACK objectEventHandler(EdsObjectEvent inEvent, EdsBaseRef inRef, EdsVoid* inContext)
{
//if the event is a request to transfer a directory item, cache the directory item reference
if(inEvent == kEdsObjectEvent_DirItemRequestTransfer)
dirItemRef = inRef;
return EDS_ERR_OK;
}
/* State event callback function. */
EdsError EDSCALLBACK stateEventHandler(EdsStateEvent inEvent, EdsUInt32 inEventData, EdsVoid* inContext)
{
//this callback is not needed for this example program
return EDS_ERR_OK;
}
/* Progress callback function. */
EdsError EDSCALLBACK progressCallback(EdsUInt32 inPercent, EdsVoid* inContext, EdsBool* outCancel)
{
//this callback is not needed for this example program
return EDS_ERR_OK;
}
/* Downloads the latest directory item. */
void downloadDirectoryItem()
{
//seperate each download with a new line
//printf("\n");
//get information about the image (directory item)
EdsDirectoryItemInfo dirItemInfo;
showResult("EdsGetDirectoryItemInfo", EdsGetDirectoryItemInfo(dirItemRef, &dirItemInfo));
//create a memory stream
EdsStreamRef memStream;
showResult("EdsCreateMemoryStream.memStream", EdsCreateMemoryStream(dirItemInfo.size, &memStream));
//download the RAW data to the memory stream
showResult("EdsDownload", EdsDownload(dirItemRef, dirItemInfo.size, memStream));
//inform the SDK that the download has completed
showResult("EdsDownloadComplete", EdsDownloadComplete(dirItemRef));
//release the directory item
showResult("EdsRelease.dirItemRef", EdsRelease(dirItemRef));
dirItemRef = NULL;
//get a reference to the image data in the RAW data stream
EdsImageRef imageRef;
showResult("EdsCreateImageRef", EdsCreateImageRef(memStream, &imageRef));
//release the memory stream
showResult("EdsRelease.memStream", EdsRelease(memStream));
//get image information
EdsImageInfo imageInfo;
showResult("EdsGetImageInfo", EdsGetImageInfo(imageRef, kEdsImageSrc_RAWFullView, &imageInfo));
//use image information to get the image data
EdsRect imageRect;
imageRect.point.x = imageRect.point.y = 0;
imageRect.size.width = imageInfo.width;
imageRect.size.height = imageInfo.height;
EdsSize imageSize;
imageSize.width = imageInfo.width;
imageSize.height = imageInfo.height;
//display the size of the image
FILE *fp;
fp=fopen("camera.txt", "a");
fprintf(fp, "Image size: %dx%d\n", imageInfo.width, imageInfo.height);
fclose(fp);
//create a stream to the actual image data
EdsStreamRef imageStream;
showResult("EdsCreateMemoryStream.imageStream", EdsCreateMemoryStream(0, &imageStream));
//get the image data (via the stream)
showResult("EdsGetImage", EdsGetImage(imageRef, kEdsImageSrc_RAWFullView, kEdsTargetImageType_RGB16, imageRect, imageSize, imageStream));
//release the image reference
showResult("EdsRelease.imageRef", EdsRelease(imageRef));
//release the image stream
showResult("EdsRelease.imageStream", EdsRelease(imageStream));
numDownloads++;
printf("Finished acquiring image %d\n\n", numDownloads);
}
CameraModel* cameraModelFactory(EdsCameraRef camera, EdsDeviceInfo deviceInfo)
{
// if Legacy protocol.
if(deviceInfo.deviceSubType == 0)
{
return new CameraModelLegacy(camera);
}
// PTP protocol.
return new CameraModel(camera);
}
Users browsing this forum: No registered users and 1 guest