用EDSDK拍照并立即检索

时间:2011-07-31 19:15:37

标签: c# image edsdk

有几天我试图从相机中取回拍摄的照片。今天我找到了

EdsSetPropertyData(cRef, PropID_SaveTo, 0, 4, 2);

不会将照片保存到相机。但我的问题是目前无法从相机中检索图像。永远不会调用我的ObjectEventHandler(有时在初始化时会调用以前的图片)。 这是我的调用者(在main函数中):

Camera camera = new Camera();
Thread thread = new Thread(new ThreadStart(camera.DoThings));
thread.Start();

这是我的CC类(相机控制)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EDSDKLib;

namespace CCCBSv2
{
class CC : EDSDK
{
    IntPtr cRef;

    // Initilizes, takes picture and uninitilizes 
    public void RunSeq()
    {
        uint err;
        err = Initialize();
        if (err != EDS_ERR_OK)
        {
            return;
        }
        err = TakePicture();
        if (err != EDS_ERR_OK)
        {
            return;
        }
        Uninitilize();
    }

    public uint Initialize()
    {
        // err init
        uint err;
        // Initilize SDK.
        // SDK initilizion is needed to use EDSDK functions
        err = EdsInitializeSDK();

        // In case of error pass it on...
        if (err != EDS_ERR_OK)
        {
            return err;
        }

        // get first camera connected to computer
        err = GetFirstCamera();

        if (err != EDS_ERR_OK)
        {
            return err;
        }

        // Open session to camera
        EdsOpenSession(cRef);

        // Set object event handler, We need this event handler to retrieve pictures from camera
        err = EdsSetObjectEventHandler(cRef, ObjectEvent_All, ObjectEventHandler, IntPtr.Zero);

        if (err != EDS_ERR_OK)
        {
            return err;
        }

        // We don't want to save pictures to the camera so lets set save target to host
        err = EdsSetPropertyData(cRef, PropID_SaveTo, 0, 4, 2);

        return err;
    }

    // uninitilizes edsdk
    public uint Uninitilize()
    {
        uint err;
        err = EdsCloseSession(cRef);
        if (err != EDS_ERR_OK)
        {
            return err;
        }
        err = EdsTerminateSDK();

        return err;
    }

    // gets the first camera to cRef
    private uint GetFirstCamera()
    {
        // Variable initilazions
        IntPtr cameraList;
        uint err;
        int count;

        // Get list of cameras;
        err = EdsGetCameraList(out cameraList);

        if (err != EDS_ERR_OK)
        {
            return err;
        }

        // Get count of cameras in list
        err = EdsGetChildCount(cameraList, out count);

        if (err != EDS_ERR_OK)
        {
            return err;
        }
        if (count == 0)
        {
            return EDS_ERR_DEVICE_NOT_FOUND;
        }

        // Get first camera to public cRef
        err = EdsGetChildAtIndex(cameraList, 0, out cRef);

        if (err != EDS_ERR_OK)
        {
            return err;
        }

        // release camera list
        if (cameraList != null)
        {
            EdsRelease(cameraList);
        }

        // everything went just fine
        return EDS_ERR_OK;
    }

    public uint ObjectEventHandler(uint inEvent, IntPtr inRef, IntPtr inContext){
        switch (inEvent)
        {
            // in case of a download request
            case ObjectEvent_DirItemRequestTransfer:
                DownloadImage(inRef);
                break;
            default:
                break;
        }
        return EDS_ERR_OK;
    }

    // Downloads the picture from camera's buffer to the computer
    private uint DownloadImage(IntPtr dItem)
    {

        EdsDirectoryItemInfo dirInfo;
        uint err;
        IntPtr stream = new IntPtr();
        err = EdsGetDirectoryItemInfo(dItem, out dirInfo);

        if (err == EDS_ERR_OK)
        {
            err = EdsCreateFileStream(dirInfo.szFileName, EdsFileCreateDisposition.CreateAlways, EdsAccess.ReadWrite, out stream);
        }
        if (err == EDS_ERR_OK)
        {
            err = EdsDownload(dItem, dirInfo.Size, stream);
        }
        if (err == EDS_ERR_OK)
        {
            err = EdsDownloadComplete(dItem);
        }
        if (stream != null)
        {
            EdsRelease(stream);
        }
        return err;
    }

    // Takes picture
    public uint TakePicture()
    {
        return EdsSendCommand(cRef, CameraCommand_TakePicture, 0);
    }
}
}

3 个答案:

答案 0 :(得分:3)

您需要一个Windows消息循环来获取和分派事件。将此添加到main()后,应调用对象事件处理程序:

while(GetMessage(&msg, NULL, NULL, NULL))
{        
    TranslateMessage(&msg); 
    DispatchMessage(&msg); 
}

另外,请记住将PropID_SaveTo设置为SaveTo_Host。您可以看到我的帖子here

答案 1 :(得分:2)

我正在开发更好的Canon EOS .NET支持。我称之为Canon.Eos.Framework。我刚刚添加了对taking pictures and downloading them any path on your compuuter的支持。这个框架仍处于Alpha模式,但随时可以测试。

答案 2 :(得分:0)

我的第一个猜测是,在回调有可能被触发之前,你过快地取消初始化驱动程序。

TakePicture只是告诉相机拍摄照片,你或多或少立即返回以取消初始化驱动程序。但实际拍摄照片涉及移动机械部件并可能需要半个月的时间,并且当相机宣布已经完成时,没有任何听众可以接收信息。