Ejemplo n.º 1
0
        protected override void Execute(CodeActivityContext context)
        {
            string path       = FileName.Get(context);
            string API_KEY    = APIKey.Get(context);
            string SECRET_KEY = SecretKey.Get(context);

            img = image.Get(context);
            try
            {
                if (path != null)
                {
                    by = SaveImage(path);
                }
                else
                {
                    by = ConvertImageToByte(img);
                }
                var client = new Ocr(API_KEY, SECRET_KEY);
                //修改超时时间
                client.Timeout = 60000;
                //参数设置
                var options = new Dictionary <string, object>
                {
                    { "detect_direction", detect_direction.ToString().ToLower() },
                    { "detect_language", detect_language.ToString().ToLower() }
                };
                //带参数调用网络图片文字识别
                string result = client.WebImage(by, options).ToString();
                Result.Set(context, result);
            }
            catch (Exception e)
            {
                SharedObject.Instance.Output(SharedObject.enOutputType.Error, "有一个错误产生", e.Message);
            }
        }
Ejemplo n.º 2
0
        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == null || textBox1.Text == "")
            {
                MessageBox.Show("请选择图片");
                return;
            }
            var image = File.ReadAllBytes(textBox1.Text);
            // 调用网络图片文字识别, 图片参数为本地图片,可能会抛出网络等异常,请使用try/catch捕获


            var options = new Dictionary <string, object>();

            if (checkBox2.Checked)
            {
                options.Add("detect_direction", "true");
            }
            var    result      = client.WebImage(image, options);
            String printresult = "";
            //输出结果
            JArray arr = (JArray)result["words_result"];

            foreach (var item in arr)  //循环获取值
            {
                var jo = (JObject)item;
                printresult = printresult + Convert.ToString(jo["words"]) + "\n";
            }
            richTextBox2.Text = printresult;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 调用网络图片文字识别, 图片参数为本地图片
        /// </summary>
        /// <param name="path">图片路径</param>
        /// <returns>list集合一行一个值</returns>
        public List <string> WebImage(string path)
        {
            List <string> contents = new List <string>();
            var           image    = File.ReadAllBytes(path);
            // 调用网络图片文字识别, 图片参数为本地图片,可能会抛出网络等异常,请使用try/catch捕获
            var result = client.WebImage(image);
            // 如果有可选参数
            Dictionary <string, object> options = new Dictionary <string, object>();

            options.Add("detect_direction", "true");
            options.Add("detect_language", "true");
            // 带参数调用网络图片文字识别, 图片参数为本地图片
            result = client.WebImage(image, options);
            string content = result["words_result"].ToString();
            JArray jarray  = JArray.Parse(content);

            for (int i = 0; i < jarray.Count; i++)
            {
                JObject jobject = JObject.Parse(jarray[i].ToString());
                string  hang    = jobject["words"].ToString();
                contents.Add(hang);
            }
            return(contents);
        }