using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using NetSDKCS; namespace HH.WCS.Mobox3.AnGang.Devices { public class SnapDevice { private IntPtr _LoginID = IntPtr.Zero; private IntPtr _PlayID = IntPtr.Zero; //private fSnapRevCallBack _SnapRevCallBack; private NET_DEVICEINFO_Ex _DeviceInfo = new NET_DEVICEINFO_Ex(); private bool _IsSpanCapture = false; //private List _channalList = new List(); private int _channalMax = 0; public SnapDevice() { try { //_SnapRevCallBack = new fSnapRevCallBack(SnapRevCallBack); NETClient.Init(null, IntPtr.Zero, null); //NETClient.SetSnapRevCallBack(_SnapRevCallBack, IntPtr.Zero); } catch (Exception ex) { LogHelper.InfoEx(ex); } } ~SnapDevice() { NETClient.Cleanup(); } public void LoadInfo(AppStart.Snap snap) { LoadInfo(snap.Ip, snap.Port, snap.Name, snap.Pwd); } public void LoadInfo(string ip, int port, string name, string pwd) { if (IntPtr.Zero == _LoginID) { ushort uPort = 0; try { uPort = Convert.ToUInt16(port); } catch { LogHelper.Info("加载Snap:端口号格式错误"); return; } _LoginID = NETClient.LoginWithHighLevelSecurity(ip, uPort, name, pwd, EM_LOGIN_SPAC_CAP_TYPE.TCP, IntPtr.Zero, ref _DeviceInfo); if (IntPtr.Zero == _LoginID) { LogHelper.Info(NETClient.GetLastError()); return; } //_channalList.Clear(); //for (int i = 0; i < _DeviceInfo.nChanNum; i++) { // _channalList.Add(i + 1); //} _channalMax = _DeviceInfo.nChanNum; } else { NETClient.Logout(_LoginID); _LoginID = IntPtr.Zero; if (_IsSpanCapture) { _IsSpanCapture = false; } } } public bool SnapPictureToFileOK(ref string filePath, int channelIndex = 0) { if (channelIndex >= _channalMax) { LogHelper.Info($"通道数 '{channelIndex}' 超出通道总数 '{_channalMax}'"); //filePath = string.Empty; return false; } //DateTime now = DateTime.Now; //string path = AppDomain.CurrentDomain.BaseDirectory + "image"; //string fileName = string.Format("{0}{1}{2}{3}{4}{5}", now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second) + ".jpg"; //filePath = path + "\\" + fileName; NET_SNAP_PARAMS asyncSnap = new NET_SNAP_PARAMS(); //asyncSnap.Channel = (uint)this._channalList[channelIndex]; asyncSnap.Channel = (uint)channelIndex; asyncSnap.Quality = 6; asyncSnap.ImageSize = 2; asyncSnap.mode = 0; asyncSnap.InterSnap = 0; NET_IN_SNAP_PIC_TO_FILE_PARAM inParam = new NET_IN_SNAP_PIC_TO_FILE_PARAM { dwSize = (uint)Marshal.SizeOf(typeof(NET_IN_SNAP_PIC_TO_FILE_PARAM)), stuParam = asyncSnap, szFilePath = filePath, }; NET_OUT_SNAP_PIC_TO_FILE_PARAM outParam = new NET_OUT_SNAP_PIC_TO_FILE_PARAM() { dwSize = (uint)Marshal.SizeOf(typeof(NET_OUT_SNAP_PIC_TO_FILE_PARAM)), dwPicBufLen = 1024000, szPicBuf = Marshal.AllocHGlobal(1024000), }; bool ret = NETClient.SnapPictureToFile(_LoginID, ref inParam, ref outParam, 1000); if (!ret) { LogHelper.Info("抓图失败"); } return ret; } public bool SnapPictureOk(int channelIndex = 0) { if (channelIndex >= _channalMax) { LogHelper.Info($"通道数 '{channelIndex}' 超出通道总数 '{_channalMax}'"); return false; } NET_SNAP_PARAMS asyncSnap = new NET_SNAP_PARAMS(); //asyncSnap.Channel = (uint)this._channalList[channelIndex]; asyncSnap.Channel = (uint)channelIndex; asyncSnap.Quality = 6; asyncSnap.ImageSize = 2; asyncSnap.mode = 0; asyncSnap.InterSnap = 0; bool ret = NETClient.SnapPictureEx(_LoginID, asyncSnap, IntPtr.Zero); if (!ret) { LogHelper.Info(NETClient.GetLastError()); return false; } return true; } 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; byte[] data = new byte[RevLen]; Marshal.Copy(pBuf, data, 0, (int)RevLen); try { //when the file is operate by local capture will throw expection. using (FileStream stream = new FileStream(filePath, FileMode.OpenOrCreate)) { stream.Write(data, 0, (int)RevLen); stream.Flush(); stream.Dispose(); } } catch (Exception ex) { LogHelper.InfoEx(ex); return; } // 替代Demo程序的Frame图片展示 LogHelper.Info($"保存图片到:{filePath}"); Process.Start(filePath); } } } }