频道分类

Delphi 调用api.ocr.space的OCR接口

作者:admin 来源: 日期:2020/2/8 21:43:38 人气: 标签:

 


unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, IdComponent,
  IdServerIOHandler, IdSSL, IdSSLOpenSSL, IdBaseComponent, IdAntiFreezeBase,
  IdAntiFreeze, IdIOHandler, IdIOHandlerSocket, IdIOHandlerStack,
  dxGDIPlusClasses, Vcl.ExtCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    IdAntiFreeze1: TIdAntiFreeze;
    IdSSLIOHandlerSocketOpenSSL1: TIdSSLIOHandlerSocketOpenSSL;
    Image1: TImage;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses IdHTTP,PngImage,EncdDecd,System.JSON, IdTCPConnection, IdTCPClient,
IdMultipartFormData,IdGlobal;


function OCR_Online( OCR_ApiKey_OcrSpace :String; aPNG: TPngImage; aFileName:String = '' ): String;
// Uses idMultipartFormData, JSON, EncdDecd,
//      System.Generics.Collections,
//      System.NetEncoding;
 function PNGToBase64Image( aPNGImage: TPngImage ): string;
 var
   Input : TMemoryStream;
   Output: TStringStream;
 begin
   Input := TMemoryStream.Create;
   try
     aPNGImage.SaveToStream(Input);
     Input.Position := 0;
     Output := TStringStream.Create('');
     try
       EncdDecd.EncodeStream(Input, Output);
       Result := Output.DataString;
     finally
       Output.Free;
     end;
   finally
     Input.Free;
   end;
 end;
var
 IdHttp              : TIdHTTP;
 IdMultiPartFormData : TIdMultiPartFormDataStream;
 idhttps:tIdIOHandlerSocket;
 JsonObject          : TJSONObject;
 JSonValue           : TJSONValue;
 sGelen              : string;
 sSonuc              : string;
begin
 IdHTTP := TIdHTTP.Create(nil);
 IdMultiPartFormData := TIdMultiPartFormDataStream.Create;
 JSONObject  := TJSONObject.Create;
 sSonuc := '';
 try

 idhttps := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
IdHTTP.IOHandler := idhttps;

   IdHTTP.Request.ContentType := 'application/x-www-form-urlencoded';

   IdMultiPartFormData.AddFormField(  'apikey',             OCR_ApiKey_OcrSpace );
   IdMultiPartFormData.AddFormField(  'language',          'chs'      );
   IdMultiPartFormData.AddFormField(  'filetype',          'png'      );
   IdMultiPartFormData.AddFormField(  'isOverlayRequired', 'false'    );

   if aFileName <> ''
     then IdMultiPartFormData.AddFile     (  'file', aFileName, 'image/png') // Fiziki Dosya İçin...
     else IdMultiPartFormData.AddFormField(  'base64Image',
                               'data:image/png;base64,' + PNGToBase64Image(aPNG),
                               'base64',
                               'image/png' ).ContentTransfer := '8bit';

   sGelen      := IdHttp.Post('https://api.ocr.space/parse/image', IdMultiPartFormData);

   JsonValue   := JSONObject.ParseJSONValue(sGelen);
   JsonValue   :=(JSONValue as TJSONObject).Get('ParsedResults').JSONValue;

   if ( JSONValue is TJSONArray)
     then sSonuc := (  (JsonValue as TJSONArray)
                       .Items[0] as TJSonObject
                    )  .Get('ParsedText')
                       .JSONValue
                       .Value;
   Result :=  sSonuc;
 finally
   JSONObject.Free;
   IdMultiPartFormData.Free;
   IdHttp.Free;
 end;
end;


procedure TForm1.Button1Click(Sender: TObject);
var
png:TPNGImage;
begin
png:=TPNGImage.Create;
png.LoadFromFile('QQ图片20200208204805.png');
//memo1.Text := OCR_Online( '5f882ApiKey8957', TPNGImage(png) );
memo1.Text := OCR_Online( '5f882ApiKey8957', nil, 'QQ图片20200208204805.png' );
end;

end.

KEY 申请地址:https://ocr.space/OCRAPI