前端开发攻略---使用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>
相关推荐
雪隐4 分钟前
用Flutter做背单词APP-03为了给单词库塞满数据,我让AI给我打了上万份工
前端·人工智能·后端
hunterandroid4 分钟前
Android 安全最佳实践:从数据存储到网络通信的防护思路
前端
H Journey5 分钟前
web开发学习:html、css、js
前端·css·html·js
hunterandroid10 分钟前
Compose 性能优化:从卡顿到丝滑的实战经验
前端
hunterandroid14 分钟前
Kotlin Coroutines 在 Android 项目中的落地实战
前端
不一样的少年_22 分钟前
不用框架,手搓 AI Agent:(一) 先让它跑起来
前端·后端·agent
味悲23 分钟前
浏览器解析机制与XSS的15种编码绕过
前端·xss
小牛itbull32 分钟前
【译】为什么我使用 React 重构了 WordPress?
前端·react.js·重构
用户0595401744632 分钟前
AI Agent 记忆存储踩坑实录:这个问题让我排查了 3 天,最终用 pytest + Docker 实现秒级回归
前端·css
用户2986985301434 分钟前
在 React 中拆分 Excel 文件:从工作表到行列的实践
javascript·react.js·excel