接口: OCR
2022/10/22约 374 字大约 1 分钟
ocr.OCR
OCR 对象。由 createOCR 创建,用于具体的文字识别。该对象不再需要时,需要调用 release() 函数释放资源。
目录
方法
方法
detect
▸ detect(image, options?): Promise<OCRResult[]>
对给定图片根据给定选项进行文字识别,将文字识别的结果作为数组异步返回。
示例
"nodejs";
const { createOCR } = require("ocr");
const { requestScreenCapture } = require("media_projection");
async function main() {
const ocr = await createOCR();
const capturer = await requestScreenCapture();
// 获取截图
const img = await capturer.nextImage();
// 识别文字
const results = await ocr.detect(img);
// 输出识别结果
for (const result of results) {
console.log(`文字: ${result.text}, 可信度: ${result.confidence}`);
console.log(`位置: (${result.bounds.left}, ${result.bounds.top})`);
}
ocr.release();
capturer.stop();
}
main();参数
| 名称 | 类型 | 描述 |
|---|---|---|
image | Image | 图片,要识别文字的图片 |
options | OCRDetectionOptions | OCR 识别选项,包括识别区域、最大识别数量、是否检测旋转等 |
返回值
Promise<OCRResult[]>
文字识别结果的数组的 Promise,包括可信度、文本内容、文本范围等。
release
▸ release(): void
释放 OCR 资源,默认会在程序退出时自动释放,但请在不使用 OCR 时及时释放以释放资源。
重要
OCR 对象占用内存和计算资源较大,使用完毕后应该及时调用 release() 方法释放资源,避免内存泄漏。
示例
"nodejs";
const { createOCR } = require("ocr");
async function main() {
const ocr = await createOCR();
// 使用 OCR 进行识别...
// const results = await ocr.detect(img);
// 使用完毕后释放资源
ocr.release();
}
main();返回值
void
