前端开发攻略---使用ocr识别图片进行文字提取功能

1、引入资源

通过链接引用

html 复制代码
 <script src="https://cdn.bootcdn.net/ajax/libs/tesseract.js/5.1.0/tesseract.min.js"></script>

npm或其他方式下载

html 复制代码
npm i tesseract

2、示例

html 复制代码
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <script src="https://cdn.bootcdn.net/ajax/libs/tesseract.js/5.1.0/tesseract.min.js"></script>
  <body>
    <img src="./en.png" alt="" />
    <button onclick="orcBtn1()">识别英文</button>
    <hr />
    <img src="./zn.png" alt="" />
    <button onclick="orcBtn2()">识别中文</button>
    <hr />
    <img src="./1.png" alt="" />
    <button onclick="orcBtn3()">识别中英文</button>
    <hr />
  </body>
  <script>
    const btn = document.querySelector('button')
    function orcBtn1() {
      Tesseract.recognize('./en.png').then(res => {
        console.log(res.data.text)
      })
    }
    function orcBtn2() {
      Tesseract.recognize('./zn.png', 'chi_sim').then(res => {
        console.log(res.data.text)
      })
    }
    function orcBtn3() {
      Tesseract.recognize('./1.png', 'eng+chi_sim').then(res => {
        console.log(res.data.text)
      })
    }
  </script>
</html>
相关推荐
其实防守也摸鱼1 小时前
Day 3 · Agent Loop:从调用模型到控制循环 —— 课件精讲笔记
服务器·开发语言·javascript·数据库·人工智能·笔记·ecmascript
夕水2 小时前
我把颜色选择器拆成了 15 个插件,最后长成了 22 个包
前端·javascript·typescript
大今野2 小时前
2026 专转本计算机|移动互联网关键技术:SOA、Web2.0 与 HTML5 考点全解(附例题)
前端·html·html5·专转本
JudithHuang2 小时前
Vue3 与后端联调:代理、Mock 与接口约定
前端·vue
Swot2 小时前
工具转换 vs LLM 转换:以 Markdown 转 AsciiDoc 为例
前端
uuware2 小时前
Lupine.js 的 Native 5 端实现:一套代码覆盖 iOS、Android、macOS、Windows 和 Web
前端·typescript
用户88956195481713 小时前
戴上眼镜就把今天听完:绿光快报的 AIUI 语音新闻智能体
javascript
lisw053 小时前
前端:概念、历史、内容、挑战与展望!
前端
stormsha3 小时前
ResizeObserver loop completed with undelivered notifications:它只是个警告,但也是你代码的供词
javascript·css