Example C program to help new members get started

Issues dealing with Canon SDK in C or C++

Re: Example C program to help new members get started

Postby stephenyeung » Thu Feb 18, 2010 11:53 pm



Great. I got it. Now I can control the shutter speed and save the file to CFcard in jpeg format. My next problem is: I can't store the image file to my harddisk. I read many post here and follow their ans but it dont work. what step I need to do in order to save the file to my hardisk?


stephenyeung
 
Posts: 3
Joined: Wed Feb 17, 2010 4:44 am

Re: Example C program to help new members get started

Postby pick » Fri Feb 19, 2010 12:24 pm

Well, if you've followed all the advice here, then I'm not sure why it's not working. Can you be more specific than "it's not working"? The basics:
-Set the SaveTo property to Host
-Set the capacity to some huge number
-Write (at least) the object event handler (probably also want to write the Property and CameraState event handlers)
-Ensure whatever thread you called EdsInitializeSDK in has a message pump running
-Call TakePicture
-In the Object event handler:
-If you want the filename of the image, call EdsGetDirectoryItemInfo(dir_item,&dir_info); and use the the dir_info struct
-Create a file stream (EdsCreateFileStream)
-install a progress callback (if you want to): EdsSetProgressCallback
-Download the picture to the file stream: EdsDownload
-Tell the camera the download is finished: EdsDownloadComplete
-Release the directory item: EdsRelease
-Release the file stream: EdsRelease

That should get you your picture from the camera.
pick
Canon SDK Expert
 
Posts: 61
Joined: Mon Jan 19, 2009 6:45 pm

Re: Example C program to help new members get started

Postby stephenyeung » Wed Feb 24, 2010 8:00 am

Is it necessary to use "object event handler" I tried to follow canon example to get the image size, the result is "0". When I tried to convert it from jpeg to raw, it turn out error 99 and the camera crash! I have sent the camera back for repairing and can't test the program until I get it back. meanwhile, anyone can give me suggestion about how to set the object event handler?
stephenyeung
 
Posts: 3
Joined: Wed Feb 17, 2010 4:44 am

Re: Example C program to help new members get started

Postby pick » Wed Feb 24, 2010 11:28 am

It's in the example code in the first post of this thread.
pick
Canon SDK Expert
 
Posts: 61
Joined: Mon Jan 19, 2009 6:45 pm

Re: Example C program to help new members get started

Postby Kevin Cain » Sun May 02, 2010 7:31 am

Thanks for such a useful post -- I attempted to follow your example verbatim (code block below) with only a few modifications:

- As noted by others in this thread, I found I had to switch from the 'bulb' commands to 'TakePicture'
- I changed the winMain type (shouldn't have any effect on the code)
- I directed output to a file, for use as a log.

When connected to a Canon 7D, I'm able to make it through 'EdsGetImageInfo', which fails with code '96'. My run log is as follows:
---------------
EdsInitializeSDK: 0
EdsGetCameraList: 0
EdsGetChildCount: 0
EdsGetChildAtIndex: 0
EdsOpenSession: 0
EdsSetCameraAddedHandler: 0
EdsSetPropertyEventHandler: 0
EdsSetObjectEventHandler: 0
EdsSetCameraStateEventHandler: 0
EdsSetPropertyData.kEdsPropID_SaveTo: 0
EdsSetCapacity: 0
EdsSendCommand.kEdsCameraCommand_TakePicture: 0
EdsGetDirectoryItemInfo: 0
EdsCreateMemoryStream.memStream: 0
EdsDownload: 0
EdsDownloadComplete: 0
EdsRelease.dirItemRef: 0
EdsCreateImageRef: 0
EdsRelease.memStream: 0
EdsGetImageInfo: 96
Program exit:Terminating:

I'm stopped for the moment -- any idea why this could be happening?

Thanks, again,

-Kevin

Code: Select all
#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);
}

Kevin Cain
 
Posts: 1
Joined: Sun May 02, 2010 7:24 am

Re: Example C program to help new members get started

Postby mattp » Mon May 24, 2010 9:24 pm

The error (from EDSDKErrors.h) refers to an invalid parameter (EDS_ERR_INVALID_PARAMETER). I don't have time to debug your program but hopefully this will help.
mattp
Canon SDK Professional
 
Posts: 22
Joined: Thu Oct 08, 2009 7:08 pm

Re: Example C program to help new members get started

Postby huckw » Wed Jun 02, 2010 9:07 pm

I had the same problem with error 96 at EdsGetImageInfo() but fixed it by setting second parameter to kEdsImageSrc_FullView instead of kEdsImageSrc_RAWFullView. I don't know why it was broke, nor why that fixed it for me. I also changed the 2nd and 3rd parameters of EdsGetImage() to be kEdsImageSrc_FullView and kEdsTargetImageType_RGB respectiveley (not because the change was needed to compile, but rather because it seemed like the prudent thing to do). Hope that helps.
huckw
 
Posts: 2
Joined: Wed Jun 02, 2010 8:59 pm

Re: Example C program to help new members get started

Postby huckw » Wed Jun 02, 2010 9:24 pm

I have modified the sample code to issue the take picture command instead of the bulb command. I've also been able to successfully set the Tv, Av and ISO and am getting the images like I would expect. However, for very long exposures (a few seconds) the program seems to stall out...I have to kill it using windows task mangler. Camera will remain in this strange mode until I (a) turn off camera, at which point the camera screen says something about "waiting for images to record" (b) unplug USB and (c) remove battery. I'm suspecting it has something to do with the Sleep(2000) in the message pump since I get the same behavior when I set that sleep value to something thats apparently too short (like 500)...in this case I hear two images captured (which the sleep(2000) is there to avoid) and then program and camera hang.

If I kill the app but don't do (a-c) to the camera above (thereby leaving camera in hung state), I can run the code again and see that it gets stuck at EdsCloseSession(). Just prior to this call I checked the error state of the camera and its 0x81=EDS_ERR_DEVICE_BUSY. Which kinda makes sense since when I power off camera it shows the little rotating circle and gives message about waiting for images to record.

Am I correct about the sleep() being the issue? Any thought on how to make the Sleep value dynamic (dependent on Tv)...or some other event to trigger on?
huckw
 
Posts: 2
Joined: Wed Jun 02, 2010 8:59 pm

Previous

Return to Canon SDK in C/C++

Who is online

Users browsing this forum: No registered users and 1 guest

cron