kazelee
2025-06-03 9e40412e6d20f07c1d98ceae5214093b50e2fb88
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:端口号格式错误");
        //            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:端口号格式错误");
                    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程序的Frame图片展示
        //        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程序的Frame图片展示
                LogHelper.Info($"保存图片到:{filePath}");
                Process.Start(filePath);
            }
        }
    }
}