Hi everybody,
We have started integrating the Canon SDK (EDSDK2.7 API) into our Java Swing application.
We're using JNI to interface with the Canon DLL, so it's mostly a C.C++ job.
Unfortunately we've been stuck for the past week.
Let me explain what we have done so far:
1. We have created the JNI infrastructure in our Java Swing app
2. We have created a wrapper for the DLL with VC++. We are calling this wrapper from JNI.
3. Inside the wrapper, we have used standard C.C++ code to interface between JNI and the Canon DLL.
Java Swing App --> JNI --> VC++ Wrapper with C.C++ code mappings for DLL --> Canon DLL
When testing, we can see that the following steps work OK:
- Get camera list --> EdsGetCameraList()
- Get no. of cameras (child objects in camera list) --> EdsGetChildCount()
- Get camera objects --> EdsGetChildAtIndex(cameraList, i, aCameraRef)
The next step however fails:
Open session with camera --> EdsOpenSession(aCameraRef)
Inside our wrapper, we're using standard C.C++ code that interfaces between JNI and the Canon DLL. This is where it gets tricky as the examples that are provided in the SDK are based on VC++. As we compare the code example provided in the API documentation and and the VC++ sample application, there's definitely some differences.
Unfortunately, we're not experienced enough with C.C++ and VC++ to know exactly how to fill in the gaps.
Please find below an example of our C++ code that interfaces with the Canon DLL:
#include "EDSDK.h"
#include "EDSDKTypes.h"
#include "EDSDKC.h"
EdsError downloadImage(EdsDirectoryItemRef directoryItem){
EdsError err = EDS_ERR_OK;
EdsStreamRef stream=NULL;
EdsDirectoryItemInfo dirItemInfor;
err = EdsGetDirectoryItemInfo(directoryItem,&dirItemInfor );
if(err = EDS_ERR_OK){
err = EdsCreateFileStream(dirItemInfor.szFileName, kEdsFileCreateDisposition_CreateAlways, kEdsAccess_ReadWrite, &stream);
}
if(err = EDS_ERR_OK){
err = EdsDownload(directoryItem,dirItemInfor.size,stream );
}
if(err = EDS_ERR_OK){
err = EdsDownloadComplete(directoryItem);
}
if(err = EDS_ERR_OK){
EdsRelease(stream);
stream = NULL;
}
return err;
}
EdsError EDSCALLBACK handleObjectEvent(EdsObjectEvent event, EdsBaseRef object, EdsVoid* context)
{
switch(event){
case kEdsObjectEvent_DirItemRequestTransfer:
downloadImage(object);
break;
default: break;
}
if(object){
EdsRelease(object);
}
return EDS_ERR_OK;
}
EdsError getFirstCamera(EdsCameraRef *camera){
EdsError err = EDS_ERR_OK;
EdsCameraListRef cameraList = NULL;
EdsUInt32 count =0;
err = EdsGetCameraList(&cameraList);
if(err = EDS_ERR_OK){
err = EdsGetChildCount(cameraList, &count);
if(count ==0){
err = EDS_ERR_DEVICE_NOT_FOUND;
}
}
if(err = EDS_ERR_OK){
err = EdsGetChildAtIndex(cameraList,0,camera);
}
if(cameraList!=NULL){
EdsRelease(cameraList);
cameraList=NULL;
}
return err;
}
EdsError applicationRun() {
EdsError err = EDS_ERR_OK; EdsCameraRef camera = NULL; bool isSDKLoaded = false;
// Initialize SDK
err = EdsInitializeSDK();
if(err == EDS_ERR_OK) {
isSDKLoaded = true;
}
// Get first camera
if(err == EDS_ERR_OK) {
err = getFirstCamera (&camera);
}
// Set event handler
if(err == EDS_ERR_OK) {
err = EdsSetObjectEventHandler(camera, kEdsObjectEvent_All, handleObjectEvent, NULL);
}
// Set event handler
if(err == EDS_ERR_OK) {
//err = EdsSetPropertyEventHandler(camera, kEdsPropertyEvent_All, handlePropertyEvent, NULL);
}
// Set event handler
if(err == EDS_ERR_OK) {
//err = EdsSetPropertyEventHandler(camera, kEdsStateEvent_All, handleSateEvent, NULL);
}
if(err == EDS_ERR_OK) {
err = EdsOpenSession(camera);
}
///// // do something ////
// Close session with camera
if(err == EDS_ERR_OK) {
err = EdsCloseSession(camera);
}
// Release camera
if(camera != NULL) {
EdsRelease(camera);
}
// Terminate SDK
if(isSDKLoaded) {
EdsTerminateSDK();
}
return err;
}
JNIEXPORT jboolean JNICALL Java_EDSDKC_EdsInitializeSDK
(JNIEnv *, jobject){
EdsError err = applicationRun();
if(err == EDS_ERR_OK)return true;
else return false;
}
Hopefully someone has gone through the same learning curve/pains as us and can give us some pointers or help. Morale on this project is pretty low now as we've been deadlocked for so long. We're really hoping someone can give us a hand here. It would be much appreciated!