本文整理自网络,侵删。
功能
使用阿里云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》频道 >>