private void InitCamera() { CameraSdkStatus status = 0; tSdkCameraDevInfo[] DevList; MvApi.CameraEnumerateDevice(out DevList); int NumDev = (DevList != null ? DevList.Length : 0); if (NumDev < 1) { MessageBox.Show("未扫描到相机"); return; } else if (NumDev == 1) { status = MvApi.CameraGrabber_Create(out m_Grabber, ref DevList[0]); } else { status = MvApi.CameraGrabber_CreateFromDevicePage(out m_Grabber); } if (status == 0) { MvApi.CameraGrabber_GetCameraDevInfo(m_Grabber, out m_DevInfo); MvApi.CameraGrabber_GetCameraHandle(m_Grabber, out m_hCamera); MvApi.CameraCreateSettingPage(m_hCamera, this.Handle, m_DevInfo.acFriendlyName, null, (IntPtr)0, 0); MvApi.CameraGrabber_SetRGBCallback(m_Grabber, m_FrameCallback, IntPtr.Zero); // 黑白相机设置ISP输出灰度图像 // 彩色相机ISP默认会输出BGR24图像 tSdkCameraCapbility cap; MvApi.CameraGetCapability(m_hCamera, out cap); if (cap.sIspCapacity.bMonoSensor != 0) { MvApi.CameraSetIspOutFormat(m_hCamera, (uint)MVSDK.emImageFormat.CAMERA_MEDIA_TYPE_MONO8); // 创建灰度调色板 Bitmap Image = new Bitmap(1, 1, PixelFormat.Format8bppIndexed); m_GrayPal = Image.Palette; for (int Y = 0; Y < m_GrayPal.Entries.Length; Y++) { m_GrayPal.Entries[Y] = Color.FromArgb(255, Y, Y, Y); } } // 设置VFlip,由于SDK输出的数据默认是从底到顶的,打开VFlip后就可以直接转换为Bitmap MvApi.CameraSetMirror(m_hCamera, 1, 1); // 为了演示如何在回调中使用相机数据创建Bitmap并显示到PictureBox中,这里不使用SDK内置的绘制操作 //MvApi.CameraGrabber_SetHWnd(m_Grabber, this.DispWnd.Handle); MvApi.CameraGrabber_StartLive(m_Grabber); } else { MessageBox.Show(String.Format("打开相机失败,原因:{0}", status)); } }
public Form1() { InitializeComponent(); m_FrameCallback = new pfnCameraGrabberFrameCallback(CameraGrabberFrameCallback); tSdkCameraDevInfo[] DevList; MvApi.CameraEnumerateDevice(out DevList); int NumDev = (DevList != null ? DevList.Length : 0); if (NumDev < 1) { MessageBox.Show("未扫描到相机"); } else if (NumDev == 1) { MvApi.CameraGrabber_Create(out m_Grabber, ref DevList[0]); } else { MvApi.CameraGrabber_CreateFromDevicePage(out m_Grabber); } if (m_Grabber != IntPtr.Zero) { MvApi.CameraGrabber_GetCameraDevInfo(m_Grabber, out m_DevInfo); MvApi.CameraGrabber_GetCameraHandle(m_Grabber, out m_hCamera); MvApi.CameraCreateSettingPage(m_hCamera, this.Handle, m_DevInfo.acFriendlyName, null, (IntPtr)0, 0); MvApi.CameraGrabber_SetRGBCallback(m_Grabber, m_FrameCallback, IntPtr.Zero); MvApi.CameraGrabber_SetHWnd(m_Grabber, this.DispWnd.Handle); MvApi.CameraGrabber_StartLive(m_Grabber); } }
private void Form1_Load(object sender, EventArgs e) { //1打开窗口、赋值线程状态 //2枚举设备 //3.初始化设备 //3.申请内存 获取特性 获取位数 //4.设置触发模式 //5.开启绑定线程 //6开始采集 //6开始采集(默认连续,不设置采集模式的话) Control.CheckForIllegalCrossThreadCalls = false; HOperatorSet.OpenWindow(0, 0, pictureBox1.Width, pictureBox1.Height, pictureBox1.Handle, "", "", out hv_WindowHandle); HDevWindowStack.Push(hv_WindowHandle); status = MvApi.CameraEnumerateDevice(out tCameraDevInfoList); //枚举设备 if (tCameraDevInfoList != null)//此时iCameraCounts返回了实际连接的相机个数。如果大于1,则初始化第一个相机 { status = MvApi.CameraInit(ref tCameraDevInfoList[0], -1, -1, ref m_hCamera); //初始化第一个相机 //获得相机特性描述 MvApi.CameraGetCapability(m_hCamera, out tCameraCapability); //申请内存 m_ImageBuffer = Marshal.AllocHGlobal(tCameraCapability.sResolutionRange.iWidthMax * tCameraCapability.sResolutionRange.iHeightMax * 3 + 1024); //MvApi.CameraSetTriggerMode(m_hCamera, 0); m_bExitCaptureThread = false; m_tCaptureThread = new Thread(new ThreadStart(CaptureThreadProc)); m_tCaptureThread.Start(); MvApi.CameraPlay(m_hCamera); } }
public void Start() { MvApi.CameraEnumerateDevice(out m_DevInfo); int NumDev = (m_DevInfo != null ? Math.Min(m_DevInfo.Length, CAMERA_NUM) : 0); IntPtr[] hDispWnds = { frm.ImageBox0.Handle, frm.ImageBox1.Handle, frm.ImageBox2.Handle, frm.ImageBox3.Handle }; for (int i = 0; i < NumDev; ++i) { if (MvApi.CameraGrabber_Create(out m_Grabber[i], ref m_DevInfo[i]) == CameraSdkStatus.CAMERA_STATUS_SUCCESS) { MvApi.CameraGrabber_GetCameraHandle(m_Grabber[i], out m_hCamera[i]); MvApi.CameraCreateSettingPage(m_hCamera[i], frm.Handle, m_DevInfo[i].acFriendlyName, null, (IntPtr)0, 0); } } }
public Form2() { InitializeComponent(); m_FrameCallback[0] = new pfnCameraGrabberFrameCallback(CameraGrabberFrameCallback0); m_FrameCallback[1] = new pfnCameraGrabberFrameCallback(CameraGrabberFrameCallback1); m_FrameCallback[2] = new pfnCameraGrabberFrameCallback(CameraGrabberFrameCallback2); m_FrameCallback[3] = new pfnCameraGrabberFrameCallback(CameraGrabberFrameCallback3); m_SaveImageComplete = new pfnCameraGrabberSaveImageComplete(CameraGrabberSaveImageComplete2); MvApi.CameraEnumerateDevice(out m_DevInfo); int NumDev = (m_DevInfo != null ? Math.Min(m_DevInfo.Length, CAMERA_NUM) : 0); IntPtr[] hDispWnds = { this.DispWnd1.Handle, this.DispWnd2.Handle, this.DispWnd3.Handle, this.DispWnd4.Handle }; for (int i = 0; i < NumDev; ++i) { if (MvApi.CameraGrabber_Create(out m_Grabber[i], ref m_DevInfo[i]) == CameraSdkStatus.CAMERA_STATUS_SUCCESS) { MvApi.CameraGrabber_GetCameraHandle(m_Grabber[i], out m_hCamera[i]); MvApi.CameraCreateSettingPage(m_hCamera[i], this.Handle, m_DevInfo[i].acFriendlyName, null, (IntPtr)0, 0); MvApi.CameraGrabber_SetRGBCallback(m_Grabber[i], m_FrameCallback[i], IntPtr.Zero); MvApi.CameraGrabber_SetSaveImageCompleteCallback(m_Grabber[i], m_SaveImageComplete, IntPtr.Zero); // 黑白相机设置ISP输出灰度图像 // 彩色相机ISP默认会输出BGR24图像 tSdkCameraCapbility cap; MvApi.CameraGetCapability(m_hCamera[i], out cap); if (cap.sIspCapacity.bMonoSensor != 0) { MvApi.CameraSetIspOutFormat(m_hCamera[i], (uint)MVSDK.emImageFormat.CAMERA_MEDIA_TYPE_MONO8); } MvApi.CameraGrabber_SetHWnd(m_Grabber[i], hDispWnds[i]); } } for (int i = 0; i < NumDev; ++i) { if (m_Grabber[i] != IntPtr.Zero) { MvApi.CameraGrabber_StartLive(m_Grabber[i]); } } }
private void Form1_Load(object sender, EventArgs e) { //1打开窗口 //2枚举设备 //3初始化设备 //4.获得位数,申请内存 //5绑定回调事件 并在回调事件中加入halcon显示代码 //6开始采集(默认连续,不设置采集模式的话) HOperatorSet.OpenWindow(0, 0, pictureBox1.Width, pictureBox1.Height, pictureBox1.Handle, "", "", out hv_WindowHandle); HDevWindowStack.Push(hv_WindowHandle); status = MvApi.CameraEnumerateDevice(out tCameraDevInfoList); if (tCameraDevInfoList != null)//此时iCameraCounts返回了实际连接的相机个数。如果大于1,则初始化第一个相机 { status = MvApi.CameraInit(ref tCameraDevInfoList[0], -1, -1, ref m_hCamera); } MvApi.CameraGetCapability(m_hCamera, out tCameraCapability); m_ImageBuffer = Marshal.AllocHGlobal(tCameraCapability.sResolutionRange.iWidthMax * tCameraCapability.sResolutionRange.iHeightMax * 3 + 1024); m_CaptureCallback = new CAMERA_SNAP_PROC(ImageCaptureCallback); MvApi.CameraSetCallbackFunction(m_hCamera, m_CaptureCallback, m_iCaptureCallbackCtx, ref pCaptureCallOld); MvApi.CameraPlay(m_hCamera); }
private bool InitCamera() { // sdk状态 CameraSdkStatus status; // 设备信息 tSdkCameraDevInfo[] tCameraDevInfoList; // 获取所有的设备信息 status = MvApi.CameraEnumerateDevice(out tCameraDevInfoList); // 设备的数量 int iCameraCounts = (tCameraDevInfoList != null ? tCameraDevInfoList.Length : 0); // 若相机个数大于2 if (iCameraCounts >= 4)//此时iCameraCounts返回了实际连接的相机个数。如果大于2,则初始化前2个相机 { // 获取第一个相机,并将句柄传递到m_hCamera里头 if (!InitSingleCamera(tCameraDevInfoList, 0, CaptureThreadProc1, PreviewBox, BtnPlay, StateLabel, "Camera 1 init error", ref cameraObjs[0])) { return(false); } if (!InitSingleCamera(tCameraDevInfoList, 1, CaptureThreadProc2, PreviewBox2, BtnPlay2, StateLabel2, "Camera 2 init error", ref cameraObjs[1])) { return(false); } if (!InitSingleCamera(tCameraDevInfoList, 2, CaptureThreadProc3, PreviewBox3, BtnPlay3, StateLabel3, "Camera 3 init error", ref cameraObjs[2])) { return(false); } if (!InitSingleCamera(tCameraDevInfoList, 3, CaptureThreadProc4, PreviewBox4, BtnPlay4, StateLabel4, "Camera 4 init error", ref cameraObjs[3])) { return(false); } return(true); } return(false); }
private bool InitCamera() { CameraSdkStatus status; tSdkCameraDevInfo[] tCameraDevInfoList; //IntPtr ptr; //int i; #if USE_CALL_BACK CAMERA_SNAP_PROC pCaptureCallOld = null; #endif if (m_hCamera > 0) { //已经初始化过,直接返回 true return(true); } status = MvApi.CameraEnumerateDevice(out tCameraDevInfoList); if (status == CameraSdkStatus.CAMERA_STATUS_SUCCESS) { if (tCameraDevInfoList != null)//此时iCameraCounts返回了实际连接的相机个数。如果大于1,则初始化第一个相机 { status = MvApi.CameraInit(ref tCameraDevInfoList[0], -1, -1, ref m_hCamera); if (status == CameraSdkStatus.CAMERA_STATUS_SUCCESS) { //获得相机特性描述 MvApi.CameraGetCapability(m_hCamera, out tCameraCapability); 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, PreviewBox.Handle); MvApi.CameraSetDisplaySize(m_hCamera, PreviewBox.Width, PreviewBox.Height); //设置抓拍通道的分辨率。 tSdkImageResolution tResolution; tResolution.uSkipMode = 0; tResolution.uBinAverageMode = 0; tResolution.uBinSumMode = 0; tResolution.uResampleMask = 0; tResolution.iVOffsetFOV = 0; tResolution.iHOffsetFOV = 0; tResolution.iWidthFOV = tCameraCapability.sResolutionRange.iWidthMax; tResolution.iHeightFOV = tCameraCapability.sResolutionRange.iHeightMax; tResolution.iWidth = tResolution.iWidthFOV; tResolution.iHeight = tResolution.iHeightFOV; //tResolution.iIndex = 0xff;表示自定义分辨率,如果tResolution.iWidth和tResolution.iHeight //定义为0,则表示跟随预览通道的分辨率进行抓拍。抓拍通道的分辨率可以动态更改。 //本例中将抓拍分辨率固定为最大分辨率。 tResolution.iIndex = 0xff; tResolution.acDescription = new byte[32];//描述信息可以不设置 tResolution.iWidthZoomHd = 0; tResolution.iHeightZoomHd = 0; tResolution.iWidthZoomSw = 0; tResolution.iHeightZoomSw = 0; 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 m_CaptureCallback = new CAMERA_SNAP_PROC(ImageCaptureCallback); MvApi.CameraSetCallbackFunction(m_hCamera, m_CaptureCallback, m_iCaptureCallbackCtx, ref pCaptureCallOld); #else //如果需要采用多线程,使用下面的方式 m_bExitCaptureThread = false; m_tCaptureThread = new Thread(new ThreadStart(CaptureThreadProc)); m_tCaptureThread.Start(); #endif //MvApi.CameraReadSN 和 MvApi.CameraWriteSN 用于从相机中读写用户自定义的序列号或者其他数据,32个字节 //MvApi.CameraSaveUserData 和 MvApi.CameraLoadUserData用于从相机中读取自定义数据,512个字节 return(true); } else { m_hCamera = 0; StateLabel.Text = "Camera init error"; return(false); } } } return(false); }
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); }
public bool cameraopen() { return(false); #if USE_CALL_BACK CAMERA_SNAP_PROC pCaptureCallOld = null; #endif if (m_hCamera > 0) { return(true); } if ((tCameraDevInfoList != null) && (tCameraDevInfoList.Length > 0)) { //已经初始化过,直接返回 true return(true); } status = MvApi.CameraEnumerateDevice(out tCameraDevInfoList); //hfgon = true; if (status == CameraSdkStatus.CAMERA_STATUS_SUCCESS) { //hfgon = true; if (tCameraDevInfoList != null)//此时iCameraCounts返回了实际连接的相机个数。如果大于1,则初始化第一个相机 { status = MvApi.CameraInit(ref tCameraDevInfoList[0], -1, -1, ref m_hCamera); if (status == CameraSdkStatus.CAMERA_STATUS_SUCCESS) { //获得相机特性描述 MvApi.CameraGetCapability(m_hCamera, out tCameraCapability); m_ImageBuffer = Marshal.AllocHGlobal(tCameraCapability.sResolutionRange.iWidthMax * tCameraCapability.sResolutionRange.iHeightMax * 3 + 1024); m_ImageBufferSnapshot = Marshal.AllocHGlobal(tCameraCapability.sResolutionRange.iWidthMax * tCameraCapability.sResolutionRange.iHeightMax * 3 + 1024); if (tCameraCapability.sIspCapacity.bMonoSensor != 0) { // 黑白相机输出8位灰度数据 MvApi.CameraSetIspOutFormat(m_hCamera, (uint)MVSDK.emImageFormat.CAMERA_MEDIA_TYPE_MONO8); } //设置抓拍通道的分辨率。 tSdkImageResolution tResolution; tResolution.uSkipMode = 0; tResolution.uBinAverageMode = 0; tResolution.uBinSumMode = 0; tResolution.uResampleMask = 0; tResolution.iVOffsetFOV = 0; tResolution.iHOffsetFOV = 0; tResolution.iWidthFOV = tCameraCapability.sResolutionRange.iWidthMax; tResolution.iHeightFOV = tCameraCapability.sResolutionRange.iHeightMax; tResolution.iWidth = tResolution.iWidthFOV; tResolution.iHeight = tResolution.iHeightFOV; //tResolution.iIndex = 0xff;表示自定义分辨率,如果tResolution.iWidth和tResolution.iHeight //定义为0,则表示跟随预览通道的分辨率进行抓拍。抓拍通道的分辨率可以动态更改。 //本例中将抓拍分辨率固定为最大分辨率。 tResolution.iIndex = 0xff; tResolution.acDescription = new byte[32];//描述信息可以不设置 tResolution.iWidthZoomHd = 0; tResolution.iHeightZoomHd = 0; tResolution.iWidthZoomSw = 0; tResolution.iHeightZoomSw = 0; MvApi.CameraSetResolutionForSnap(m_hCamera, ref tResolution); //try { if (exposuretime > 0) MvApi.CameraSetExposureTime(m_hCamera, exposuretime); } catch { } //try { if (gain > 0) MvApi.CameraSetGain(m_hCamera, Convert.ToInt32(gain / 100), Convert.ToInt32(gain / 100), Convert.ToInt32(gain / 100)); } catch { } //try { if (contrast > 0) MvApi.CameraSetContrast(m_hCamera, contrast); } catch { } //try { if (gamma > 0) MvApi.CameraSetGamma(m_hCamera, Convert.ToInt32(gamma)); } catch { } //两种方式来获得预览图像,设置回调函数或者使用定时器或者独立线程的方式, //主动调用CameraGetImageBuffer接口来抓图。 //本例中仅演示了两种的方式,注意,两种方式也可以同时使用,但是在回调函数中, //不要使用CameraGetImageBuffer,否则会造成死锁现象。 #if USE_CALL_BACK m_CaptureCallback = new CAMERA_SNAP_PROC(ImageCaptureCallback); MvApi.CameraSetCallbackFunction(m_hCamera, m_CaptureCallback, m_iCaptureCallbackCtx, ref pCaptureCallOld); #else //如果需要采用多线程,使用下面的方式 m_bExitCaptureThread = false; //m_tCaptureThread = new Thread(new ThreadStart(CaptureThreadProc)); //m_tCaptureThread.Start(); return(true); #endif } else { m_hCamera = 0; String errstr = string.Format("相机初始化错误,错误码{0},错误原因是", status); String errstring = MvApi.CameraGetErrorString(status); // string str1 MessageBox.Show(errstr + errstring, "ERROR"); Environment.Exit(0); return(false); } } } else { MessageBox.Show("没有找到相机,如果已经接上相机,可能是权限不够,请尝试使用管理员权限运行程序。"); Environment.Exit(0); } return(false); //InitCamera(); //if (cname == "") return; //hfgon = false; //try //{ // hfg = new HFramegrabber(); // //open_framegrabber ('GigEVision', 0, 0, 0, 0, 0, 0, 'default', -1, 'default', -1, 'false', 'default', 'xiangji1', 0, -1, AcqHandle) // hfg.OpenFramegrabber("GigEVision", 0, 0, 0, 0, 0, 0, "default", -1, "default", -1, "false", "default", cname, 0, -1); // Thread.Sleep(300); // try // { // if (cname.IndexOf("MindVision") > -1) // { // HOperatorSet.SetFramegrabberParam(hfg, "AcquisitionMode", "SingleFrame"); // HOperatorSet.SetFramegrabberParam(hfg, "AcquisitionFrameRate", "Mid"); // } // try { if (exposuretime > 0) hfg.SetFramegrabberParam("ExposureTime", exposuretime); } catch { } // try { if (gain > 0) hfg.SetFramegrabberParam("Gain", gain / 100); } catch { } // try { if (contrast > 0) hfg.SetFramegrabberParam("Contrast", contrast); } catch { } // try { if (gamma > 0) hfg.SetFramegrabberParam("Gamma", gamma); } catch { } // } // catch { } // hfg.GrabImageStart(-1); // //#himg = hfg.GrabImageAsync(-1); // //#string ctype = ""; // //#himg.GetImagePointer1(out ctype, out iw, out ih); // hfgon = true; //} //catch //{ // //MessageBox.Show("相机未连接!"); //} }
private bool InitCamera() { CameraSdkStatus status; tSdkCameraDevInfo[] tCameraDevInfoList; status = MvApi.CameraEnumerateDevice(out tCameraDevInfoList); int iCameraCounts = (tCameraDevInfoList != null ? tCameraDevInfoList.Length : 0); if (iCameraCounts >= 2)//此时iCameraCounts返回了实际连接的相机个数。如果大于2,则初始化前2个相机 { if (MvApi.CameraInit(ref tCameraDevInfoList[0], -1, -1, ref m_hCamera1) == CameraSdkStatus.CAMERA_STATUS_SUCCESS) { //获得相机特性描述 MvApi.CameraGetCapability(m_hCamera1, out tCameraCapability1); m_ImageBuffer1 = Marshal.AllocHGlobal(tCameraCapability1.sResolutionRange.iWidthMax * tCameraCapability1.sResolutionRange.iHeightMax * 3 + 1024); //初始化显示模块,使用SDK内部封装好的显示接口 MvApi.CameraDisplayInit(m_hCamera1, PreviewBox.Handle); MvApi.CameraSetDisplaySize(m_hCamera1, PreviewBox.Width, PreviewBox.Height); //让SDK来根据相机的型号动态创建该相机的配置窗口。 MvApi.CameraCreateSettingPage(m_hCamera1, this.Handle, tCameraDevInfoList[0].acFriendlyName, /*SettingPageMsgCalBack*/ null, /*m_iSettingPageMsgCallbackCtx*/ (IntPtr)null, 0); MvApi.CameraPlay(m_hCamera1); BtnPlay.Text = "Pause"; //两种方式来获得预览图像,设置回调函数或者使用定时器或者独立线程的方式, //主动调用CameraGetImageBuffer接口来抓图。 //本例中仅演示了两种的方式,注意,两种方式也可以同时使用,但是在回调函数中, //不要使用CameraGetImageBuffer,否则会造成死锁现象。 m_bExitCaptureThread1 = false; m_tCaptureThread1 = new Thread(new ThreadStart(CaptureThreadProc1)); m_tCaptureThread1.Start(); } else { m_hCamera1 = 0; StateLabel.Text = "Camera 1 init error"; return(false); } if (MvApi.CameraInit(ref tCameraDevInfoList[1], -1, -1, ref m_hCamera2) == CameraSdkStatus.CAMERA_STATUS_SUCCESS) { //获得相机特性描述 MvApi.CameraGetCapability(m_hCamera2, out tCameraCapability2); m_ImageBuffer2 = Marshal.AllocHGlobal(tCameraCapability2.sResolutionRange.iWidthMax * tCameraCapability2.sResolutionRange.iHeightMax * 3 + 1024); //初始化显示模块,使用SDK内部封装好的显示接口 MvApi.CameraDisplayInit(m_hCamera2, PreviewBox2.Handle); MvApi.CameraSetDisplaySize(m_hCamera2, PreviewBox2.Width, PreviewBox2.Height); //让SDK来根据相机的型号动态创建该相机的配置窗口。 MvApi.CameraCreateSettingPage(m_hCamera2, this.Handle, tCameraDevInfoList[1].acFriendlyName, /*SettingPageMsgCalBack*/ null, /*m_iSettingPageMsgCallbackCtx*/ (IntPtr)null, 0); MvApi.CameraPlay(m_hCamera2); BtnPlay2.Text = "Pause"; m_bExitCaptureThread2 = false; m_tCaptureThread2 = new Thread(new ThreadStart(CaptureThreadProc2)); m_tCaptureThread2.Start(); } else { m_hCamera2 = 0; StateLabel.Text = "Camera 2 init error"; return(false); } return(true); } return(false); }