Exemplo n.º 1
0
        public void BaiduAPI()
        {
            var API_KEY    = "p8Tgf4cVCWi0QOGjnqfu22G9";
            var SECRET_KEY = "UvzNMtiR728kmjai8UjMLEctfZ2eVPNm";
            var client     = new Baidu.Aip.Ocr.Ocr(API_KEY, SECRET_KEY);

            client.Timeout = 6000;  // 修改超时时间
            var image = File.ReadAllBytes("jietu.jpg");

            try
            {
                var result = client.GeneralBasic(image);
                // 调用通用文字识别, 图片参数为本地图片,可能会抛出网络等异常,请使用try/catch捕获
                for (int i = 0; i < result["words_result"].Count(); i++)
                {
                    OCRresult = OCRresult + result["words_result"][i]["words"].ToString() + "\r\n";
                }
            }
            catch (OverflowException)
            {
                info = "网络出错请重试";
                massage Ma = new massage();
                Ma.Show();
                return;
            }
        }
Exemplo n.º 2
0
        public async void SougouAPI()//POST一个多部分编码
        {
            HttpClient client = new HttpClient();

            client.MaxResponseContentBufferSize = 256000;
            client.DefaultRequestHeaders.Add("user-agent", "User-Agent    Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; Touch; MALNJS; rv:11.0) like Gecko");//设置请求头
            string url = "http://ocr.shouji.sogou.com/v2/ocr/json";
            HttpResponseMessage      response;
            MultipartFormDataContent mulContent = new MultipartFormDataContent("----WebKitFormBoundaryrXRBKlhEeCbfHIY"); //创建用于可传递文件的容器
            string      path        = "jietu.jpg";
            FileStream  fs          = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);              // 读文件流
            HttpContent fileContent = new StreamContent(fs);                                                             //为文件流提供的HTTP容器

            fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data");                         //设置媒体类型
            mulContent.Add(fileContent, "pic", "pic.jpg");                                                               //这里第二个参数是表单名,第三个是文件名。如果接收的时候用表单名来获取文件,那第二个参数就是必要的了
            try
            {
                response = await client.PostAsync(new Uri(url), mulContent);

                response.EnsureSuccessStatusCode();
                string result = await response.Content.ReadAsStringAsync();

                JObject sougouJson = (JObject)JsonConvert.DeserializeObject(result);
                if (sougouJson.Count == 1)
                {
                    info = "当前选择没有文本";
                    massage Ms = new massage();
                    Ms.Show();
                    return;
                }
                for (int i = 0; i < sougouJson["result"].Count(); i++)
                {
                    OCRresult = OCRresult + sougouJson["result"][i]["content"].ToString(); //+ "\n";
                }

                Clipboard.SetText(OCRresult);
                showTrans.textBox1.Text = Translate(OCRresult);
                showTrans.textBox2.Text = OCRresult;
                info = "文字已复制到剪切板";
                massage Ma = new massage();
                Ma.Show();
            }
            catch (OverflowException)
            {
                info = "网络出错请重试";
                massage Ma = new massage();
                Ma.Show();
                return;
            }
        }
Exemplo n.º 3
0
        private void jietu()
        {
            Bitmap   CatchBmp = new Bitmap(Screen.AllScreens[0].Bounds.Width, Screen.AllScreens[0].Bounds.Height);
            Graphics g        = Graphics.FromImage(CatchBmp);

            g.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(Screen.AllScreens[0].Bounds.Width, Screen.AllScreens[0].Bounds.Height));
            Cutter cutter1 = new Cutter();

            cutter1.BackgroundImage = CatchBmp;
            cutter1.Show();
            if (cutter1.ShowDialog() == DialogResult.OK)
            {
                info = "截图已复制到剪切板";
                massage Ma = new massage();
                Ma.Show();
            }
        }
Exemplo n.º 4
0
        private void OCR()
        {
            OCRresult = "";
            Bitmap   CatchBmp = new Bitmap(Screen.AllScreens[0].Bounds.Width, Screen.AllScreens[0].Bounds.Height);
            Graphics g        = Graphics.FromImage(CatchBmp);

            g.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(Screen.AllScreens[0].Bounds.Width, Screen.AllScreens[0].Bounds.Height));
            Cutter cutter1 = new Cutter();

            cutter1.BackgroundImage = CatchBmp;
            cutter1.Show();
            if (cutter1.ShowDialog() == DialogResult.OK)
            {
                if (OCRchoose == "sougou")
                {
                    SougouAPI();
                    return;
                }
                else if (OCRchoose == "baidu")
                {
                    BaiduAPI();
                }
                else
                {
                    info = "没有选择合适的API接口";
                    massage Ma = new massage();
                    Ma.Show();
                }
                if (OCRresult == "")
                {
                    info = "当前选择没有文本";
                    massage Ma = new massage();
                    Ma.Show();
                }
                else
                {
                    Clipboard.SetText(OCRresult);
                    //Translate(OCRresult);
                    info = "文字已复制到剪切板:" + Translate(OCRresult);
                    massage Ma = new massage();
                    Ma.Show();
                }
            }
        }