Example #1
0
 private void threshTrackBar_ValueChanged(object sender, EventArgs e)
 {
     if (PreHandleCheckBox.Checked == true)
     {
         srcPic = (Bitmap)ScreenCapture.GetWindowRectCapture((IntPtr)SelectedHwnd, Common.OCRrec, Common.isAllWindowCap);
         TesseractOCR.thresh = (int)threshTrackBar.Value;
         PreviewBox.Image    = TesseractOCR.Thresholding(srcPic);
     }
 }
Example #2
0
        private void TestOCRBtn_BtnClick(object sender, EventArgs e)
        {
            srcPic = (Bitmap)ScreenCapture.GetWindowRectCapture((IntPtr)SelectedHwnd, Common.OCRrec, Common.isAllWindowCap);
            if (srcPic == null)
            {
                MessageBox.Show("请选择正确的截屏区域", "警告");
                return;
            }

            Bitmap bm;

            if (PreHandleCheckBox.Checked == true)
            {
                TesseractOCR.thresh = (int)threshTrackBar.Value;
                bm = TesseractOCR.Thresholding(srcPic);
            }
            else
            {
                bm = srcPic;
            }


            if (Common.settings.OCRsource == "BaiduOCR")
            {
                BaiduGeneralOCRBasic.BaiduGeneralOCRBasic_Init();

                string ret = BaiduGeneralOCRBasic.BaiduGeneralBasicOCR(bm, srcLangCombox.SelectedValue);
                MessageBox.Show(ret, "百度OCR结果");
            }
            else
            {
                if (srcLangCombox.SelectedValue == "JAP")
                {
                    TesseractOCR.srcLangCode = "jpn";
                }
                else if (srcLangCombox.SelectedValue == "ENG")
                {
                    TesseractOCR.srcLangCode = "eng";
                }
                TesseractOCR.TesseractOCR_Init();


                string ret = TesseractOCR.OCRProcess(bm);
                MessageBox.Show(ret, "Tesseract离线OCR结果");
            }
        }
        /// <summary>
        /// OCR:鼠标点击的挂载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Hook_OnMouseActivity(object sender, MouseEventArgs e)
        {
            if (Common.TransMode == 2)
            {
                if (isPause == false)
                {
                    //OCR方式下单击鼠标进行更新
                    //先判断是单窗口还是全屏幕,如果是单窗口再判断是不是在游戏窗口中点的

                    if ((Common.isAllWindowCap == true && Process.GetCurrentProcess().Id != FindWindowInfo.GetProcessIDByHWND(FindWindowInfo.GetWindowHWND(e.X, e.Y))) ||
                        Common.OCRWinHwnd == (IntPtr)FindWindowInfo.GetWindowHWND(e.X, e.Y))
                    {
                        if (IsOCRingFlag == false)
                        {
                            IsOCRingFlag = true;
                            ThreadPool.QueueUserWorkItem(state =>
                            {
                                int j = 0;

                                for (; j < 3; j++)
                                {
                                    Thread.Sleep(Common.OCRdelay);

                                    Image img = ScreenCapture.GetWindowRectCapture(Common.OCRWinHwnd, Common.OCRrec, Common.isAllWindowCap);

                                    if (TesseractOCR.thresh != -1)
                                    {
                                        img = TesseractOCR.Thresholding((Bitmap)img);
                                    }

                                    string srcText = "";
                                    string ret;
                                    if (Common.settings.OCRsource == "BaiduOCR")
                                    {
                                        ret = BaiduGeneralOCRBasic.BaiduGeneralBasicOCR(img, Common.OCRsrcLangCode);
                                        BaiduOCRresOutInfo oinfo = JsonConvert.DeserializeObject <BaiduOCRresOutInfo>(ret);
                                        if (oinfo.words_result != null)
                                        {
                                            for (int i = 0; i < oinfo.words_result_num; i++)
                                            {
                                                srcText = srcText + oinfo.words_result[i].words + "\n";
                                            }
                                        }
                                    }
                                    else
                                    {
                                        ret     = TesseractOCR.OCRProcess((Bitmap)img);
                                        srcText = ret.Replace(" ", "").Replace("\r\n\r\n", "\r\n");//去空格和空行
                                    }


                                    if (srcText != "")
                                    {
                                        GameTranslateAuto(srcText, Common.srcLang, Common.desLang);

                                        srcTextLabel.BeginInvoke(new Action(() =>
                                        {
                                            srcTextLabel.Text = srcText;
                                        }));

                                        firstTransTextLabel.BeginInvoke(new Action(() =>
                                        {
                                            firstTransTextLabel.Text = firstTransText;
                                        }));

                                        secondTransTextLabel.BeginInvoke(new Action(() =>
                                        {
                                            secondTransTextLabel.Text = secondTransText;
                                        }));

                                        Common.AddHistoryText(srcText, firstTransText, secondTransText);

                                        IsOCRingFlag = false;
                                        break;
                                    }
                                }

                                if (j == 3)
                                {
                                    srcTextLabel.BeginInvoke(new Action(() =>
                                    {
                                        srcTextLabel.Text = "[OCR]识别三次均为空,请自行刷新!";
                                    }));
                                    IsOCRingFlag = false;
                                }
                            });
                        }
                    }
                }
            }
        }
        /// <summary>
        /// 刷新OCR识别,重置窗口结果,使用多线程解决卡顿
        /// </summary>
        public void ReNewOCR()
        {
            if (Common.TransMode == 2)
            {
                if (IsOCRingFlag == false)
                {
                    IsOCRingFlag = true;

                    ThreadPool.QueueUserWorkItem(state =>
                    {
                        Image img = ScreenCapture.GetWindowRectCapture(Common.OCRWinHwnd, Common.OCRrec, Common.isAllWindowCap);

                        if (TesseractOCR.thresh != -1)
                        {
                            img = TesseractOCR.Thresholding((Bitmap)img);
                        }

                        string srcText = "";
                        string ret;
                        if (Common.settings.OCRsource == "BaiduOCR")
                        {
                            ret = BaiduGeneralOCRBasic.BaiduGeneralBasicOCR(img, Common.OCRsrcLangCode);
                            BaiduOCRresOutInfo oinfo = JsonConvert.DeserializeObject <BaiduOCRresOutInfo>(ret);
                            if (oinfo.words_result != null)
                            {
                                for (int i = 0; i < oinfo.words_result_num; i++)
                                {
                                    srcText = srcText + oinfo.words_result[i].words + "\n";
                                }
                            }
                        }
                        else
                        {
                            ret     = TesseractOCR.OCRProcess((Bitmap)img);
                            srcText = ret.Replace(" ", "").Replace("\r\n\r\n", "\r\n");//去空格和空行
                        }

                        srcTextLabel.BeginInvoke(new Action(() =>
                        {
                            srcTextLabel.Text = srcText;
                        }));

                        GameTranslateAuto(srcText, Common.srcLang, Common.desLang);
                        firstTransTextLabel.BeginInvoke(new Action(() =>
                        {
                            firstTransTextLabel.Text = firstTransText;
                        }));

                        secondTransTextLabel.BeginInvoke(new Action(() =>
                        {
                            secondTransTextLabel.Text = secondTransText;
                        }));
                        Common.AddHistoryText(srcText, firstTransText, secondTransText);
                    });

                    IsOCRingFlag = false;
                }
            }
            else
            {
                MessageBox.Show("当前处于非OCR翻译模式,无法刷新!", "错误");
            }
        }
        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;
        }