微信小程序 识别身份证,银行卡

微信小程序实现 身份证,银行卡…识别

OCR 识别

1. 使用微信的OCR

在这里插入图片描述
购买成功后在 设置 插件中添加,详情中查看使用方法–开发文档
在这里插入图片描述
开发文档中的代码直接使用就可以了

2. 使用百度的 OCR

百度云链接
在这里插入图片描述
在这里插入图片描述
wxml

  <button bindtap="uploadImg">百度上传身份证</button>

js

Page({

  /**
   * 页面的初始数据
   */
  data: {
    a_token: '',
  },
  
  onLoad: function (options) {
    this.getBaiduToken()
  },
  
  // 获取百度access_token
  getBaiduToken: function() {
    const apiKey = 'API Key';
    const seckey = 'Secret Key';
    const tokenUrl = `https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=${apiKey}&client_secret=${seckey}`;
 
    let that = this;
    wx.request({
      url: tokenUrl,
      method: 'POST',
      dataType: 'json',
      header: {
        'content-type': 'application/json; charset=UTF-8'
      },
      success: function(res) {
        console.log('getBaiduToken提示pass', res);
        that.setData({
          baiduToken: res.data.access_token
        })
      },
      fail: function(res) {
        console.log('getBaiduToken提示fail', res);
      }
    })
  },
  
   // 上传图片
   uploadImg: function() {
    let that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        console.log(tempFilePaths)
        // 路径转化为base64图片
        wx.getFileSystemManager().readFile({
          filePath: tempFilePaths[0],
          encoding: 'base64',
          success: res => {
            console.log('读图片数据pass', res.data);
            that.scanImageInfo(res.data);
          },
          fail: res => {
            console.log('读图片数据fail', res.data);
          }
        })
      }
    })
  },
  
  // 扫描图片中的数据
  scanImageInfo: function(imageData) {
    let that = this;
    const detecUrl = 'https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?access_token=' + this.data.baiduToken;
 
    wx.showLoading({
      title: '加载中',
    });
    wx.request({
      url: detecUrl,
      data: {
        image: imageData,
        id_card_side: 'front'
      },
      method: 'POST',
      dataType: 'json',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        console.log('success', res.data.words_result)
      },
      fail: res => {
        console.log('fail')
      },
      complete: res => {
        wx.hideLoading();
      }
    })
  },

})

百度接口参数文档

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值