C#调用海康威视人脸识别接口

C#调用海康威视人脸识别接口_第1张图片

先登录,然后预览。之后打开人脸抓拍即可。
以下为人脸识别的核心代码。
**

 public Preview()
        {
            //
            // Windows 窗体设计器支持所必需的
            //
            InitializeComponent();
            m_bInitSDK = CHCNetSDK.NET_DVR_Init();
            if (m_bInitSDK == false)
            {
                MessageBox.Show("NET_DVR_Init error!");
                return;
            }
            else
            {
                //保存SDK日志 To save the SDK log
                CHCNetSDK.NET_DVR_SetLogToFile(3, "E:\\SdkLog\\", true);
            }
            //
            // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
            //
            //设置透传报警信息类型
            CHCNetSDK.NET_DVR_LOCAL_GENERAL_CFG struLocalCfg = new CHCNetSDK.NET_DVR_LOCAL_GENERAL_CFG();
            struLocalCfg.byAlarmJsonPictureSeparate = 1;//控制JSON透传报警数据和图片是否分离,0-不分离(COMM_VCA_ALARM返回),1-分离(分离后走COMM_ISAPI_ALARM回调返回)

            Int32 nSize = Marshal.SizeOf(struLocalCfg);
            IntPtr ptrLocalCfg = Marshal.AllocHGlobal(nSize);
            Marshal.StructureToPtr(struLocalCfg, ptrLocalCfg, false);

            if (!CHCNetSDK.NET_DVR_SetSDKLocalCfg(17, ptrLocalCfg))  //NET_DVR_LOCAL_CFG_TYPE_GENERAL
            {
                iLastErr = CHCNetSDK.NET_DVR_GetLastError();
                strErr = "NET_DVR_SetSDKLocalCfg failed, error code= " + iLastErr;
                MessageBox.Show(strErr);
            }
            Marshal.FreeHGlobal(ptrLocalCfg);

            for (int i = 0; i < 200; i++)
            {
                m_lAlarmHandle[i] = -1;
            }

            //设置报警回调函数
            if (m_falarmData_V31 == null)
            {
                m_falarmData_V31 = new CHCNetSDK.MSGCallBack_V31(MsgCallback_V31);
            }
            CHCNetSDK.NET_DVR_SetDVRMessageCallBack_V31(m_falarmData_V31, IntPtr.Zero);

        }

        public bool MsgCallback_V31(int lCommand, ref CHCNetSDK.NET_DVR_ALARMER pAlarmer, IntPtr pAlarmInfo, uint dwBufLen, IntPtr pUser)
        {
            ProcessCommAlarm_FaceSnap(ref pAlarmer, pAlarmInfo, dwBufLen, pUser);
            return true; //回调函数需要有返回,表示正常接收到数据
        }

        private void ProcessCommAlarm_FaceSnap(ref CHCNetSDK.NET_DVR_ALARMER pAlarmer, IntPtr pAlarmInfo, uint dwBufLen, IntPtr pUser)
        {
            CHCNetSDK.NET_VCA_FACESNAP_RESULT struFaceSnapInfo = new CHCNetSDK.NET_VCA_FACESNAP_RESULT();
            uint dwSize = (uint)Marshal.SizeOf(struFaceSnapInfo);
            struFaceSnapInfo = (CHCNetSDK.NET_VCA_FACESNAP_RESULT)Marshal.PtrToStructure(pAlarmInfo, typeof(CHCNetSDK.NET_VCA_FACESNAP_RESULT));
            byte eg = struFaceSnapInfo.struFeature.byEyeGlass;
            float x = struFaceSnapInfo.struRegion.fX;
            //报警设备IP地址
            //string strIP = System.Text.Encoding.UTF8.GetString(pAlarmer.sDeviceIP).TrimEnd('\0');
            string strIP = "192.168.1.64";
            
            if ((struFaceSnapInfo.dwBackgroundPicLen != 0) && (struFaceSnapInfo.pBuffer2 != IntPtr.Zero))
            {
                DateTime now = DateTime.Now;
                //保存抓拍图片数据
                string str = ".\\picture\\FaceSnap_" + now.ToString("yyyyMMddHHmmss") + ".jpg";
                string _FilePath = String.Format(".\\picture\\{0}", now.ToString("yyyy-MM-dd"));
                if (!Directory.Exists(_FilePath))
                {
                    Directory.CreateDirectory(_FilePath);
                }
                str = String.Format(".\\picture\\{0}\\FaceSnap_{1}.jpg", now.ToString("yyyy-MM-dd"), now.ToString("HH_mm_ss"));
                FileStream fs = new FileStream(str, FileMode.CreateNew);
                int iLen = (int)struFaceSnapInfo.dwBackgroundPicLen;
                byte[] by = new byte[iLen];
                Marshal.Copy(struFaceSnapInfo.pBuffer2, by, 0, iLen);
                fs.Write(by, 0, iLen);
                fs.Close();
               

                //保存一帧图片
                string sJpegPicFileName;
                //图片保存路径和文件名 the path and file name to save
                sJpegPicFileName = String.Format(".\\picture\\{0}\\JPEG_test_{1}.jpg", now.ToString("yyyy-MM-dd"), now.ToString("HH_mm_ss")); 

                int lChannel = Int16.Parse(textBoxChannel.Text); //通道号 Channel number

                CHCNetSDK.NET_DVR_JPEGPARA lpJpegPara = new CHCNetSDK.NET_DVR_JPEGPARA();
                lpJpegPara.wPicQuality = 0; //图像质量 Image quality
                lpJpegPara.wPicSize = 0xff; //抓图分辨率 Picture size: 2- 4CIF,0xff- Auto(使用当前码流分辨率),抓图分辨率需要设备支持,更多取值请参考SDK文档

                //JPEG抓图 Capture a JPEG picture
                if (!CHCNetSDK.NET_DVR_CaptureJPEGPicture(m_lUserID, lChannel, ref lpJpegPara, sJpegPicFileName))
                {
                    iLastErr = CHCNetSDK.NET_DVR_GetLastError();
                    str = "NET_DVR_CaptureJPEGPicture failed, error code= " + iLastErr;
                    MessageBox.Show(str);
                    return;
                }
                else
                {
                    str = "Successful to capture the JPEG file and the saved file is " + sJpegPicFileName;
                    //MessageBox.Show(str);
                }

                iFileNumber++;
            }

        }

你可能感兴趣的:(C#调用海康威视人脸识别接口)