using Aurora.Api; using Hermes; using System.Collections.Generic; using System.IO; namespace VisionCommon { public class VisionImageTester : VisionImageTesterBase { private static Logger _Logger = new Logger(typeof(VisionImageTester).Name); private FovInfo _FovInfo; protected CameraBase _CamDev; private object _TestImage = null; private object _CroppedImage = null; public VisionImageTester(FovInfo fovInfo) { _FovInfo = fovInfo; var camRecipe = VisionRecipeManager.GetCamRecipe(_FovInfo); _CamDev = VisionEnv.AuroraEnv.DeviceManager[camRecipe.CamId.ToString()] as CameraBase; ImagePaths = new List(); PatternResults = new List(); } public override int SetTestImagePath(List imagePath) { ImagePaths.Clear(); PatternResults.Clear(); Count = 0; ImagePaths = imagePath; return E1.OK; } public override int GetCurrentImage(ICameraDisplay cameraDisplay, ref object image) { FitAndClearImage(cameraDisplay); var path = ImagePaths[Count++]; var fovRecipe = VisionRecipeManager.GetFovRecipe(_FovInfo); var camConfig = VisionEnv.AuroraEnv.DeviceManager.GetDeviceConfig(fovRecipe.CamRecipe.CamId.ToString()) as CamConfig; if (File.Exists(path)) { _TestImage = IpAdapter.Instance.IpEnvironment.LoadImage(path, camConfig); if (_TestImage != null) { CropImage(); SetDisplayImage(cameraDisplay); image = _CroppedImage; return E1.OK; } } return E1.GENERIC; } private void FitAndClearImage(ICameraDisplay cameraDisplay) { if (cameraDisplay == null) { return; } cameraDisplay.FitImage(_CamDev.GetImageSize()); cameraDisplay.ClearMeasureGraphics(); //Application.Current.Dispatcher.BeginInvoke( // new System.Action(() => { // milCameraDisplay.FitImage(_CamDev.GetImageSize()); // milCameraDisplay.ClearMeasureGraphics(); // })); } private void CropImage() { var cropRoi = GetCropRoi(); if(_TestImage != null) { _CroppedImage = IpAdapter.Instance.IpEnvironment.CropImage(_TestImage, cropRoi); } } private ROI GetCropRoi() { var cropRoi = new ROI(); var fovRecipe = VisionRecipeManager.GetFovRecipe(_FovInfo); var camConfig = VisionEnv.AuroraEnv.DeviceManager.GetDeviceConfig(fovRecipe.CamRecipe.CamId.ToString()) as CamConfig; if (IpAdapter.Instance.IpEnvironment.IsHeightLongerThanWidth(camConfig)) { cropRoi.CenterX = camConfig.ImageSize.Height * 0.5; cropRoi.CenterY = camConfig.ImageSize.Width * 0.5; cropRoi.Width = (int)(camConfig.ImageSize.Height * camConfig.ImageSizeRatioY); cropRoi.Height = (int)(camConfig.ImageSize.Width * camConfig.ImageSizeRatioX); } else { cropRoi.CenterX = camConfig.ImageSize.Width * 0.5; cropRoi.CenterY = camConfig.ImageSize.Height * 0.5; cropRoi.Width = (int)(camConfig.ImageSize.Width * camConfig.ImageSizeRatioX); cropRoi.Height = (int)(camConfig.ImageSize.Height * camConfig.ImageSizeRatioY); } return cropRoi; } private void SetDisplayImage(ICameraDisplay cameraDisplay) { if(cameraDisplay == null) { return; } else { cameraDisplay.SetImage(_CroppedImage); } } } }