@@ -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
0 commit comments