Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 244 lines (180 sloc) 8.5 KB
#pragma once
//Written by Theo Watson -
//We are missing code for legacy devices
//We are missing some mac specific snippets
//Both should be easy to integrate
//You also need the Canon SDK which you can request from them or possibily find by other means floating around in the ether.
#include "EDSDK.h"
#include "EDSDKErrors.h"
#include "EDSDKTypes.h"
#include "Freeimage.h"
#include "ofMain.h"
#ifndef BYTE
#define BYTE unsigned char
typedef enum{
static int sdkRef = 0;
static void easyRelease(EdsBaseRef &ref){
if(ref != NULL){
ref = NULL;
static void putBmpIntoPixels(FIBITMAP * bmp, ofPixels &pix){
int width = FreeImage_GetWidth(bmp);
int height = FreeImage_GetHeight(bmp);
int bpp = FreeImage_GetBPP(bmp);
int bytesPerPixel = bpp / 8;
// call the allocation routine (which checks if really need to allocate) here
pix.allocate(width, height, OF_IMAGE_COLOR);
FreeImage_ConvertToRawBits(pix.getPixels(), bmp, width*bytesPerPixel, bpp, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK, true); // get bits
class memoryImage : public ofImage{
bool loadFromMemory(int bytesToRead, unsigned char * jpegBytes, int rotateMode = 0){
hmem = FreeImage_OpenMemory((BYTE *)jpegBytes, bytesToRead);
if (hmem == NULL){
printf("couldn't create memory handle! \n");
return false;
//get the file type!
FREE_IMAGE_FORMAT fif = FreeImage_GetFileTypeFromMemory(hmem);
if( fif == -1 ){
printf("unable to guess format", fif);
return false;
//make the image!!
FIBITMAP * tmpBmp = FreeImage_LoadFromMemory(fif, hmem, 0);
if( rotateMode > 0 && rotateMode < 4){
FIBITMAP * oldBmp = tmpBmp;
if( rotateMode == 1)tmpBmp = FreeImage_RotateClassic(tmpBmp, 90);
if( rotateMode == 2)tmpBmp = FreeImage_RotateClassic(tmpBmp, 180);
if( rotateMode == 3)tmpBmp = FreeImage_RotateClassic(tmpBmp, 270);
putBmpIntoPixels(tmpBmp, getPixelsRef());
width = FreeImage_GetWidth(tmpBmp);
height = FreeImage_GetHeight(tmpBmp);
bpp = FreeImage_GetBPP(tmpBmp);
return true;
//shouldn't have to redefine this but a gcc bug means we do
inline void swapRgb(ofPixels &pix){
if (pix.getBitsPerPixel() != 8){
int sizePixels = pix.getWidth()*pix.getHeight();
int cnt = 0;
unsigned char temp;
int byteCount = pix.getBitsPerPixel()/8;
while (cnt < sizePixels){
temp = pix.getPixels()[cnt*byteCount];
pix.getPixels()[cnt*byteCount] = pix.getPixels()[cnt*byteCount+2];
pix.getPixels()[cnt*byteCount+2] = temp;
//We are missing code for legacy devices
//We are missing some mac specific snippets
//Both should be easy to integrate
class CanonCameraWrapper{
ofEvent<string> evtImageDownloaded;
bool setup(int cameraID); //You must call this to init the canon sdk
void destroy(); //To clean up - also called by destructor
bool openSession(); //Begins communication with camera
bool closeSession(); //Ends communication with camera.
//Note on sessions: Commands like takePicture
//will open a session if none exists. This
//is slower though so consider calling it
//once at the begining of your app.
void setDeleteFromCameraAfterDownload(bool deleteAfter);
void setDownloadPath(string downloadPathStr);
void enableDownloadOnTrigger(); //Trigger meaning takePicture
void disableDownloadOnTrigger(); //Trigger meaning takePicture
bool takePicture(); //Takes a picture. If enabled it will also download
//the image to the folder set by the download path.
bool sendCommand( EdsCameraCommand inCommand, EdsInt32 inParam = 0);
bool beginLiveView(); //starts live view
bool endLiveView(); //ends live view
bool grabPixelsFromLiveView(int rotateByNTimes90 = 0); //capture the live view to rgb pixel array
bool saveImageFromLiveView(string saveName);
bool getLiveViewActive(); //true if live view is enabled
int getLiveViewFrameNo(); //returns the number of live view frames passed
void resetLiveViewFrameCount(); //resets to 0
bool isLiveViewPixels(); //true if there is captured pixels
int getLiveViewPixelWidth(); //width of live view pixel data
int getLiveViewPixelHeight(); //height of live view pixel data
unsigned char * getLiveViewPixels(); //returns captured pixels
string getLastImageName(); //The name of the last downloaded image
string getLastImagePath(); //The full path of the last downloaded image
//This doesn't work perfectly - for some reason it can be one image behind
//something about how often the camera updates the SDK.
//Having the on picture event registered seems to help.
//But downloading via event is much more reliable at the moment.
//WARNING - If you are not taking pictures and you have bDeleteAfterDownload set to true
//you will be deleting the files that are on the camera.
//Simplified: be careful about calling this when you haven't just taken a photo.
bool downloadLastImage();
//Hmm - might be needed for threading - currently doesn't work
bool isTransfering();
bool downloadImage(EdsDirectoryItemRef directoryItem);
static EdsError EDSCALLBACK handleObjectEvent(EdsObjectEvent inEvent, EdsBaseRef object, EdsVoid *inContext);
static EdsError EDSCALLBACK handlePropertyEvent(EdsPropertyEvent inEvent, EdsPropertyID inPropertyID, EdsUInt32 inParam, EdsVoid * inContext);
static EdsError EDSCALLBACK handleStateEvent(EdsStateEvent inEvent, EdsUInt32 inEventData, EdsVoid * inContext);
void registerCallback();
bool preCommand();
void postCommand();
int livePixelsWidth;
int livePixelsHeight;
unsigned char * livePixels;
EdsUInt32 evfMode;
EdsUInt32 device;
int liveViewCurrentFrame;
string lastImageName;
string lastImagePath;
string downloadPath;
bool downloadEnabled;
bool bDeleteAfterDownload;
bool registered;
bool needToOpen;
cameraState state;
EdsCameraRef theCamera ;
EdsCameraListRef theCameraList;
Something went wrong with that request. Please try again.