目录

camera

Represents the system camera, which supports taking photos and recording videos.

Since: 1.0.4

Required permissions: scope.camera (and scope.record in video recording scenarios)

Associated document: has.createCameraContext

Properties

Name

Type

Default Value

Mandatory

Description

device-position

string

back

No

Orientation of the camera.

  • back: rear camera.
  • front: front camera.

flash

string

auto

No

Flash mode.

  • auto: automatic.
  • on: enabled.
  • off: disabled.
  • torch: always on.

bindstop

event

-

No

Event triggered when the camera terminates abnormally, for example, exiting the background.

binderror

event

-

No

Event triggered when a camera is not allowed to use.

bindinitdone

event

-

No

Event triggered when the camera initialization is complete, with e.detail = {maxZoom}.

Example

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

JS file:

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