EDSDK,C#:尝试获取摄像机上的文件列表

EDSDK,C#:尝试获取摄像机上的文件列表,c#,edsdk,C#,Edsdk,我正在用C#编写一个应用程序,它使用EDSDK将数码相机与PC连接起来;触发软件中的事件;然后软件将新的图像复制到电脑上。这很好 现在,我希望该软件能够优雅地处理没有电脑或相机与电脑失去连接的情况。因此,每当用户使用该软件启动新会话时,它首先检查相机上是否有图像,如果有,则在本地复制。为了做到这一点,我需要一种方法来获取指向每个目录项的指针。到目前为止,我还没有在文档或网上找到任何关于如何做到这一点的信息 EDSDK有没有办法从摄像机中获取现有文件的列表?我编写了一个函数来实现这一点,纯粹是为了

我正在用C#编写一个应用程序,它使用EDSDK将数码相机与PC连接起来;触发软件中的事件;然后软件将新的图像复制到电脑上。这很好

现在,我希望该软件能够优雅地处理没有电脑或相机与电脑失去连接的情况。因此,每当用户使用该软件启动新会话时,它首先检查相机上是否有图像,如果有,则在本地复制。为了做到这一点,我需要一种方法来获取指向每个目录项的指针。到目前为止,我还没有在文档或网上找到任何关于如何做到这一点的信息


EDSDK有没有办法从摄像机中获取现有文件的列表?

我编写了一个函数来实现这一点,纯粹是为了教我自己使用SDK。因此,该代码不用于生产中的任何内容,因此我为您提供了相关函数:

       /// <summary>
    /// Gets a list of files on the CF Card. If dest is given, downloads them
    /// </summary>
    /// <param name="dest">string.empty if you just want a list of file names, otherwise the folder in which you want the files</param>
    /// <returns>a list of file names</returns>
    public  List<string> GetPicturesOnCard(string dest)
    {
        IntPtr volumeRef = IntPtr.Zero;
        IntPtr dirRef = IntPtr.Zero;
        IntPtr subDirRef = IntPtr.Zero;
        List<string> result = new List<string>();
        try
        {
            openSession();
            int nFiles;
            if (EDSDK.EdsGetChildAtIndex(camera, 0, out volumeRef) != EDSDK.EDS_ERR_OK)
                throw new CannonException("Couldn't access Memory Card");
            //first child is DCIM
            if (EDSDK.EdsGetChildAtIndex(volumeRef, 0, out dirRef) != EDSDK.EDS_ERR_OK)
                throw new CannonException("Memory Card formatting Error");
           //first child of DCIM has the actual photos
            if(EDSDK.EdsGetChildAtIndex(dirRef, 0, out subDirRef)!= EDSDK.EDS_ERR_OK)
                throw new CannonException("Memory Card formatting Error");
            EDSDK.EdsGetChildCount(subDirRef, out nFiles);  
            for (int i = 0; i < nFiles; i++)
            {      

                IntPtr fileRef = IntPtr.Zero;
                EDSDK.EdsDirectoryItemInfo fileInfo;
                try
                {
                    EDSDK.EdsGetChildAtIndex(subDirRef, i, out fileRef);
                    EDSDK.EdsGetDirectoryItemInfo(fileRef, out fileInfo);
                    if (dest != string.Empty)
                    {
                        IntPtr fStream = IntPtr.Zero;//it's a cannon sdk file stream, not a managed stream
                        uint fSize = fileInfo.Size;
                        try
                        {
                            EDSDK.EdsCreateMemoryStream(fSize, out fStream);
                            if (EDSDK.EdsDownload(fileRef, fSize, fStream) != EDSDK.EDS_ERR_OK)
                                throw new CannonException("Image Download failed");
                            if (EDSDK.EdsDownloadComplete(fileRef) != EDSDK.EDS_ERR_OK)
                                throw new CannonException("Image Download failed to complete");
                            byte[] buffer = new byte[fSize];
                            IntPtr imgLocation;
                            if (EDSDK.EdsGetPointer(fStream, out imgLocation) == EDSDK.EDS_ERR_OK)
                            {
                                Marshal.Copy(imgLocation, buffer, 0, (int)fSize - 1);
                                File.WriteAllBytes(dest + fileInfo.szFileName, buffer);
                            }
                            else
                                throw new CannonException("Interal Error #1");//because the expection text coud land up in a message box some where
                        }
                        finally
                        {
                            EDSDK.EdsRelease(fStream);
                        }
                    }
                }
                finally
                {
                    EDSDK.EdsRelease(fileRef);
                }
                result.Add(fileInfo.szFileName);
            }

        }
        finally
        {
            EDSDK.EdsRelease(subDirRef);
            EDSDK.EdsRelease(dirRef);
            EDSDK.EdsRelease(volumeRef);
            closeSession();
        }

        return result;
    }
//
///获取CF卡上的文件列表。如果给定dest,则下载它们
/// 
///如果只需要文件名列表,则为string.empty,否则为所需文件所在的文件夹
///文件名列表
公共列表GetPicturesOnCard(字符串目标)
{
IntPtr volumeRef=IntPtr.Zero;
IntPtr dirRef=IntPtr.Zero;
IntPtr SUBRREF=IntPtr.0;
列表结果=新列表();
尝试
{
openSession();
内部文件;
如果(EDSDK.edsgetchildataindex(摄像头,0,输出卷ref)!=EDSDK.EDS\u ERR\u OK)
抛出新的CannonException(“无法访问存储卡”);
//第一个孩子是DCIM
if(EDSDK.edsgetchildataindex(volumeRef,0,out dirRef)!=EDSDK.EDS\u ERR\u OK)
抛出新的CannonException(“存储卡格式化错误”);
//DCIM的第一个孩子拥有真实的照片
if(EDSDK.edsgetchildataindex(dirRef,0,out subdrref)!=EDSDK.EDS\u ERR\u OK)
抛出新的CannonException(“存储卡格式化错误”);
EDSDK.EdsGetChildCount(子文件夹、输出文件);
对于(int i=0;i
实际上,您要做的是:初始化SDK,然后初始化摄像头,然后打开会话。 完成后,您将获得对卡的引用,然后是文件夹(您想要的文件夹是第一个子项的第一个子项),然后遍历其中的所有子项-它们就是文件


SDK文档阅读起来并不有趣,但会告诉您我遗漏的部分(如何初始化相机等等,我假设您从项目的其余部分就知道如何进行初始化)。

我编写了一个函数来实现这一点,纯粹是为了教我自己使用SDK。因此,该代码不用于生产中的任何内容,因此我为您提供了相关函数:

       /// <summary>
    /// Gets a list of files on the CF Card. If dest is given, downloads them
    /// </summary>
    /// <param name="dest">string.empty if you just want a list of file names, otherwise the folder in which you want the files</param>
    /// <returns>a list of file names</returns>
    public  List<string> GetPicturesOnCard(string dest)
    {
        IntPtr volumeRef = IntPtr.Zero;
        IntPtr dirRef = IntPtr.Zero;
        IntPtr subDirRef = IntPtr.Zero;
        List<string> result = new List<string>();
        try
        {
            openSession();
            int nFiles;
            if (EDSDK.EdsGetChildAtIndex(camera, 0, out volumeRef) != EDSDK.EDS_ERR_OK)
                throw new CannonException("Couldn't access Memory Card");
            //first child is DCIM
            if (EDSDK.EdsGetChildAtIndex(volumeRef, 0, out dirRef) != EDSDK.EDS_ERR_OK)
                throw new CannonException("Memory Card formatting Error");
           //first child of DCIM has the actual photos
            if(EDSDK.EdsGetChildAtIndex(dirRef, 0, out subDirRef)!= EDSDK.EDS_ERR_OK)
                throw new CannonException("Memory Card formatting Error");
            EDSDK.EdsGetChildCount(subDirRef, out nFiles);  
            for (int i = 0; i < nFiles; i++)
            {      

                IntPtr fileRef = IntPtr.Zero;
                EDSDK.EdsDirectoryItemInfo fileInfo;
                try
                {
                    EDSDK.EdsGetChildAtIndex(subDirRef, i, out fileRef);
                    EDSDK.EdsGetDirectoryItemInfo(fileRef, out fileInfo);
                    if (dest != string.Empty)
                    {
                        IntPtr fStream = IntPtr.Zero;//it's a cannon sdk file stream, not a managed stream
                        uint fSize = fileInfo.Size;
                        try
                        {
                            EDSDK.EdsCreateMemoryStream(fSize, out fStream);
                            if (EDSDK.EdsDownload(fileRef, fSize, fStream) != EDSDK.EDS_ERR_OK)
                                throw new CannonException("Image Download failed");
                            if (EDSDK.EdsDownloadComplete(fileRef) != EDSDK.EDS_ERR_OK)
                                throw new CannonException("Image Download failed to complete");
                            byte[] buffer = new byte[fSize];
                            IntPtr imgLocation;
                            if (EDSDK.EdsGetPointer(fStream, out imgLocation) == EDSDK.EDS_ERR_OK)
                            {
                                Marshal.Copy(imgLocation, buffer, 0, (int)fSize - 1);
                                File.WriteAllBytes(dest + fileInfo.szFileName, buffer);
                            }
                            else
                                throw new CannonException("Interal Error #1");//because the expection text coud land up in a message box some where
                        }
                        finally
                        {
                            EDSDK.EdsRelease(fStream);
                        }
                    }
                }
                finally
                {
                    EDSDK.EdsRelease(fileRef);
                }
                result.Add(fileInfo.szFileName);
            }

        }
        finally
        {
            EDSDK.EdsRelease(subDirRef);
            EDSDK.EdsRelease(dirRef);
            EDSDK.EdsRelease(volumeRef);
            closeSession();
        }

        return result;
    }
//
///获取CF卡上的文件列表。如果给定dest,则下载它们
/// 
///如果只需要文件名列表,则为string.empty,否则为所需文件所在的文件夹
///文件名列表
公共列表GetPicturesOnCard(字符串目标)
{
IntPtr volumeRef=IntPtr.Zero;
IntPtr dirRef=IntPtr.Zero;
IntPtr SUBRREF=IntPtr.0;
列表结果=新列表();
尝试
{
openSession();
内部文件;
如果(EDSDK.edsgetchildataindex(摄像头,0,输出卷ref)!=EDSDK.EDS\u ERR\u OK)
抛出新的CannonException(“无法访问存储卡”);
//第一个孩子是DCIM
if(EDSDK.edsgetchildataindex(volumeRef,0,out dirRef)!=EDSDK.EDS\u ERR\u OK)
扔新的CannonExc