Dynamics 365 for phone 如何调用PAAS服务实现ID Card的OCR识别

Posted Vic.Tang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Dynamics 365 for phone 如何调用PAAS服务实现ID Card的OCR识别相关的知识,希望对你有一定的参考价值。

      之前的博文已有介绍Dynamics 365 for phone已实现调用手机相机进行拍照,文末也提到可以借助OCR服务实现名片、证件等的识别,本片就来分享下怎么使用OCR服务实现身份证的识别。

      OCR的服务提供商有很多,本篇用的是阿里云的服务,花1分钱做个POC

      识别服务的API就写在了自定义Action,封装好供前端的JS调用,下面是自定义Action中的部分代码,供参考,OCR服务的调用代码也可以参考阿里云网上上的指引。

            String base64 = context.InputParameters["image"].ToString();          
            String url = "http://dm-51.data.aliyun.com/rest/160601/ocr/ocr_idcard.json";
            String appcode = "阿里云里查看";
            //如果输入带有inputs, 设置为True,否则设为False
            bool is_old_format = false;
            //如果没有configure字段,config设为''
            //String config = '';
            String config = "\\\\\\"side\\\\\\":\\\\\\"face\\\\\\"";
            String method = "POST";
            String querys = "";
            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 = (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)
            
                Stream st = httpResponse.GetResponseStream();
                StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8"));
                tracingService.Trace("false");
                context.InputParameters["success"]=false;
            
            else
            
                Stream st = httpResponse.GetResponseStream();
                StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8"));
                string result = reader.ReadToEnd();
                tracingService.Trace("true");
                context.InputParameters["success"]=true;
                context.InputParameters["result"] = result;
            

         身份证识别出来的Json字符串是如下的格式(具体的格式参见你所使用的OCR服务供应商)

"address":"上海市",
    "angle":90,
    "birth":"19880229",
    "card_region":["x":109,"y":61,"x":1122,"y":68,"x":1110,"y":1643,"x":123,"y":1646],
    "config_str":"\\"side\\":\\"face\\"",
    "face_rect":"angle":90,"center":"x":668,"y":1277,"size":"height":487,"width":577,
    "face_rect_vertices":["x":958,"y":1034,"x":956,"y":1522,"x":378,"y":1519,"x":380,"y":1032],
    "is_fake":false,"name":"名字","nationality":"汉",
    "num":"身份证号",
    "request_id":"ED05F6D5-9EAD-4F00-BFAD-34567506C55C","sex":"男","success":true

      前端获取到这个信息后解析Json的信息,前端赋值或者执行逻辑即可,和上篇中的JS代码类似。

function CapturePhoto(clientContext) 
    debugger;
    var client = Xrm.Utility.getGlobalContext().client;
    if (client.getClient() == 'Mobile') 

        var imageOptions = 
            allowEdit: true,
            height: 250,
            width: 400,
            preferFrontCamera: true,
            quality: 100
        ;

        Xrm.Device.captureImage(imageOptions).then(
            function success(result) 
                var Id = clientContext.data.entity.getId().replace('', '').replace('', '');

                // var alertStrings =  confirmButtonLabel: 'Yes', text: 'fileName:' + result.fileName + 'fileSize:' + result.fileSize ;
                // var alertOptions =  height: 120, width: 260 ;
                // Xrm.Navigation.openAlertDialog(alertStrings, alertOptions);

                // perform operations on the captured image
                var actionPara =
                
                    "image": result.fileContent
                
                var info = ExecuteAction("contacts(" + Id + ")/Microsoft.Dynamics.CRM.new_OCR", actionPara);
                //页面赋值处理逻辑
            ,
            function (error) 
                console.log(error.message);
                // handle error conditions
            
        );
    


function ExecuteAction(actionUrl, actionPara) 
    /*
    *  Common method to execute action  in sync mode based on custom query.
    */
    var lResponse = null;
    var lXMLHttpRequest = new XMLHttpRequest();
    lXMLHttpRequest.open("POST", encodeURI(Xrm.Utility.getGlobalContext().getClientUrl() + "/api/data/v9.2/" + actionUrl), false);
    lXMLHttpRequest.setRequestHeader("Accept", "application/json");
    lXMLHttpRequest.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    lXMLHttpRequest.setRequestHeader("OData-MaxVersion", "4.0");
    lXMLHttpRequest.setRequestHeader("OData-Version", "4.0");
    lXMLHttpRequest.send(JSON.stringify(actionPara));
    if (lXMLHttpRequest.status === 200) 
        lResponse = JSON.parse(lXMLHttpRequest.responseText);      
    
    else 
        //added code to avoid object null error
        if (lXMLHttpRequest.statusText !== null && lXMLHttpRequest.statusText !== undefined && lXMLHttpRequest.statusText !== "") 
            Xrm.Navigation.openAlertDialog(lXMLHttpRequest.statusText);
        
    
    return lResponse;

以上是关于Dynamics 365 for phone 如何调用PAAS服务实现ID Card的OCR识别的主要内容,如果未能解决你的问题,请参考以下文章

Dynamics 365 for phone 如何调用PAAS服务实现ID Card的OCR识别

Dynamics 365 for Phones Android APK(v4.3.22042.2)下载

Dynamics 365 for Phones Android APK(v4.3.22042.2)下载

Dynamics 365 App for Outlook 与 Dynamics 365 for Outlook(已被弃用)

Monthly update for Dynamics 365 for Operation

Dynamics 365 for CRM: Sitemap站点图的可视化编辑功能