开发
API

CameraFrameListener.stop

基础库 1.78.0 开始支持本方法,低版本需做兼容处理

停止监听帧数据。

语法

CameraFrameListener.stop(options)

参数说明

options 为 object 类型,属性如下:

属性名类型默认值必填说明最低支持版本
successfunction接口调用成功的回调函数1.78.0
failfunction接口调用失败的回调函数1.78.0
completefunction接口调用结束的回调函数(调用成功、失败都会执行),回调参数同成功或失败回调1.78.0

代码示例

<!-- index.ttml -->
<camera class="camera" frame-size="medium" device-position="back" binderror="onError"></camera>
<view class="page-body">
  <button type="primary" bindtap="auth">相机授权</button>
  <button type="primary" bindtap="startOnFrame">开始接收帧数据</button>
  <button type="primary" bindtap="stopOnFrame">停止接收帧数据</button>
  <view>第 {{counter}} 帧</view>
  <view hidden="{{counter === 0}}">frameWidth{{frameWidth}}; frameHeight:{{frameHeight}}</view>
</view>
// index.js
var listener;
Page({
  data: {
    counter: 0,
    frameWidth: 0,
    frameHeight: 0,
  },
  onLoad: function (options) {
    this.openCamera();
  },
  openCamera() {
    tt.getSetting({
      success: (res) => {
        let cameraAllowed = res.authSetting["scope.camera"];
        if (cameraAllowed) {
          this.ctx = tt.createCameraContext();
        } else {
          tt.showToast({
            title: "请授权相机后重新进入", // 内容
            success: (res) => {
              this.auth();
            },
          });
        }
      },
      fail: (err) => {
        tt.showModal({
          title: "获取授权失败",
          content: JSON.stringify(err),
        });
      },
    });
  },
  auth() {
    tt.authorize({
      scope: "scope.camera",
      success: () => {
        tt.showToast({
          title: "授权成功",
          success() {
            tt.reLaunch({
              url: "/pages/index/index",
            });
          },
        });
      },
      fail: (err) => {
        tt.showModal({
          title: "授权失败",
          content: JSON.stringify(err),
        });
      },
    });
  },
  startOnFrame(e) {
    listener = this.ctx.onCameraFrame((frame) => {
      let { width, height, data } = frame;
      this.setData({
        counter: this.data.counter + 1,
        frameWidth: width,
        frameHeight: height,
      });
    });

    listener.start({
      success: (res) => {
        tt.showToast({ title: "开始接收帧数据" });
      },
      fail: (err) => {
        tt.showModal({
          title: "接收失败",
          content: JSON.stringify(err),
        });
      },
    });
  },
  stopOnFrame() {
    listener.stop({
      success: (res) => {
        tt.showToast({ title: "停止接收帧数据" });
      },
      fail: (err) => {
        tt.showModal({
          title: "停止接收出错",
          content: JSON.stringify(err),
        });
      },
    });
  },
  onError(err) {
    tt.showModal({
      title: "相机出错了",
      content: JSON.stringify(err),
    });
  },
});

Bug & Tip

  • Tip:开发者工具暂不支持此能力,请用真机扫码调试。
点击纠错
评价此篇文档