private void CaptureThreadProc_General(tpvCameraObj cameraObj) { CameraSdkStatus eStatus; tSdkFrameHead FrameHead; IntPtr uRawBuffer; //rawbuffer由SDK内部申请。应用层不要调用delete之类的释放函数 while (cameraObj.m_bExitCaptureThread == false) { //500毫秒超时,图像没捕获到前,线程会被挂起,释放CPU,所以该线程中无需调用sleep eStatus = MvApi.CameraGetImageBuffer(cameraObj.m_hCamera, out FrameHead, out uRawBuffer, 500); if (eStatus == CameraSdkStatus.CAMERA_STATUS_SUCCESS) //如果是触发模式,则有可能超时 { //图像处理,将原始输出转换为RGB格式的位图数据,同时叠加白平衡、饱和度、LUT等ISP处理。 MvApi.CameraImageProcess(cameraObj.m_hCamera, uRawBuffer, cameraObj.m_ImageBuffer, ref FrameHead); //叠加十字线、自动曝光窗口、白平衡窗口信息(仅叠加设置为可见状态的)。 MvApi.CameraImageOverlay(cameraObj.m_hCamera, cameraObj.m_ImageBuffer, ref FrameHead); //调用SDK封装好的接口,显示预览图像 MvApi.CameraDisplayRGB24(cameraObj.m_hCamera, cameraObj.m_ImageBuffer, ref FrameHead); //成功调用CameraGetImageBuffer后必须释放,下次才能继续调用CameraGetImageBuffer捕获图像。 MvApi.CameraReleaseImageBuffer(cameraObj.m_hCamera, uRawBuffer); if (FrameHead.iWidth != cameraObj.m_tFrameHead.iWidth || FrameHead.iHeight != cameraObj.m_tFrameHead.iHeight) { cameraObj.m_bEraseBk = true; cameraObj.m_tFrameHead = FrameHead; } cameraObj.m_iDisplayedFrames++; } } }
private void BasicForm_FormClosing(object sender, FormClosingEventArgs e) { for (int i = 0; i < CAMERA_NUM; i++) { tpvCameraObj cameraObj = cameraObjs[i]; //相机1反初始化 if (cameraObj.m_tCaptureThread != null) { cameraObj.m_bExitCaptureThread = true; cameraObj.m_tCaptureThread.Join(); cameraObj.m_tCaptureThread = null; } if (cameraObj.m_hCamera != 0) { MvApi.CameraUnInit(cameraObj.m_hCamera); cameraObj.m_hCamera = 0; } if (cameraObj.m_ImageBuffer != IntPtr.Zero) { Marshal.FreeHGlobal(cameraObj.m_ImageBuffer); cameraObj.m_ImageBuffer = IntPtr.Zero; } } }
//相机1 //private tpvCameraObj cameraObj1; //相机2 //private tpvCameraObj cameraObj2; #endregion public BasicForm() { InitializeComponent(); //cameraObj1 = new tpvCameraObj(); //cameraObj2 = new tpvCameraObj(); cameraObjs = new tpvCameraObj[CAMERA_NUM]; for (int i = 0; i < CAMERA_NUM; i++) { cameraObjs[i] = new tpvCameraObj(); } InitCamera(); }
/// <summary> /// 根据设备列表,初始化单个相机 /// </summary> /// <param name="tCameraDevInfoList">相机设备列表</param> /// <param name="cameraDevID">第i个相机</param> /// <param name="PreviewBox">相机对应的PictureBox</param> /// <param name="CaptureThreadProc1">相机对应的线程处理函数</param> /// <param name="cameraObj">相机对象</param> /// <returns></returns> protected bool InitSingleCamera(tSdkCameraDevInfo[] tCameraDevInfoList, int cameraDevID, ThreadStart CaptureThreadProc1, PictureBox PreviewBox, Button BtnPlay, Label StateLabel, string errMsg, ref tpvCameraObj cameraObj) { if (MvApi.CameraInit(ref tCameraDevInfoList[cameraDevID], -1, -1, ref cameraObj.m_hCamera) == CameraSdkStatus.CAMERA_STATUS_SUCCESS) { //获得相机特性描述 MvApi.CameraGetCapability(cameraObj.m_hCamera, out cameraObj.tCameraCapability); cameraObj.m_ImageBuffer = Marshal.AllocHGlobal(cameraObj.tCameraCapability.sResolutionRange.iWidthMax * cameraObj.tCameraCapability.sResolutionRange.iHeightMax * 3 + 1024); //初始化显示模块,使用SDK内部封装好的显示接口 MvApi.CameraDisplayInit(cameraObj.m_hCamera, PreviewBox.Handle); MvApi.CameraSetDisplaySize(cameraObj.m_hCamera, PreviewBox.Width, PreviewBox.Height); //让SDK来根据相机的型号动态创建该相机的配置窗口。 MvApi.CameraCreateSettingPage(cameraObj.m_hCamera, this.Handle, tCameraDevInfoList[cameraDevID].acFriendlyName, /*SettingPageMsgCalBack*/ null, /*m_iSettingPageMsgCallbackCtx*/ (IntPtr)null, 0); MvApi.CameraPlay(cameraObj.m_hCamera); BtnPlay.Text = "Pause"; //两种方式来获得预览图像,设置回调函数或者使用定时器或者独立线程的方式, //主动调用CameraGetImageBuffer接口来抓图。 //本例中仅演示了两种的方式,注意,两种方式也可以同时使用,但是在回调函数中, //不要使用CameraGetImageBuffer,否则会造成死锁现象。 cameraObj.m_bExitCaptureThread = false; cameraObj.m_tCaptureThread = new Thread(new ThreadStart(CaptureThreadProc1)); cameraObj.m_tCaptureThread.Start(); } else { cameraObj.m_hCamera = 0; //StateLabel.Text = "Camera 1 init error"; StateLabel.Text = errMsg; return(false); } return(true); }