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


    识别银行卡

    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
      }
    }
    
  • 相关阅读:
    Springboot学习七 spring的一些注解
    Spring boot 学习六 spring 继承 mybatis (基于注解)
    Spring boot 学习 五:domain的定义
    Spring boot 学习 四:spring boot 配置文件 application.yml
    python学习(十九) 查看Python的安装路径
    docker 学习(九) docker部署静态网站
    maven学习9 关于maven一些參數
    docker 学习(八) docker file
    HTTP VS HTTPS
    【Python】【亲测好用】安装第三方包报错:AttributeError:'module' object has no attribute 'main'
  • 原文地址:https://www.cnblogs.com/dongxuelove/p/13060187.html
Copyright © 2020-2023  润新知