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); } }
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]); } } }
/// <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); }
public Form1() { InitializeComponent(); m_HDevelopExport = new HDevelopExport(); m_HDevelopExport.InitHalcon(hWindowControl1.HalconWindow); m_FrameCallback = new pfnCameraGrabberFrameCallback(CameraGrabberFrameCallback); m_SaveImageComplete = new pfnCameraGrabberSaveImageComplete(CameraGrabberSaveImageComplete); if (MvApi.CameraGrabber_CreateFromDevicePage(out m_Grabber) == CameraSdkStatus.CAMERA_STATUS_SUCCESS) { 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_SetSaveImageCompleteCallback(m_Grabber, m_SaveImageComplete, IntPtr.Zero); MvApi.CameraGrabber_SetHWnd(m_Grabber, this.DispWnd.Handle); MvApi.CameraGrabber_StartLive(m_Grabber); } }
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); }
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); }