如何绘制矩形到OCR文本检测到

时间:2017-03-14 13:28:07

标签: xamarin ocr draw rectangles microsoft-cognitive

我在Xamarin PLC中使用Xamarin Media Plugin来选择图像,使用ProjectOxford OCR进行文本识别。 OCR API允许我知道检测到的文本行的那些参数: 宽度,高度,顶部,左侧。 那么,我如何用一个新文本(例如另一种语言)绘制一个带有检测文本的矩形?谢谢!

源代码:


       public partial class OcrRecognitionPage : ContentPage
       {
        public Exception Error
        {
            get;
            private set;
        }

        private readonly VisionServiceClient visionClient;

        int Top = 0;
        int Left = 0;
        int width = 0;
        int height = 0;

        public OcrRecognitionPage()
        {
            this.Error = null;
            InitializeComponent();
            this.visionClient = new VisionServiceClient("Enter your code");

        }

        private async Task AnalyzePictureAsync(Stream inputFile)
        {
            if (!CrossConnectivity.Current.IsConnected)
            {
                await DisplayAlert("Network error", "Please check your network connection and retry.", "OK");
                return null;
            }

            OcrResults ocrResult = await visionClient.RecognizeTextAsync(inputFile);
            return ocrResult;
        }

        private async void TakePictureButton_Clicked(object sender, EventArgs e)
        {
            try
            {
                await CrossMedia.Current.Initialize();

                if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
                {
                    await DisplayAlert("No Camera", "No camera available.", "OK");
                    return;
                }
                var file = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions
                {
                    SaveToAlbum = false,
                    Name = "test.jpg",
                    CompressionQuality = 75

                });
                if (file == null)
                    return;
                Image1.Source = ImageSource.FromStream(() => file.GetStream());
                var ocrResult = await AnalyzePictureAsync(file.GetStream());

                this.BindingContext = null;
                this.BindingContext = ocrResult;
                sourceLanguage = ocrResult.Language;

                PopulateUIWithRegions(ocrResult);
            }
            catch (Exception ex)
            {
                this.Error = ex;
            }
        }
        private void PopulateUIWithRegions(OcrResults ocrResult)
        {
                //Iterate the regions
                foreach (var region in ocrResult.Regions)
            {
                //Iterate lines per region
                foreach (var line in region.Lines)
                {
                 //    For each line, add a panel to present words horizontally
                    var lineStack = new StackLayout
                    { Orientation = StackOrientation.Horizontal };

                    //Iterate words per line and add the word
                    //to the StackLayout
                    foreach (var word in line.Words)
                    {
                        var textLabel = new Label
                        {
                            TextColor = Xamarin.Forms.Color.Black,
                            Text = word.Text,

                        };

                        lineStack.Children.Add(textLabel);
                    }

                    height = line.Rectangle.Height;
                    width = line.Rectangle.Width;
                    Left = line.Rectangle.Left;
                    Top = line.Rectangle.Top;
                    // Here i get coordinates. How can i use it to wraw rectangle onto it with another text?    
                }      
            }
        }
 

1 个答案:

答案 0 :(得分:0)

项目oxford API将单词坐标作为JSON格式列表返回。您可以使用此数据绘制框。