/// <summary> /// Funciton to browse for an image. Text in images will be recognized by running a text analysis API call /// </summary> /// <param name="obj"></param> private async void BrowseAndAnalyze(object obj) { var openDialog = new Microsoft.Win32.OpenFileDialog(); openDialog.Filter = "JPEG Image(*.jpg)|*.jpg"; bool?result = openDialog.ShowDialog(); if (!(bool)result) { return; } string filePath = openDialog.FileName; Uri fileUri = new Uri(filePath); BitmapImage image = new BitmapImage(fileUri); image.CacheOption = BitmapCacheOption.None; image.UriSource = fileUri; ImageSource = image; try { using (Stream fileStream = File.OpenRead(filePath)) { OcrResults analysisResult = await _visionClient.RecognizeTextAsync(fileStream); if (analysisResult != null) { OcrResult = PrintOcrResult(analysisResult); } } } catch (Exception ex) { Debug.WriteLine($"Failed to analyze image: {ex.Message}"); } }
public async Task <string> RecognizeTextAsync(IRandomAccessStream stream, string language = "unk", bool detectOrientation = true) { var response = await _client.RecognizeTextAsync(stream.AsStreamForRead(), language, detectOrientation); return(response.Regions.SelectMany(r => r.Lines).SelectMany(l => l.Words).Select(w => w.Text).StringJoin(" ")); }
public static string RecognizeText(string imageUrl) { var ocrData = visionClient.RecognizeTextAsync(imageUrl).GetAwaiter().GetResult(); return(JsonConvert.SerializeObject(ocrData)); }