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() { 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("相机未连接!"); //} }
public static extern CameraSdkStatus CameraSetCallbackFunction( CameraHandle hCamera, CAMERA_SNAP_PROC pCallBack, IntPtr pContext, ref CAMERA_SNAP_PROC pCallbackOld );