本文主要是介绍DELPHI XE10 百度车牌识别,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
源码:https://download.csdn.net/download/weixin_44387646/11060732
一、申请百度账号
usesWinapi.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
varstrtemp: string;temp: tjsonobject;
beginRESTClient1.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 KeyRESTRequest1.AddParameter('client_secret','SwYYpCNL6MAcl2hokkgR4rq003P3h1Ku'); // baidu后台里的Secret KeyRESTRequest1.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
varidhttp2: TIdHTTP;authHost: string;tokSl: Tstringlist;
beginauthHost := 'http://aip.baidubce.com/oauth/2.0/token';trytokSl := 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);finallytokSl.Free;idhttp2.DisposeOf;end;
end;
三、先点击前面的按钮再点,显示token
varjo: tjsonobject;jv: tjsonvalue;jsonstr: string; // 要转换的json字符串
beginjo := nil;jsonstr := Memo1.Text;tryjo := 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);finallyjo.Free;end;
四、restclient控件方式获取车牌
varstrtemp: string;temp: tjsonobject;strm: TMemoryStream;ss: TStringStream;s: string;
beginif Form2.Image1.Picture.Graphic <> nil thenbeginstrm := 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);
五、显示车牌号
varjo: 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;tryjv := jo2.get('number').jsonvalue;Edit1.Text := jv.value;showmessage(jv.value);finallyjo.Free;end;
六、总结
过程是曲折的,结果还是可以,必要的时候还得看官方的文档
这篇关于DELPHI XE10 百度车牌识别的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!