private int InitCamera() { int m_hCamera = -1; tSdkCameraDevInfo[] m_DevInfo = new tSdkCameraDevInfo[] { new tSdkCameraDevInfo() }; MvApi.CameraEnumerateDevice(out m_DevInfo); if (m_DevInfo != null) { IntPtr m_Grabber = new IntPtr(); if (MvApi.CameraGrabber_Create(out m_Grabber, ref m_DevInfo[0]) == CameraSdkStatus.CAMERA_STATUS_SUCCESS) { MvApi.CameraGrabber_GetCameraHandle(m_Grabber, out m_hCamera); } } return(m_hCamera); }
private bool InitCamera() { tSdkCameraDevInfo[] tCameraDevInfoList = new tSdkCameraDevInfo[12]; IntPtr ptr; int i; #if USE_CALL_BACK CAMERA_SNAP_PROC pCaptureCallOld = null; #endif ptr = Marshal.AllocHGlobal(Marshal.SizeOf(new tSdkCameraDevInfo()) * 12); int iCameraCounts = 12;//如果有多个相机时,表示最大只获取最多12个相机的信息列表。该变量必须初始化,并且大于1 if (m_hCamera > 0) { //已经初始化过,直接返回 true return(true); } if (MvApi.CameraEnumerateDevice(ptr, ref iCameraCounts) == CameraSdkStatus.CAMERA_STATUS_SUCCESS) { for (i = 0; i < 12; i++) { tCameraDevInfoList[i] = (tSdkCameraDevInfo)Marshal.PtrToStructure((IntPtr)((int)ptr + i * Marshal.SizeOf(new tSdkCameraDevInfo())), typeof(tSdkCameraDevInfo)); } Marshal.FreeHGlobal(ptr); if (iCameraCounts >= 1)//此时iCameraCounts返回了实际连接的相机个数。如果大于1,则初始化第一个相机 { if (MvApi.CameraInit(ref tCameraDevInfoList[0], -1, -1, ref m_hCamera) == CameraSdkStatus.CAMERA_STATUS_SUCCESS) { //获得相机特性描述 ptr = Marshal.AllocHGlobal(Marshal.SizeOf(new tSdkCameraCapbility())); MvApi.CameraGetCapability(m_hCamera, ptr); tCameraCapability = (tSdkCameraCapbility)Marshal.PtrToStructure(ptr, typeof(tSdkCameraCapbility)); Marshal.FreeHGlobal(ptr); Marshal.FreeHGlobal(m_ImageBuffer); m_ImageBuffer = Marshal.AllocHGlobal(tCameraCapability.sResolutionRange.iWidthMax * tCameraCapability.sResolutionRange.iHeightMax * 3 + 1024); m_ImageBufferSnapshot = Marshal.AllocHGlobal(tCameraCapability.sResolutionRange.iWidthMax * tCameraCapability.sResolutionRange.iHeightMax * 3 + 1024); //初始化显示模块,使用SDK内部封装好的显示接口 MvApi.CameraDisplayInit(m_hCamera, pic_Image.Handle); MvApi.CameraSetDisplaySize(m_hCamera, pic_Image.Width, pic_Image.Height); //设置抓拍通道的分辨率。 tSdkImageResolution tResolution; tResolution.fZoomScale = 1.0F; tResolution.iVOffset = 0; tResolution.iHOffset = 0; tResolution.uBinMode = 0; tResolution.uSkipMode = 0; tResolution.iWidth = tCameraCapability.sResolutionRange.iWidthMax; tResolution.iHeight = tCameraCapability.sResolutionRange.iHeightMax; //tResolution.iIndex = 0xff;表示自定义分辨率,如果tResolution.iWidth和tResolution.iHeight //定义为0,则表示跟随预览通道的分辨率进行抓拍。抓拍通道的分辨率可以动态更改。 //本例中将抓拍分辨率固定为最大分辨率。 tResolution.iIndex = 0xff; tResolution.acDescription = new byte[32];//描述信息可以不设置 MvApi.CameraSetResolutionForSnap(m_hCamera, ref tResolution); //让SDK来根据相机的型号动态创建该相机的配置窗口。 MvApi.CameraCreateSettingPage(m_hCamera, this.Handle, tCameraDevInfoList[0].acFriendlyName, /*SettingPageMsgCalBack*/ null, /*m_iSettingPageMsgCallbackCtx*/ (IntPtr)null, 0); //两种方式来获得预览图像,设置回调函数或者使用定时器或者独立线程的方式, //主动调用CameraGetImageBuffer接口来抓图。 //本例中仅演示了两种的方式,注意,两种方式也可以同时使用,但是在回调函数中, //不要使用CameraGetImageBuffer,否则会造成死锁现象。 #if USE_CALL_BACK MvApi.CameraSetCallbackFunction(m_hCamera, ImageCaptureCallback, m_iCaptureCallbackCtx, ref pCaptureCallOld); #else //如果需要采用多线程,使用下面的方式 m_bExitCaptureThread = false; m_tCaptureThread = new Thread(new ThreadStart(CaptureThreadProc)); m_tCaptureThread.Start(); #endif return(true); } else { m_hCamera = 0; //StateLabel.Text = "相机初始化失败"; return(false); } } } return(false); }