5 分钟接入印刷体OCR识别:从注册到第一条识别结果
# 5 分钟接入印刷体OCR识别:从注册到第一条识别结果
> 接口 926(接入点 926-1) · 免费 · POST/GET · JSON · 适用人群:新注册用户、初级开发者 · 阅读时间:约 5 分钟
## 核心要点
- 印刷体OCR识别是**免费接口**,注册并拿到 AppKey 后即可调用,无需付费。
- 调用只需一个参数:图片(`img_url` 远程地址,或 `img_base64` 本地编码)二选一。
- 返回业务数据在 `showapi_res_body` 内,识别成功时 `ret_code=0`;带坐标需传 `need_all_region=1`。
## Why:这跟你有什么关系
你有一张带文字的图片(截图、文档、名片、票据),想把它变成可编辑的文字——手动敲太慢,上大模型又重。印刷体OCR识别就是为这个场景准备的:发一张图,返回文字。它**免费**、**文档清晰**、**支持中文英文**,适合做文档电子化、搜索索引、内容审核的前置提取。
## What:前置条件与接口速览
**前置条件**
- 一个 ShowAPI 账号(注册免费)。
- 一对 AppKey(在控制台「我的应用」创建并获取)。
- 一张 jpg/png 图片,像素 < 1200×1200,base64 方式 ≤ 0.7M、URL 方式 ≤ 1M。
**接口速览**
| 项目 | 说明 |
|------|------|
| 接口地址 | `https://route.showapi.com/926-1?appKey={your_appKey}` |
| 接入点 | 926-1(印刷体图片文字识别) |
| 请求方式 | POST / GET |
| 鉴权 | query 参数 `appKey` |
| 返回格式 | JSON |
| 计费 | 免费(按使用档次限流) |
| 集成 | MCP、OpenAPI 3.0(YAML/JSON) |
## How:第一次调用
### 步骤 1:准备 AppKey
打开 [AppKey 管理页](https://www.showapi.com/console#/myApp),创建应用并复制 AppKey。把下面代码里的 `YOUR_APPKEY` 替换成它。
### 步骤 2:用一张图片发起识别
下面以「远程图片 URL」方式演示(`need_all_region=1` 让返回带每行坐标与置信度)。
**Python(requests)**
```python
import requests
url = "https://route.showapi.com/926-1"
params = {"appKey": "YOUR_APPKEY"}
data = {
"img_url": "http://showapi-pub-hangzhou.oss-cn-hangzhou.aliyuncs.com/huangye/img_2d05ae9b-0823-4cde-9f21-79bf89e6b87d.png",
"need_all_region": "1",
}
try:
r = requests.post(url, params=params, data=data, timeout=10)
r.raise_for_status()
body = r.json()["showapi_res_body"]
if body["ret_code"] != 0:
print("识别失败:", body["ret_code"], body.get("remark"))
else:
for item in body.get("list", []):
print(item["text"], round(item["confidence"], 3))
except requests.RequestException as e:
print("请求异常:", e)
```
**cURL**
```bash
curl -X POST "https://route.showapi.com/926-1?appKey=YOUR_APPKEY" \
-H "content-type: application/x-www-form-urlencoded" \
-d "img_url=http%3A%2F%2Fshowapi-pub-hangzhou.oss-cn-hangzhou.aliyuncs.com%2Fhuangye%2Fimg_2d05ae9b-0823-4cde-9f21-79bf89e6b87d.png&need_all_region=1"
```
**Node.js(fetch)**
```javascript
const url = "https://route.showapi.com/926-1?appKey=YOUR_APPKEY";
const body = new URLSearchParams({
img_url: "http://showapi-pub-hangzhou.oss-cn-hangzhou.aliyuncs.com/huangye/img_2d05ae9b-0823-4cde-9f21-79bf89e6b87d.png",
need_all_region: "1",
});
const res = await fetch(url, {
method: "POST",
headers: { "content-type": "application/x-www-form-urlencoded" },
body,
});
const data = await res.json();
const rb = data.showapi_res_body;
if (rb.ret_code !== 0) {
console.error("识别失败:", rb.ret_code, rb.remark);
} else {
(rb.list || []).forEach((it) => console.log(it.text, it.confidence));
}
```
### 步骤 3:解析返回
识别成功时 `ret_code=0`,文字在 `list`(传了 `need_all_region=1`)或 `str`(未传,整段字符串)中。详见下一篇《返回字段全解》。
## 返回示例与解析
```json
{
"showapi_res_error": "",
"showapi_fee_num": 1,
"showapi_res_code": 0,
"showapi_res_id": "68b69116fb638ca1bd1a1fa0",
"showapi_res_body": {
"ret_code": 0,
"remark": "",
"list": [
{
"text": "新接口上线一ip查询专业版",
"confidence": 0.99603927135468,
"text_region": [
[373, 40], [683, 40], [683, 65], [373, 65]
]
}
]
}
}
```
| 字段 | 类型 | 说明 |
|------|------|------|
| `showapi_res_code` | int | 系统级状态码,0 表示请求成功 |
| `showapi_res_body.ret_code` | number | 业务码:0 成功;10~90 为错误(见错误码篇) |
| `showapi_res_body.list` | array | 每行识别结果(需 `need_all_region=1`);元素含 `text`/`confidence`/`text_region` |
| `showapi_res_body.str` | string | 整段识别结果(未传 `need_all_region` 时返回,按行以换行符分隔) |
| `showapi_res_body.remark` | string | 错误信息(失败时) |
## 进阶 / 边界
- **本地图片**用 `img_base64`:读取文件转 base64 传入(示例见《img_base64 与 img_url 怎么选》)。
- **并发限制为 10**、图片像素建议 < 1200×1200;超限容易触发 `50 文件内容过大` 或 `80 服务超时`。
- 免费接口按「使用档次」限流,高调用量可用平台积分兑换更高档位(见免费档位说明)。
## FAQ
**Q1:提示「拒绝访问 / 无权限」?**
A1:检查 AppKey 是否正确、是否已在本账号「我的应用」中创建;接口为免费接口,注册即可用,无需单独购买。
**Q2:返回里没有 list 只有 str?**
A2:未传 `need_all_region=1` 时返回 `str`(整段字符串);需要每行坐标和置信度就传 `need_all_region=1`,此时返回 `list`。
**Q3:img_url 和 img_base64 必须都传吗?**
A3:都不需要「都传」,二者**二选一**即可;都不传会返回 `10 参数错误`。
**Q4:支持手写吗?**
A4:文档说明支持「部分手写楷书中文」,但非全部手写体;不稳定时建议人工复核(见手写楷书篇)。
**Q5:返回中文乱码?**
A5:接口返回 UTF-8 JSON,确保客户端按 UTF-8 解析(如 Python `r.json()`、前端 `res.json()`)。
## 相关能力 / 下一步阅读
- [印刷体OCR识别返回字段全解:ret_code 与识别结果一文读懂](https://www.showapi.com/guides/printed-ocr-response-codes-926)
- [印刷体OCR识别:img_base64 与 img_url 两种入参怎么选?](https://www.showapi.com/guides/printed-ocr-base64-vs-url-926)
- [印刷体OCR识别错误码排查:10 参数错误到 90 识别异常逐条对照](https://www.showapi.com/guides/printed-ocr-error-handling-926)
- **本系列共 12 篇**:查看[印刷体OCR识别指南总目录](https://www.showapi.com/guides/printed-ocr-guides-926)