Ejemplo n.º 1
0
        public override Task <string> OCRProcessAsync(Bitmap img)
        {
            try
            {
                Process p = Process.Start(new ProcessStartInfo()
                {
                    FileName               = path,
                    Arguments              = "- - " + args,
                    UseShellExecute        = false,
                    CreateNoWindow         = true,
                    RedirectStandardInput  = true,
                    RedirectStandardOutput = true,
                    RedirectStandardError  = true,
                    StandardOutputEncoding = Encoding.UTF8
                });
                var imgdata = ImageProcFunc.Image2Bytes(img);
                p.StandardInput.BaseStream.Write(imgdata, 0, imgdata.Length);
                p.StandardInput.Close();
                p.WaitForExit();

                string err = p.StandardError.ReadToEnd();
                if (err.ToLower().Contains("error"))
                {
                    errorInfo = err;
                    return(Task.FromResult <string>(null));
                }

                string result = p.StandardOutput.ReadToEnd();
                p.Dispose();
                return(Task.FromResult(result));
            }
            catch (Exception ex)
            {
                errorInfo = ex.Message;
                return(Task.FromResult <string>(null));
            }
        }
Ejemplo n.º 2
0
        public override async Task <string> OCRProcessAsync(Bitmap img)
        {
            if (img == null || langCode == null || langCode == "")
            {
                errorInfo = "Param Missing";
                return(null);
            }

            byte[] imgdata = ImageProcFunc.Image2Bytes(img);

            // 计算sign
            MD5 md5 = MD5.Create();
            var ms  = new MemoryStream();
            var sw  = new StreamWriter(ms);

            sw.Write(appId);
            sw.Write(BitConverter.ToString(md5.ComputeHash(imgdata)).Replace("-", "").ToLower());
            sw.Write(salt);
            sw.Write("APICUIDmac");
            sw.Write(secretKey);
            sw.Flush();
            string sign = BitConverter.ToString(md5.ComputeHash(ms.ToArray())).Replace("-", "").ToLower();

            sw.Close();
            md5.Dispose();

            string endpoint = "https://fanyi-api.baidu.com/api/trans/sdk/picture?cuid=APICUID&mac=mac&salt=" + salt + "&appid=" + appId + "&sign=" + sign + "&to=zh&from=" + langCode;


            HttpWebRequest request = WebRequest.CreateHttp(endpoint);

            request.Method    = "POST";
            request.Timeout   = 8000;
            request.UserAgent = "MisakaTranslator";
            const string boundary = "boundary";

            request.ContentType = "multipart/form-data;boundary=" + boundary;

            var rsw = new StreamWriter(request.GetRequestStream());

            rsw.WriteLine("--" + boundary);
            rsw.WriteLine("Content-Disposition: form-data;name=image;filename=data.png");
            rsw.WriteLine("Content-Type: application/octet-stream");
            rsw.WriteLine("Content-Transfer-Encoding: binary\r\n");
            rsw.Flush();
            rsw.BaseStream.Write(imgdata, 0, imgdata.Length);
            rsw.WriteLine("\r\n--" + boundary + "--");
            rsw.Close();

            try
            {
                using (var resp = await request.GetResponseAsync())
                {
                    string retStr = new StreamReader(resp.GetResponseStream()).ReadToEnd();
                    var    result = JsonConvert.DeserializeObject <Result>(retStr);
                    if (result.error_code == "0")
                    {
                        return(result.data.sumDst);
                    }
                    else
                    {
                        errorInfo = result.error_code + " " + result.error_msg;
                        return(null);
                    }
                }
            }
            catch (WebException ex)
            {
                errorInfo = ex.Message;
                return(null);
            }
        }