1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
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; // 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(); // SNAP
        private bool _IsSpanCapture = false;
 
        //private List<int> _channalList = new List<int>();
        private int _channalMax = 0;
 
        public SnapDevice() {
            try {
                //_SnapRevCallBack = new fSnapRevCallBack(SnapRevCallBack);
                NETClient.Init(null, IntPtr.Zero, null); // SNAP
                //NETClient.SetSnapRevCallBack(_SnapRevCallBack, IntPtr.Zero);
            }
            catch (Exception ex) {
                LogHelper.Info($"发生了异常:{ex.Message}");
            }
        }
 
        ~SnapDevice() {
            NETClient.Cleanup(); // SNAP
        }
 
        // SNAP
 
        public void LoadInfo(Config.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.Info($"发生了异常:{ex.Message}");
                    return;
                }
                // 替代Demo程序的Frame图片展示
                LogHelper.Info($"保存图片到:{filePath}");
                Process.Start(filePath);
            }
        }
 
        public string GetCapturePictureLocalPath() {
            string path = AppDomain.CurrentDomain.BaseDirectory + "image";
            if (!Directory.Exists(path)) {
                Directory.CreateDirectory(path);
            }
            DateTime now = DateTime.Now;
            string filePath = path + "\\" + string.Format("{0}-{1}-{2}-{3}-{4}-{5}", now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second) + ".jpg";
            bool ret = NETClient.CapturePicture(_PlayID, filePath, EM_NET_CAPTURE_FORMATS.JPEG);
            if (!ret) {
                return "";
            }
            return filePath;
        }
    }
}