From 8633a0b6e52d9899fda4f3ec2decc1c39b960e5f Mon Sep 17 00:00:00 2001 From: kazelee <1847801760@qq.com> Date: 星期二, 27 五月 2025 14:42:00 +0800 Subject: [PATCH] 恢复拍照模块,优化任务号生成机制 --- device/SnapDevice.cs | 240 ++++++++++++++++++++++++++++++------------------------------ 1 files changed, 120 insertions(+), 120 deletions(-) diff --git a/device/SnapDevice.cs b/device/SnapDevice.cs index c465b81..216bc0e 100644 --- a/device/SnapDevice.cs +++ b/device/SnapDevice.cs @@ -7,14 +7,14 @@ using System.Text; using System.Threading.Tasks; -//using NetSDKCS; // TODO SNAP +using NetSDKCS; // SNAP namespace HH.WCS.Mobox3.AnGang.device { 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(); // TODO SNAP + private NET_DEVICEINFO_Ex _DeviceInfo = new NET_DEVICEINFO_Ex(); // SNAP private bool _IsSpanCapture = false; //private List<int> _channalList = new List<int>(); @@ -23,7 +23,7 @@ public SnapDevice() { try { //_SnapRevCallBack = new fSnapRevCallBack(SnapRevCallBack); - //NETClient.Init(null, IntPtr.Zero, null); // TODO SNAP + NETClient.Init(null, IntPtr.Zero, null); // SNAP //NETClient.SetSnapRevCallBack(_SnapRevCallBack, IntPtr.Zero); } catch (Exception ex) { @@ -32,134 +32,134 @@ } ~SnapDevice() { - //NETClient.Cleanup(); // TODO SNAP + NETClient.Cleanup(); // SNAP } - //public void LoadInfo(Config.Snap snap) { - // LoadInfo(snap.Ip, snap.Port, snap.Name, snap.Pwd); - //} - - // TODO SNAP + // SNAP - //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锛氱鍙e彿鏍煎紡閿欒"); - // 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 void LoadInfo(Config.Snap snap) { + LoadInfo(snap.Ip, snap.Port, snap.Name, snap.Pwd); + } - //public bool SnapPictureToFileOK(ref string filePath, int channelIndex = 0) { - // if (channelIndex >= _channalMax) { - // LogHelper.Info($"閫氶亾鏁�'{channelIndex}' 瓒呭嚭閫氶亾鎬绘暟 '{_channalMax}'"); - // //filePath = string.Empty; - // return false; - // } + 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锛氱鍙e彿鏍煎紡閿欒"); + 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; - // //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; + public bool SnapPictureToFileOK(ref string filePath, int channelIndex = 0) { + if (channelIndex >= _channalMax) { + LogHelper.Info($"閫氶亾鏁�'{channelIndex}' 瓒呭嚭閫氶亾鎬绘暟 '{_channalMax}'"); + //filePath = string.Empty; + return false; + } - // 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, - // }; + //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_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), - // }; + 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.SnapPictureToFile(_LoginID, ref inParam, ref outParam, 1000); - // if (!ret) { - // LogHelper.Info("鎶撳浘澶辫触"); - // } - // return ret; - //} + 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, + }; - //public bool SnapPictureOk(int channelIndex = 0) { - // if (channelIndex >= _channalMax) { - // LogHelper.Info($"閫氶亾鏁�'{channelIndex}' 瓒呭嚭閫氶亾鎬绘暟 '{_channalMax}'"); - // return false; - // } + 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), + }; - // 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; - //} + bool ret = NETClient.SnapPictureToFile(_LoginID, ref inParam, ref outParam, 1000); + if (!ret) { + LogHelper.Info("鎶撳浘澶辫触"); + } + return ret; + } - //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.Info($"鍙戠敓浜嗗紓甯革細{ex.Message}"); - // return; - // } - // // 鏇夸唬Demo绋嬪簭鐨凢rame鍥剧墖灞曠ず - // LogHelper.Info($"淇濆瓨鍥剧墖鍒帮細{filePath}"); - // Process.Start(filePath); - // } - //} + 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.Info($"鍙戠敓浜嗗紓甯革細{ex.Message}"); + return; + } + // 鏇夸唬Demo绋嬪簭鐨凢rame鍥剧墖灞曠ず + LogHelper.Info($"淇濆瓨鍥剧墖鍒帮細{filePath}"); + Process.Start(filePath); + } + } } } -- Gitblit v1.9.1