ARKBreedingStats.ArkOCR.doOCR C# (CSharp) Method

doOCR() public method

public doOCR ( string &OCRText, string &dinoName, string useImageFilePath = "", bool changeForegroundWindow = true ) : float[]
OCRText string
dinoName string
useImageFilePath string
changeForegroundWindow bool
return float[]
        public float[] doOCR(out string OCRText, out string dinoName, string useImageFilePath = "", bool changeForegroundWindow = true)
        {
            string finishedText = "";
            dinoName = "";
            float[] finalValues = new float[1] { 0 };

            Bitmap screenshotbmp = null;// = (Bitmap)Bitmap.FromFile(@"D:\ScreenshotsArk\Clipboard12.png");
            Bitmap testbmp;

            debugPanel.Controls.Clear();

            if (System.IO.File.Exists(useImageFilePath))
            {
                screenshotbmp = (Bitmap)Bitmap.FromFile(useImageFilePath);
            }
            else
            {
                // grab screenshot from ark
                screenshotbmp = Win32Stuff.GetSreenshotOfProcess("ShooterGame");
                //screenshotbmp.Save(@"D:\ScreenshotsArk\Clipboard02.png");
            }
            if (screenshotbmp == null)
            {
                OCRText = "Error: no image for OCR. Is ARK running?";
                return finalValues;
            }
            if (!calibrate(screenshotbmp))
            {
                OCRText = "Error while calibrating: probably game-resolution is not supported by this OCR";
                return finalValues;
            }
            finalValues = new float[statPositions.Count];

            AddBitmapToDebug(screenshotbmp);
            if (changeForegroundWindow)
                Win32Stuff.SetForegroundWindow(Application.OpenForms[0].Handle);

            int count = -1;
            foreach (string statName in statPositions.Keys)
            {
                count++;
                testbmp = SubImage(screenshotbmp, statPositions[statName].X, statPositions[statName].Y, 500, 30); // 300 is enough, except for the name
                AddBitmapToDebug(testbmp);

                bool onlyNumbers = coordsAfterDot; // hack for 1050, position just for coordinates

                string statOCR = "";

                if (statName == "NameAndLevel")
                    statOCR = readImage(currentResolution, testbmp, true, false);
                else if (statName == "Imprinting")
                    statOCR = readImage(currentResolution + 1, testbmp, true, true, false); // imprinting is written in lower letters
                else
                    statOCR = readImage(currentResolution, testbmp, true, onlyNumbers);

                if (statOCR == "" &&
                    (statName == "Oxygen" || statName == "Imprinting"))
                    continue; // these can be missing, it's fine

                lastLetterPositions[statName] = new Point(statPositions[statName].X + lastLetterPosition(removePixelsUnderThreshold(GetGreyScale(testbmp), whiteThreshold)), statPositions[statName].Y);

                finishedText += "\r\n" + statName + ": " + statOCR;

                // parse the OCR String

                Regex r;
                if (onlyNumbers)
                    r = new Regex(@"((\d+[\.,']?\d?\d?)%?\/)?(\d+[\.,']?\d?\d?)%?"); //new Regex(@"((\d*[\.,']?\d?\d?)\/)?(\d*[\.,']?\d?\d?)");
                else
                    r = new Regex(@"([a-zA-Z]*)[:;]((\d*[\.,']?\d?\d?)\/)?(\d*[\.,']?\d?\d?)");
                if (statName == "NameAndLevel")
                    r = new Regex(@"(.*)-?Lv[liI](\d*)Eq");

                MatchCollection mc = r.Matches(statOCR);

                if (mc.Count == 0)
                {
                    if (statName == "NameAndLevel")
                        continue;
                    else
                    {
                        OCRText = finishedText + "error reading stat " + statName;
                        return finalValues;
                    }
                }

                string testStatName = mc[0].Groups[1].Value;
                float v = 0;
                float.TryParse(mc[0].Groups[mc[0].Groups.Count - 1].Value.Replace('\'', '.').Replace(',', '.').Replace('O', '0'), System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.GetCultureInfo("en-US"), out v); // common substitutions: comma and apostrophe to dot,

                if (statName == "NameAndLevel")
                {
                    if (testStatName.EndsWith("-"))
                        dinoName = testStatName.Substring(0, testStatName.Length - 1);
                    else
                        dinoName = testStatName;
                }
                // TODO: test here that the read stat name corresponds to the stat supposed to be read
                finalValues[count] = v;
            }

            OCRText = finishedText;

            return finalValues;

            Bitmap grab = Win32Stuff.GetSreenshotOfProcess("ShooterGame");
            AddBitmapToDebug(grab);

            //grab.Save("E:\\Temp\\Calibration8.png", ImageFormat.Png);
            if (changeForegroundWindow)
                Win32Stuff.SetForegroundWindow(Application.OpenForms[0].Handle);
        }