目录

camera

系统相机,支持拍摄、录像功能。

起始版本:1.0.4

需要权限:需要用户授权scope.camera权限,录像场景还需授权scope.record权限。

关联文档:has.createCameraContext

属性

名称

类型

默认值

必填

描述

device-position

string

back

否

摄像头朝向。

  • back:后置。
  • front:前置。

flash

string

auto

否

闪光灯。

  • auto:自动。
  • on:打开。
  • off:关闭。
  • torch:常亮。

bindstop

event

-

否

摄像头在非正常终止时触发,如退出后台等情况。

binderror

event

-

否

用户不允许使用摄像头时触发。

bindinitdone

event

-

否

相机初始化完成时触发,e.detail = {maxZoom}。

示例

hxml文件:
<camera 
  flash={{enableflash}}  
  resolution="low"  
  device-position="{{position}}" 
  binderror="error" 
  bindstop="stop" 
  bindinitdone="initdone"
/>

js文件:

Page({
  onReady: function () {
    this.ctx = has.createCameraContext();
  },
  data: {
    position: 'back',
    enableflash: 'auto'
  },
  takePhoto() {
    this.ctx.takePhoto({
      success: (res) => {
        console.log("takePhoto success", JSON.stringify(res));
      },
      fail: (res) => {
        console.log("takePhoto fail", res);
      }
    })
  },
  startRecord() {
    const that = this
    has.authorize({
      scope: 'scope.record',
      success(res) {
        that.ctx.startRecord({
          timeoutCallback: (res) => {
            console.log("timeoutCallback Result", JSON.stringify(res));
          },
          success: () => {
            console.log("startRecord success");
          },
          fail: (res) => {
            console.log("startRecord fail", res);
          }
        })
      },
    })
  },
  stopRecord() {
    this.ctx.stopRecord({
      success: (res) => {
        console.log("stopRecord success", JSON.stringify(res));
      },
      fail: (res) => {
        console.log("stopRecord fail", res);
      }
    })
  },
})