Example #1
0
 /// <summary>
 /// 指定设备初始化
 /// </summary>
 /// <param name="deviceInfo">设备描述信息</param>
 /// <param name="paraLoadMode">参数加载模式</param>
 /// <param name="paraLoadTeam">参数加载组</param>
 /// <param name="deviceRef">设备引用</param>
 /// <returns></returns>
 private MVSDK.CameraSdkStatus InitCamera(MVSDK.tSdkCameraDevInfo deviceInfo, MVSDK.emSdkParameterMode paraLoadMode, MVSDK.emSdkParameterTeam paraLoadTeam, ref int deviceRef)
 {
     return(MVSDK.MvApi.CameraInit(ref deviceInfo, Convert.ToInt32(paraLoadMode), Convert.ToInt32(paraLoadTeam), ref deviceRef));
 }
Example #2
0
        /// <summary>
        /// 根据相机索引获取相机
        /// [相机索引号由其上电顺序得来,非固定]
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public override bool DoGetCameraByIdx(int index)
        {
            bool rt = false;

            MVSDK.CameraSdkStatus status;
            try
            {
                if (_deviceInfoList != null && _deviceInfoList.GetLength(0) > 0)
                {
                    if (index < _deviceInfoList.GetLength(0))
                    {
                        _deviceInfo = _deviceInfoList[index];

                        if (_deviceRef != 0)
                        {
                            MVSDK.MvApi.CameraUnInit(_deviceRef);
                        }

                        status = CreateCameraGrabberByIndex(index, out _grabber);
                        rt     = (MVSDK.CameraSdkStatus.CAMERA_STATUS_SUCCESS == status) ? true : false;
                        if (!rt)
                        {
                            if (DriverExceptionDel != null)
                            {
                                DriverExceptionDel(string.Format("错误:迈德威视相机创建采集器失败!\n索引:{0}\n错误代码:{1:X8}",
                                                                 index, System.Convert.ToInt32(status)));
                            }
                        }
                        else
                        {
                            status = MVSDK.MvApi.CameraGrabber_GetCameraHandle(_grabber, out _deviceRef);
                            rt     = (MVSDK.CameraSdkStatus.CAMERA_STATUS_SUCCESS == status) ? true : false;

                            if (!rt)
                            {
                                if (DriverExceptionDel != null)
                                {
                                    DriverExceptionDel(string.Format("错误:迈德威视相机获取索引对应设备失败!\n索引:{0}\n错误代码:{1:X8}",
                                                                     index, System.Convert.ToInt32(status)));
                                }
                            }
                        }
                    }
                    else
                    {
                        if (DriverExceptionDel != null)
                        {
                            DriverExceptionDel(string.Format("错误:迈德威视相机获取索引对应设备失败!\n索引:{0}\n超出范围", index));
                        }
                    }
                }
                else
                {
                    if (DriverExceptionDel != null)
                    {
                        DriverExceptionDel(string.Format("错误:迈德威视相机获取索引对应设备失败!\n:{0}", "设备列表为空"));
                    }
                }
            }
            catch
            {
            }
            finally
            {
            }

            return(rt);
        }
Example #3
0
        /// <summary>
        /// 根据相机SN地址获取相机
        /// </summary>
        /// <param name="camSN"></param>
        /// <returns></returns>
        public override bool DoGetCameraBySN(string camSN)
        {
            bool rt = false;

            MVSDK.CameraSdkStatus status;
            try
            {
                if (_deviceInfoList != null && _deviceInfoList.GetLength(0) > 0)
                {
                    MVSDK.tSdkCameraDevInfo tmpDeviceInfo;
                    int j = 0;
                    for (int i = 0; i < _deviceInfoList.GetLength(0); i++)
                    {
                        tmpDeviceInfo = _deviceInfoList[i];
                        string s  = System.Text.Encoding.ASCII.GetString(tmpDeviceInfo.acSn);
                        string sn = s.Substring(0, 12);
                        if (sn == camSN)
                        {
                            _deviceInfo = tmpDeviceInfo;
                            break;
                        }
                        j++;
                    }

                    if (j == _deviceInfoList.GetLength(0))
                    {
                        if (DriverExceptionDel != null)
                        {
                            DriverExceptionDel(string.Format("错误:迈德威视相机获取设备失败!\n设备SN:{0}\n异常描述{1}",
                                                             camSN, "指定SN不匹配"));
                        }
                    }
                    else
                    {
                        if (_deviceRef != 0)
                        {
                            MVSDK.MvApi.CameraUnInit(_deviceRef);
                        }

                        status = CreateCameraGrabberByIndex(j, out _grabber);
                        rt     = (MVSDK.CameraSdkStatus.CAMERA_STATUS_SUCCESS == status) ? true : false;
                        if (!rt)
                        {
                            if (DriverExceptionDel != null)
                            {
                                DriverExceptionDel(string.Format("错误:迈德威视相机创建采集器失败!\n设备SN:{0}\n错误代码:{1:X8}",
                                                                 camSN, System.Convert.ToInt32(status)));
                            }
                        }
                        else
                        {
                            status = MVSDK.MvApi.CameraGrabber_GetCameraHandle(_grabber, out _deviceRef);
                            rt     = (MVSDK.CameraSdkStatus.CAMERA_STATUS_SUCCESS == status) ? true : false;

                            if (!rt)
                            {
                                if (DriverExceptionDel != null)
                                {
                                    DriverExceptionDel(string.Format("错误:迈德威视相机获取序列号对应设备失败!\n设备SN:{0}\n错误代码:{1:X8}",
                                                                     camSN, System.Convert.ToInt32(status)));
                                }
                            }
                        }
                    }
                }
                else
                {
                    if (DriverExceptionDel != null)
                    {
                        DriverExceptionDel(string.Format("错误:迈德威视相机获取索引对应设备失败!\n:{0}", "设备列表为空"));
                    }
                }
            }
            catch
            {
            }
            finally
            {
            }

            return(rt);
        }
Example #4
0
        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, PreviewBox.Handle);
                        MvApi.CameraSetDisplaySize(m_hCamera, PreviewBox.Width, PreviewBox.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;
        }
Example #5
0
        private void InitCamera(tSdkCameraDevInfo devInfo, bool isFirst)
        {
            CameraHandle m_hCamera = 0;
            if (MvApi.CameraInit(ref devInfo, -1, -1, ref m_hCamera) == CameraSdkStatus.CAMERA_STATUS_SUCCESS)
            {
                IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(new tSdkCameraCapbility()));
                MvApi.CameraGetCapability(m_hCamera, ptr);
                tSdkCameraCapbility tCameraCapability = (tSdkCameraCapbility)Marshal.PtrToStructure(ptr, typeof(tSdkCameraCapbility));
                Marshal.FreeHGlobal(ptr);
                var tmpBuffer = Marshal.AllocHGlobal(tCameraCapability.sResolutionRange.iWidthMax * tCameraCapability.sResolutionRange.iHeightMax * 3 + 1024);
                if (isFirst)
                {
                    m_ImageBuffer1 = tmpBuffer;
                }
                else
                {
                    m_ImageBuffer2 = tmpBuffer;
                }

                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 = tCameraCapability.sResolutionRange.iWidthMax;
                tResolution.iHeight = tCameraCapability.sResolutionRange.iHeightMax;
                tResolution.iIndex = 0xff;
                tResolution.acDescription = new byte[32];
                tResolution.iWidthZoomHd = 0;
                tResolution.iHeightZoomHd = 0;
                tResolution.iWidthZoomSw = 0;
                tResolution.iHeightZoomSw = 0;
                //MvApi.CameraShowSettingPage(m_hCamera, 1);
                //double exposureTime = 0;
                //MvApi.CameraGetExposureTime(m_hCamera, ref exposureTime);
                //MvApi.CameraSetExposureTime(m_hCamera, exposureTime/5);
                MvApi.CameraSetResolutionForSnap(m_hCamera, ref tResolution);
            }
            else
            {
                if (isFirst)
                    m_hCamera1 = 0;
                else
                    m_hCamera2 = 0;
                throw new Exception("初始化相机失败!");
            }
            if (isFirst)
                m_hCamera1 = m_hCamera;
            else
                m_hCamera2 = m_hCamera;
        }
Example #6
0
        public void Init()
        {
            tSdkCameraDevInfo[] tCameraDevInfoList = new tSdkCameraDevInfo[cameraCnt];
            IntPtr ptr;
            int i;

            ptr = Marshal.AllocHGlobal(Marshal.SizeOf(new tSdkCameraDevInfo()) * cameraCnt);
            int iCameraCounts = cameraCnt;//max 2 camera2

            if (MvApi.CameraEnumerateDevice(ptr, ref iCameraCounts) != CameraSdkStatus.CAMERA_STATUS_SUCCESS)
                throw new Exception("未能找到相机");
            if (iCameraCounts < cameraCnt)
                throw new Exception(string.Format("未能找到{0}台相机", iCameraCounts));
            for (i = 0; i < cameraCnt; i++)
            {
                tCameraDevInfoList[i] = (tSdkCameraDevInfo)Marshal.PtrToStructure((IntPtr)((int)ptr + i * Marshal.SizeOf(new tSdkCameraDevInfo())), typeof(tSdkCameraDevInfo));
            }

            Marshal.FreeHGlobal(ptr);
            bool containFirst = tCameraDevInfoList.Any(x => IsFirst(x.acFriendlyName));
            bool containSecond = tCameraDevInfoList.Any(x => IsSecond(x.acFriendlyName));

            if (!containFirst || (cameraCnt == 2 && !containSecond))
            {
                throw new Exception("程序未注册,请先注册!");
            }
            tSdkCameraDevInfo firstDevInfo = tCameraDevInfoList.Where(x => IsFirst(x.acFriendlyName)).First();
            InitCamera(firstDevInfo, true);

            tSdkCameraDevInfo secondDevInfo = tCameraDevInfoList.Where(x => IsSecond(x.acFriendlyName)).First();
            InitCamera(secondDevInfo, false);
        }
Example #7
0
 public static extern CameraSdkStatus CameraInit(
     ref tSdkCameraDevInfo pCameraInfo,
     int emParamLoadMode,
     int emTeam,
     ref CameraHandle pCameraHandle
     );
Example #8
0
 public static extern CameraSdkStatus CameraGetEnumInfo(
     CameraHandle hCamera,
     ref tSdkCameraDevInfo pCameraInfo
     );