Delphi实现阿里云印刷文字识别_身份证识别


本文整理自网络,侵删。

 功能
使用阿里云API,通过 APPCODE 的方式,实现身份证
介绍
阿里云使用 AppCode 调用(简单身份认证)原文地址:https://market.aliyun.com/products/57124001/cmapi010401.html?spm=5176.2020520132.101.1.6f0672184ZF393#sku=yuncode440100000

注:阿里云身份证

 

 

uses
  IdBaseComponent, IdComponent,IdTCPConnection, IdTCPClient,IdHTTP,
System.NetEncoding,System.JSON;

function  appcode_demo(appcode,img_file:string):string;
var
 FIdhttp:Tidhttp;
 Fstream:TMemoryStream;
 ss:Tstringstream;
 url:string;
 bodys: TstringList;
 is_old_format:Boolean;
 config:string;
 bodystream:TStringStream;
{*******************************************************}
{                                                       }
{      Delphi实现阿里云印刷文字识别_身份证识别          }
{                                                       }
{ 功能:通过 APPCODE 的方式,实现到被调用接口的身份认证 }
{                                                       }
{                    作者: 阿明                         }
{                  转载请注明出处                       }
{            Copyright (C) amingstudio.com.             }
{                  All Rights Reserved.                 }
{                                                       }
{*******************************************************}
{                      QQ:328783896                    }
{*******************************************************}
begin
   url:='http://dm-51.data.aliyun.com/rest/160601/ocr/ocr_idcard.json';
   Fstream:=TMemoryStream.Create;
   Fstream.LoadFromFile(img_file);
   ss := TStringStream.Create;
   bodys := TstringList.Create;
   config := '{\"side\":\"face\"}';    //正面face,反面back
   is_old_format := false;  //如果输入带有inputs, 设置为True,否则设为False
   try
     TNetEncoding.Base64.Encode(Fstream,ss);
     if is_old_format then
     begin
     bodys.Add('{"inputs" :' + '[{"image" :' + '{"dataType" : 50,' + '"dataValue" :"'   +  StringReplace(ss.DataString, chr(13) + chr(10), '', [rfReplaceAll])   + '"' );
     if config.Length > 0 then
     bodys.Add(',"configure" :' + '{"dataType" : 50,' + '"dataValue" : "' + config + '"}' + ']}');
     end
     else
     begin
     bodys.Add('{"image":  "' + StringReplace(ss.DataString, chr(13) + chr(10), '', [rfReplaceAll]) + '",');
     if config.Length > 0 then
     bodys.Add('"configure": "' + config + '"}');
     end;

     try
     bodystream:=Tstringstream.Create(bodys.Text,TEncoding.UTF8);
     bodystream.Position :=0;
     FIdhttp:=Tidhttp.Create(nil);
     FIdhttp.Request.CustomHeaders.Add('Authorization:' + 'APPCODE ' + appcode);//自定义Header,配置Authorization字段的值为'APPCODE + 半角空格 + APPCODE值'。
     FIdhttp.Request.ContentType:='application/json;charset=UTF-8';//阿里云接受json格式
     Result:=FIdhttp.Post(url,bodystream);
     except
     on e:exception do
      result:=e.Message;
     end;
   finally
     Fstream.Free;
     ss.Free;
     bodystream.Free;
     Fidhttp.Free;
   end;
end;

procedure TForm1.Button1Click(Sender: TObject);//调用
var
 rejson:string;
 m_JsonObject: TJSONObject;
begin
rejson:= appcode_demo('这里填入你的appcode',Edit1.Text);
m_JsonObject := TJSONObject.ParseJSONValue(TEncoding.UTF8.GetBytes(Trim(rejson)), 0) as TJSONObject; //读取json
memo1.Lines.Add( m_JsonObject.Format(4))//将返回的json结果格式化输出

end;

 

来源:https://www.amingstudio.com/delphi/617.html

相关阅读 >>

Delphi 如何设置热键

Delphi截获和干扰窗体最小化�p最大化和关闭消息

Delphi根据进程pid等待进程结束或者结束进程

Delphi 删除整个目录

Delphi 反转字符串方法2

Delphi 取windows登录用户名

Delphi xe6 android拨号函数

Delphi获取系统电源状态的信息

Delphi 通过控件句柄获取控件实例

Delphi的几种类型转换

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...