Esempio n. 1
0
        private async void InkRecoTimer_Tick(object sender, object e)
        {
            inkRecoTimer.Stop();

            int    index  = currentCanvas.Name.IndexOf("Canvas");
            string prefix = currentCanvas.Name.Substring(0, index);

            // Get strokes of the currently selected ink canvas and convert them to JSON for the request
            var strokes = currentCanvas.InkPresenter.StrokeContainer.GetStrokes();

            inkRecognizer.ClearStrokes();
            inkRecognizer.AddStrokes(strokes);
            JObject json = inkRecognizer.ConvertInkToJson();

            // Recognize the strokes of the current ink canvas and convert the response JSON into an InkResponse
            var response = await inkRecognizer.RecognizeAsync(json);

            string responseString = await response.Content.ReadAsStringAsync();

            inkResponse = JsonConvert.DeserializeObject <InkResponse>(responseString);

            if (response.StatusCode == HttpStatusCode.OK)
            {
                var result = this.FindName($"{prefix}Result") as TextBlock;
                result.Text = string.Empty;

                foreach (var recoUnit in inkResponse.RecognitionUnits)
                {
                    if (recoUnit.category == "line")
                    {
                        if (prefix == "damage")
                        {
                            result.Text += $"{recoUnit.recognizedText}\n";
                        }
                        else
                        {
                            result.Text += $"{recoUnit.recognizedText} ";
                        }
                    }
                }
            }
            else
            {
                if (response.StatusCode == HttpStatusCode.Unauthorized || response.StatusCode == HttpStatusCode.NotFound)
                {
                    await new MessageDialog("Access denied due to invalid subscription key or wrong API endpoint. Make sure to provide a valid key for an active subscription and use a correct API endpoint in the Settings page.", $"Response Code: {inkResponse.Error.code}").ShowAsync();
                }
                else
                {
                    await new MessageDialog(inkResponse.Error.message, $"Response Code: {inkResponse.Error.code}").ShowAsync();
                }
            }
        }
        private async void RecognizeButton_Click(object sender, RoutedEventArgs e)
        {
            var strokes = inkCanvas.InkPresenter.StrokeContainer.GetStrokes();

            if (strokes.Count > 0)
            {
                // Disable use of toolbar during recognition and rendering
                ToggleInkToolbar();
                ToggleProgressRing();

                // Clear result canvas and viewable JSON before recognition and rendering of results
                ViewCanvasButton_Click(null, null);
                ClearJson();

                // Set language code, convert ink to JSON for request, and display it
                string languageCode = languageDropdown.SelectedValue.ToString();
                inkRecognizer.SetLanguage(languageCode);

                inkRecognizer.ClearStrokes();
                inkRecognizer.AddStrokes(strokes);
                JObject json = inkRecognizer.ConvertInkToJson();
                requestJson.Text = FormatJson(json.ToString());

                try
                {
                    // Recognize Ink from JSON and display response
                    var response = await inkRecognizer.RecognizeAsync(json);

                    string responseString = await response.Content.ReadAsStringAsync();

                    inkResponse = JsonConvert.DeserializeObject <InkResponse>(responseString);

                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        // Generate JSON tree view and draw result on right side canvas
                        CreateJsonTree();
                        responseJson.Text = FormatJson(responseString);

                        var resultCanvas = this.FindName("resultCanvas") as CanvasControl;
                        resultCanvas.Invalidate();
                    }
                    else
                    {
                        if (response.StatusCode == HttpStatusCode.Unauthorized || response.StatusCode == HttpStatusCode.NotFound)
                        {
                            await new MessageDialog("Access denied due to invalid subscription key or wrong API endpoint. Make sure to provide a valid key for an active subscription and use a correct API endpoint in the Settings page.", $"Response Code: {inkResponse.Error.code}").ShowAsync();
                        }
                        else
                        {
                            await new MessageDialog(inkResponse.Error.message, $"Response Code: {inkResponse.Error.code}").ShowAsync();
                        }
                    }
                }
                catch (TaskCanceledException)
                {
                    // This may occur when a user attempts to navigate to another page during recognition. In this case, we just want to continue on to the selected page
                }

                // Re-enable use of toolbar after recognition and rendering
                ToggleInkToolbar();
                ToggleProgressRing();
            }
            else
            {
                // Clear viewable JSON if there is no strokes on canvas
                ClearJson();
            }
        }
        private async void RecognizeButton_Click(object sender, RoutedEventArgs e)
        {
            var strokes = inkCanvas.InkPresenter.StrokeContainer.GetStrokes();

            if (strokes.Count > 0)
            {
                // Disable use of toolbar during recognition and rendering and
                // Clear result canvas and viewable JSON before recognition and rendering of results
                ToggleInkToolbar();
                ToggleProgressRing();
                ClearJson();

                // Convert ink stroke data to JSON to be sent with request
                inkRecognizer.ClearStrokes();
                inkRecognizer.AddStrokes(strokes);
                JObject json = inkRecognizer.ConvertInkToJson();
                requestJsonText.Text = FormatJson(json.ToString());

                // Recognize Ink from JSON and display response
                var response = await inkRecognizer.RecognizeAsync(json);

                string responseString = await response.Content.ReadAsStringAsync();

                inkResponse = JsonConvert.DeserializeObject <InkResponse>(responseString);

                if (response.StatusCode == HttpStatusCode.OK)
                {
                    // Generate JSON tree view and draw result on right side canvas
                    CreateJsonTree();
                    responseJsonText.Text = FormatJson(responseString);

                    try
                    {
                        var resultCanvas = this.FindName("resultCanvas") as CanvasControl;
                        resultCanvas.Invalidate();
                    }
                    catch (NullReferenceException)
                    {
                        // This occurs when the page is changed before recognition finishes.
                        // Since the result canvas gets disposed when navigating away it becomes null.
                    }
                }
                else
                {
                    if (response.StatusCode == HttpStatusCode.Unauthorized || response.StatusCode == HttpStatusCode.NotFound)
                    {
                        await new MessageDialog("Access denied due to invalid subscription key or wrong API endpoint. Make sure to provide a valid key for an active subscription and use a correct API endpoint in the Settings page.", $"Response Code: {inkResponse.Error.code}").ShowAsync();
                    }
                    else
                    {
                        await new MessageDialog(inkResponse.Error.message, $"Response Code: {inkResponse.Error.code}").ShowAsync();
                    }
                }

                // Re-enable use of toolbar after recognition and rendering
                ToggleInkToolbar();
                ToggleProgressRing();
            }
            else
            {
                // Clear viewable JSON if there is no strokes on canvas
                ClearJson();
            }
        }