DELPHI XE10 百度车牌识别

源码:https://download.csdn.net/download/weixin_44387646/11060732

一、申请百度账号

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;

六、总结

过程是曲折的,结果还是可以,必要的时候还得看官方的文档

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值