c#大华摄像头调用,抓取图片

目前需引用的文件还在审核中,这边提供有道云的链接提供下载:

文档:c#大华摄像头调用,抓取图片.note
链接:http://note.youdao.com/noteshare?id=1b539df41362d9605d59239c08c16e42&sub=8A046E28B3E0439C8DE0965271ADE159
 

 /// <summary>
        /// 抓取远程相机图片
        /// </summary>
        public void GETPhoto(string ip)//【摄像头ip】
        {
            #region 调用大华摄像头
           
            IntPtr _PlayID = IntPtr.Zero;
            fSnapRevCallBack _SnapRevCallBack;
            NET_DEVICEINFO_Ex _DeviceInfo = new NET_DEVICEINFO_Ex();
            #endregion 调用大华摄像头
            try
            {
                _SnapRevCallBack = new fSnapRevCallBack(SnapRevCallBack);
                NETClient.Init(null, IntPtr.Zero, null);
                NETClient.SetSnapRevCallBack(_SnapRevCallBack, IntPtr.Zero);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                Process.GetCurrentProcess().Kill();
            }
            #region 其中一台相机的端口号及密码,目前先放着,后期处理

            ushort port = 37777;//端口号
            string username1 = "username";//摄像头用户名,自己输入
            string password1 = "passworf";//摄像头密码,自己输入
            #endregion
            try
            {
                _LoginID = NETClient.Login(ip, port, username1, password1, EM_LOGIN_SPAC_CAP_TYPE.TCP, IntPtr.Zero, ref _DeviceInfo);
                MessageBox.Show("远程相机连接成功,正在抓取照片");
                log.Info("\r\n" + DateTime.Now.ToString() + ":\r\n连接远程成功");//写入一条新log
            }
            catch (Exception ex)
            {
                MessageBox.Show("远程相机连接成功:" + ex.Message);
                log.Error("\r\n" + DateTime.Now.ToString() + ":\r\n连接远程失败:详细信息:" + ex.Message);//写入一条新log
                throw;
            }

            NET_SNAP_PARAMS asyncSnap = new NET_SNAP_PARAMS();
            asyncSnap.Channel = 0;
            asyncSnap.Quality = 6;
            asyncSnap.ImageSize = 2;
            asyncSnap.mode = 0;
            asyncSnap.InterSnap = 0;
            bool ret = NETClient.SnapPictureEx(_LoginID, asyncSnap, IntPtr.Zero);
            if (!ret)
            {
                MessageBox.Show(this, NETClient.GetLastError());
                return;
            }
        }
//回调函数,保存图片的操作在此进行
        private void SnapRevCallBack(IntPtr lLoginID, IntPtr pBuf, uint RevLen, uint EncodeType, uint CmdSerial, IntPtr dwUser)
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + "image";
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            if (EncodeType == 10) //.jpg
            {
                DateTime now = DateTime.Now;
                string fileName = string.Format("{0}-{1}-{2}-{3}-{4}-{5}", now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second) + ".jpg";
                string filePath = path + "\\" + fileName;
                attrpath = filePath;
                byte[] data = new byte[RevLen];
                Marshal.Copy(pBuf, data, 0, (int)RevLen);
                try
                {
                    using (FileStream stream = new FileStream(filePath, FileMode.OpenOrCreate))
                    {
                        stream.Write(data, 0, (int)RevLen);
                        stream.Flush();
                        stream.Dispose();
                       //将拍到的图片传置前台
                        Dispatcher.BeginInvoke(new Action(delegate
                        {
                            BitmapImage ima = new BitmapImage(new Uri(attrpath, UriKind.Absolute));
                            anjian2.Source = ima;
                        }));
                        //将图片上传至服务器
                       // interfacecall.UploadPalletimage(attrpath);//上传图片的接口自己写
                    
                    }
                }
                catch(Exception ex)
                {
                    return;
                }
            }
        }

发布了13 篇原创文章 · 获赞 1 · 访问量 2917

猜你喜欢

转载自blog.csdn.net/huxinyu0208/article/details/105193761
今日推荐