小程序实现身份证取景框拍摄

2023-11-18

身份证取景框的实现主要是借助于camera 组件及cover-view组件
先看下案例
在这里插入图片描述
wxml代码

<view class="camera_box">
  <camera class="camera" wx:if="{{!show}}" device-position="back" flash="off" binderror="error">
    <cover-view class="id_m">
      <cover-image class="img" src="https://cdn.ctoku.com/201910234221235312.png"></cover-image>
    </cover-view>
  </camera>
  <image class="camera_img" src="{{src}}" wx:if="{{show}}"></image>
  <view class="action">
    <button class="takeBtn" type="primary" bindtap="takePhoto" wx:if="{{!show}}"></button>
    <button class="saveImg" type="primary" bindtap="saveImg" wx:if="{{show}}"></button>
    <button class="cancelBtn" wx:if="{{show}}" type="primary" bindtap="cancelBtn"></button>
  </view>
</view>

js代码

Page({

  /**
   * 页面的初始数据
   */
  data: {
    src: '',
    show: false
  },
  cancelBtn () {
    this.setData({show: false})
  },
  saveImg () {
    wx.showModal({
      title: '图片地址',
      content: this.data.src,
    })
  },
  takePhoto() {
    const ctx = wx.createCameraContext()
    const listener = ctx.onCameraFrame((frame) => {
      console.log(frame)
    })
    ctx.takePhoto({
      quality: 'high',
      success: (res) => {
        console.log(res)
        this.setData({
          src: res.tempImagePath,
          show: true
        })
        listener.stop({
          success: (res) => {
            console.log(res)
          },
          fail: (err) =>{
            console.log(err)
          }
        })
      },
      fail: (err) => {
        console.log(err)
      }
    })
  },
  error(e) {
    console.log(e.detail)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showModal({
      title: '提示',
      content: '该功能仅供演示,不会上传图片信息,请放心使用!!!',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})

css 代码

.camera_box {
  height: 100vh; width: 100vw;
  position: relative;
}
.camera {
  height: 85vh; width: 100vw;
  z-index: 1;
}
.id_m {
  height: 85vh; width: 100vw;
  z-index: 999;
  background: rgba(0, 0, 0, 0.1);
  display: flex;
  position: absolute;
}
.id_m .img {
  width: 550rpx;
  height: 900rpx;
  display: block;
  position: absolute;
  left: 0; right: 0; margin: auto auto;
  top: 0; bottom: 0;
}
.id_m .tips_txt {
  transform:rotate(90deg);
}
.camera_box .action {
  height: 15vh;
  position: relative;
  display: flex;
  justify-content: space-around;
  align-items: center;
} 
.camera_box .takeBtn {
  height: 120rpx; width: 120rpx; border-radius: 50%;
  font-size: 24rpx;
  background: url('https://cdn.ctoku.com/1123123123123e3241.png') no-repeat center;
  background-size: contain;
  border: none;
}
.camera_box .cancelBtn {
  font-size: 24rpx;
  height: 120rpx; width: 120rpx; border-radius: 50%;
  background: url('https://cdn.ctoku.com/12311123342312231.png') no-repeat center;
  background-size: contain;
  border: none;
}
.camera_box .saveImg {
  background: url('https://cdn.ctoku.com/1232123434231231231.png') no-repeat center;
  font-size: 24rpx;
  height: 120rpx; width: 120rpx; border-radius: 50%;
  background-size: contain;
  border: none;
}
.camera_box .takeBtn::after {
  border: none;
}

.camera_img {
  height: 85vh; width: 100%;
}

更多小程序开发案例请关注微信小程序【插件集】,项目地址:
https://github.com/yancekang/wechat-plug.git

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

小程序实现身份证取景框拍摄 的相关文章

  • ChatGPT3.0、ChatGPT3.5和ChatGPT4.0版本。

    ChatGPT3 0版本是目前最先进的对话生成系统之一 已经在多个应用场景中得到了广泛应用 相较于以往的版本 ChatGPT3 0在模型规模和语言能力上都有了明显的提升 这一版本的模型包含了1 75万亿个参数 而且其生成的对话内容更加流畅
  • 性能优化点

    Arts and Sciences Computer Science myUSF 索引3层 高度为3 一般对于数据库地址千万级别的表 大于2000万的数据进行分库分表存储 JVM整体结构及内存模型 JVM调优 主要为减少FULL GC的执行
  • javascript下的protype

    了解下JavaScript中的prototype JS中的phototype是JS中比较难理解的一个部分 javascript的方法可以分为三类 类方法 对象方法 原型方法 例子 view sourceprint 01 function P

随机推荐