Esempio n. 1
0
        public void SetCameraConfigFile(string file)
        {
            CameraConfigFile = file;

            if (!string.IsNullOrEmpty(file))
            {
                //获取相机配置参数
                string configFile = $"{System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase}/VisionPlatform/Camera/CameraConfig/{CameraSerial}/ConfigFile/{CameraConfigFile}";

                if (File.Exists(configFile))
                {
                    cameraConfigParam = JsonSerialization.DeserializeObjectFromFile <CameraConfigParam>(configFile);

                    //还原相机配置
                    try
                    {
                        CameraFactory.ConfigurateCamera(CameraSerial, cameraConfigParam);
                    }
                    catch (Exception)
                    {
                    }
                }
                else
                {
                    cameraConfigParam = CameraFactory.GetCameraConfigration(CameraSerial);
                    JsonSerialization.SerializeObjectToFile(cameraConfigParam, configFile);
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 设置相机
        /// </summary>
        /// <param name="cameraSerial"></param>
        /// <exception cref="InvalidOperationException">
        /// 打开相机失败
        /// </exception>
        public void SetCamera(string cameraSerial)
        {
            //若相机无效,则不报异常
            try
            {
                if (VisionFrame.IsEnableCamera)
                {
                    CameraSerial = cameraSerial;

                    Camera = CameraFactory.GetCamera(cameraSerial);

                    if (CameraFactory.DefaultCameraSdkType != ECameraSdkType.VirtualCamera)
                    {
                        //配置相机参数
                        if (CameraFactory.DefaultCameraSdkType != ECameraSdkType.VirtualCamera)
                        {
                            if (string.IsNullOrEmpty(CameraConfigFile))
                            {
                                CameraConfigFile = $"default.json";
                            }
                            SetCameraConfigFile(CameraConfigFile);
                        }

                        //if (string.IsNullOrEmpty(CalibrationFile))
                        //{
                        //    CalibrationFile = $"default.json";
                        //}
                        //配置标定信息
                        SetCameraCalibrationFile(CalibrationFile);
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
Esempio n. 3
0
        public RunStatus Execute(int timeout, out string visionResult)
        {
            bool isTimeout = false;
            var  runStatus = new RunStatus();

            visionResult = "";

            try
            {
                //参数校验
                if (VisionFrame == null)
                {
                    throw new NullReferenceException("VisionFrame cannot be null");
                }

                if (!IsInit)
                {
                    throw new Exception($"scene is uninitialized({nameof(IsVisionFrameInit)}:{IsVisionFrameInit} {nameof(IsCameraInit)}:{IsCameraInit})");
                }

                ItemCollection outputs;

                //开始计时
                totalProcessStopwatch.Restart();

                //如果使能相机,则调用相机采集
                if (VisionFrame.IsEnableCamera && IsCameraInit)
                {
                    lock (threadLock)
                    {
                        isVisionProcessed = false;
                    }

                    //注册相机采集完成事件
                    Camera.NewImageEvent -= Camera_NewImageEvent;
                    Camera.NewImageEvent += Camera_NewImageEvent;

                    //配置相机参数
                    CameraFactory.ConfigurateCamera(CameraSerial, cameraConfigParam);

                    //触发拍照
                    Camera.TriggerSoftware();

                    //阻塞等待视觉处理完成
                    grapTimeoutStopwatch.Restart();
                    while (true)
                    {
                        lock (threadLock)
                        {
                            if (isVisionProcessed)
                            {
                                grapTimeoutStopwatch.Stop();
                                break;
                            }
                        }

                        if ((timeout > 0) && (grapTimeoutStopwatch.Elapsed.TotalMilliseconds > timeout))
                        {
                            isTimeout = true;
                            grapTimeoutStopwatch.Stop();
                            throw new TimeoutException("grab image timeout");
                        }

                        System.Threading.Thread.Sleep(2);
                    }

                    //执行视觉框架
                    VisionFrame.ExecuteByImageInfo(imageInfo, out outputs);

                    //结果拼接
                    visionResult = ConvertItemCollectionToString(outputs, MainSeparatorChar, SubSeparatorChar);
                }
                else
                {
                    //执行视觉框架
                    VisionFrame.Execute(timeout, out outputs);

                    //结果拼接
                    visionResult = ConvertItemCollectionToString(outputs, MainSeparatorChar, SubSeparatorChar);
                }
            }
            catch (Exception ex)
            {
                return(new RunStatus(0, EResult.Error, ex.Message));
            }
            finally
            {
                //停止计时
                totalProcessStopwatch.Stop();
                VisionFrame.RunStatus.TotalTime = totalProcessStopwatch.Elapsed.TotalMilliseconds;

                if (!isTimeout)
                {
                    //释放图像资源,否则可能会导致资源泄露
                    imageInfo.DisposeImageIntPtr?.Invoke(imageInfo.ImagePtr);
                }
            }

            return(VisionFrame.RunStatus);
        }