DELPHI XE10 百度车牌识别


本文整理自网络,侵删。

 
一、申请百度账号


uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs,System.json, IPPeerClient, REST.Client,
  Data.Bind.Components, Data.Bind.ObjectScope, Vcl.StdCtrls, Vcl.Imaging.jpeg,
  Vcl.ExtCtrls, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,EncdDecd,
  IdHTTP;
二、获取access_token

方法1:restclient控件方式获取access_token

var
  strtemp: string;
  temp: tjsonobject;
begin
  RESTClient1.BaseURL := 'https://aip.baidubce.com/oauth/2.0/token';
  RESTRequest1.Params.Clear;
  RESTRequest1.AddParameter('grant_type', 'client_credentials');
  RESTRequest1.AddParameter('client_id', 'SErhXXNrcu6TtZg8ztNYvGDY');
  // baidu后台里的API Key
  RESTRequest1.AddParameter('client_secret',
    'SwYYpCNL6MAcl2hokkgR4rq003P3h1Ku'); // baidu后台里的Secret Key
  RESTRequest1.Execute;
  strtemp := RESTResponse1.Content;
  Memo1.Clear;
  Memo1.Lines.Add(strtemp);
方法2:idhttp控件方式获取access_token

Memo1.Clear;
  Memo1.Lines.Add(Getaccess_token('client_credentials',
    'SErhXXNrcu6TtZg8ztNYvGDY', 'SwYYpCNL6MAcl2hokkgR4rq003P3h1Ku'));
小函数

function Getaccess_token(grant_type, client_id, client_secret: string): string;
// 获取token
var
  idhttp2: TIdHTTP;
  authHost: string;
  tokSl: Tstringlist;
begin
  authHost := 'http://aip.baidubce.com/oauth/2.0/token';
  try
    tokSl := Tstringlist.Create;
    idhttp2 := TIdHTTP.Create(nil);
    tokSl.Add('grant_type=' + grant_type);
    tokSl.Add('client_id=' + client_id);
    tokSl.Add('client_secret=' + client_secret);
    Result := idhttp2.Post(authHost, tokSl);
  finally
    tokSl.Free;
    idhttp2.DisposeOf;
  end;
end;
三、先点击前面的按钮再点,显示token

var
  jo: tjsonobject;
  jv: tjsonvalue;
  jsonstr: string; // 要转换的json字符串
begin
 
  jo := nil;
  jsonstr := Memo1.Text;
 
  try
    jo := tjsonobject.Create;
    jo := tjsonobject.parsejsonvalue(tencoding.utf8.getbytes(jsonstr), 0)
      as tjsonobject;
 
    jv := jo.get('access_token').jsonvalue;
    Edit3.Text := jv.value;
    showmessage(jv.value);
  finally
    jo.Free;
  end;
四、restclient控件方式获取车牌

var
  strtemp: string;
  temp: tjsonobject;
  strm: TMemoryStream;
  ss: TStringStream;
  s: string;
begin
  if Form2.Image1.Picture.Graphic <> nil then
  begin
    strm := TMemoryStream.Create;
    Form2.Image1.Picture.SaveToStream(strm);
    ss := TStringStream.Create('');
    strm.Position := 0;
    EncodeStream(strm, ss); // 将内存流编码为base64字符流
    s := ss.DataString;
    strm.Free;
    ss.Free;
  end;
  RESTClient1.BaseURL :=
    'https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate';
  RESTRequest1.Params.Clear;
  RESTRequest1.AddParameter('access_token', Edit3.Text);
  RESTRequest1.AddParameter('image', s);
  RESTRequest1.Execute;
  strtemp := RESTResponse1.Content;
  Memo1.Clear;
  Memo1.Lines.Add(strtemp);
五、显示车牌号

var
  jo: tjsonobject;
  jo2:tjsonobject;
  jv: tjsonvalue;
  jsonstr: string; // 要转换的json字符串
  jsonstr2: string; // 要转换的json字符串
begin
//先取出嵌套的那个字符串
  jo := tjsonobject.parsejsonvalue(memo1.Text) as tjsonobject;
  jsonstr := jo.GetValue('words_result').ToString;
//再对这个字符串取值
  jo2:= tjsonobject.parsejsonvalue(jsonstr) as tjsonobject;
   jsonstr2:= jo.GetValue('words_result').ToString;
  try
    jv := jo2.get('number').jsonvalue;
    Edit1.Text := jv.value;
    showmessage(jv.value);
  finally
    jo.Free;
  end;
六、总结

过程是曲折的,结果还是可以,必要的时候还得看官方的文档
--------------------- 
作者:以后换名字 
来源:CSDN 
原文:https://blog.csdn.net/weixin_44387646/article/details/88816322 

相关阅读 >>

Delphi来实现全屏截图

Delphi之tclientsocket和tserversocket使用tcp keepalive心跳机制实现“断网”、"断电"检测

Delphi使用xmlhttp获取时间

Delphi版的隐藏模块单元 hidemoduleunit.pas

Delphi opentextfiledialog用法

Delphi 搜索指定目录下的文件

Delphi生成guid

Delphi 合并字符串的函数

Delphi多线程学习:多线程数据库查询(ado)

Delphi根据不同分隔符获取字符串内容

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



打赏

取消

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

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

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

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

评论

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