private async Task RecognizeImageAsync() { while (_isRunning) { await Task.Delay(100); } if (this.Image1.Source == null) { return; } // 表示している画像を SoftwareBitmap として取得 UwpSoftwareBitmap bitmap = await UwpSoftwareBitmapHelper.ConvertFrom(this.Image1.Source as BitmapFrame); if (bitmap == null) { return; } // 認識言語を変えて再認識させたいときのために保持 if (_lastRecognizedBitmap != bitmap) { _lastRecognizedBitmap?.Dispose(); _lastRecognizedBitmap = bitmap; } // SoftwareBitmap を OCR に掛ける await RecognizeBitmapAsync(bitmap); }
public async Task RecognizeAsync(BitmapFrame bitmapFrame) { if (!CanExecute) { return; } ClearResults(); // SoftwareBitmapを取得 UwpSoftwareBitmap bitmap = await UwpSoftwareBitmapHelper.ConvertFrom(bitmapFrame).ConfigureAwait(true); if (bitmap == null) { return; } // OCR実行 UwpOcrResult ocrResult = await engine.RecognizeAsync(bitmap); bitmap.Dispose(); // Angle ocrAngle = ocrResult.TextAngle ?? 0.0; // Line foreach (var ocrLine in ocrResult.Lines) { WinOcrResult result = new WinOcrResult(); string words = ""; double left = ocrLine.Words[0].BoundingRect.Left; double right = ocrLine.Words[0].BoundingRect.Right; double top = ocrLine.Words[0].BoundingRect.Top; double bottom = ocrLine.Words[0].BoundingRect.Bottom; foreach (var word in ocrLine.Words) { words += word.Text; if (word.BoundingRect.Left < left) { left = word.BoundingRect.Left; } if (right < word.BoundingRect.Right) { right = word.BoundingRect.Right; } if (word.BoundingRect.Top < top) { top = word.BoundingRect.Top; } if (bottom < word.BoundingRect.Bottom) { bottom = word.BoundingRect.Bottom; } } result.Words = words; result.RectLeft = left; result.RectTop = top; result.RectWidth = right - left; result.RectHeight = bottom - top; this.ocrResults.Add(result); } }