Exemplo n.º 1
0
        Color secondTransTextColor;        //第二翻译源结果颜色

        /// <summary>
        /// UI初始化、翻译API初始化、如果是OCR模式则Hook鼠标点击事件
        /// </summary>
        private void InitUI()
        {
            srcTextLabel           = new Label();
            srcTextLabel.AutoSize  = false;
            srcTextLabel.Text      = "等待源文本";
            srcTextLabel.BackColor = TransparencyColor; //背景色透明
            srcTextLabel.TextAlign = ContentAlignment.TopLeft;

            firstTransTextLabel           = new Label();
            firstTransTextLabel.AutoSize  = false;
            firstTransTextLabel.Text      = "等待源文本";
            firstTransTextLabel.BackColor = TransparencyColor; //背景色透明
            firstTransTextLabel.TextAlign = ContentAlignment.TopLeft;

            secondTransTextLabel           = new Label();
            secondTransTextLabel.AutoSize  = false;
            secondTransTextLabel.Text      = "等待源文本";
            secondTransTextLabel.BackColor = TransparencyColor; //背景色透明
            secondTransTextLabel.TextAlign = ContentAlignment.TopLeft;

            LabelInit();
            this.Controls.Add(srcTextLabel);
            this.Controls.Add(firstTransTextLabel);
            this.Controls.Add(secondTransTextLabel);

            this.FormBorderStyle = FormBorderStyle.None;
            this.SizeChanged    += GameTranslateForm_SizeChanged;
            this.FormClosing    += GameTranslateForm_FormClosing;

            BaiduTranslator.BaiduTrans_Init();
            TencentTranslator.TencentTrans_Init();
            TencentOldTranslator.TencentOldTrans_Init();

            if (Common.TransMode == 2)
            {
                BaiduGeneralOCRBasic.BaiduGeneralOCRBasic_Init();

                //初始化钩子对象
                if (hook == null)
                {
                    hook = new GlobalMouseHook();
                    hook.OnMouseActivity += new MouseEventHandler(Hook_OnMouseActivity);
                }

                IsOCRingFlag = false;
                bool r = hook.Start();
                if (r == false)
                {
                    MessageBox.Show("安装钩子失败!");
                }
            }


            TextFontColorInit();

            firstTransAPI = IniFileHelper.ReadItemValue(Environment.CurrentDirectory + "\\settings.ini",
                                                        "Translate_All", "FirstTranslator", "NoTranslate");
            secondTransAPI = IniFileHelper.ReadItemValue(Environment.CurrentDirectory + "\\settings.ini",
                                                         "Translate_All", "SecondTranslator", "NoTranslate");
        }
        public static string TencentOld_Translate(string sourceText, string desLang, string srcLang)
        {
            // 原文
            string q = sourceText;

            Random rd   = new Random();
            string salt = rd.Next(100000).ToString();

            string url = "https://tmt.tencentcloudapi.com/?";

            string req = "Action=TextTranslate";

            req += "&Nonce=" + salt;
            req += "&ProjectId=0";
            req += "&Region=ap-shanghai";
            req += "&SecretId=" + SecretId;
            req += "&Source=" + srcLang;
            req += "&SourceText=" + sourceText;
            req += "&Target=" + desLang;
            req += "&Timestamp=" + TencentTranslator.GetTimeStamp();
            req += "&Version=2018-03-21";

            HMACSHA1 hmac = new HMACSHA1()
            {
                Key = System.Text.Encoding.UTF8.GetBytes(SecretKey)
            };

            byte[] data   = Encoding.UTF8.GetBytes("GETtmt.tencentcloudapi.com/?" + req);
            var    result = hmac.ComputeHash(data);

            req = req + "&Signature=" + HttpUtility.UrlEncode(Convert.ToBase64String(result));

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + req);

            request.Method      = "GET";
            request.ContentType = "text/html;charset=UTF-8";
            request.UserAgent   = null;
            request.Timeout     = 6000;
            try
            {
                HttpWebResponse response         = (HttpWebResponse)request.GetResponse();
                Stream          myResponseStream = response.GetResponseStream();
                StreamReader    myStreamReader   = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
                string          retString        = myStreamReader.ReadToEnd();
                myStreamReader.Close();
                myResponseStream.Close();

                return(retString);
            }
            catch (WebException ex)
            {
                return("Request Timeout");
            }
        }
Exemplo n.º 3
0
        private void TXTransTextTestBtn_BtnClick(object sender, EventArgs e)
        {
            IniFileHelper.WriteValue(Environment.CurrentDirectory + "\\settings.ini", "TencentTranslator", "appID", TXappidTextBox.InputText);
            IniFileHelper.WriteValue(Environment.CurrentDirectory + "\\settings.ini", "TencentTranslator", "appKey", TXKeyTextBox.InputText);

            TencentTranslator.TencentTrans_Init();
            string ret = TencentTranslator.Fanyijun_Translate(TXTestTextBox.InputText, TXDesLangBox.InputText, TXSrcLangBox.InputText);

            TencentTransOutInfo oinfo = JsonConvert.DeserializeObject <TencentTransOutInfo>(ret);

            if (oinfo.ret == "0")
            {
                MessageBox.Show(oinfo.data.target_text, "翻译结果");
            }
            else
            {
                MessageBox.Show("翻译过程中出现错误,请先进行API认证测试!", "错误");
            }
        }
Exemplo n.º 4
0
        private void TXAPITestBtn_BtnClick(object sender, EventArgs e)
        {
            IniFileHelper.WriteValue(Environment.CurrentDirectory + "\\settings.ini", "TencentTranslator", "appID", TXappidTextBox.InputText);
            IniFileHelper.WriteValue(Environment.CurrentDirectory + "\\settings.ini", "TencentTranslator", "appKey", TXKeyTextBox.InputText);

            TencentTranslator.TencentTrans_Init();
            string ret = TencentTranslator.Fanyijun_Translate("apple", "zh", "en");

            TencentTransOutInfo oinfo = JsonConvert.DeserializeObject <TencentTransOutInfo>(ret);

            if (oinfo.ret == "0")
            {
                MessageBox.Show("腾讯翻译API工作正常!", "提示");
            }
            else
            {
                MessageBox.Show("腾讯翻译API工作异常,错误代码:" + oinfo.ret + " \n错误描述:" + oinfo.msg, "错误");
            }
        }
Exemplo n.º 5
0
        private void TXTransTextTestBtn_BtnClick(object sender, EventArgs e)
        {
            Common.settings.TXappID  = TXappidTextBox.InputText;
            Common.settings.TXappKey = TXKeyTextBox.InputText;

            TencentTranslator.TencentTrans_Init();
            string ret = TencentTranslator.Fanyijun_Translate(TXTestTextBox.InputText, TXDesLangBox.InputText, TXSrcLangBox.InputText);

            TencentTransOutInfo oinfo = JsonConvert.DeserializeObject <TencentTransOutInfo>(ret);

            if (oinfo.ret == "0")
            {
                MessageBox.Show(oinfo.data.target_text, "翻译结果");
            }
            else
            {
                MessageBox.Show("翻译过程中出现错误,请先进行API认证测试!", "错误");
            }
        }
Exemplo n.º 6
0
        private void TXAPITestBtn_BtnClick(object sender, EventArgs e)
        {
            Common.settings.TXappID  = TXappidTextBox.InputText;
            Common.settings.TXappKey = TXKeyTextBox.InputText;

            TencentTranslator.TencentTrans_Init();
            string ret = TencentTranslator.Fanyijun_Translate("apple", "zh", "en");

            TencentTransOutInfo oinfo = JsonConvert.DeserializeObject <TencentTransOutInfo>(ret);

            if (oinfo.ret == "0")
            {
                MessageBox.Show("腾讯翻译API工作正常!", "提示");
            }
            else
            {
                MessageBox.Show("腾讯翻译API工作异常,错误代码:" + oinfo.ret + " \n错误描述:" + oinfo.msg, "错误");
            }
        }
Exemplo n.º 7
0
        /*
         *
         *
         * const int WM_NCHITTEST = 0x0084;
         * const int HTLEFT = 10;
         * const int HTRIGHT = 11;
         * const int HTTOP = 12;
         * const int HTTOPLEFT = 13;
         * const int HTTOPRIGHT = 14;
         * const int HTBOTTOM = 15;
         * const int HTBOTTOMLEFT = 0x10;
         * const int HTBOTTOMRIGHT = 17;
         * protected override void WndProc(ref Message m)
         * {
         *  base.WndProc(ref m);
         *  switch (m.Msg)
         *  {
         *      case WM_NCHITTEST:
         *          Point vPoint = new Point((int)m.LParam & 0xFFFF,
         *              (int)m.LParam >> 16 & 0xFFFF);
         *          vPoint = PointToClient(vPoint);
         *          if (vPoint.X <= 5)
         *              if (vPoint.Y <= 5)
         *                  m.Result = (IntPtr)HTTOPLEFT;
         *              else if (vPoint.Y >= ClientSize.Height - 5)
         *                  m.Result = (IntPtr)HTBOTTOMLEFT;
         *              else m.Result = (IntPtr)HTLEFT;
         *          else if (vPoint.X >= ClientSize.Width - 5)
         *              if (vPoint.Y <= 5)
         *                  m.Result = (IntPtr)HTTOPRIGHT;
         *              else if (vPoint.Y >= ClientSize.Height - 5)
         *                  m.Result = (IntPtr)HTBOTTOMRIGHT;
         *              else m.Result = (IntPtr)HTRIGHT;
         *          else if (vPoint.Y <= 5)
         *              m.Result = (IntPtr)HTTOP;
         *          else if (vPoint.Y >= ClientSize.Height - 5)
         *              m.Result = (IntPtr)HTBOTTOM;
         *          break;
         *  }
         * }
         *
         * [DllImport("user32.dll")]
         * public static extern bool ReleaseCapture();
         * [DllImport("user32.dll")]
         * public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
         * public const int WM_SYSCOMMAND = 0x0112;
         * public const int SC_MOVE = 0xF010;
         * public const int HTCAPTION = 0x0002;
         */
        /*
         * //之前的窗口改变大小部分代码,已弃用
         * private void GameTranslateForm_Move(object sender, EventArgs e)
         * {
         *  ReleaseCapture();
         *
         *  SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
         *
         * }
         */
        /*
         * //之前的窗口拖动部分代码,已弃用
         * private void TransparentPanel_MouseDown(object sender, MouseEventArgs e)
         * {
         *  mouse_offset = new Point(-e.X, -e.Y);
         * }
         *
         * private void TransparentPanel_MouseMove(object sender, MouseEventArgs e)
         * {
         *
         *  if (e.Button == MouseButtons.Left)
         *  {
         *      Point mousePos = Control.MousePosition;
         *      mousePos.Offset(mouse_offset.X, mouse_offset.Y);
         *      Location = mousePos;
         *  }
         *
         * }
         */


        /// <summary>
        /// 自动进行游戏翻译
        /// 检查是否设置了分行翻译、去掉一些乱码类型的符号、检查是否空文本
        /// </summary>
        /// <param name="text"></param>
        /// <param name="srcLang"></param>
        /// <param name="desLang"></param>
        public void GameTranslateAuto(string text, string srcLang, string desLang)
        {
            //先检查玩家是否设置了分行翻译
            bool eachRowTrans = Convert.ToBoolean(IniFileHelper.WriteValue(Environment.CurrentDirectory + "\\settings.ini", "Translate_All", "EachRowTrans", "True"));

            if (eachRowTrans == false)
            {
                text = text.Replace("<br>", "").Replace("</br>", "").Replace("\n", "").Replace("\t", "").Replace("\r", "");
            }

            //处理:去掉一些乱码类型的符号
            text = text.Replace("_", "").Replace("-", "").Replace("+", "");

            string currentAPI;
            string ret = "";

            for (int i = 1; i <= 2; i++)
            {
                if (i == 1)
                {
                    currentAPI = firstTransAPI;
                }
                else
                {
                    currentAPI = secondTransAPI;
                }

                if (text == "")
                {
                    ret = "";
                }
                else
                {
                    if (currentAPI == "BaiduTranslator")
                    {
                        ret = BaiduTranslator.Baidu_Translate(text, desLang, srcLang);
                        if (ret == "Request Timeout")
                        {
                            ret = "[百度]翻译超时";
                        }
                        else
                        {
                            BaiduTransOutInfo oinfo = JsonConvert.DeserializeObject <BaiduTransOutInfo>(ret);
                            if (oinfo.trans_result != null)
                            {
                                ret = "";
                                for (int k = 0; k < oinfo.trans_result.Count; k++)
                                {
                                    ret = ret + oinfo.trans_result[k].dst + "\n";
                                }
                            }
                            else
                            {
                                ret = "[百度]翻译错误";
                            }
                        }
                    }
                    else if (currentAPI == "TencentTranslator")
                    {
                        ret = TencentTranslator.Fanyijun_Translate(text, desLang, srcLang);
                        if (ret == "Request Timeout")
                        {
                            ret = "[腾讯]翻译超时";
                        }
                        else
                        {
                            TencentTransOutInfo oinfo = JsonConvert.DeserializeObject <TencentTransOutInfo>(ret);
                            if (oinfo.data != null)
                            {
                                ret = oinfo.data.target_text;
                            }
                            else
                            {
                                ret = "[腾讯]翻译错误";
                            }
                        }
                    }
                    else if (currentAPI == "JBjTranslator")
                    {
                        ret = JBeijingTranslator.Translate_JapanesetoChinese(text);
                        if (ret == null || ret == "")
                        {
                            ret = "";
                        }
                    }
                    else if (currentAPI == "TencentOldTranslator")
                    {
                        ret = TencentOldTranslator.TencentOld_Translate(text, desLang, srcLang);
                        if (ret == "Request Timeout")
                        {
                            ret = "[腾讯旧]翻译超时";
                        }
                        else
                        {
                            TencentOldTransOutInfo oinfo = JsonConvert.DeserializeObject <TencentOldTransOutInfo>(ret);
                            if (oinfo.Response.TargetText != null)
                            {
                                ret = oinfo.Response.TargetText;
                            }
                            else
                            {
                                ret = "[腾讯旧]翻译错误";
                            }
                        }
                    }
                    else
                    {
                        ret = "";
                    }
                }

                if (i == 1)
                {
                    firstTransText = ret;
                }
                else
                {
                    secondTransText = ret;
                }
            }
        }
        Color secondTransTextColor;     //第二翻译源结果颜色

        /// <summary>
        /// UI初始化、翻译API初始化、如果是OCR模式则Hook鼠标点击事件
        /// </summary>
        private void InitUI()
        {
            srcTextLabel           = new Label();
            srcTextLabel.AutoSize  = false;
            srcTextLabel.Text      = "等待源文本";
            srcTextLabel.BackColor = TransparencyColor; //背景色透明
            srcTextLabel.TextAlign = ContentAlignment.TopLeft;

            firstTransTextLabel           = new Label();
            firstTransTextLabel.AutoSize  = false;
            firstTransTextLabel.Text      = "等待源文本";
            firstTransTextLabel.BackColor = TransparencyColor; //背景色透明
            firstTransTextLabel.TextAlign = ContentAlignment.TopLeft;

            secondTransTextLabel           = new Label();
            secondTransTextLabel.AutoSize  = false;
            secondTransTextLabel.Text      = "等待源文本";
            secondTransTextLabel.BackColor = TransparencyColor; //背景色透明
            secondTransTextLabel.TextAlign = ContentAlignment.TopLeft;

            LabelInit();
            this.Controls.Add(srcTextLabel);
            this.Controls.Add(firstTransTextLabel);
            this.Controls.Add(secondTransTextLabel);

            this.FormBorderStyle = FormBorderStyle.None;
            this.SizeChanged    += GameTranslateForm_SizeChanged;
            this.FormClosing    += GameTranslateForm_FormClosing;

            BaiduTranslator.BaiduTrans_Init();
            TencentTranslator.TencentTrans_Init();
            TencentOldTranslator.TencentOldTrans_Init();

            if (Common.TransMode == 2)
            {
                if (Common.settings.OCRsource == "BaiduOCR")
                {
                    BaiduGeneralOCRBasic.BaiduGeneralOCRBasic_Init();
                }
                else
                {
                    TesseractOCR.TesseractOCR_Init();
                }



                //初始化钩子对象
                if (hook == null)
                {
                    hook = new GlobalMouseHook();
                    hook.OnMouseActivity += new MouseEventHandler(Hook_OnMouseActivity);
                }

                IsOCRingFlag = false;
                bool r = hook.Start();
                if (r == false)
                {
                    MessageBox.Show("安装钩子失败!");
                }
            }


            TextFontColorInit();

            firstTransAPI  = Common.settings.FirstTranslator;
            secondTransAPI = Common.settings.SecondTranslator;
        }