这是微信wxml的代码,构建出一个照相机,并且可以实现前后转换摄像头
<!--pages/camera/camera.wxml-->
<!-- <text>pages/camera/camera.wxml</text> -->
<camera device-position="{
{status}}" flash="off" binderror="error" style="width: 100%; height: 300px;"></camera>
<view class="weui-btn-area">
<button class="weui-btn" type="primary" bindtap="takePhoto">拍照 </button>
</view>
<switch checked bindchange='switch1Change'/>
以下是js代码,实现转换摄像头与访问接口
// pages/camera/camera.js
Page({
/**
* 页面的初始数据
*/
data: {
path:null,
status:'front'
},
switch1Change:function(e){
if (e.detail.value) {
this.setData({ status: 'back' })
} else {
this.setData({ status: 'front'})
}
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
this.ctx=wx.createCameraContext()
},
takePhoto(){
this.ctx.takePhoto({
quality: 'high',
success: (res) => {
console.log(res.tempImagePath);
this.setData({ path: res.tempImagePath})
wx.uploadFile({
url: 'login', //仅为示例,非真实的接口地址
filePath: this.data.path,
name: 'file',
success: (res)=> {
// var data = res.data;
// var msg=JSON.parse(data);
// console.log(msg);
wx.showModal({
title: '提示',
content: res.data,
})
}
})
}
})
},
startRecord(){
this.ctx.startRecord({
success: (res) => {
console.log('startRecord')
}
})
},
stopRecord(){
this.ctx.stopRecord({
success: (res) => {
this.setData({
src: res.tempThumbPath,
videoSrc:res.tempVideoPath
})
}
})
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
}