/// <summary> /// /// </summary> /// <param name="imageUrl"></param> /// <param name="language">en, ja, zh</param> /// <returns></returns> public static async Task <OcrResult.Rootobject> DoOCR(string imageUrl, string language) { try { using (HttpClient hc = new HttpClient()) { ByteArrayContent content = CreateHeader(hc, imageUrl); var uri = OcrEndPointV1 + language; HttpResponseMessage resp = await hc.PostAsync(uri, content); string result = string.Empty; if (resp.StatusCode == System.Net.HttpStatusCode.OK) { string json = await resp.Content.ReadAsStringAsync(); Debug.WriteLine(json); OcrResult.Rootobject ro = Newtonsoft.Json.JsonConvert.DeserializeObject <OcrResult.Rootobject>(json); return(ro); } } return(null); } catch (Exception ex) { Debug.Write(ex.Message); return(null); } }
private async void btn_OCR_Click(object sender, RoutedEventArgs e) { this.Version = GetVersion(); this.Language = GetLanguage(); if (Version == "OCR") { ocrResult = await CognitiveServiceAgent.DoOCR(this.tb_Url.Text, Language); foreach (OcrResult.Region region in ocrResult.regions) { foreach (OcrResult.Line line in region.lines) { if (line.Convert()) { Rectangle rect = new Rectangle() { Margin = new Thickness(line.BB[0], line.BB[1], 0, 0), Width = line.BB[2], Height = line.BB[3], Stroke = Brushes.Red, //Fill =Brushes.White }; this.canvas_1.Children.Add(rect); } } } } else { newOcrResult = await CognitiveServiceAgent.DoRecognizeText(this.tb_Url.Text); // 1 - erase the original text foreach (NewOcrResult.Line line in newOcrResult.recognitionResult.lines) { Polygon p = new Polygon(); PointCollection pc = new PointCollection(); pc.Add(new Point(line.boundingBox[0], line.boundingBox[1])); pc.Add(new Point(line.boundingBox[2], line.boundingBox[3])); pc.Add(new Point(line.boundingBox[4], line.boundingBox[5])); pc.Add(new Point(line.boundingBox[6], line.boundingBox[7])); p.Points = pc; p.Stroke = Brushes.Red; this.canvas_1.Children.Add(p); } } }