这次给大家带来微信小程序调用相机拍照,微信小程序调用相机拍照的注意事项有哪些,下面就是实战案例,一起来看一下。
在某些进行签到的场景,为了防止用户选择相册的照片或者不实时拍照,设置相机倒计时自动拍照。
一、首先是视图层index.wxml,视图层主要负责显示组件和图片。
二、逻辑层index.js,调用倒计时函数并且调用摄像头拍照并保存图片。//index.js
const app = getApp()
Page({
data: {
userInfo: {},
counting: false//倒计时
},
onLoad: function () {
this.daojishi();//一进来就拍照倒计时
this.ctx = wx.createCameraContext()//创建摄像头对象
},
//倒计时
daojishi: function () {
var that = this;
if (!that.data.counting) {
//开始倒计时5秒
countDown(that, 5);
}
}
})
//倒计时函数 在page外
function countDown(that, count) {
if (count == 0) {
//等于0时拍照
that.ctx.takePhoto({
quality: 'high',
success: (res) => {
that.setData({
src: res.tempImagePath
})
wx.showToast({
title: '拍照完成',
})
}
})
that.setData({
counting: false
})
return;
}
wx.showLoading({//加载时显示倒计时
title: '拍照倒计时'+count+'秒',
})
setTimeout(function () {
wx.hideLoading()
}, 1000)
that.setData({
counting: true,
})
setTimeout(function () {
count--;
countDown(that, count);
}, 1000);
}
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读: