Pic2txt API 开发文档
欢迎使用 Pic2txt 智能文字识别 API。本文档将帮助您快速集成 OCR 服务到您的应用程序中。
概述
Pic2txt 提供强大的 OCR 文字识别能力,支持身份证、营业执照、银行卡、名片、发票等多种证件票据的智能识别。通过简单的 HTTP API 调用,您可以快速将图片中的文字信息提取为结构化数据。
主要特性
- 多格式支持 - 支持 JPG、PNG、BMP 等常见图片格式
- 多种上传方式 - 支持图片文件上传和 URL 地址两种方式
- 高精度识别 - 基于深度学习模型,识别准确率达 99.9%
- 快速响应 - 平均响应时间小于 100ms
快速开始
按照以下步骤,您可以在几分钟内完成 API 集成:
- 注册账号 - 访问 控制台 注册并登录您的账号
- 获取 API Key - 在控制台创建并获取您的 API 密钥
- 调用接口 - 使用 API Key 调用 OCR 识别接口
新用户注册即可获得 500 次免费调用额度,有效期 30 天。
认证方式
Pic2txt API 使用 Bearer Token 认证方式。您需要在请求头中添加 Authorization 字段,格式如下:
Authorization Header
Authorization: Bearer YOUR_API_KEY
请妥善保管您的 API Key,不要在客户端代码中暴露,建议在服务端调用 API。
OCR 识别接口
这是核心的 OCR 识别接口,用于识别图片中的文字信息并返回结构化数据。
POST
/api/v1/ocr
请求方式
支持以下两种方式传递图片:
- 文件上传 - 通过 multipart/form-data 上传图片文件
- URL 地址 - 通过表单字段传递图片的 URL 地址
请求参数
Header 参数
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| Authorization | string | 必填 | Bearer Token 认证信息,格式:Bearer YOUR_API_KEY |
| Content-Type | string | 必填 | 文件上传时使用 multipart/form-data;URL 方式使用 application/json |
Body 参数
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| image | file | 二选一 | 图片文件,支持 JPG、PNG、BMP 格式,最大 5MB |
| image_url | string | 二选一 | 图片 URL 地址,需要可公开访问 |
响应格式
API 返回 JSON 格式的响应数据,包含识别结果和状态信息。
成功响应
响应示例
{
"status": "success",
"data": {
"type": "身份证",
"fields": {
"姓名": "张三",
"性别": "男",
"民族": "汉",
"出生": "1990年01月01日",
"住址": "北京市朝阳区某某街道某某号",
"公民身份号码": "110101199001011234"
}
}
}
响应字段说明
| 字段名 | 类型 | 说明 |
|---|---|---|
| status | string | 请求状态,success 表示成功,error 表示失败 |
| data | object | 识别结果数据 |
| data.type | string | 识别出的证件/票据类型 |
| data.fields | object | 识别出的字段信息,键为字段名,值为字段值 |
代码示例
以下是不同编程语言的调用示例,帮助您快速集成 API。
# 文件上传方式
curl -X POST 'https://your-domain.com/api/v1/ocr' \
-H 'Authorization: Bearer YOUR_API_KEY' \
-F 'image=@/path/to/image.jpg'
# URL 方式
curl -X POST 'https://your-domain.com/api/v1/ocr' \
-H 'Authorization: Bearer YOUR_API_KEY' \
-H 'Content-Type: application/json' \
-d '{"image_url": "https://example.com/image.jpg"}'
// 文件上传方式
const FormData = require('form-data');
const fs = require('fs');
const form = new FormData();
form.append('image', fs.createReadStream('/path/to/image.jpg'));
fetch('https://your-domain.com/api/v1/ocr', {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
...form.getHeaders()
},
body: form
})
.then(res => res.json())
.then(data => console.log(data));
// URL 方式
fetch('https://your-domain.com/api/v1/ocr', {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json'
},
body: JSON.stringify({
image_url: 'https://example.com/image.jpg'
})
})
.then(res => res.json())
.then(data => console.log(data));
import requests
# 文件上传方式
url = 'https://your-domain.com/api/v1/ocr'
headers = {'Authorization': 'Bearer YOUR_API_KEY'}
files = {'image': open('/path/to/image.jpg', 'rb')}
response = requests.post(url, headers=headers, files=files)
print(response.json())
# URL 方式
url = 'https://your-domain.com/api/v1/ocr'
headers = {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json'
}
data = {'image_url': 'https://example.com/image.jpg'}
response = requests.post(url, headers=headers, json=data)
print(response.json())
new CURLFile('/path/to/image.jpg')
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
// URL 方式
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://your-domain.com/api/v1/ocr');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer YOUR_API_KEY',
'Content-Type: application/json'
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
'image_url' => 'https://example.com/image.jpg'
]));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
?>
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
public class Pic2txtExample {
public static void main(String[] args) throws Exception {
// 文件上传方式
String boundary = "----WebKitFormBoundary" + System.currentTimeMillis();
URL url = new URL("https://your-domain.com/api/v1/ocr");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization", "Bearer YOUR_API_KEY");
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
OutputStream os = conn.getOutputStream();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(os, "UTF-8"), true);
writer.append("--" + boundary).append("\r\n");
writer.append("Content-Disposition: form-data; name=\"image\"; filename=\"image.jpg\"").append("\r\n");
writer.append("Content-Type: image/jpeg").append("\r\n\r\n");
writer.flush();
FileInputStream fis = new FileInputStream("/path/to/image.jpg");
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
}
fis.close();
writer.append("\r\n--" + boundary + "--\r\n");
writer.close();
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
}
}
错误码说明
当 API 调用失败时,会返回相应的错误信息,请根据错误码进行排查。
错误响应格式
{
"status": "error",
"message": "错误描述信息"
}
常见错误码
| HTTP 状态码 | 错误信息 | 说明 |
|---|---|---|
| 401 | 缺少API密钥 | 请求头中未包含 Authorization 字段 |
| 401 | 无效的API密钥 | 提供的 API Key 不存在或已失效 |
| 403 | API密钥已被禁用 | 该 API Key 已被管理员禁用 |
| 400 | 请提供图片文件或图片URL地址 | 请求中未包含图片数据 |
| 500 | 图片处理失败 | 服务器内部错误,请稍后重试 |