W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
解釋:swan.createCameraContext 的返回值。
屬性名 | 說(shuō)明 |
---|---|
CameraContext.takePhoto | 拍照 |
CameraContext.startRecord | 開始錄像 |
CameraContext.stopRecord | 結(jié)束錄像 |
<view class="wrap">
<view class="card-area">
<camera
device-position="{{device}}"
flash="off"
bindstop="stop"
binderror="error"
class="preview"
style="width: 100%; height: 3rem;">
</camera>
<button type="primary" bind:tap="switchCamera">切換攝像頭</button>
<button type="primary" bind:tap="takePhoto">拍照</button>
<button type="primary" bind:tap="startRecord">開始錄像</button>
<button type="primary" bind:tap="stopRecord">結(jié)束錄像</button>
<view class="preview">預(yù)覽</view>
<image s-if="imageSrc" class="image" mode="widthFix" src="{{imageSrc}}"></image>
<video s-if="videoSrc" class="video" src="{{videoSrc}}"></video>
</view>
</view>
Page({
data: {
imageSrc: '',
device: 'front',
videoSrc: ''
},
onLoad() {
this.ctx = swan.createCameraContext();
},
onShow() {
swan.authorize({
scope: 'scope.camera',
success: res => {
swan.showToast({
title: '已成功授權(quán)',
icon: 'none'
});
},
fail: err => {
if (err.errCode === 10003) {
swan.showToast({
title: '用戶已拒絕授權(quán)申請(qǐng),請(qǐng)自行開啟相機(jī)授權(quán)',
icon: 'none'
});
}
}
});
},
switchCamera() {
const devices = this.getData('device');
if (devices === 'back') {
this.setData({
device: 'front'
});
} else {
this.setData({
device: 'back'
});
}
},
takePhoto() {
this.ctx.takePhoto({
quality: 'high',
success: res => {
this.setData({
imageSrc: res.tempImagePath
});
}
});
},
startRecord() {
this.ctx.startRecord({
success: res => {
swan.showToast({
title: 'startRecord',
icon: 'none'
});
}
});
},
stopRecord() {
this.ctx.stopRecord({
success: res => {
swan.showModal({
title: '提示',
content: res.tempVideoPath
});
this.setData({
videoSrc: res.tempVideoPath
});
}
});
},
error(e) {
swan.showModal({
title: '加載出錯(cuò)',
content: e.detail
});
}
});
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: