• C# 印刷文字识别-身份证识别


    1.阿里免费提供500次识别。需要购买该产品。

    2.产品购买成功后,获取appcode值,这个要放代码中的appcode中。

    3.识别的图片路径

         public static void Main(string[] args)
            {
                //身份证识别
                // Console.WriteLine("Hello World!");
                String url = "http://dm-51.data.aliyun.com/rest/160601/ocr/ocr_idcard.json";
                String appcode = "自己的AppCode";  
                String img_file = "识别的图片路径"; 
    
                //如果输入带有inputs, 设置为True,否则设为False
                bool is_old_format = false;
    
                //如果没有configure字段,config设为''
                //String config = '';
                String config = "{\"side\":\"face\"}";
    
                String method = "POST";
    
                String querys = "";
    
                FileStream fs = new FileStream(img_file, FileMode.Open);
                BinaryReader br = new BinaryReader(fs);
                byte[] contentBytes = br.ReadBytes(Convert.ToInt32(fs.Length));
                String base64 = System.Convert.ToBase64String(contentBytes);
                String bodys;
                if (is_old_format)
                {
                    bodys = "{"inputs" :" +
                                        "[{"image" :" +
                                            "{"dataType" : 50," +
                                             ""dataValue" :"" + base64 + """ +
                                             "}";
                    if (config.Length > 0)
                    {
                        bodys += ","configure" :" +
                                        "{"dataType" : 50," +
                                         ""dataValue" : "" + config + ""}" +
                                         "}";
                    }
                    bodys += "]}";
                }
                else
                {
                    bodys = "{"image":"" + base64 + """;
                    if (config.Length > 0)
                    {
                        bodys += ","configure" :"" + config + """;
                    }
                    bodys += "}";
                }
                HttpWebRequest httpRequest = null;
                HttpWebResponse httpResponse = null;
    
                if (0 < querys.Length)
                {
                    url = url + "?" + querys;
                }
    
                if (url.Contains("https://"))
                {
                    ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                    //httpRequest 路径
                    httpRequest = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url));
                }
                else
                {
                    httpRequest = (HttpWebRequest)WebRequest.Create(url);
                }
                httpRequest.Method = method;
                httpRequest.Headers.Add("Authorization", "APPCODE " + appcode);
                //根据API的要求,定义相对应的Content-Type
                httpRequest.ContentType = "application/json; charset=UTF-8";
                if (0 < bodys.Length)
                {
                    byte[] data = Encoding.UTF8.GetBytes(bodys);
                    using (Stream stream = httpRequest.GetRequestStream())
                    {
                        stream.Write(data, 0, data.Length);
                    }
                }
                try
                {
                    httpResponse = (HttpWebResponse)httpRequest.GetResponse();
                }
                catch (WebException ex)
                {
                    httpResponse = (HttpWebResponse)ex.Response;
                }
    
                if (httpResponse.StatusCode != HttpStatusCode.OK)
                {
                    Console.WriteLine("http error code: " + httpResponse.StatusCode);
                    Console.WriteLine("error in header: " + httpResponse.GetResponseHeader("X-Ca-Error-Message"));
                    Console.WriteLine("error in body: ");
                    Stream st = httpResponse.GetResponseStream();
                    StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8"));
                    Console.WriteLine(reader.ReadToEnd());
                }
                else
                {
    
                    Stream st = httpResponse.GetResponseStream();
                    StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8"));
                    Console.WriteLine(reader.ReadToEnd());
    
                }
                Console.WriteLine("
    ");
                Console.ReadLine();
            }
    
            public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
            {
                return true;
            }
    

      

  • 相关阅读:
    ConcurrentHashMap
    Linux中如何开启8080端口供外界访问 和开启允许对外访问的端口8000
    CentOs 7 Linux系统下我的/etc/sysconfig/路径下无iptables文件
    CentOS7开启SSH服务
    Centos7下Samba服务器配置
    CentOS7(Linux)网络yum源配置
    Linux(Centos7)中配置Java环境变量
    SpringAOP-什么是面向切面编程?
    Swagger Demo
    自定义个Bean名称生成策略, 解决不同包下同名类问题/AnnotationBeanNameGenerator
  • 原文地址:https://www.cnblogs.com/BabyRui/p/10731341.html
Copyright © 2020-2023  润新知