Ejemplo n.º 1
0
        /// <summary>
        /// 从设备内部下载图片
        /// </summary>
        /// <param name="StartTime">DateTime类开始扫描时间</param>
        /// <param name="EndTime">结束扫描时间</param>
        unsafe public void SearchAndDown(DateTime StartTime, DateTime EndTime)
        {
            try
            {
                InitSDK();
                if (m_InitSDK)
                {
                    LoginSDK();
                    if (m_UserID == 0)
                    {
                        Findhandler = getFindHandler(StartTime, EndTime);
                        if (Findhandler < 0)
                        {
                            iLastErr = CHCNetSDK.NET_DVR_GetLastError();
                            Console.WriteLine("NET_DVR_FindPicture failed, error code = {0}", iLastErr);
                            return;
                        }

                        //用于存储图片信息
                        CHCNetSDK.NET_DVR_FIND_PICTURE_V50 struSavePictrue = new CHCNetSDK.NET_DVR_FIND_PICTURE_V50();
                        CHCNetSDK.NET_DVR_PIC_PARAM        temp            = new CHCNetSDK.NET_DVR_PIC_PARAM();

                        bool isFinding   = true;
                        int  picCounter  = 0;
                        int  downCounter = 0;
                        while (isFinding)
                        {
                            int lFindHandler2 = CHCNetSDK.NET_DVR_FindNextPicture_V50(Findhandler, ref struSavePictrue);
                            switch (lFindHandler2)
                            {
                            case CHCNetSDK.NET_DVR_FILE_SUCCESS:
                                if (struSavePictrue.sFileName.ToString().Contains("SNAP") && ((int)struSavePictrue.dwFileSize) > 0)       //重要
                                {
                                    picCounter++;
                                    Console.WriteLine("Picture name ===> {0}", struSavePictrue.sFileName.ToString());
                                    //初始化变量
                                    temp.pDVRFileName = struSavePictrue.sFileName;

                                    //申请内存
                                    temp.pSavedFileBuf = Marshal.AllocHGlobal((int)struSavePictrue.dwFileSize);
                                    temp.dwBufLen      = struSavePictrue.dwFileSize;
                                    temp.struAddr      = struSavePictrue.struAddr;
                                    //如果获取图片信息成功
                                    if (CHCNetSDK.NET_DVR_GetPicture_V50(m_UserID, ref temp))
                                    {
                                        CHCNetSDK.NET_DVR_GetPicture(m_UserID, temp.pDVRFileName,
                                                                     string.Format("{0}{1}{2}", m_savepath, temp.pDVRFileName, ".jpeg"));
                                        downCounter++;
                                    }
                                    else
                                    {
                                        iLastErr = CHCNetSDK.NET_DVR_GetLastError();
                                        Console.WriteLine("NET_DVR_GetPicture_V50 Pictrue name: [{0}] failed! erro code = {1}",
                                                          struSavePictrue.sFileName.ToString(), iLastErr);
                                    }

                                    //释放内存
                                    Marshal.FreeHGlobal(temp.pSavedFileBuf);
                                }
                                break;

                            case CHCNetSDK.NET_DVR_ISFINDING:
                                break;

                            case CHCNetSDK.NET_DVR_FILE_NOFIND:
                                Console.WriteLine("\n未找到文件!");
                                isFinding = false;
                                break;

                            case CHCNetSDK.NET_DVR_FILE_EXCEPTION:
                                iLastErr = CHCNetSDK.NET_DVR_GetLastError();
                                Console.WriteLine("\n查找文件时异常:error code: {0}", iLastErr);
                                isFinding = false;
                                break;

                            case CHCNetSDK.NET_DVR_NOMOREFILE:
                            default:
                                Console.WriteLine("\n文件查找结束。共找到 {0} 张图片,下载 {1} 张图片... {2}",
                                                  picCounter, downCounter, DateTime.Now.ToString());
                                isFinding = false;
                                break;
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                //停止查找
                if (Findhandler > 0)
                {
                    CHCNetSDK.NET_DVR_CloseFindPicture(Findhandler);
                }
                //注销用户
                if (m_UserID > 0)
                {
                    CHCNetSDK.NET_DVR_Logout(m_UserID);
                }
                //释放SDK资源
                if (m_InitSDK)
                {
                    CHCNetSDK.NET_DVR_Cleanup();
                }
            }
        }