zoukankan      html  css  js  c++  java
  • 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获取。

     
  • 相关阅读:
    Vue基本使用
    缓存数据库
    Web框架
    爬虫基础知识及scrapy框架使用和基本原理
    轮播组件/瀑布流/组合搜索/KindEditor插件
    Model&Form&ModelForm拾遗
    评论操作展示
    评论操作
    windows 下安装 redis
    Notepad++ 列块模式编辑,替换换行符
  • 原文地址:https://www.cnblogs.com/yangxuming/p/15034874.html
Copyright © 2011-2022 走看看