tt.createCameraContext
收藏
我的收藏

基础库 1.78.0 开始支持本方法,这是一个同步方法。​
创建并返回 camera 上下文 CameraContext 对象,cameraContext 与页面的 camera 组件绑定,一个页面只能有一个 camera ,通过它可以操作对应的组件。​

语法​

Plain Text
复制
tt.createCameraContext()

参数说明​

无​

返回值​

扫码体验

代码示例​

html
复制
<!-- index.ttml -->
<camera
class="camera"
device-position="back"
flash="off"
binderror="onError"
></camera>
<button type="primary" bindtap="createContext">授权并创建cameraContext</button>
<button type="primary" bindtap="setZoom">设置缩放级别</button>
js
复制
// index.js
Page({
onError(e) {
tt.showModal({
title: "相机出错",
content: JSON.stringify(e),
showCancel: false,
});
},
createContext() {
tt.getSetting({
success: (res) => {
let cameraAllowed = res.authSetting["scope.camera"];
if (cameraAllowed) {
this.ctx = tt.createCameraContext();
} else {
tt.showToast({
title: "请授权相机后重新进入",
});
}
},
fail: (err) => {
tt.showModal({
title: "获取授权失败",
content: JSON.stringify(err),
});
},
});
},
setZoom() {
this.ctx.setZoom({
zoom: 8,
success(res) {
tt.showToast({
title: `设置 zoom 为: ${res.zoom}`,
});
},
fail(err) {
tt.showModal({
content: `设置缩放失败: ${JSON.stringify(err)}`,
});
},
});
},
});

Bug & Tip​

    Tip:开发者工具暂不支持此能力,请用真机扫码调试。​