コード例 #1
0
        public static async Task <NewOcrResult.Rootobject> DoRecognizeText(string imageUrl)
        {
            try
            {
                using (HttpClient hc = new HttpClient())
                {
                    ByteArrayContent    content = CreateHeader(hc, imageUrl);
                    HttpResponseMessage resp    = await hc.PostAsync(OcrEndPointV2, content);

                    string json = string.Empty;
                    if (resp.StatusCode == System.Net.HttpStatusCode.Accepted)
                    {
                        var headers = resp.Headers;
                        foreach (var kv in headers)
                        {
                            if (kv.Key == "Operation-Location")
                            {
                                string url = kv.Value.First();
                                while (true)
                                {
                                    // check status every 200ms
                                    await Task.Delay(200);

                                    json = await CheckStatus(url);

                                    if (!string.IsNullOrEmpty(json))
                                    {
                                        break;
                                    }
                                }
                                break;
                            }
                        }
                    }
                    if (string.IsNullOrEmpty(json))
                    {
                        return(null);
                    }
                    else
                    {
                        NewOcrResult.Rootobject ro = Newtonsoft.Json.JsonConvert.DeserializeObject <NewOcrResult.Rootobject>(json);
                        return(ro);
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return(null);
            }
        }
コード例 #2
0
        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);
                }
            }
        }