public async Task LookForBpScreen(CancellationToken cancellationToken) { try { OcrAsyncChecker.CheckThread(OcrAsyncChecker.LookForBpScreenAsyncChecker); var finder = new Finder(); while (!cancellationToken.IsCancellationRequested) { if (SuspendScanning) { await Task.Delay(1000, cancellationToken).ConfigureAwait(false); continue; } lock (ImageProcessingHelper.GDILock) { var screenPath = finder.CaptureScreen(); var isBp = ImageProcessingHelper.CheckIfInBp(screenPath); if (isBp) { return; } } await Task.Delay(1000, cancellationToken).ConfigureAwait(false); } } finally { OcrAsyncChecker.CleanThread(OcrAsyncChecker.LookForBpScreenAsyncChecker); } }
public void AdjustPlaceHolderPosition() { var finder = new Finder(); FilePath screenPath = finder.CaptureScreen(); var points = ImageProcessingHelper.LookForPoints(screenPath); var height = ScreenUtil.GetScreenResolution().Height; App.AppSetting.Position.Left.HeroName1 = new Point(points[0] + (int)(0.0017 * height), points[1] + (int)(0.0035 * ScreenUtil.GetScreenResolution().Height)); App.AppSetting.Position.Right.HeroName1 = new Point(points[2] + (int)(0.0035 * height), points[3] + (int)(0.0045 * ScreenUtil.GetScreenResolution().Height)); File.WriteAllText(@".\coord.txt", App.AppSetting.Position.Left.HeroName1.X + @" " + App.AppSetting.Position.Left.HeroName1.Y + " " + App.AppSetting.Position.Right.HeroName1.X + " " + App.AppSetting.Position.Right.HeroName1.Y); }
public async Task ScanLabelAsync(List <int> ids, BpViewModel bpViewModel, ScanSide side, CancellationToken cancellationToken) { try { OcrAsyncChecker.CheckThread(OcrAsyncChecker.ScanLabelAsyncChecker); var finder = new Finder(); float rotation = side == ScanSide.Left ? (float)29.7 : (float)-29.7; var bpVm = new Dictionary <int, HeroSelectorViewModel>(); var checkedDic = new Dictionary <int, bool>(); foreach (var id in ids) { var vm = bpViewModel.HeroSelectorViewModels.First(v => v.Id == id); if (vm == null) { return; } bpVm[id] = vm; } for (var i = 0; i < ids.Count; ++i) { checkedDic[i] = false; } var logUtil = new LogUtil(@".\logPick" + string.Join("&", ids) + ".txt"); logUtil.Log("PickChecked"); int bpScreenFail = 0; bool warned = false; bool awaitFlag = false; while (checkedDic.Any(c => !c.Value)) { var sb = new StringBuilder(); var sbConfirm = new StringBuilder(); var i = checkedDic.FirstOrDefault(l => !l.Value).Key; if (awaitFlag) { await Task.Delay(1000, cancellationToken).ConfigureAwait(false); awaitFlag = false; } if (checkedDic.ContainsKey(i) && checkedDic[i]) { continue; } if (bpVm[ids[i]].Selected) { checkedDic[i] = true; continue; } if (cancellationToken.IsCancellationRequested) { return; } if (SuspendScanning) { await Task.Delay(1000).ConfigureAwait(false); continue; } logUtil.Log("Starting detect quit"); var processedResult = ProcessedResult.Fail; int realPositionId = ids[i]; // first attempt lock (ImageProcessingHelper.GDILock) { using (var bitmap = new ImageUtils().CaptureScreen()) { if (!warned && bpViewModel.BpStatus.CurrentStep < 11 && StageFinder.ProcessStageInfo(bitmap).Step == -1) { bpScreenFail++; if (bpScreenFail == 5) { warned = true; bpViewModel.WarnNotInBp(); } } else { bpScreenFail = 0; } if (CheckIsOverlap(side, logUtil, bitmap)) { awaitFlag = true; continue; } sb.Clear(); sbConfirm.Clear(); foreach (var scanSideId in IdList[side].Where(id => !ProcessedPositions.ContainsKey(id))) { using (var bitMap = finder.AddNewTemplateBitmap(scanSideId, bitmap)) { logUtil.Log("Capture Complete " + scanSideId); processedResult = _recognizer.Recognize(bitMap, rotation, sb, _trustableThreashold); if (processedResult != ProcessedResult.Fail) { realPositionId = scanSideId; break; } } } } } logUtil.Log("Checked " + ids[i] + " (" + sb + ")"); if (sb.ToString() == _recognizer.PickingText || string.IsNullOrEmpty(sb.ToString()) || processedResult == ProcessedResult.Fail) { continue; } if (cancellationToken.IsCancellationRequested) { return; } if (SuspendScanning) { logUtil.Log("SuspendScanning delay 1000 " + ids[i]); await Task.Delay(1000).ConfigureAwait(false); continue; } // second attempt FilePath tempscreenshotPath = null; if (processedResult != ProcessedResult.Trustable) { logUtil.Log("Delay 500 " + ids[i]); await Task.Delay(500).ConfigureAwait(false); if (cancellationToken.IsCancellationRequested) { return; } if (SuspendScanning) { await Task.Delay(1000).ConfigureAwait(false); continue; } lock (ImageProcessingHelper.GDILock) { tempscreenshotPath = finder.CaptureScreen(); using (var bitMap = finder.AddNewTemplateBitmap(realPositionId, tempscreenshotPath)) { logUtil.Log("Capture Complete " + realPositionId); _recognizer.Recognize(bitMap, rotation, sbConfirm, _trustableThreashold); } } } logUtil.Log("Second checked " + ids[i] + " (" + sbConfirm.ToString() + ")"); if (SuspendScanning) { logUtil.Log("SuspendScanning delay 1000 " + ids[i]); await Task.Delay(1000).ConfigureAwait(false); continue; } if (bpVm[ids[i]].Selected) { logUtil.Log("Vm already selected delay 1000 " + ids[i]); checkedDic[i] = true; continue; } if (processedResult == ProcessedResult.Trustable) { bpScreenFail = 0; } if ((processedResult == ProcessedResult.Trustable || sb.ToString() == sbConfirm.ToString()) && !cancellationToken.IsCancellationRequested && CheckIfInFocus()) { bpScreenFail = 0; tempscreenshotPath?.DeleteIfExists(); var text = sb.ToString(); var index = ids[checkedDic.First(l => !l.Value).Key]; if (ids.Contains(realPositionId) && !checkedDic[ids.IndexOf(realPositionId)]) { index = realPositionId; } Execute.OnUIThread(() => { bpViewModel.ShowHeroSelector(index, text); }); ProcessedPositions[realPositionId] = true; checkedDic[ids.IndexOf(index)] = true; logUtil.Log("Confirmed " + index + " " + text); } } logUtil.Flush(); } catch (Exception e) { File.WriteAllText(@".\error" + string.Join("&", ids) + ".txt", e.Message + e.StackTrace + e); throw; } finally { OcrAsyncChecker.CleanThread(OcrAsyncChecker.ScanLabelAsyncChecker); } }
public async Task <string> LookForMap(CancellationToken cancellationToken) { try { OcrAsyncChecker.CheckThread(OcrAsyncChecker.LookForMapAsyncChecker); var finder = new Finder(); var sb = new StringBuilder(); var logUtil = new LogUtil(@".\logLookForMap.txt"); logUtil.Log("Started"); int attempts = 0; while (!cancellationToken.IsCancellationRequested && IsInitialized) { sb.Clear(); if (SuspendScanning) { await Task.Delay(1000, cancellationToken).ConfigureAwait(false); continue; } bool fullyTrustable = false; lock (ImageProcessingHelper.GDILock) { var screenPath = finder.CaptureScreen(); fullyTrustable = _recognizer.ProcessMap(finder.CaptureMapArea(screenPath), sb); } if (!string.IsNullOrEmpty(sb.ToString())) { if (fullyTrustable) { break; } await Task.Delay(500, cancellationToken).ConfigureAwait(false); var sbConfirm = new StringBuilder(); lock (ImageProcessingHelper.GDILock) { var screenPath = finder.CaptureScreen(); _recognizer.ProcessMap(finder.CaptureMapArea(screenPath), sbConfirm); if (sbConfirm.ToString() == sb.ToString()) { break; } } } attempts++; if (attempts == 5) { return(string.Empty); } await Task.Delay(1000, cancellationToken).ConfigureAwait(false); } logUtil.Flush(); return(sb.ToString()); } finally { OcrAsyncChecker.CleanThread(OcrAsyncChecker.LookForMapAsyncChecker); } }