来访~103061 文章~106 评论~25
2022年6月7日 作者 张临志

uni-app集成微信小程序OCR扫描

  1. 在manifest.json中mp-weixin里面添加
    "plugins": {
        "ocr-plugin": {
            "version": "3.1.2",
            "provider": "wx4418e3e031e551be"
        }
    }
  2. demo如下
    <template>
      <view class="uni-container">
        <uni-nav-bar :fixed="true" color="#ffffff"
                     background-color="#04a0eb"
                     :status-bar="true"
                     title="OCR识别"
                      />
        <button class="button" @click="clickCertificate">扫描合格证</button>
        <view>扫描结果:{{certificate}}</view>
        <button class="button" @click="clickVin">扫描车辆</button>
        <view>扫描结果:{{vin}}</view>
      </view>
    
    </template>
    <script>
    
    
    export default {
      components: {
      },
      data() {
        return {
          certificate: '',
          vin: ''
        }
      },
      created() {
      },
      //每次进页面都会执行的方法
      onShow(){
        //工作状态,运维人员才显示
      },
      methods: {
        clickCertificate() {
          let that = this
          // 选择图片
          wx.chooseImage({
            count: 1,
            success: async function(res) {
              try {
                let invokeRes = await wx.serviceMarket.invokeService({
                  service: 'wx79ac3de8be320b71',
                  api: 'OcrAllInOne',
                  data: {
                    // 用 CDN 方法标记要上传并转换成 HTTP URL 的文件
                    img_url: new wx.serviceMarket.CDN({
                      type: 'filePath',
                      filePath: res.tempFilePaths[0],
                    }),
                    data_type: 3,
                    ocr_type: 8
                  },
                })
                console.log('invokeService success', invokeRes)
                let tmp = invokeRes.data.ocr_comm_res.items
                for (let i = 0; i < tmp.length;i++) {
                  let no = tmp[i].text
                  if (no.substring(0,3) === 'LSG') {
                    that.certificate = no
                  }
                }
              } catch (err) {
                console.error('invokeService fail', err)
                wx.showModal({
                  title: 'fail',
                  content: err,
                })
              }
            },
            fail: function(res) {},
            complete: function(res) {},
          })
        },
        clickVin() {
          let that = this
          // 选择图片
          wx.chooseImage({
            count: 1,
            success: async function(res) {
              try {
                let invokeRes = await wx.serviceMarket.invokeService({
                  service: 'wx79ac3de8be320b71',
                  api: 'OcrAllInOne',
                  data: {
                    // 用 CDN 方法标记要上传并转换成 HTTP URL 的文件
                    img_url: new wx.serviceMarket.CDN({
                      type: 'filePath',
                      filePath: res.tempFilePaths[0],
                    }),
                    data_type: 3,
                    ocr_type: 8
                  },
                })
                console.log('invokeService success', invokeRes)
                for (let i = 0; i < invokeRes.data.ocr_comm_res.items.length;i++) {
                  let no = invokeRes.data.ocr_comm_res.items[i].text
                  if (no.substring(0,3) == 'LSG') {
                    that.vin = no
                  }
                }
              } catch (err) {
                console.error('invokeService fail', err)
                wx.showModal({
                  title: 'fail',
                  content: err,
                })
              }
            },
            fail: function(res) {},
            complete: function(res) {},
          })
        },
      }
    }
    </script>
    
    <style>
    button {
      background-color: #007aff;
      color: #ffffff;
    }
    </style>