微信小程序中如何识别银行卡和身份证 - 求知若渴;虚心若愚

时间:2024-03-03 14:15:40

识别银行卡云函数card2/index.js:

const cloud = require(\'wx-server-sdk\')
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV,
})
exports.main = async (event, context) => {
try {
const result = await cloud.openapi.ocr.bankcard({ //识别银行卡
"type": \'photo\',
"imgUrl": event.imgCard2
})
return result
} catch (err) {
return err
}
}

识别身份证云函数card1/index.js:

const cloud = require(\'wx-server-sdk\')
cloud.init({
})
exports.main = async (event, context) => {
try {
const result = await cloud.openapi.ocr.idcard({
"type": \'photo\',
"imgUrl": event.imgCard
})
console.log(result)
return result
} catch (err) {
return err
}
}

shibie.wxml:

<button bindtap="shibie2">识别银行卡</button>
<text>银行卡号是:{{number}}</text>

<button bindtap="shibie">识别身份证</button>
<view>姓名是:{{name}}</view>
<view>身份证号是:{{id}}</view>
<view>性别是:{{gender}}</view>

shibie.js

Page({
  shibie2(){ //识别银行卡
    var that=this
    wx.cloud.callFunction({
    name:"card2",
    data:{
    imgCard2:"https://6a69-xiaochengxu-2gscmw0o879d5385-1305877130.tcb.qcloud.la/%E9%93%B6%E8%A1%8C%E5%8D%A1.jpg?sign=71270da3612790663bf818d02ee3f994&t=1624794493"
    },
    success(res){
    console.log("识别成功",res)
    that.setData({
    number:res.result.number
    })
    },
    fail(res){
    console.log("识别失败",res)
    },
    })
    },
  
    shibie(){ //识别身份证
      var that=this
      wx.cloud.callFunction({
      name:"card1",
      data:{
      imgCard:"https://6a69-xiaochnegxu-2gscmw0o879d5385-1305877130.tcb.qcloud.la/%E8%BA%AB%E4%BB%BD%E8%AF%81.jpg?sign=2fa017e88a2bd0e96f18a0655c8034a6&t=1624794751"
      },
      success(res){
      console.log("识别成功",res)
      that.setData({
      name:res.result.name,
      id:res.result.id,
      gender:res.result.gender
      })
      },
      fail(res){
      console.log("识别失败",res)
      },
      })
      },
        


})