微信小程序> 微信小程序相机组件的使用

微信小程序相机组件的使用

浏览量:830 时间: 来源:kevin_xfzy

最近开发人脸识别登录系统,需要在微信中调用相机实现拍照上传功能,最开始使用H5input控件实现,但界面不太美观,
H5的getMedia只兼容几个主流的浏览器,所以就研究了微信相机组件的开发,过程经历了许多坑 . . . . . .
H5利用input标签直接调用相机:https://blog.csdn.net/qq_25101225/article/details/81034626

首先需要获取使用微信JS-SDK的授权配置(参数可以后台ajax获取):

var url = window.location.href;$.ajax({    url: url_root + "/wechat/get_signature",    type: "post",    data: { "url" : url },    dataType: "json",    success: function (data) {        wx.config({    appId: data.data.appId,    timestamp: data.data.timestamp,    nonceStr: data.data.nonceStr,    signature: data.data.signature,    jsApiList: ['createCameraContext','camera']});    }});

index.Wxml
device-position:前置或后置,值为front, back; flash:闪光灯,值为auto, on, off; bindstop:摄像头在非正常终止时触发,如退出后台等情况;binderror:用户不允许使用摄像头时触发。

view class="page-body"  view class="page-body-wrapper"    camera device-position="front" flash="off" binderror="error" style="width: 100%; height: 300px;"/camera    view class="btn-area"      button type="primary" bindtap="takePhoto"拍照/button    /view    view class="btn-area"      button type="primary" bindtap="startRecord"开始录像/button    /view    view class="btn-area"      button type="primary" bindtap="stopRecord"结束录像/button    /view    view class="preview-tips"预览/view    image wx:if="{{src}}" mode="widthFix" src="{{src}}"/image    video wx:if="{{videoSrc}}" class="video" src="{{videoSrc}}"/video  /view/view

index.js

Page({  onLoad() {    this.ctx = wx.createCameraContext()  },  takePhoto() {    this.ctx.takePhoto({      quality: 'high',      success: (res) = {        this.setData({          src: res.tempImagePath        })      }    })  },  startRecord() {    this.ctx.startRecord({      success: (res) = {        console.log('startRecord')      }    })  },  stopRecord() {    this.ctx.stopRecord({      success: (res) = {        this.setData({          src: res.tempThumbPath,          videoSrc: res.tempVideoPath        })      }    })  },  error(e) {    console.log(e.detail)  }})

官网文档:https://developers.weixin.qq.com/miniprogram/dev/api/api-camera.html

                https://mp.weixin.qq.com/wiki?action=doc&id=mp1421141115&t=0.4213981683521064#1

W3Cschool文档:https://www.w3cschool.cn/weixinapp/weixinapp-c2u72j31.html


版权声明

即速应用倡导尊重与保护知识产权。如发现本站文章存在版权问题,烦请提供版权疑问、身份证明、版权证明、联系方式等发邮件至197452366@qq.com ,我们将及时处理。本站文章仅作分享交流用途,作者观点不等同于即速应用观点。用户与作者的任何交易与本站无关,请知悉。

产品经理

手机 : 13312967497

擅长 : 小程序流量变现

扫码领取礼包

热门模板

  • 头条
  • 搜狐
  • 微博
  • 百家
  • 一点资讯
  • 知乎