微信小程序:OCR识别 通用字体

参考链接:微信小程序:OCR识别_weixin_42325507的博客-CSDN博客_微信小程序ocr识别

上面链接js部分里面有好几个错误地方,仔细看下

1、先购买

https://fuwu.weixin.qq.com/service/detail/000ce4cec24ca026d37900ed551415

 

 

 2、 这个比较方便,直接在前端搞定,不需要开发者服务器再进行API访问。一共就两个步骤:购买,加前端

程序

wxml部分:

<camera device-position="back" flash="off" binderror="error" style="width: 100%; height: 300px;"></camera>
<view style="flex-direction: row;text-align: center;">
    <button size="mini" type="default" bindtap="photoAlbum">相册</button>
</view>
<view style="text-align: center;">预览</view>
<image mode="widthFix" src="{{src}}"></image>
 
  js部分:
// index.js
Page({
  data: {
    src: '',
    text: '' //要赋的值
  },

  /**
   * OCR 识别
   * @param {*} tempFilePath 
   */
  ocrtt(tempFilePath){
    const invokeRes = wx.serviceMarket.invokeService({
        service: 'wx79ac3de8be320b71', // '固定为服务商OCR的appid,非小程序appid',
        api: 'OcrAllInOne',
        data: {
            // 用 CDN 方法标记要上传并转换成 HTTP URL 的文件
            img_url: new wx.serviceMarket.CDN({
                type: 'filePath',
                filePath: tempFilePath,
            }),
            data_type: 3,
            ocr_type: 8
        },
        success:function(res){
            wx.showModal({
                title: 'success',
                content: JSON.stringify(res),
            })
        }
    })
},
  /**
     * 相册
     */
    photoAlbum() {
      const thas = this;
      wx.chooseImage({
          count: 1,
          sizeType: ['original', 'compressed'],
          sourceType: ['album', 'camera'],
          success(res) {
              // tempFilePath可以作为img标签的src属性显示图片
              const tempFilePath = res.tempFilePaths[0]
              const fileSys = wx.getFileSystemManager();
              const base64 = fileSys.readFileSync(tempFilePath, "base64");
              thas.setData({
                  src: tempFilePath,
                  base64: base64
              })

              thas.ocrtt(tempFilePath);
          }
      })
  },
 /**
  * 生命周期函数--监听页面加载
  */
  onLoad: function (options) {
  },
})



posted @ 2022-11-09 14:55  小小菜鸟04  阅读(868)  评论(0)    收藏  举报