Beispiel #1
0
        public void Setup(string fileName)
        {
            Repository = new Repository();
            Repository.Load();
            _window = Substitute.For <IEmulatorWindow>();
            var settings      = Substitute.For <ISettings>();
            var recognition   = new TextRecognition();
            var loggerFactory = Substitute.For <ILoggerFactory>();

            _videoProvider = Substitute.For <IVideoProvider>();
            Game           = new GameImpl(_window, _videoProvider, Repository, loggerFactory, recognition, settings);
#if DEBUG
            Settings.SaveImages    = true;
            Settings.SaveRawImages = true;
#endif

            using var bitmap = new Bitmap(fileName);
            _window.Width.Returns(bitmap.Width);
            _window.Height.Returns(bitmap.Height);
            _window.GrabScreen(Arg.Any <int>(), Arg.Any <int>(), Arg.Any <int>(), Arg.Any <int>())
            .Returns(info =>
            {
                using var bitmapLocal = new Bitmap(fileName);
                return(bitmapLocal.Crop(info.ArgAt <int>(0), info.ArgAt <int>(1), info.ArgAt <int>(2), info.ArgAt <int>(3)));
            });
            _videoProvider.GetCurrentFrame().Returns(info => bitmap);
        }
Beispiel #2
0
        public void OnError(TaskError taskError)
        {
            if (_settings.RestartOnError)
            {
                RestartGame();
            }

            switch (taskError)
            {
            case ElementNotFoundError elementNotFoundError:
            {
                using var screen    = _videoProvider.GetCurrentFrame().Crop(0, 0, _window.Width, _window.Height);
                using var screenMat = screen.ToMat();

                var missingElement = elementNotFoundError.MissingElement;
                var x = (int)(missingElement.X.GetValueOrDefault() * _window.Width);
                var y = (int)(missingElement.Y.GetValueOrDefault() * _window.Height);
                var w = (int)(missingElement.W.GetValueOrDefault() * _window.Width);
                var h = (int)(missingElement.H.GetValueOrDefault() * _window.Height);


                var timeStamp      = $"{DateTime.Now:yyyyMMddTHHmmss}";
                var screenFileName = Path.Combine("logs", $"ElementNotFound {timeStamp} - Screen.bmp");

                if (missingElement.XOffset.HasValue || missingElement.YOffset.HasValue)
                {
                    DrawElementGrid(screenMat, missingElement);
                }
                else
                {
                    DrawElement(screenMat, missingElement);
                }


                using var cropped = screen.Crop(x, y, w, h);


                if (missingElement.Image == null)
                {
                    var foundText = _recognition.GetText(cropped, missingElement);
                    var text      = $"Found Text: {foundText}";
                    var size      = GetTextSize(text);
                    DrawText(screenMat, x, y + size.Height, text);
                }
                else
                {
                    var missingElementFileName =
                        Path.Combine("logs", $"ElementNotFound {timeStamp} - MissingElement.bmp");
                    cropped.Save(missingElementFileName);
                }
#if DEBUG
                //Cv2.ImShow("Error", screenMat);
                //Cv2.WaitKey();
#endif
                screenMat.SaveImage(screenFileName);
            }
            break;
            }

            throw new Exception("Cancelling task by throwing exception?");


            void DrawElementGrid(Mat screenMat, UiElement element)
            {
                var xCount = element.XOffset.HasValue ? Math.Ceiling((1f - element.X.GetValueOrDefault()) / element.XOffset.Value) : 1;
                var yCount = element.YOffset.HasValue ? Math.Ceiling((1f - element.Y.GetValueOrDefault()) / element.YOffset.Value) : 1;
                int y;

                for (y = 0; y < yCount; y++)
                {
                    int x;
                    for (x = 0; x < xCount; x++)
                    {
                        var dynUiElement = _repository[element.Id, x, y];
                        DrawElement(screenMat, dynUiElement);
                    }
                }
            }

            OpenCvSharp.Size GetTextSize(string text)
            {
                var fontScale = 0.75;
                var font      = HersheyFonts.HersheyComplex;
                var size      = Cv2.GetTextSize(text, font, fontScale, 1, out var _);

                return(size);
            }

            void DrawElement(Mat screenMat, UiElement uiElement)
            {
                var x = (int)(uiElement.X.GetValueOrDefault() * _window.Width);
                var y = (int)(uiElement.Y.GetValueOrDefault() * _window.Height);
                var w = (int)(uiElement.W.GetValueOrDefault() * _window.Width);
                var h = (int)(uiElement.H.GetValueOrDefault() * _window.Height);

                Cv2.Rectangle(screenMat, new Rect(x, y, w, h), new Scalar(0, 0, 255));
            }

            void DrawText(Mat screenMat, int x, int y, string text)
            {
                var fontScale = 0.75;
                var font      = HersheyFonts.HersheyComplex;
                var size      = Cv2.GetTextSize(text, font, fontScale, 1, out var _);
                var textX     = x;

                if (textX + size.Width > _window.Width)
                {
                    // Move text so it fits onto the screenshot.
                    textX -= (textX + size.Width) - _window.Width;
                }
                Cv2.PutText(screenMat, text, new OpenCvSharp.Point(textX, y), font, fontScale, new Scalar(0, 0, 255));
            }
        }
Beispiel #3
0
 public static bool AutoFocus(IVideoProvider provider, float maxPos, float minPos, float step = 0.2f)
 {
     try
     {
         if (provider == null)
         {
             return(false);
         }
         bool isCapture = provider.IsRunning;
         Dictionary <double, float> pos = new Dictionary <double, float>();
         Z_Axis.AxisJogAbsWork(maxPos);
         if (!WaitAxisMoveDone())
         {
             return(false);
         }
         if (!isCapture)
         {
             provider.StartCapture();
         }
         int    stepTick  = (int)((maxPos - minPos) / step);
         double scalValue = 0;
         int    tick      = 0;
         for (int i = 0; i < stepTick; i++)
         {
             Z_Axis.AxisJogIncWork(-step, true);
             Thread.Sleep(20);
             Bitmap map = provider.GetCurrentFrame();
             double qua = map.GetLinearQuality();
             if (!pos.ContainsKey(qua))
             {
                 if (qua < scalValue)
                 {
                     if (tick++ > 3)
                     {
                         break;
                     }
                 }
                 else
                 {
                     tick = 0;
                 }
                 scalValue = qua;
                 pos.Add(qua, z_Axis.RealPos);
             }
         }
         double maxKey = pos.Max(r => r.Key);
         float  desPos = pos[maxKey];
         Z_Axis.AxisJogAbsWork(desPos);
         if (!isCapture)
         {
             provider.StopCapture();
         }
         pos.Clear();
         GC.Collect();
         if (step / 20 < 0.005)
         {
             return(true);
         }
         else
         {
             return(AutoFocus(provider, desPos + step, desPos - step, step / 10));
         }
     }
     catch
     {
         return(false);
     }
 }