开发文档
Delphi
通用识别(JSON / Base64)
Delphi 10 及以上,使用自带的 REST 组件,不需要安装第三方库。新建一个控制台工程,把下面的过程加进去后调用即可。把账号、密码、图片路径换成你自己的。
// 准快识别 Delphi 示例(Delphi 10 及以上,使用自带的 REST 组件,不需要安装第三方库)
// 新建一个控制台工程,把下面的过程加进去,然后调用它即可。
uses
System.SysUtils, System.Classes, System.JSON, System.NetEncoding,
REST.Client, REST.Types;
// 通用识别:JSON + 图片 base64
procedure PredictBase64;
var
Client: TRESTClient;
Request: TRESTRequest;
Response: TRESTResponse;
JsonObj: TJSONObject;
ImgStream: TFileStream;
ImgBytes: TBytes;
ImgBase64: string;
begin
// 接口地址。同时支持 HTTPS,把 http:// 换成 https:// 即可
Client := TRESTClient.Create('http://api.zhunkuai.cn/predict');
Request := TRESTRequest.Create(nil);
Response := TRESTResponse.Create(nil);
JsonObj := TJSONObject.Create;
try
Request.Client := Client;
Request.Response := Response;
Request.Method := rmPOST; // 用 POST 提交
Request.Timeout := 60000; // 超时 60 秒。文档要求 60 秒:太短会被主动断开,仍然扣费
// 读取图片并转成 base64,改成你自己的图片路径。
// 只要图片内容的 base64,不要带 data:image/png;base64, 前缀
ImgStream := TFileStream.Create('captcha.jpg', fmOpenRead);
try
SetLength(ImgBytes, ImgStream.Size);
ImgStream.ReadBuffer(ImgBytes, ImgStream.Size);
ImgBase64 := TNetEncoding.Base64.EncodeBytesToString(ImgBytes);
finally
ImgStream.Free;
end;
// 请求体是 JSON
JsonObj.AddPair('username', '你的账号');
JsonObj.AddPair('password', '你的密码');
JsonObj.AddPair('typeid', '3'); // 数英混合,其他类型见“HTTP接口”文档
JsonObj.AddPair('image', ImgBase64);
// 可选参数用得上再添加,例如:
// JsonObj.AddPair('remark', '题目的文字要求,例如计算题、点选的文字说明');
Request.AddBody(JsonObj.ToJSON, ctAPPLICATION_JSON);
Request.Execute;
// Response.Content 是接口返回的 JSON 文本:
// 成功:{"success":true,"code":"0","message":"success","data":{"result":"hhum","id":"00504808..."}}
// 失败:{"success":false,"code":"-1","message":"失败原因","data":""}
// 失败时 HTTP 状态码通常也是 200,成败只看 success 字段。
// data.result 是识别结果,data.id 是识别编号(识别结果不对时,用它调报错接口)
Writeln(Response.Content);
finally
JsonObj.Free;
Response.Free;
Request.Free;
Client.Free;
end;
end;
报错脚本
// 报错接口:识别结果不对时调用,返还本次的名额或金额。只需要识别编号,不需要账号密码。
// uses 同“通用识别”:System.SysUtils, System.Classes, System.JSON, REST.Client, REST.Types
procedure ReportError;
var
Client: TRESTClient;
Request: TRESTRequest;
Response: TRESTResponse;
JsonObj: TJSONObject;
begin
Client := TRESTClient.Create('http://api.zhunkuai.cn/reporterror.json');
Request := TRESTRequest.Create(nil);
Response := TRESTResponse.Create(nil);
JsonObj := TJSONObject.Create;
try
Request.Client := Client;
Request.Response := Response;
Request.Method := rmPOST;
Request.Timeout := 60000; // 超时 60 秒
JsonObj.AddPair('id', '识别接口返回的 data.id'); // 换成识别接口返回的识别编号
Request.AddBody(JsonObj.ToJSON, ctAPPLICATION_JSON);
Request.Execute;
// 成功返回示例:{"success":true,"code":"0","message":"success","data":"报错成功"}
// 注意 data 是一段文字,不是对象;失败时原因在 message 里
Writeln(Response.Content);
finally
JsonObj.Free;
Response.Free;
Request.Free;
Client.Free;
end;
end;
余额查询(GET)
// 余额查询(GET 请求)。成功时返回账户信息,失败时返回失败原因。
// uses 同“通用识别”:System.SysUtils, System.Classes, System.JSON, REST.Client, REST.Types
procedure QueryAccountInfo;
var
Client: TRESTClient;
Request: TRESTRequest;
Response: TRESTResponse;
begin
Client := TRESTClient.Create('http://api.zhunkuai.cn/queryAccountInfo.json');
Request := TRESTRequest.Create(nil);
Response := TRESTResponse.Create(nil);
try
Request.Client := Client;
Request.Response := Response;
Request.Method := rmGET; // 这个接口是 GET 请求
Request.Timeout := 60000; // 超时 60 秒
// 账号和密码是放在 URL 参数里传的。AddItem 默认就是 URL 参数,会自动做 URL 编码,
// 密码里有 & + % # 或中文也不会传错。密码属于明文传输,建议改用 HTTPS。
Request.Params.AddItem('username', '你的账号');
Request.Params.AddItem('password', '你的密码');
Request.Execute;
// 成功返回示例:
// {"success":true,"code":"0","message":"success","data":{"balance":"0.156","consumed":"0.844","successNum":"557","failNum":"0"}}
// balance 实时余额
// consumed 实时总消费
// successNum 实时总识别成功的次数
// failNum 实时总识别错误数
// 失败时:success 为 false,原因在 message 里
Writeln(Response.Content);
finally
Response.Free;
Request.Free;
Client.Free;
end;
end;