微信小程序实现身份证拍及摄裁剪功能

效果图

23

23

设计思路

设计一个遮罩层图片,打开摄像头,拍照,把照片放进画布上,根据遮罩层的尺寸进行裁剪,得出处理后的图片。
请添加图片描述

wxml文件

把遮罩层放进去,并加上按钮和点击事件

<camera wx:if="{{isShowCamera}}" flash="off" style="height:100vh;">
  <cover-view class='camerabgImage-view'>
    <cover-image wx:if="{{showPhoto}}" class='bgImage' src='{{image}}'> </cover-image>
    <!-- 遮罩层照片 -->
    <cover-image class='bgImage1' src='../icons/zhezhao.png'></cover-image>
    <cover-view class='cameratop-view1'>中华人民共和国居民身份证</cover-view>
    <cover-view class='cameratop-view2'>(正面)</cover-view>
    <cover-view class='cameracenter-view' style='top:62%'>请对准四个角拍照</cover-view>

    <!-- 拍照按钮 -->
    <cover-view class='camerabotton-view' style='bottom:0px'>
      <cover-image class='cancelphoto'
        src='{{show == true ? "../icons/close.png" : "../icons/return.png" }}'
        bindtap='cancelPhotoAction'></cover-image>
      <cover-image class='takephoto'
        src='{{show == true ? "../icons/paizhao.png" : "../icons/sure.png" }}'
        bindtap='takePhotoAction'></cover-image>
      <cover-view class='skipphoto' bindtap='skipphotoAction'>{{skipphotoStatus==1?"跳过":""}}
      </cover-view>
    </cover-view>
  </cover-view>
</camera>

<canvas wx:if='{{isShowImage}}' canvas-id="image-canvas"
  style='width:{{phoneWidth*2}}rpx;height:{{phoneHeight*2}}rpx;'></canvas>

js文件

先获取摄像头权限,并打开摄像头

  data: {
    isShowCamera: false,
    width: 300,
    height: 190,
    src: "",
    image: "",
    skipphotoStatus: "0", // 1跳过 0没有跳过
    isShowImage: false,
    url: "",
    phoneWeight: "",
    phoneHeight: "",
    image:"http://print.jiaynet.cn/icons/zhezhao.png",
    show:true,
    showPhoto:false,
    imgUrlZ:"",
    imgUrlF:"",
    chooseID:""
  },
      //获取摄像头权限并打开
  onShow: function() {
    wx.authorize({
      scope: 'scope.camera',
      success: function(res) {
        this.setData({
          isShowCamera: true,
        })
      },
      fail: function(res) {
        wx.showModal({
          title: '请求授权您的摄像头',
          content: '如需正常使用此小程序功能,请您按确定并在设置页面授权用户信息',
          confirmText: '确定',
          success: res => {
            if (res.confirm) {
              wx.openSetting({
                success: function(res) {
                  console.log('成功');
                  console.log(res);
                  if (res.authSetting['scope.camera']) { //设置允许获取摄像头
                    console.log('设置允许获取摄像头')
                    wx.showToast({
                      title: '授权成功',
                      icon: 'success',
                      duration: 1000
                    })
                    this.setData({
                      isShowCamera: true,
                    })

                  } else { //不允许
                    wx.showToast({
                      title: '授权失败',
                      icon: 'none',
                      duration: 1000
                    })
                  }
                }
              })
            } else { //取消
              wx.showToast({
                title: '授权失败',
                icon: 'none',
                duration: 1000
              })
            }
          }
        })
      }
    })
  },

点击拍照事件

//点击拍照  
takePhotoAction: function() {
    if (this.data.show == true){
      this.ctx.takePhoto({
        quality: 'high', //高质量
        success: (res) => {
          this.loadTempImagePath(res.tempImagePath);
          this.setData({
            image: res.tempImagePath,
            showPhoto: true
          })
        },
      })
    }else{
        //根据拍证件照是反面还是正面进行判断
      if (this.data.chooseID == 1){
          //正面
        let imgUrl = { id: this.data.chooseID, src: this.data.imgUrlZ}
        wx.redirectTo({
          url: '../idcard/idcard?imgUrl=' + JSON.stringify(imgUrl),
        })
      }else{
           //反面
        let imgUrl = { id: this.data.chooseID, src: this.data.imgUrlF }
        wx.redirectTo({
          url: '../idcard/idcard?imgUrl=' + JSON.stringify(imgUrl),
        })
      }
    }
  },

处理图像

//处理图像
  loadTempImagePath(src) {
    wx.getSystemInfo({
      success: (res) => {
        // 矩形的位置
        var res = wx.getSystemInfoSync()
        this.setData({
          phoneWidth: res.screenWidth,
          phoneHeight: res.screenHeight
        })
          //根据遮罩层的尺寸设配机型进行定位x,y轴,进行裁剪
        var imageX = 0.1 * this.data.phoneWidth;
        var imageY = 0.25 * this.data.phoneHeight;
        var imageWidth = 0.8 * this.data.phoneWidth;
        var imageHeight =0.25 * this.data.phoneHeight;
        wx.getImageInfo({
          src,
          success: (res) => {
            this.setData({
              isShowImage: true,
            })
            const canvas = wx.createCanvasContext("image-canvas", this)
            //过渡页面中,图片的路径坐标和大小
            canvas.drawImage(src, 0, 0, this.data.phoneWidth, this.data.phoneHeight)
            wx.showLoading({
              title: '数据处理中...',
              icon: 'loading',
              duration: 10000
            })
            canvas.draw(false,
              setTimeout(() => {
                wx.canvasToTempFilePath({ //裁剪对参数
                  canvasId: "image-canvas",
                  x: imageX, //画布x轴起点
                  y: imageY, //画布y轴起点
                  width: imageWidth, //画布宽度
                  height: imageHeight, //画布高度
                  destWidth: imageWidth, //输出图片宽度
                  destHeight: imageHeight, //输出图片高度
                  success: (res) => {
                    wx.hideLoading()
                    this.setData({
                      isShowImage: false,
                      show: false,
                    })
                      //根据正反面,赋值
                      if (this.data.chooseID == 1) {
                        this.data.imgUrlZ = res.tempFilePath
                      } else {
                        this.data.imgUrlF = res.tempFilePath
                      }
                  },
                  fail: function(e) {
                    wx.hideLoading()
                    wx.showToast({
                      title: '出错啦...',
                      icon: 'loading'
                    })
                    if (this.data.skipphotoStatus == 1) {
                      // wx.redirectTo({
                      //   url: 'addCarInfo/addCarInfo',
                      // })
                    } else {
                      wx.navigateBack({
                        delta: 1
                      });
                    }
                  }
                });
              }, 1000)
            )
          }
        })
      }
    })
  },

点击拍照的时候直接跳转此页面就可以拍照处理图片并且自定义裁剪。

  • 4
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值