コード例 #1
0
 private void ButtonApiTest_Click(object sender, EventArgs e)
 {
     try
     {
         //string url = "https://aip.baidubce.com/oauth/2.0/token";
         //string data = string.Format("grant_type=client_credentials&client_id={0}&client_secret={1}", TextBoxApiKey.Text, TextBoxSecretKey.Text);
         //string result = HttpClient.Post(data, url);
         //BaiduToken token = JsonConvert.DeserializeObject<BaiduToken>(result);
         string token = OCRHelper.GetBaiduToken(TextBoxApiKey.Text, TextBoxSecretKey.Text);
         if (token.Contains("错误"))
         {
             MessageBox.Show(this, token, "错误");
         }
         else
         {
             TextBoxToken.Text = token;
             IniHelper.SetValue("百度接口", "API Key", TextBoxApiKey.Text.Trim());
             IniHelper.SetValue("百度接口", "Secret Key", TextBoxSecretKey.Text.Trim());
             IniHelper.SetValue("百度接口", "Access Token", TextBoxToken.Text.Trim());
             IniHelper.SetValue("百度接口", "Date Token", DateTime.Now.ToString("yyyy-MM-dd"));
             MessageBox.Show(this, "已生成并保存密钥,有效期30天!", "提示");
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(this, ex.Message, "错误");
     }
 }
コード例 #2
0
ファイル: FmMain.cs プロジェクト: mehome/jyocr
        private void FormMain_Load(object sender, EventArgs e)
        {
            // RichTextBox 段落缩进
            RichTextBoxValue.SelectionIndent        = 40;
            RichTextBoxValue.SelectionHangingIndent = -35;

            // RichTextBox 拖放事件绑定
            RichTextBoxValue.AllowDrop  = true;
            RichTextBoxValue.DragEnter += new DragEventHandler(FormMain_DragEnter);
            RichTextBoxValue.DragDrop  += new DragEventHandler(FormMain_DragDrop);

            // 读取 ini 配置
            IniHelper.IniLoad("Setting.ini");
            OCRHelper.ApiKey      = IniHelper.GetValue("百度接口", "API Key");
            OCRHelper.SecretKey   = IniHelper.GetValue("百度接口", "Secret Key");
            OCRHelper.AccessToken = IniHelper.GetValue("百度接口", "Access Token");
            string check = IniHelper.GetValue("百度接口", "使用高精度接口");

            OCRHelper.Accurate = check == "" ? false : bool.Parse(check);

            // 判断 token 是否过期
            OCRHelper.DateToken = IniHelper.GetValue("百度接口", "Date Token");
            TimeSpan day = DateTime.Now - DateTime.Parse(OCRHelper.DateToken);

            if (day.Days >= 30 && OCRHelper.ApiKey != "" && OCRHelper.SecretKey != "")
            {
                try
                {
                    string token = OCRHelper.GetBaiduToken(OCRHelper.ApiKey, OCRHelper.SecretKey);
                    if (token.Contains("错误"))
                    {
                        MessageBox.Show(this, token, "错误");
                    }
                    else
                    {
                        IniHelper.SetValue("百度接口", "Access Token", token);
                        IniHelper.SetValue("百度接口", "Date Token", DateTime.Now.ToString("yyyy-MM-dd"));
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, ex.Message, "错误");
                }
            }

            // 注册热键
            string value = IniHelper.GetValue("热键", "截图识别");

            if (value != "" && value != "请按下快捷键")
            {
                HotKey.SetHotkey(Handle, "None", "F4", value, 200);
            }
        }
コード例 #3
0
        private void FormMain_Load(object sender, EventArgs e)
        {
            // RichTextBox 段落缩进
            RichTextBoxValue.SelectionIndent        = 40;
            RichTextBoxValue.SelectionHangingIndent = -35;

            // RichTextBox 拖放事件绑定
            RichTextBoxValue.AllowDrop  = true;
            RichTextBoxValue.DragEnter += new DragEventHandler(FormMain_DragEnter);
            RichTextBoxValue.DragDrop  += new DragEventHandler(FormMain_DragDrop);

            // 读取 ini 配置
            IniHelper.IniLoad("Setting.ini");

            Setting.TextPlus    = IniHelper.GetValue("常规", "识别后文本累加") == "" ? false : bool.Parse(IniHelper.GetValue("常规", "识别后文本累加"));
            Setting.TextCopy    = IniHelper.GetValue("常规", "识别后自动复制") == "" ? false : bool.Parse(IniHelper.GetValue("常规", "识别后自动复制"));
            Setting.FormHide    = IniHelper.GetValue("常规", "截图时隐藏窗体") == "" ? false : bool.Parse(IniHelper.GetValue("常规", "截图时隐藏窗体"));
            Setting.FormTray    = IniHelper.GetValue("常规", "右下角显示托盘") == "" ? false : bool.Parse(IniHelper.GetValue("常规", "右下角显示托盘"));
            this.Notify.Visible = Setting.FormTray;
            Setting.SelfStart   = IniHelper.GetValue("常规", "开机自启") == "" ? false : bool.Parse(IniHelper.GetValue("常规", "开机自启"));

            OCRHelper.ApiKey      = IniHelper.GetValue("百度接口", "API Key");
            OCRHelper.SecretKey   = IniHelper.GetValue("百度接口", "Secret Key");
            OCRHelper.AccessToken = IniHelper.GetValue("百度接口", "Access Token");
            OCRHelper.Accurate    = IniHelper.GetValue("百度接口", "使用高精度接口") == "" ? false : bool.Parse(IniHelper.GetValue("百度接口", "使用高精度接口"));

            // 判断 token 是否过期
            OCRHelper.DateToken = IniHelper.GetValue("百度接口", "Date Token");
            TimeSpan day = OCRHelper.DateToken == "" ? TimeSpan.MaxValue : DateTime.Now - DateTime.Parse(OCRHelper.DateToken);

            if (day.Days >= 30 && OCRHelper.ApiKey != "" && OCRHelper.SecretKey != "")
            {
                try
                {
                    string token = OCRHelper.GetBaiduToken(OCRHelper.ApiKey, OCRHelper.SecretKey);
                    if (token.Contains("错误"))
                    {
                        MessageBox.Show(this, token, "错误");
                    }
                    else
                    {
                        OCRHelper.AccessToken = token;
                        IniHelper.SetValue("百度接口", "Access Token", token);
                        IniHelper.SetValue("百度接口", "Date Token", DateTime.Now.ToString("yyyy-MM-dd"));
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, ex.Message, "错误");
                }
            }

            Setting.TranItem   = IniHelper.GetValue("翻译", "默认网址") == "" ? 0 : int.Parse(IniHelper.GetValue("翻译", "默认网址"));
            Setting.TranOption = IniHelper.GetValue("翻译", "翻译选项") == "" ? 0 : int.Parse(IniHelper.GetValue("翻译", "翻译选项"));

            // 注册热键
            Setting.HotkeyCut = IniHelper.GetValue("热键", "截图识别");
            if (Setting.HotkeyCut != "" && Setting.HotkeyCut != "请按下快捷键")
            {
                HotKey.SetHotkey(Handle, "None", "F4", Setting.HotkeyCut, 200);
            }
            Setting.HotkeyShow = IniHelper.GetValue("热键", "显示/隐藏");
            if (Setting.HotkeyShow != "" && Setting.HotkeyShow != "请按下快捷键")
            {
                HotKey.SetHotkey(Handle, "None", "F4", Setting.HotkeyShow, 201);
            }
        }