小程序-小程序后台原生图片识别

识别银行卡

index.wxml

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

index.js

  • 云函数将图片信息直接传入进行验证
Page({
  //识别银行卡
  yinhangka(){
    let that=this
    wx.cloud.callFunction({
      name:"tupianshibie",
      data:{
        imgUUU:"https://6d79-myminiprogram-xdqs5-1302292245.tcb.qcloud.la/ocr/yinghangka1.jpg?sign=8421781fbe63ce33ca36ade3d5f1d936&t=1590994105"
      },
      success(res){
        console.log("识别成功",res);
        that.setData({
          number:res.result.errMsg
        })
      },
      fail(err){
        console.log("识别失败",err);
      }
    })
  }
})

云函数js

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

识别身份证

shibie.wxml

<button bindtap="shengfenzhen">识别身份证</button>

shibie.js

  • 上传图片到云存储保存,然后使用云数据库存储,之后进行调用
Page({
  //选择图片
  shengfenzhen() {
    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);
        that.uploadFile(tempFilePaths[0])
      }
    })
  },
  //上传图片到云存储
  uploadFile(tempFile) {
    let that = this
    wx.cloud.uploadFile({
      cloudPath: 'ocr/shengfenzheng03.png', // 上传至云端的路径
      filePath: tempFile, // 小程序临时文件路径
      success: res => {
        // 返回文件 ID
        console.log("上传图片成功", res.fileID)
        that.getImgUrl(res.fileID)
      },
      fail: err => {
        console.log("上传图片失败", err);
      }
    })
  },
  //获取图片url
  getImgUrl(fileid) {
    let that=this
    wx.cloud.getTempFileURL({
      fileList: [fileid],
      success: res => {
        // get temp file URL
        console.log("获取图片url成功",res.fileList)
        let tempUrl=res.fileList[0].tempFileURL
        console.log("获取图片url成功",tempUrl)
        that.shibie(tempUrl)
      },
      fail: err => {
        console.log("获取图片url失败",err)
      }
    })
  },
  //调用云函数
  shibie(tempurl){
    let that=this
    wx.cloud.callFunction({
      name:"shengfengzheng",
      data:{
        imgQQ:tempurl
      },
      success(res){
        console.log("识别成功",res)
      },
      fail(err){
        console.log("识别失败",err);
      }
    })
  }
})

云函数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.imgQQ
    })
    console.log(result);
    return result
  } catch (err) {
    return err
  }
}
posted @ 2020-06-07 12:46  东血  阅读(392)  评论(0编辑  收藏  举报

载入天数...载入时分秒...