使用 EDSDK 2.9 从佳能相机检索图片

发布于 2024-10-05 02:29:09 字数 3813 浏览 7 评论 0原文

我很长时间以来一直在尝试检索相机拍摄的图像。我可以使用 SDK 拍照:

err = EDSDK.EdsSendCommand(cameraDev, EDSDK.CameraCommand_TakePicture, 0);

我在打开有效的相机会话后执行此操作。 添加了一个

我还为事件

    public static uint stateEventHandler(uint inEvent, uint inParameter, IntPtr inContext)
    {
        switch (inEvent)
        {
            case EDSDK.StateEvent_JobStatusChanged:
                Debug.WriteLine(String.Format("There are objects waiting to be transferred.  Job status {0}", inParameter));
                break;

            case EDSDK.StateEvent_ShutDownTimerUpdate:
                if (inParameter != 0)
                    Debug.WriteLine(String.Format("shutdown timer update: {0}", inParameter));
                break;

            default:
                Debug.WriteLine(String.Format("StateEventHandler: event {0}, parameter {1}", inEvent, inParameter));
                break;
        }
        return 0;
    }

    public static uint objectEventHandler(uint inEvent, IntPtr inRef, IntPtr inContext)
    {
        switch (inEvent)
        {
            case EDSDK.ObjectEvent_VolumeInfoChanged:
                Debug.WriteLine("volume info changed");

                #region retrieve volume info

                EDSDK.EdsVolumeInfo volumeInfo;
                err = EDSDK.EdsGetVolumeInfo(inRef, out volumeInfo);
                if (err == EDSDK.EDS_ERR_OK)
                {
                    switch (volumeInfo.StorageType)
                    {
                        case (uint)EDSDK.EdsStorageType.Non:
                            Debug.WriteLine("No card inserted");
                            break;
                        default:
                        case (uint)EDSDK.EdsStorageType.CF:
                        case (uint)EDSDK.EdsStorageType.SD:
                            if (volumeInfo.Access == (uint)EDSDK.EdsAccess.ReadWrite)
                            {
                                Debug.WriteLine(String.Format("Label: {0}, Max Capacity: {1}, Free Space: {2}",
                                    volumeInfo.szVolumeLabel, volumeInfo.MaxCapacity, volumeInfo.FreeSpaceInBytes));

                                /*
                                err = EDSDK.EdsGetChildAtIndex(volumeInfo, 0, directoryList);
                                if (err != EDSDK.EDS_ERR_OK)
                                    throw new Exception(String.Format("EdsGetChildAtIndex: " + err.ToString()));
                                */
                            }
                            else
                                Debug.WriteLine(String.Format("Volume access rights: {0}", volumeInfo.Access));
                            break;
                    }
                }

                #endregion retrieve volume info

                break;

            case EDSDK.ObjectEvent_DirItemCreated:
                downloadImage(inContext);
                Debug.WriteLine("dir item created");
                break;

            default:
                Debug.WriteLine(String.Format("ObjectEventHandler: event {0}", inEvent));
                break;
        }
        return 0;
    }

objectEventHandler 和一个 stateEventHandler ,如下所示:我尝试了多种方法(以检索此图像),但它们似乎都不起作用。 一些方法是:

http://tech.groups.yahoo.com/group /CanonSDK/message/1575

[http://stackoverflow.com/questions/3762530/take-picture-and-directly-save-image-to-pc-using-edsdk-2-8]

[http: //canonsdk.com/phpBB3/viewtopic.php?f=7&t=94&sid=7fcbe7ad6eadb399dbcb4b61a7333112]

问题是它们都只是代码的一部分,当我尝试将其放入我的代码中时,它永远不会起作用 可能是因为我不是内存流、指针方面的专家。 我遇到的大多数错误是在读取流并将它们复制到本地缓冲区时出现的,它表示流为空。

是否有人有完整的示例代码用于拍照并将其下载到磁盘(或内存),或者我需要完成此操作的正确方法?

谢谢 费尔南多

I been trying to retrieve images taken on the camera for a very long time now. I am able to take pictures with the SDK using:

err = EDSDK.EdsSendCommand(cameraDev, EDSDK.CameraCommand_TakePicture, 0);

I do this after opening a valid camera session. I have also added a

objectEventHandler and a stateEventHandler for the events that look like:

    public static uint stateEventHandler(uint inEvent, uint inParameter, IntPtr inContext)
    {
        switch (inEvent)
        {
            case EDSDK.StateEvent_JobStatusChanged:
                Debug.WriteLine(String.Format("There are objects waiting to be transferred.  Job status {0}", inParameter));
                break;

            case EDSDK.StateEvent_ShutDownTimerUpdate:
                if (inParameter != 0)
                    Debug.WriteLine(String.Format("shutdown timer update: {0}", inParameter));
                break;

            default:
                Debug.WriteLine(String.Format("StateEventHandler: event {0}, parameter {1}", inEvent, inParameter));
                break;
        }
        return 0;
    }

    public static uint objectEventHandler(uint inEvent, IntPtr inRef, IntPtr inContext)
    {
        switch (inEvent)
        {
            case EDSDK.ObjectEvent_VolumeInfoChanged:
                Debug.WriteLine("volume info changed");

                #region retrieve volume info

                EDSDK.EdsVolumeInfo volumeInfo;
                err = EDSDK.EdsGetVolumeInfo(inRef, out volumeInfo);
                if (err == EDSDK.EDS_ERR_OK)
                {
                    switch (volumeInfo.StorageType)
                    {
                        case (uint)EDSDK.EdsStorageType.Non:
                            Debug.WriteLine("No card inserted");
                            break;
                        default:
                        case (uint)EDSDK.EdsStorageType.CF:
                        case (uint)EDSDK.EdsStorageType.SD:
                            if (volumeInfo.Access == (uint)EDSDK.EdsAccess.ReadWrite)
                            {
                                Debug.WriteLine(String.Format("Label: {0}, Max Capacity: {1}, Free Space: {2}",
                                    volumeInfo.szVolumeLabel, volumeInfo.MaxCapacity, volumeInfo.FreeSpaceInBytes));

                                /*
                                err = EDSDK.EdsGetChildAtIndex(volumeInfo, 0, directoryList);
                                if (err != EDSDK.EDS_ERR_OK)
                                    throw new Exception(String.Format("EdsGetChildAtIndex: " + err.ToString()));
                                */
                            }
                            else
                                Debug.WriteLine(String.Format("Volume access rights: {0}", volumeInfo.Access));
                            break;
                    }
                }

                #endregion retrieve volume info

                break;

            case EDSDK.ObjectEvent_DirItemCreated:
                downloadImage(inContext);
                Debug.WriteLine("dir item created");
                break;

            default:
                Debug.WriteLine(String.Format("ObjectEventHandler: event {0}", inEvent));
                break;
        }
        return 0;
    }

I have tried multiple approaches (to retrieve this image) none of them seem to work.
Some of the approaches are:

http://tech.groups.yahoo.com/group/CanonSDK/message/1575

[http://stackoverflow.com/questions/3762530/take-picture-and-directly-save-image-to-pc-using-edsdk-2-8]

[http://canonsdk.com/phpBB3/viewtopic.php?f=7&t=94&sid=7fcbe7ad6eadb399dbcb4b61a7333112]

The thing is all of them are just part of the code, when I try to put it in mine, it never works
properly.Probably because I am no expert on memoryStreams, pointers an so.
Most of the errors I get are when reading the streams and copying them to a local buffer, it says the stream is empty.

Does anybody have full sample code for taking a picture and downloading it to disk (or to memory), or the rigth approach that I need in order to complete this?

Thanks
Fernando

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

双手揣兜 2024-10-12 02:29:09

您的代码中的错误是:

downloadImage(inContext);

它应该是

 downloadImage(inRef);

the error in your code is:

downloadImage(inContext);

it should be

 downloadImage(inRef);
伏妖词 2024-10-12 02:29:09

将捕获的图像作为文件下载到硬盘中,您可以查看

void DownloadImage(IntPtr DirectoryRef)
{
    IntPtr stream = IntPtr.Zero;

    try
    {
        Error = EDSDK.EdsGetDirectoryItemInfo(DirectoryRef, out dirItemInfo);

        if (Error == OK)
        {
            Error = EDSDK.EdsCreateFileStream(dirItemInfo.szFileName,
            EDSDK.EdsFileCreateDisposition.CreateAlways, EDSDK.EdsAccess.ReadWrite, out stream);
        }

        if (Error == OK)
        {
            Error = EDSDK.EdsDownload(DirectoryRef, dirItemInfo.Size, stream);
        }

        if (Error == OK)
        {
            Error = EDSDK.EdsDownloadComplete(DirectoryRef);
        }
        else
        {
            Error = EDSDK.EdsDownloadCancel(DirectoryRef);
        }

        Error = EDSDK.EdsGetPointer(stream, out data);
        if (Error != OK)
                throw new Exception("Invalid Pointer Handler. Error Code:" + err.getErrorName(Error) + "(" + Error + ")" );

        Error = EDSDK.EdsGetLength(stream, out size);

        Bitmap retImage = null;
   }
   catch (Exception ex)
   {

   }

   EDSDK.EdsRelease(stream);
   EDSDK.EdsRelease(data);            
}

downloading the capture image into a harddisk as a file , you can check it out

void DownloadImage(IntPtr DirectoryRef)
{
    IntPtr stream = IntPtr.Zero;

    try
    {
        Error = EDSDK.EdsGetDirectoryItemInfo(DirectoryRef, out dirItemInfo);

        if (Error == OK)
        {
            Error = EDSDK.EdsCreateFileStream(dirItemInfo.szFileName,
            EDSDK.EdsFileCreateDisposition.CreateAlways, EDSDK.EdsAccess.ReadWrite, out stream);
        }

        if (Error == OK)
        {
            Error = EDSDK.EdsDownload(DirectoryRef, dirItemInfo.Size, stream);
        }

        if (Error == OK)
        {
            Error = EDSDK.EdsDownloadComplete(DirectoryRef);
        }
        else
        {
            Error = EDSDK.EdsDownloadCancel(DirectoryRef);
        }

        Error = EDSDK.EdsGetPointer(stream, out data);
        if (Error != OK)
                throw new Exception("Invalid Pointer Handler. Error Code:" + err.getErrorName(Error) + "(" + Error + ")" );

        Error = EDSDK.EdsGetLength(stream, out size);

        Bitmap retImage = null;
   }
   catch (Exception ex)
   {

   }

   EDSDK.EdsRelease(stream);
   EDSDK.EdsRelease(data);            
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文