图片文字识别接口
支持 JPG、PNG、WEBP、BMP 图片。普通用户每成功识别一张消耗 1 积分,识别失败自动退回积分。
POST
/ocr-api/api.php身份鉴权
外部程序调用时,将用户 API Key 放入 X-API-Key 请求头。也支持 Bearer Token。
API Key 可在识别平台登录后复制。不要把 Key 写入公开网页、公开仓库或客户端安装包。
推荐请求头
X-API-Key: ocr_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Bearer 方式
Authorization: Bearer ocr_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
请求参数
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
| image | File | 是 | 单张图片,最大 10MB |
| mode | String | 否 | fast 或 best,默认 best |
| keep | Integer | 否 | 传 1 时保留上传文件,默认识别后删除 |
| format | String | 否 | json 或 text,默认 json |
批量识别时,请为每张图片分别发送一个请求。客户端最多可同时提交 30 个请求,服务器会根据 OCR 引擎数量排队处理。
调用示例
curl -X POST "" \ -H "X-API-Key: ocr_你的用户Key" \ -F "image=@test.png" \ -F "mode=fast" \ -F "format=text"
$curl = curl_init('');
$file = new CURLFile('/path/test.png');
curl_setopt_array($curl, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => ['X-API-Key: ocr_你的用户Key'],
CURLOPT_POSTFIELDS => [
'image' => $file,
'mode' => 'fast',
'format' => 'text',
],
]);
$result = curl_exec($curl);
echo $result;
const data = new FormData();
data.append('image', fileInput.files[0]);
data.append('mode', 'fast');
data.append('format', 'text');
const response = await fetch('', {
method: 'POST',
headers: { 'X-API-Key': 'ocr_你的用户Key' },
body: data
});
const text = await response.text();
console.log(text);
成功返回
默认返回 JSON。传 format=text 时,响应类型为 text/plain,响应体只包含识别文字。
{
"ok": true,
"text": "识别出的完整文字",
"lines": [
{
"text": "单行文字",
"score": 0.98,
"box": [[0,0],[100,0],[100,30],[0,30]]
}
],
"confidence": 0.98,
"duration_ms": 1284,
"credits_remaining": 9,
"total_recognitions": 21
}
错误码
| HTTP 状态码 | 含义 | 处理方式 |
|---|---|---|
| 400 | 参数或图片格式错误 | 检查字段名、格式和大小 |
| 401 | 缺少 Key 或 Key 无效 | 检查 X-API-Key |
| 402 | 积分不足 | 联系管理员增加积分 |
| 403 | 用户被停用 | 联系管理员启用账号 |
| 500 | OCR 或服务器异常 | 稍后重试并检查返回 message |