首先:我打算编写一个c#应用程序,使用户可以立即将JRPG和Visual Novels中的文本框从日语翻译为英语或任何其他语言。后面的代码将包括检测文本框的方法,这些方法主要是受汽车牌照识别的启发,但是在不久的将来。到目前为止,我还处于起步阶段,我正在将IronOCR用于OCR,并将日语字符串通过URL发送给google翻译。但这出于明显的原因导致了临时禁令。我想阻止该禁令,或者在我的用例中找到一种更安全的方式来使用Google翻译。到目前为止,这里是相关代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using IronOcr;
using IronOcr.Languages;



namespace OCRTranslate
{
    class Program
    {

        static void Main(string[] args)
        {

            Bitmap imgsource = new Bitmap(@"C:\temp\Unbenannt.png");
            var Ocr = new AdvancedOcr()
            {
                Language = IronOcr.Languages.Japanese.OcrLanguagePack,
            };
            var Resulta = Ocr.Read(@"C:\temp\Unbenannt.png");
            System.IO.File.WriteAllText(@"C:\temp\Unbenanntiron.txt", TranslateGoogle());

            string TranslateGoogle()
            {
                string html = null;
                string url = string.Format(@"http://translate.google.com/translate_a/t?client=j&text={0}&h1=en&sl=ja&tl=en", Resulta);
                System.Net.WebClient web = new System.Net.WebClient();
                web.Headers.Add(System.Net.HttpRequestHeader.UserAgent, "Mozilla/5.0");
                web.Headers.Add(System.Net.HttpRequestHeader.AcceptCharset, "UTF-8");
                web.Encoding = Encoding.UTF8;
                html = web.DownloadString(url);
                return html;
            }
        }
    }
}


它的工作原理非常完美,令我惊讶的是,该图片的日语文本已返回翻译,但即使尝试也可能会导致暂时禁止。欢迎任何建议,甚至可以替代Google翻译。

编辑:在我的Firefox中使用该URL显然不会引起禁止。从技术上讲,我可以从c#应用程序通过浏览器打开URL。浏览器使用URL输出文本文件...因此,可以通过c#捕获texfile吗?

另一个想法:我可以将URL发送到已经打开的Firefox窗口吗?

最佳答案

公开可用的Google翻译网站旨在供操作人员通过浏览器使用。它不是为开发人员设计的API。

请使用Google Translate API

这使您可以选择适用于您的需求的定价/报价组合。此外,various nuget Packages随时可用于将C#应用程序连接到Google Translation API。

关于c# - 通过C#访问Google翻译,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50059118/

10-12 00:34
查看更多