Skip to content

Commit b164a9f

Browse files
author
luominghua
committed
增加变焦模式
1 parent 0fa9180 commit b164a9f

File tree

6 files changed

+184
-9
lines changed

6 files changed

+184
-9
lines changed

HkNetLib/Hardware/CHCNetSDK.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10672,7 +10672,35 @@ public enum TRAFFIC_DATA_VARY_TYPE
1067210672
VEHICLE_LEAVE, //�����뿪������Ȧ
1067310673
UEUE_VARY //���б仯
1067410674
}
10675+
/*************************************************
10676+
�������ýṹ������(����_V30Ϊ9000����)
10677+
**************************************************/
10678+
10679+
public const int NET_DVR_SET_FOCUSMODECFG = 3306;
10680+
public const int NET_DVR_GET_FOCUSMODECFG = 3305;
1067510681

10682+
[StructLayoutAttribute(LayoutKind.Sequential)]
10683+
public struct NET_DVR_FOCUSMODE_CFG
10684+
{
10685+
public uint dwSize;
10686+
public byte byFocusMode;
10687+
public byte byAutoFocusMode;
10688+
public ushort wMinFocusDistance;
10689+
public byte byZoomSpeedLevel;
10690+
public byte byFocusSpeedLevel;
10691+
public byte byOpticalZoom;
10692+
public byte byDigtitalZoom;
10693+
public float fOpticalZoomLevel;
10694+
public uint dwFocusPos;
10695+
public byte byFocusDefinitionDisplay;
10696+
public byte byFocusSensitivity;
10697+
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 2, ArraySubType = UnmanagedType.I1)]
10698+
public byte[] byRes1;
10699+
public uint dwRelativeFocusPos;
10700+
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 48, ArraySubType = UnmanagedType.I1)]
10701+
public byte[] byRes;
10702+
10703+
}
1067610704
[StructLayoutAttribute(LayoutKind.Sequential)]
1067710705
public struct NET_DVR_LANE_PARAM
1067810706
{

HkNetLib/HkNetLib.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
<Compile Include="Properties\AssemblyInfo.cs" />
4848
<Compile Include="Wrapper\CameraLoginInfo.cs" />
4949
<Compile Include="Wrapper\CodeStreamType.cs" />
50+
<Compile Include="Wrapper\FocusModeType.cs" />
5051
<Compile Include="Wrapper\HkCamera.cs" />
5152
<Compile Include="Wrapper\ICamera.cs" />
5253
<Compile Include="Wrapper\PtzCommand.cs" />

HkNetLib/Wrapper/FocusModeType.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
namespace HkNetLib.Wrapper
2+
{
3+
/// <summary>
4+
/// 海康对焦模式
5+
/// </summary>
6+
public enum FocusModeType
7+
{
8+
/// <summary>
9+
/// 自动
10+
/// </summary>
11+
Auto = 0,
12+
13+
/// <summary>
14+
/// 手动
15+
/// </summary>
16+
Manual = 1,
17+
18+
/// <summary>
19+
/// 半自动
20+
/// </summary>
21+
HalfAuto = 2,
22+
23+
/// <summary>
24+
/// 未知/无
25+
/// </summary>
26+
None = 4
27+
}
28+
}

HkNetLib/Wrapper/HkCamera.cs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,101 @@ public bool CapturePicture(string fileName)
190190
}
191191
}
192192

193+
194+
public bool SetFocusMode(FocusModeType focusModeType)
195+
{
196+
bool res = false;
197+
var destMode = focusModeType;
198+
CHCNetSDK.NET_DVR_FOCUSMODE_CFG focusmode_cfg = new CHCNetSDK.NET_DVR_FOCUSMODE_CFG();
199+
focusmode_cfg.byRes = new byte[48];
200+
focusmode_cfg.byRes1 = new byte[2];
201+
int nSize = Marshal.SizeOf(focusmode_cfg);
202+
focusmode_cfg.dwSize = (uint)nSize;
203+
IntPtr ptrDeviceCfg = Marshal.AllocHGlobal(nSize);
204+
205+
try
206+
{
207+
// 获取当前模式
208+
Marshal.StructureToPtr(focusmode_cfg, ptrDeviceCfg, false);
209+
uint rSize = (uint)nSize;
210+
if (!CHCNetSDK.NET_DVR_GetDVRConfig(_userId, CHCNetSDK.NET_DVR_GET_FOCUSMODECFG, 1, ptrDeviceCfg, (uint)nSize, ref rSize))
211+
{
212+
var errorStr = CHCNetSDK.NET_DVR_GetLastError();
213+
return res;
214+
}
215+
216+
// 如果当前模式与请求模式相同,则直接返回true
217+
CHCNetSDK.NET_DVR_FOCUSMODE_CFG getObj = (CHCNetSDK.NET_DVR_FOCUSMODE_CFG)Marshal.PtrToStructure(ptrDeviceCfg, typeof(CHCNetSDK.NET_DVR_FOCUSMODE_CFG));
218+
var curMode = (FocusModeType)getObj.byFocusMode;
219+
if (curMode == destMode)
220+
{
221+
res = true;
222+
return res;
223+
}
224+
225+
// 否则调用设置方法
226+
getObj.fOpticalZoomLevel = 0;
227+
getObj.byOpticalZoom = 32;
228+
getObj.byFocusMode = (byte)destMode;
229+
IntPtr ptrDeviceCfg1 = Marshal.AllocHGlobal((int)getObj.dwSize);
230+
Marshal.StructureToPtr(getObj, ptrDeviceCfg1, false);
231+
res = CHCNetSDK.NET_DVR_SetDVRConfig(_userId, CHCNetSDK.NET_DVR_SET_FOCUSMODECFG, 1, ptrDeviceCfg1, focusmode_cfg.dwSize);
232+
if (!res)
233+
{
234+
var errorStr = CHCNetSDK.NET_DVR_GetLastError();
235+
}
236+
}
237+
catch (Exception ex)
238+
{
239+
throw ex;
240+
}
241+
finally
242+
{
243+
Marshal.FreeHGlobal(ptrDeviceCfg);
244+
}
245+
246+
return res;
247+
}
248+
249+
/// <summary>
250+
/// 获取对焦距模式
251+
/// </summary>
252+
/// <returns></returns>
253+
public FocusModeType GetFocusMode()
254+
{
255+
FocusModeType retValue = FocusModeType.None;
256+
257+
CHCNetSDK.NET_DVR_FOCUSMODE_CFG focusmode_cfg = new CHCNetSDK.NET_DVR_FOCUSMODE_CFG
258+
{
259+
byRes = new byte[48],
260+
byRes1 = new byte[2]
261+
};
262+
int nSize = Marshal.SizeOf(focusmode_cfg);
263+
focusmode_cfg.dwSize = (uint)nSize;
264+
IntPtr ptrDeviceCfg = Marshal.AllocHGlobal(nSize);
265+
266+
try
267+
{
268+
Marshal.StructureToPtr(focusmode_cfg, ptrDeviceCfg, false);
269+
uint rSize = (uint)nSize;
270+
if (CHCNetSDK.NET_DVR_GetDVRConfig(this._userId, CHCNetSDK.NET_DVR_GET_FOCUSMODECFG, 1, ptrDeviceCfg, (uint)nSize, ref rSize))
271+
{
272+
CHCNetSDK.NET_DVR_FOCUSMODE_CFG getObj = (CHCNetSDK.NET_DVR_FOCUSMODE_CFG)Marshal.PtrToStructure(ptrDeviceCfg, typeof(CHCNetSDK.NET_DVR_FOCUSMODE_CFG));
273+
retValue = (FocusModeType)getObj.byFocusMode;
274+
}
275+
}
276+
catch (Exception ex)
277+
{
278+
throw ex;
279+
}
280+
finally
281+
{
282+
Marshal.FreeHGlobal(ptrDeviceCfg);
283+
}
284+
285+
return retValue;
286+
}
287+
193288
public bool Shutdown()
194289
{
195290
try

HkNetLib/Wrapper/ICamera.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ public interface ICamera: IDisposable
3232
/// 登出
3333
/// </summary>
3434
void Logout();
35-
35+
36+
bool SetFocusMode(FocusModeType focusModeType);
37+
FocusModeType GetFocusMode();
3638
/// <summary>
3739
/// 控制云台开始
3840
/// </summary>

HkNetLibTest/CameraTest.cs

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ public void TestCameraLogin()
3939
Assert.IsNotNull(camera);
4040
var result = camera.Login(new CameraLoginInfo()
4141
{
42-
IP = "192.168.4.172",
42+
IP = "192.168.21.84",
4343
Port = 8000,
4444
UserName = "admin",
45-
Password = "1234qwer",
45+
Password = "hik12345",
4646
});
4747
Assert.IsTrue(result);
4848
//iDS-2ZMN2507N20230531AARRAB9360262
@@ -110,22 +110,21 @@ public void MultipleLoginLogout()
110110
Assert.IsNotNull(camera);
111111
var result = camera.Login(new CameraLoginInfo()
112112
{
113-
IP = "192.168.4.19",
113+
IP = "192.168.21.84",
114114
Port = 8000,
115115
UserName = "admin",
116-
Password = "Ancn1111",
117-
ChannelNo = 8
116+
Password = "hik12345",
118117
});
119118
Assert.IsTrue(result);
120119
for (int i = 0; i < 1000; i++)
121120
{
121+
Thread.Sleep(2000);
122122
result = camera.Login(new CameraLoginInfo()
123123
{
124-
IP = "192.168.4.19",
124+
IP = "192.168.21.84",
125125
Port = 8000,
126126
UserName = "admin",
127-
Password = "Ancn1111",
128-
ChannelNo = 8
127+
Password = "hik12345",
129128
});
130129
var bOnline = camera.IsOnline();
131130
Assert.IsTrue(bOnline);
@@ -169,7 +168,29 @@ public void TestCaptureImage()
169168
Assert.IsTrue(rtn);
170169
}
171170

171+
[TestMethod]
172+
public void TestCameraFocus()
173+
{
174+
var camera = new HkCamera();
175+
176+
Assert.IsNotNull(camera);
177+
var result = camera.Login(new CameraLoginInfo()
178+
{
179+
IP = "192.168.21.84",
180+
Port = 8000,
181+
UserName = "admin",
182+
Password = "hik12345",
183+
});
184+
Assert.IsTrue(result);
185+
//iDS-2ZMN2507N20230531AARRAB9360262
186+
var sn = camera.GetSerialNumber();
187+
Assert.IsNotNull(sn);
188+
189+
var type = camera.GetFocusMode();
190+
var b = camera.SetFocusMode(FocusModeType.Manual);
172191

192+
type = camera.GetFocusMode();
193+
}
173194

174195
}
175196
}

0 commit comments

Comments
 (0)