delphi调用百度API识别图片中的身份证号
procedure TForm1.cxButton_checkIDClick(Sender: TObject); var base64: string; token: string; host: string; postData: string; ss: TStringStream; mm: TMemoryStream; StringBytes: TBytes; Encoding: TEncoding; begin ss := TStringStream.Create('', TEncoding.UTF8); // mm := TMemoryStream.Create; try host := 'https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?access_token=' + access_token; // mm.LoadFromFile(checkedFileName); base64 := LoadFileToBase64(checkedFileName); // cxMemo1.Lines.Add(System.Net.URLClient.TURI.URLEncode(base64)); postData := 'id_card_side=front&image=' + System.Net.URLClient.TURI.URLEncode(base64); ss.WriteString(postData); self.NetHTTPClient1.Accept := 'text/html, application/xhtml+xml, */*'; self.NetHTTPClient1.ContentType := 'application/x-www-form-urlencoded'; //application/json self.NetHTTPClient1.AcceptEncoding := 'UTF-8'; ss.Position := 0; // cxMemo1.Clear; cxMemo1.Lines.Add(NetHTTPClient1.Post(host, ss).ContentAsString(tencoding.UTF8)); finally ss.Free; mm.Free; end; end; function TForm1.LoadFileToBase64(const AFileName: string): string; var getStream: TFileStream; {声明一个文件流} arr: TBytes; base64: string; bs64: TBase64Encoding; begin try bs64 := TBase64Encoding.Create(0); getStream := TFileStream.Create(AFileName, fmOpenRead or fmShareExclusive); SetLength(arr, getStream.Size); getStream.Position := 0; getStream.Read(arr, getStream.size); base64 := bs64.EncodeBytesToString(arr); Result := base64; finally bs64.Free; getStream.Free; end; end;
token 自行调用百度api获取。