示例#1
0
        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key.ToString().Equals(Enum.Parse(typeof(Key), pref.Get("key")).ToString()))
            {
                if (!gazer.State)
                {
                    gazer.Start();
                }
                else
                {
                    var coords = gazer.Stop();
                    // Get screenshot size
                    string           ssize = pref.Get("sssize");
                    Tuple <int, int> size;
                    switch (ssize)
                    {
                    case "Small":
                        size = Tuple.Create(200, 200);
                        break;

                    case "Medium":
                        size = Tuple.Create(400, 400);
                        break;

                    case "Large":
                        size = Tuple.Create(600, 600);
                        break;

                    case "Custom":
                        size = Tuple.Create(Int32.Parse(pref.Get("screenx")), Int32.Parse(pref.Get("screeny")));
                        break;

                    default:
                        throw new ArgumentException("No such screenshot size: " + ssize);
                    }
                    var screen = ScreenshotUtil.TakeScreen(coords.Item1, coords.Item2, size);
                    var text   = OCRUtil.RecognizeImage(screen, Int32.Parse(pref.Get("magnifyFactor")));
                    srcTextTB.Text = text;

                    translator = Translator.Create(pref.Get("translator"), SourceLanguage, TargetLanguage);
                    try
                    {
                        var tl = translator.Translate(text);
                        if (translator is GoogleTranslator)
                        {
                            var gt = (GoogleTranslator)translator;
                            if (gt.Error != null)
                            {
                                MessageBox.Show("Google translate error.\nMessage: " + gt.Error.Message + "\nStack trace: " + gt.Error.StackTrace, "GT Error", MessageBoxButton.OK, MessageBoxImage.Error);
                            }
                        }
                        Console.WriteLine("MW: translated text: " + tl);
                        targTextTB.Text = tl;
                    }
                    catch (Newtonsoft.Json.JsonReaderException ex)
                    {
                        MessageBox.Show("Error while parsing json: " + ex.Message + "\n", "JSON Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                }
            }
        }