百度智能小程序 CameraContext

2020-09-05 14:17 更新

CameraContext

解釋:swan.createCameraContext 的返回值。

屬性說(shuō)明

屬性名 說(shuō)明
CameraContext.takePhoto 拍照
CameraContext.startRecord 開始錄像
CameraContext.stopRecord 結(jié)束錄像

示例 

在開發(fā)者工具中打開



圖片示例

代碼示例

<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
        });
    }
});


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)