Skip to content

OCR 接口 ​

OCR 接口包含 OCR 文件导出、单图文字块提取和静态图片条码/二维码识别。当前成者(CZUR)提供的实现中,OCR provider 为 czur-ocr-provider,静态识别 provider 为 czur-recognition-provider。

OCR 与识别方法 ​

方法参数响应 data说明
ocr.recognizeinput_upload_id?: string,input_upload_ids?: string[],input_path?: string,input_files?: string[],output_path?: string,output_dir?: string,format?: txt|pdf|docx|xlsx|ofd|json=docx,exportType?: multi-page|single-page=multi-page,export_type?: multi-page|single-page,params?: object,ext_params?: objecttask_id,task,input_count,output_path,output_dir,output_paths[],format,exportType,provider提交异步 OCR 导出任务。
ocr.gettask_id: string 必填task,provider查询 OCR 任务快照。
ocr.canceltask_id: string 必填cancelled,task,provider请求取消 OCR 任务。
ocr.extract_textinput_upload_id?: string 或 input_path?: stringrecognized,input_path,width,height,blocks[],provider对单张图片做轻量 OCR,返回图片坐标系下的文字块。
recognition.barcode_detectinput_upload_id?: string 或 input_path?: string,formats?: string[],兼容 detect_type?: string[]detected,count,input_path,width,height,barcodes[],provider检测单张静态图片中的条码/二维码。实时条码识别属于采集/视频流能力。

ocr.recognize 输入规则 ​

项目说明
输入图片input_upload_id 和 input_upload_ids 会解析为当前连接可访问的 asset-original 本地文件;input_path 和 input_files 直接使用本地路径。四类输入会合并,最终至少需要一个输入文件。
导出格式format 支持 txt、pdf、docx、xlsx、ofd、json。未传时优先从 output_path 后缀推断;仍为空时默认 docx。jpg / jpeg 不是 OCR 导出格式,会返回参数错误。
多页导出exportType / export_type 为 multi-page 时,多个输入合并导出到一个文件,必须传 output_path。这是默认模式。
单页导出exportType / export_type 为 single-page 时,每个输入导出一个文件,必须能得到 output_dir。如果只传 output_path,当它没有后缀时作为目录使用;有后缀时取父目录作为输出目录。
导出参数params 和 ext_params 会合并后透传给 OCR 引擎;顶层 encoding、paperSize、exportType、ocrPreference、quality、exportFormat 也会写入透传参数。最终会覆盖写入标准化后的 format 和 exportType。
任务状态ocr.recognize 返回排队后的任务快照;后续进度通过 ocr.get 查询。当前 OCR 模块不推送任务事件。

export_type 兼容下划线写法:single_page 会标准化为 single-page,multi_page 会标准化为 multi-page。

ocr.task 字段 ​

字段类型说明
task_idstringOCR 任务 ID,例如 ocr-1。
statusstringqueued、processing、completed、failed、cancelled 或 unknown。
progressnumber任务进度,范围 0..100。
output_pathstring多页导出时为目标文件;单页导出时为输出目录。
output_paths[]string[]已规划或已完成的输出路径。多页导出通常只有一个路径;单页导出会包含每个输入对应的路径。
formatstring标准化后的导出格式。
exportTypestring标准化后的导出方式:multi-page 或 single-page。
messagestring当前阶段或结果消息。
errorstring失败原因;无错误时为空字符串。

ocr.extract_text 输出 ​

ocr.extract_text 只处理单张图片。输入可以是 input_upload_id 或 input_path,响应中的 width、height 和 blocks[] 均使用原图坐标系。

字段类型说明
recognizedbooleanOCR 调用是否成功完成。
input_pathstring实际处理的本地图片路径。
width / heightnumber输入图片尺寸。
blocks[]array识别出的文字块。
providerstring当前为 czur-ocr-provider。

blocks[] 字段:

字段类型说明
textstring识别文本。
x / ynumber文字块左上角坐标。
width / heightnumber文字块矩形尺寸。
confidencenumber识别置信度。
font_sizenumber字号估计值;引擎未返回字号时按文字块高度估算。

recognition.barcode_detect 输出 ​

recognition.barcode_detect 只处理单张静态图片。formats 为空时默认尝试 qrcode、pdf417、code128、ean13、ean8、upca、upce、code39、codabar。

字段类型说明
detectedboolean是否检测到条码/二维码。
countnumberbarcodes[] 数量。当前实现返回单个最优结果。
input_pathstring实际处理的本地图片路径。
width / heightnumber输入图片尺寸。
barcodes[]array条码/二维码结果。
providerstring当前为 czur-recognition-provider。

支持的 formats[] 值包括:qrcode / qr_code、pdf417 / pdf_417、code128 / code_128、code39 / code_39、ean13 / ean_13、ean8 / ean_8、upca / upc_a、upce / upc_e、codabar、datamatrix / data_matrix、aztec、maxicode、itf、rss14 / rss_14、rss_expanded、upc_ean_extension。

barcodes[] 字段:

字段类型说明
formatnumberZXing 内部格式枚举值。
format_namestringZXing 格式名称。
textstring条码/二维码内容。
points[]array识别定位点,字段为 x、y。

请求示例 ​

OCR 导出为单个多页文件:

json
{
  "request_id": "req-ocr-001",
  "method": "ocr.recognize",
  "params": {
    "input_upload_ids": ["img-1760000000-1", "img-1760000000-2"],
    "output_path": "/tmp/demo.txt",
    "format": "txt",
    "exportType": "multi-page",
    "encoding": "utf-8",
    "quality": 90
  }
}

OCR 导出为单页文件:

json
{
  "request_id": "req-ocr-single-001",
  "method": "ocr.recognize",
  "params": {
    "input_files": ["/tmp/page-1.jpg", "/tmp/page-2.jpg"],
    "output_dir": "/tmp/ocr-pages",
    "format": "docx",
    "exportType": "single-page"
  }
}

提取单图文字块:

json
{
  "request_id": "req-ocr-text-001",
  "method": "ocr.extract_text",
  "params": {
    "input_upload_id": "img-1760000000-1"
  }
}

静态图片条码识别:

json
{
  "request_id": "req-barcode-001",
  "method": "recognition.barcode_detect",
  "params": {
    "input_upload_id": "img-1760000000-1",
    "formats": ["qrcode", "pdf417", "code128"]
  }
}

CZUR Open Platform Documentation