#include "StdAfx.h"
#include "CanonCamera.h"

int CanonCamera::numOfCameras = 0;

CanonCamera::CanonCamera(void)
{
liveView = false;
camID = numOfCameras;
numOfCameras++;

windowName = "Camera ";
windowName += '1' + camID;
windowName += " Window";

EdsCameraListRef cameraList = NULL;

EdsGetCameraList(&cameraList);

EdsUInt32   camCount = 0;
EdsGetChildCount(cameraList, &camCount);

if(camCount==0)
{
std::cout<<"No Camera Found"<<". Press Any Key to Exit.";
getch();
exit(-1);
}
else
{
std::cout<<camCount<<" Camera(s) found!\n";
} 

EdsError err = EDS_ERR_OK;

err = EdsGetChildAtIndex(cameraList , camID , &camera);

//Release camera list
if(cameraList != NULL)
{
EdsRelease(cameraList);
}

detectedCams=camCount;

//open sesion
err=EdsOpenSession(camera);

if(err!=EDS_ERR_OK)
{
std::cout<<"Problem with Camera connection"<<". Press Any Key to Exit";
getch();
exit(-1);
}

}

CanonCamera::~CanonCamera(void)
{
if(liveView)
endLiveview();
EdsCloseSession(camera);
}

EdsError CanonCamera::startLiveview()
{

cvNamedWindow(windowName.c_str(),CV_WINDOW_AUTOSIZE);
cvResizeWindow(windowName.c_str(),640,480);

EdsError err = EDS_ERR_OK;

EdsUInt32 device;
err = EdsGetPropertyData(camera, kEdsPropID_Evf_OutputDevice, 0 , sizeof(device), &device );

if(err == EDS_ERR_OK)
{
device |= kEdsEvfOutputDevice_PC;
err = EdsSetPropertyData(camera, kEdsPropID_Evf_OutputDevice, 0 , sizeof(device), &device);
}

liveView = true;
return err;
}

EdsError CanonCamera::endLiveview()
{
EdsError err = EDS_ERR_OK;

// Get the output device for the live view image
EdsUInt32 device;
err = EdsGetPropertyData(camera, kEdsPropID_Evf_OutputDevice, 0 , sizeof(device), &device );

if(err == EDS_ERR_OK)
{
device &= ~kEdsEvfOutputDevice_PC;
err = EdsSetPropertyData(camera, kEdsPropID_Evf_OutputDevice, 0 , sizeof(device), &device);
}

cvDestroyWindow(windowName.c_str());

liveView=false;

return err;
}

void CanonCamera::UpdateView()
{

if(!liveView)
return;

EdsError err = EDS_ERR_OK;
EdsStreamRef stream = NULL;
EdsEvfImageRef evfImage = NULL;

// Create memory stream.
err = EdsCreateMemoryStream(0, &stream);

if(err != EDS_ERR_OK)
{
printf("Error in EdsCreateMemoryStream: 0x%X\n", err);
} 

err = EdsCreateEvfImageRef(stream, &evfImage);
if(err != EDS_ERR_OK)
{
printf("Error in EdsCreateEvfImageRef: 0x%X\n", err); 
} 

bool wait = true;

int numTries = 0;

while(wait && numTries++ < 20)
{
err = EdsDownloadEvfImage(camera, evfImage);
if(err != EDS_ERR_OK && err != EDS_ERR_OBJECT_NOTREADY)
{
printf("Error in EdsDownloadEvfImage: 0x%X\n", err);

}
if(err == EDS_ERR_OBJECT_NOTREADY)
{
Sleep(250);
}
else
{
wait = false;
}
}
if(numTries > 20)
{
printf("ERROR: camera is taking too long for EdsDownloadEvfImage\n");

}

unsigned char* pByteImage = NULL;

// Get image (JPEG) pointer.
err = EdsGetPointer(stream, (EdsVoid**)&pByteImage );
if(err != EDS_ERR_OK)
{
printf("Error in EdsGetPointer Histogram: 0x%X\n", err);

}

EdsUInt64 size;
err = EdsGetLength(stream, &size);
if(err != EDS_ERR_OK)
{
printf("Error in EdsGetLength Histogram: 0x%X\n", err);

}

EdsImageRef image = NULL;
EdsImageInfo imageInfo;

err = EdsCreateImageRef(stream, &image);
if(err != EDS_ERR_OK)
{
printf("Error in EdsCreateImageRef: 0x%X\n", err);

}

err = EdsGetImageInfo(image, kEdsImageSrc_FullView, &imageInfo);
if(err != EDS_ERR_OK)
{
printf("Error in EdsGetImageInfo: 0x%X\n", err);

}

if(imageInfo.componentDepth != 8)
{
printf("Error imageInfo.componentDepth != 8\n");

}

liveImage = cvCreateImage(cvSize(imageInfo.width, imageInfo.height), IPL_DEPTH_8U, imageInfo.numOfComponents);

EdsUInt32 DataSize = 0;

CImage cImage;
HRESULT hr;

CComPtr<IStream> iStream = NULL;
HGLOBAL hMem = GlobalAlloc(GHND, size);
LPVOID pBuff = GlobalLock(hMem);
memcpy(pBuff, pByteImage, size);
GlobalUnlock(hMem);
hr = CreateStreamOnHGlobal(hMem, TRUE, &iStream);

// Get the bitmap image from the stream
if ((hr = cImage.Load(iStream)) == S_OK)
{

int pitch = cImage.GetPitch();
int height = cImage.GetHeight();
BYTE* pBits = (BYTE*)cImage.GetBits();
if (pitch < 0)
pBits += (pitch *(height -1));
memcpy(liveImage->imageData, pBits, abs(pitch) * height);

}

cImage.~CImage();

GlobalFree(hMem);

cvFlip(liveImage, NULL, 0);

// Release stream
if(stream != NULL)
{
err = EdsRelease(stream);
if(err != EDS_ERR_OK)
{
printf("Error in EdsRelease: 0x%X\n", err);

}
stream = NULL;
}

if(evfImage != NULL)
{
err = EdsRelease(evfImage);
if(err != EDS_ERR_OK)
{
printf("Error in EdsRelease: 0x%X\n", err);

}
evfImage = NULL;
}

EdsRelease(image);

cvShowImage(windowName.c_str(), liveImage);

cvReleaseImage(&liveImage);

}

int CanonCamera::getNumOfCams()
{
return detectedCams;
}

void CanonCamera::captureImg()
{
EdsError    err = EDS_ERR_OK;
do 
{
err = EdsSendCommand(camera, kEdsCameraCommand_TakePicture, 0);
if (err != EDS_ERR_OK)  
Sleep(500);
} 
while (err != EDS_ERR_OK);
}

void TakePhoto()
{
EdsError err = EDS_ERR_OK;
EdsCameraRef camera = NULL;
EdsCameraListRef cameraList = NULL;
EdsUInt32 count = 0;

err = EdsInitializeSDK();
err = EdsGetCameraList(&cameraList);
err = EdsGetChildCount(cameraList, &count);
if (count > 0)
{
err = EdsGetChildAtIndex(cameraList, 0, &camera);
cameraList = NULL;
err = EdsSetObjectEventHandler(camera, kEdsObjectEvent_All, handleObjectEvent, NULL);
err = EdsOpenSession(camera);
err = EdsSendCommand(camera, kEdsCameraCommand_TakePicture, 0);
}
}

static EdsError EDSCALLBACK handleObjectEvent(EdsObjectEvent event, EdsBaseRef object, EdsVoid * context)
{
if (event == kEdsObjectEvent_DirItemRequestTransfer)
{
EdsError err = EDS_ERR_OK;
EdsStreamRef stream = NULL;
EdsDirectoryItemInfo dirItemInfo;
err = EdsGetDirectoryItemInfo(object, &dirItemInfo);
err = EdsCreateFileStream(dirItemInfo.szFileName, kEdsFileCreateDisposition_CreateAlways, kEdsAccess_ReadWrite, &stream);
err = EdsDownload(object, dirItemInfo.size, stream);
err = EdsDownloadComplete(object);
EdsRelease(stream);
stream = NULL;
}
if (object) EdsRelease(object);
}

Dani AI

Generated

Short diagnosis and next steps for :

Your Canon contact is correct — the EdsCreateFileStream call creates/opens a file on the host PC, so the first parameter must point to a host-side filename (full or relative path). The EDSDK description for EdsCreateFileStream shows it creates a file on the host and takes an inFileName string, and community reports repeatedly recommend prepending a fully-qualified path (dirItemInfo.szFileName is only the camera-side file name). (gist.github.com)

Fix to apply (minimal, safe change):

  • Build a full path on the PC (a folder you control), then pass that string to EdsCreateFileStream instead of passing only dirItemInfo.szFileName.
  • Make sure the target directory exists and the process has write permission before calling EdsCreateFileStream.
  • On Windows, use EdsCreateFileStreamEx if you need Unicode (wide-char) paths.

Example pattern (replace "C:\MyPhotos" with your folder):

char dest[MAX_PATH];
_snprintf(dest, sizeof(dest), "%s\\%s", "C:\\MyPhotos", dirItemInfo.szFileName);
err = EdsCreateFileStream(dest, kEdsFileCreateDisposition_CreateAlways, kEdsAccess_ReadWrite, &stream);

If you need Unicode paths use the SDK "Ex" API. (gist.github.com)

Troubleshooting checklist

  • Check and log the return value from EdsCreateFileStream and EdsDownload; an EDS_ERR_INTERNAL or other non-zero return typically means the stream/path was invalid or not writable (there are real cases showing EdsDownload failing when the stream was bad). (stackoverflow.com)
  • If using a console app, don’t close the session immediately after EdsSendCommand — events may not fire automatically. Pump events with EdsGetEvent (or wait/synchronize) until the DirItem transfer event completes. Several reports show missing events when the app exits too fast. (stackoverflow.com)

Checklist summary: prepend an absolute host path, verify directory & permissions, check SDK return codes, and ensure your app runs the event loop long enough for the transfer callback to execute.

note canon respond to my problem with

Thank you for your inquiry.

According to your code, you specify wrong parameter for EdsCreateFileStream.
Please specify the destination path on your computer as the first argument of EdsCreateFileStream.

Kind regards,
CDP Admin team

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.