目录

Camera

has.createCameraContext

has.createCameraContext(): CameraContext

Creates a CameraContext object for a <camera> component.

Since: 1.0.4

Return value

A CameraContext object.

Example

const cameraContext = has.createCameraContext();

CameraContext

Camera context object.

Since: 1.0.4

[h2]CameraContext.startRecord

CameraContext.startRecord(Object object)

Starts recording.

Since: 1.0.4

Required permissions: ohos.permission.CAMERA and ohos.permission.MICROPHONE, which must be declared in module.json5.

Parameters

The input parameter is an object containing the following fields.

Name

Type

Default Value

Mandatory

Description

timeoutCallback

function

-

No

Callback function invoked when the recording duration exceeds the upper limit or the recording terminates unexpectedly.

timeout

number

30

No

Maximum recording duration, in seconds. The maximum value is 5 minutes.

success

function

-

No

Callback to be invoked when the API call is successful.

fail

function

-

No

Callback to be invoked when the API call fails.

complete

function

-

No

Callback to be invoked when the API call is complete, regardless of success or failure.

Return values in timeoutCallback

Name

Type

Description

tempThumbPath

string

Temporary path (local path) of the thumbnail file.

tempVideoPath

string

Temporary path (local path) of the video file.

Example
const cameraContext = has.createCameraContext();
cameraContext.startRecord({
  timeoutCallback: (res) => {
    console.log('startRecord timeoutCallback ' + JSON.stringify(res));
  },
  timeout: 20,
  success: (res) => {
    console.log('startRecord success');
  },
  fail: (res) => {
    console.log('startRecord fail');
  },
  complete: (res) => {
    console.log('startRecord complete');
  }
});

[h2]CameraContext.stopRecord

CameraContext.stopRecord(Object object)

Stops recording.

Since: 1.0.4

Parameters

The input parameter is an object containing the following fields.

Name

Type

Default Value

Mandatory

Description

success

function

-

No

Callback to be invoked when the API call is successful.

fail

function

-

No

Callback to be invoked when the API call fails.

complete

function

-

No

Callback to be invoked when the API call is complete, regardless of success or failure.

Return value of success

Name

Type

Description

tempThumbPath

string

Temporary path (local path) of the thumbnail file.

tempVideoPath

string

Temporary path (local path) of the video file.

Example
const cameraContext = has.createCameraContext();
cameraContext.stopRecord({
  success: (res) => {
    console.log('stopRecord success' + JSON.stringify(res));
  },
  fail: (res) => {
    console.log('stopRecord fail');
  },
  complete: (res) => {
    console.log('stopRecord complete');
  }
});

[h2]CameraContext.takePhoto

CameraContext.takePhoto(Object object)

Captures a photo.

Since: 1.0.4

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

Parameters

The input parameter is an object containing the following fields.

Name

Type

Default Value

Mandatory

Description

quality

string

normal

No

Quality of the photo.

  • high: high quality.
  • normal: medium quality.
  • low: low quality.

selfieMirror

boolean

true

No

Whether to enable mirroring.

success

function

-

No

Callback to be invoked when the API call is successful.

fail

function

-

No

Callback to be invoked when the API call fails.

complete

function

-

No

Callback to be invoked when the API call is complete, regardless of success or failure.

Return value of success

Name

Type

Description

tempImagePath

string

Temporary path (local path) of the photo file.

Example
const cameraContext = has.createCameraContext();
cameraContext.takePhoto({
  quality: 'high',
  selfieMirror: false,
  success: (res) => {
    console.log('takePhoto success' + JSON.stringify(res));
  },
  fail: (res) => {
    console.log('takePhoto fail');
  },
  complete: (res) => {
    console.log('takePhoto complete');
  }
});

[h2]CameraContext.setZoom

CameraContext.setZoom(Object object): void

Sets the camera zoom level, which can be used for both photo capture and video recording scenarios. The zoom level resets to the default value when you switch between photo and video modes.

Since: 1.0.14

Parameters

The input parameter is an object containing the following fields.

Name

Type

Default Value

Mandatory

Description

zoom

number

-

Yes

Zoom level, within the range [1, maxZoom]. It can be a decimal, precise to one decimal place. The value of maxZoom can be obtained from the return value of bindinitdone.

success

function

-

No

Callback to be invoked when the API call is successful.

fail

function

-

No

Callback to be invoked when the API call fails.

complete

function

-

No

Callback to be invoked when the API call is complete, regardless of success or failure.

Return value of success

Name

Type

Description

zoom

number

Actual zoom ratio set, as only values within the precision range are retained.

Example
const cameraContext = has.createCameraContext();
cameraContext.setZoom ({
    zoom: 3,
    success: (res) => {
      console.log(setZoom success' + JSON.stringify(res));
    },
    fail: (res) => {
      console.log(setZoom fail');
    },
    complete: (res) => {
      console.log(setZoom complete');
    }
});

[h2]CameraContext.onCameraFrame

CameraContext.onCameraFrame (callback: (res: onCameraFrameListenResult) => void): CameraFrameListener

Sets the callback function listener and the controller CameraFrameListener for real-time frame listening.

Since: 1.0.14

Parameters

Name

Type

Default Value

Mandatory

Description

callback

function

-

Yes

Callback function after real-time frame listening is enabled.

onCameraFrameListenResult:

Name

Type

Description

width

number

Width of the real-time frame.

height

number

Height of the real-time frame.

data

ArrayBuffer

ArrayBuffer data of the real-time frame.

Return value

CameraFrameListener, a controller that can be used to enable or disable real-time frame listening.

Example
this.ctx = has.createCameraContext();
this.listener = this.ctx.onCameraFrame((data) => {
    // Frame data:
    const width = data.width;
    const height = data.height;
    const arraybuffer = data.data;
});

CameraFrameListener.start

CameraFrameListener.start(Object object): void

Starts listening for frame data, triggering the callback function.

Since: 1.0.14

Parameters

The input parameter is an object containing the following fields.

Name

Type

Default Value

Mandatory

Description

success

function

-

No

Callback to be invoked when the API call is successful.

fail

function

-

No

Callback to be invoked when the API call fails.

complete

function

-

No

Callback to be invoked when the API call is complete, regardless of success or failure.

Example

this.ctx = has.createCameraContext();
this.listener = this.ctx.onCameraFrame((data) => {
    // Frame data:
    const width = data.width;
    const height = data.height;
    const arraybuffer = data.data;
});
this.listener && this.listener.start({
      success: (res) => {
        console.log('CameraFrameListener start success', res);
      },
      fail: (err) => {
        console.log('CameraFrameListener start fail', err);
      },
      complete: (res) => {
        console.log('CameraFrameListener start complete', res);
      }
});

CameraFrameListener.stop

CameraFrameListener.stop(Object object): void

Stops listening for frame data, stopping the callback function from being triggered.

Since: 1.0.14

Parameters

The input parameter is an object containing the following fields.

Name

Type

Default Value

Mandatory

Description

success

function

-

No

Callback to be invoked when the API call is successful.

fail

function

-

No

Callback to be invoked when the API call fails.

complete

function

-

No

Callback to be invoked when the API call is complete, regardless of success or failure.

Example

this.ctx = has.createCameraContext();
this.listener = this.ctx.onCameraFrame((data) => {
    // Frame data:
    const width = data.width;
    const height = data.height;
    const arraybuffer = data.data;
});
this.listener && this.listener.start({
      success: (res) => {
        console.log('CameraFrameListener start success', res);
      },
      fail: (err) => {
        console.log('CameraFrameListener start fail', err);
      },
      complete: (res) => {
        console.log('CameraFrameListener start complete', res);
      }
});
this.listener && this.listener.stop({
      success: (res) => {
        console.log('CameraFrameListener stop success', res);
      },
      fail: (err) => {
        console.log('CameraFrameListener stop fail', err);
      },
      complete: (res) => {
        console.log('CameraFrameListener stop complete', res);
      },
});