harmony 鸿蒙@ohos.multimedia.camera (Camera Management)

2022-08-09 浏览 (1252)

@ohos.multimedia.camera (Camera Management)

NOTE

The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version.

Modules to Import

import camera from '@ohos.multimedia.camera';

camera.getCameraManager

getCameraManager(context: BaseContext): CameraManager

Obtains a CameraManager instance. This API returns the result synchronously.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
contextBaseContextYesApplication context.

Return value

TypeDescription
CameraManagerCameraManager instance obtained.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400101Parameter missing or parameter type incorrect
7400201Camera service fatal error.

Example

import common from '@ohos.app.ability.common';
import { BusinessError } from '@ohos.base';

function getCameraManager(context: common.BaseContext): camera.CameraManager|undefined {
  let cameraManager: camera.CameraManager|undefined = undefined;
  try {
    cameraManager = camera.getCameraManager(context);
  } catch (error) {
    let err = error as BusinessError;
    console.error(`The getCameraManager call failed. error code: ${err.code}`);
  }
  return cameraManager;
}

camera.getModeManager

getModeManager(context: BaseContext): ModeManager

Obtains a ModeManager instance. This API returns the result synchronously.

Mode management, as an enhancement to CameraManager, is used to manage advanced camera features such as portrait mode.

System API: This is a system API.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
contextBaseContextYesApplication context.

Return value

TypeDescription
ModeManagerMode manager.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400101Parameter missing or parameter type incorrect
7400201Camera service fatal error.

Example

import common from '@ohos.app.ability.common';
import { BusinessError } from '@ohos.base';

function getModeManager(context: common.BaseContext): camera.ModeManager|undefined {
  let modeManager: camera.ModeManager|undefined = undefined;
  try {
    modeManager = camera.getModeManager(context);
  } catch (error) {
    let err = error as BusinessError;
    console.error(`The getModeManager call failed. error code: ${err.code}`);
  }
  return modeManager;
}

CameraStatus

Enumerates the camera statuses.

System capability: SystemCapability.Multimedia.Camera.Core

NameValueDescription
CAMERA_STATUS_APPEAR0A camera appears.
CAMERA_STATUS_DISAPPEAR1The camera disappears.
CAMERA_STATUS_AVAILABLE2The camera is available.
CAMERA_STATUS_UNAVAILABLE3The camera is unavailable.

Profile

Defines the camera profile.

System capability: SystemCapability.Multimedia.Camera.Core

NameTypeMandatoryDescription
formatCameraFormatYesOutput format.
sizeSizeYesResolution.

FrameRateRange

Defines the frame rate range.

System capability: SystemCapability.Multimedia.Camera.Core

NameTypeMandatoryDescription
minnumberYesMinimum frame rate.
maxnumberYesMaximum frame rate.

VideoProfile

Defines the video profile.

System capability: SystemCapability.Multimedia.Camera.Core

NameTypeMandatoryDescription
frameRateRangeFrameRateRangeYesFrame rate range, in units of frames per second (FPS).

CameraOutputCapability

Defines the camera output capability.

System capability: SystemCapability.Multimedia.Camera.Core

NameTypeMandatoryDescription
previewProfilesArray<Profile>YesSupported preview profiles.
photoProfilesArray<Profile>YesSupported shooting profiles.
videoProfilesArray<VideoProfile>YesSupported video recording profiles.
supportedMetadataObjectTypesArray<MetadataObjectType>YesSupported metadata object types.

CameraErrorCode

Enumerates the camera error codes, which are returned when an API call is incorrect or the on() API is used to listen for the error status.

System capability: SystemCapability.Multimedia.Camera.Core

NameValueDescription
INVALID_ARGUMENT7400101A parameter is missing or the parameter type is incorrect.
OPERATION_NOT_ALLOWED7400102The operation is not allowed.
SESSION_NOT_CONFIG7400103The session is not configured.
SESSION_NOT_RUNNING7400104The session is not running.
SESSION_CONFIG_LOCKED7400105The session configuration is locked.
DEVICE_SETTING_LOCKED7400106The device setting is locked.
CONFLICT_CAMERA7400107The device is already started.
DEVICE_DISABLED7400108The camera is disabled for security reasons.
DEVICE_PREEMPTED7400109The camera is preempted.
SERVICE_FATAL_ERROR7400201An error occurs in the camera service.

CameraMode

Enumerates the camera modes.

System API: This is a system API.

System capability: SystemCapability.Multimedia.Camera.Core

NameValueDescription
PORTRAIT1Portrait mode.

FilterType

Enumerates the filter types.

System API: This is a system API.

System capability: SystemCapability.Multimedia.Camera.Core

NameValueDescription
NONE0No filter.
CLASSIC1Classic.
DAWN2Dawn.
PURE3Pure.
GREY4Gray.
NATURAL5Natural.
MORI6Mori.
FAIR7Fair.
PINK8Pink.

PortraitEffect

Enumerates the portrait effects.

System API: This is a system API.

System capability: SystemCapability.Multimedia.Camera.Core

NameValueDescription
OFF0Disabled.
CIRCLES1Circles.

BeautyType

Enumerates the beauty types.

System API: This is a system API.

System capability: SystemCapability.Multimedia.Camera.Core

NameValueDescription
AUTO0Automatic.
SKIN_SMOOTH1Skin smoothing.
FACE_SLENDER2Face slimming.
SKIN_TONE3Skin tone perfection.

CameraManager

Implements camera management. Before calling any API in CameraManager, you must use getCameraManager to obtain a CameraManager instance.

getSupportedCameras

getSupportedCameras(): Array<CameraDevice>

Obtains supported cameras. This API returns the result synchronously.

System capability: SystemCapability.Multimedia.Camera.Core

Return value

TypeDescription
Array<CameraDevice>An array of supported cameras.

Example

import { BusinessError } from '@ohos.base';

function getSupportedCameras(cameraManager: camera.CameraManager): Array<camera.CameraDevice> {
  let cameras: Array<camera.CameraDevice> = [];
  try {
    cameras = cameraManager.getSupportedCameras();
  } catch (error) {
    let err = error as BusinessError;
    console.error(`The getSupportedCameras call failed. error code: ${err.code}`);
  }
  return cameras;
}

getSupportedOutputCapability

getSupportedOutputCapability(camera:CameraDevice): CameraOutputCapability

Obtains the output capability supported by a camera. This API returns the result synchronously.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
cameraDeviceCameraDeviceYesTarget camera, which is obtained through getSupportedCameras.

Return value

TypeDescription
CameraOutputCapabilityCamera output capability obtained.

Example

function getSupportedOutputCapability(cameraDevice: camera.CameraDevice, cameraManager: camera.CameraManager): camera.CameraOutputCapability {
  let cameraOutputCapability: camera.CameraOutputCapability = cameraManager.getSupportedOutputCapability(cameraDevice);
  return cameraOutputCapability;
}

isCameraMuted

isCameraMuted(): boolean

Checks whether the camera is muted.

Before calling the API, ensure that the camera can be muted. You can use isCameraMuteSupported to check whether the camera can be muted.

System capability: SystemCapability.Multimedia.Camera.Core

Return value

TypeDescription
booleanReturns true if the camera is muted; returns false otherwise.

Example

function isCameraMuted(cameraManager: camera.CameraManager): boolean {
  let isMuted: boolean = cameraManager.isCameraMuted();
  return isMuted;
}

isCameraMuteSupported

isCameraMuteSupported(): boolean

Checks whether the camera can be muted.

System API: This is a system API.

System capability: SystemCapability.Multimedia.Camera.Core

Return value

TypeDescription
booleanReturns true if the camera can be muted; returns false otherwise.

Example

function isCameraMuteSupported(cameraManager: camera.CameraManager): boolean {
  let isMuteSupported: boolean = cameraManager.isCameraMuteSupported();
  return isMuteSupported;
}

muteCamera

muteCamera(mute: boolean): void

Mutes or unmutes the camera.

System API: This is a system API.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
mutebooleanYesWhether to mute the camera. The value true means to mute the camera, and false means the opposite.

Example

function muteCamera(cameraManager: camera.CameraManager): void {
  let mute: boolean = true;
  cameraManager.muteCamera(mute);
}

createCameraInput

createCameraInput(camera: CameraDevice): CameraInput

Creates a CameraInput instance with the specified CameraDevice object. This API returns the result synchronously.

Required permissions: ohos.permission.CAMERA

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
cameraDeviceCameraDeviceYesTarget CameraDevice object, which is obtained through getSupportedCameras.

Return value

TypeDescription
CameraInputCameraInput instance created. If the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400101Parameter missing or parameter type incorrect

Example

import { BusinessError } from '@ohos.base';

function createCameraInput(cameraDevice: camera.CameraDevice, cameraManager: camera.CameraManager): camera.CameraInput|undefined {
  let cameraInput: camera.CameraInput|undefined = undefined;
  try {
    cameraInput = cameraManager.createCameraInput(cameraDevice);
  } catch (error) {
    // If the operation fails, error.code is returned and processed.
    let err = error as BusinessError;
    console.error(`The createCameraInput call failed. error code: ${err.code}`);
  }
  return cameraInput;
}

createCameraInput

createCameraInput(position: CameraPosition, type: CameraType): CameraInput

Creates a CameraInput instance with the specified camera position and type. This API returns the result synchronously.

Required permissions: ohos.permission.CAMERA

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
positionCameraPositionYesCamera position, which is obtained through getSupportedCameras. This API obtains a CameraDevice object, which contains the camera position information.
typeCameraTypeYesCamera type, which is obtained through getSupportedCameras. This API obtains a CameraDevice object, which contains the camera type information.

Return value

TypeDescription
CameraInputCameraInput instance created. If the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400101Parameter missing or parameter type incorrect

Example

import { BusinessError } from '@ohos.base';

function createCameraInput(cameraDevice: camera.CameraDevice, cameraManager: camera.CameraManager): camera.CameraInput|undefined {
  let position: camera.CameraPosition = cameraDevice.cameraPosition;
  let type: camera.CameraType = cameraDevice.cameraType;
  let cameraInput: camera.CameraInput|undefined = undefined;
  try {
    cameraInput = cameraManager.createCameraInput(position, type);
  } catch (error) {
    // If the operation fails, error.code is returned and processed.
    let err = error as BusinessError;
    console.log(`The createCameraInput call failed. error code: ${err.code}`);
  }
  return cameraInput;
}

createPreviewOutput

createPreviewOutput(profile: Profile, surfaceId: string): PreviewOutput

Creates a PreviewOutput instance. This API returns the result synchronously.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
profileProfileYesSupported preview profiles, which are obtained through getSupportedOutputCapability.
surfaceIdstringYesSurface ID, which is obtained from XComponent or ImageReceiver.

Return value

TypeDescription
PreviewOutputPreviewOutput instance created. If the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400101Parameter missing or parameter type incorrect

Example

import { BusinessError } from '@ohos.base';

function createPreviewOutput(cameraOutputCapability: camera.CameraOutputCapability, cameraManager: camera.CameraManager, surfaceId: string): camera.PreviewOutput|undefined {
  let profile: camera.Profile = cameraOutputCapability.previewProfiles[0];
  let previewOutput: camera.PreviewOutput|undefined = undefined;
  try {
    previewOutput = cameraManager.createPreviewOutput(profile, surfaceId);
  } catch (error) {
    // If the operation fails, error.code is returned and processed.
    let err = error as BusinessError;
    console.log(`The createPreviewOutput call failed. error code: ${err.code}`);
  }
  return previewOutput;
}

createPhotoOutput

createPhotoOutput(profile: Profile, surfaceId: string): PhotoOutput

Creates a PhotoOutput instance. This API returns the result synchronously.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
profileProfileYesSupported shooting profiles, which are obtained through getSupportedOutputCapability.
surfaceIdstringYesSurface ID, which is obtained from ImageReceiver.

Return value

TypeDescription
PhotoOutputPhotoOutput instance created. If the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400101Parameter missing or parameter type incorrect

Example

import { BusinessError } from '@ohos.base';

function createPhotoOutput(cameraOutputCapability: camera.CameraOutputCapability, cameraManager: camera.CameraManager, surfaceId: string): camera.PhotoOutput|undefined {
  let profile: camera.Profile = cameraOutputCapability.photoProfiles[0];
  let photoOutput: camera.PhotoOutput|undefined = undefined;
  try {
    photoOutput = cameraManager.createPhotoOutput(profile, surfaceId);
  } catch (error) {
    // If the operation fails, error.code is returned and processed.
    let err = error as BusinessError;
    console.log(`The createPhotoOutput call failed. error code: ${err.code}`);
  }
  return photoOutput;
}

createVideoOutput

createVideoOutput(profile: VideoProfile, surfaceId: string): VideoOutput

Creates a VideoOutput instance. This API returns the result synchronously.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
profileVideoProfileYesSupported video recording profiles, which are obtained through getSupportedOutputCapability.
surfaceIdstringYesSurface ID, which is obtained from VideoRecorder.

Return value

TypeDescription
VideoOutputVideoOutput instance created. If the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400101Parameter missing or parameter type incorrect

Example

import { BusinessError } from '@ohos.base';

function createVideoOutput(cameraOutputCapability: camera.CameraOutputCapability, cameraManager: camera.CameraManager, surfaceId: string): camera.VideoOutput|undefined {
  let profile: camera.VideoProfile = cameraOutputCapability.videoProfiles[0];
  let videoOutput: camera.VideoOutput|undefined = undefined;
  try {
    videoOutput = cameraManager.createVideoOutput(profile, surfaceId);
  } catch (error) {
    // If the operation fails, error.code is returned and processed.
    let err = error as BusinessError;
    console.log(`The createPhotoOutput call failed. error code: ${err.code}`);
  }
  return videoOutput;
}

createMetadataOutput

createMetadataOutput(metadataObjectTypes:Array<MetadataObjectType>): MetadataOutput

Creates a MetadataOutput instance. This API returns the result synchronously.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
metadataObjectTypesArray<MetadataObjectType>YesMetadata object types, which are obtained through getSupportedOutputCapability.

Return value

TypeDescription
MetadataOutputMetadataOutput instance created. If the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400101Parameter missing or parameter type incorrect

Example

import { BusinessError } from '@ohos.base';

function createMetadataOutput(cameraManager: camera.CameraManager, cameraOutputCapability: camera.CameraOutputCapability): void {
  let metadataObjectTypes: Array<camera.MetadataObjectType> = cameraOutputCapability.supportedMetadataObjectTypes;
  let metadataOutput: camera.MetadataOutput|undefined = undefined;
  try {
    metadataOutput = cameraManager.createMetadataOutput(metadataObjectTypes);
  } catch (error) {
    // If the operation fails, error.code is returned and processed.
    let err = error as BusinessError;
    console.log(`createMetadataOutput error. error code: ${err.code}`);
  }
}

createCaptureSession

createCaptureSession(): CaptureSession

Creates a CaptureSession instance. This API returns the result synchronously.

System capability: SystemCapability.Multimedia.Camera.Core

Return value

TypeDescription
CaptureSessionCaptureSession instance created. If the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400201Camera service fatal error.

Example

import { BusinessError } from '@ohos.base';

function createCaptureSession(cameraManager: camera.CameraManager): camera.CaptureSession|undefined {
  let captureSession: camera.CaptureSession|undefined = undefined;
  try {
    captureSession = cameraManager.createCaptureSession();
  } catch (error) {
    // If the operation fails, error.code is returned and processed.
    let err = error as BusinessError;
    console.log(`createCaptureSession error. error code: ${err.code}`);
  }
  return captureSession;
}

on('cameraStatus')

on(type: 'cameraStatus', callback: AsyncCallback<CameraStatusInfo>): void

Subscribes to camera status events. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value is fixed at 'cameraStatus'. The event can be listened for when a CameraManager instance is obtained. This event is triggered and the corresponding information is returned only when the device is enabled or disabled.
callbackAsyncCallback<CameraStatusInfo>YesCallback used to return the camera status change.

Example

import { BusinessError } from '@ohos.base';

function registerCameraStatus(cameraManager: camera.CameraManager): void {
  cameraManager.on('cameraStatus', (err: BusinessError, cameraStatusInfo: camera.CameraStatusInfo) => {
    console.log(`camera : ${cameraStatusInfo.camera.cameraId}`);
    console.log(`status: ${cameraStatusInfo.status}`);
  });
}

off('cameraStatus')

off(type: 'cameraStatus', callback?: AsyncCallback<CameraStatusInfo>): void

Unsubscribes from camera status events. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value is fixed at 'cameraStatus'. The event can be listened for when a CameraManager instance is obtained.
callbackAsyncCallback<CameraStatusInfo>NoCallback used for unsubscription. If a callback is passed in, only the subscription matching that callback is canceled.

Example

function unregisterCameraStatus(cameraManager: camera.CameraManager): void {
  cameraManager.off('cameraStatus');
}

on('cameraMute')

on(type: 'cameraMute', callback: AsyncCallback<boolean>): void

Subscribes to camera mute status events. This API uses an asynchronous callback to return the result.

System API: This is a system API.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value is fixed at 'cameraMute', indicating the camera mute status. The event can be listened for when a CameraManager instance is obtained. This event is triggered and the status is returned when the camera is enabled or disabled.
callbackAsyncCallback<boolean>YesCallback used to return the mute status. The value true means that the camera is enabled, and false means that the camera is disabled.

Example

import { BusinessError } from '@ohos.base';

function registerCameraMute(cameraManager: camera.CameraManager): void {
  cameraManager.on('cameraMute', (err: BusinessError, curMuted: boolean) => {
    let isMuted: boolean = curMuted;
    console.log(`cameraMute status: ${isMuted}`);
  })
}

off('cameraMute')

off(type: 'cameraMute', callback?: AsyncCallback<boolean>): void

Unsubscribes from camera mute status events. This API uses an asynchronous callback to return the result.

System API: This is a system API.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value is fixed at 'cameraMute', indicating the camera mute status. The event can be listened for when a CameraManager instance is obtained.
callbackAsyncCallback<boolean>NoCallback used for unsubscription. If a callback is passed in, only the subscription matching that callback is canceled.

Example

import { BusinessError } from '@ohos.base';

function callback(err: BusinessError, curMuetd: boolean): void {
  let isMuted: boolean = curMuetd;
}

function unregisterCameraMute(cameraManager: camera.CameraManager): void {
  cameraManager.on('cameraMute', callback);
  cameraManager.off('cameraMute', callback);
}

isPrelaunchSupported

isPrelaunchSupported(camera: CameraDevice): boolean

Checks whether a camera supports prelaunch.

System API: This is a system API.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
cameraCameraDeviceYesCamera object.

Return value

TypeDescription
booleanReturns whether the camera supports prelaunch. The value true means that the camera supports prelaunch, and false means the opposite.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400101Parameter missing or parameter type incorrect.

Example

import common from '@ohos.app.ability.common';

function isPreLaunchSupported(context: common.BaseContext): boolean {
  let cameraManager: camera.CameraManager = camera.getCameraManager(context);
  let cameras: Array<camera.CameraDevice> = cameraManager.getSupportedCameras();
  let isSupported: boolean = false;
  if (cameras && cameras.length >= 1) {
    isSupported = cameraManager.isPrelaunchSupported(cameras[0]);
    console.log(`PreLaunch supported states: ${isSupported}`);
    return isSupported;
  }
  return isSupported;
}

setPrelaunchConfig

setPrelaunchConfig(prelaunchConfig: PrelaunchConfig): void

Sets prelaunch configuration.

Before the setting, use isPrelaunchSupported to check whether the device supports prelaunch.

System API: This is a system API.

Required permissions: ohos.permission.CAMERA

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
prelaunchConfigPrelaunchConfigYesPrelaunch configuration.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400101Parameter missing or parameter type incorrect.
7400102Operation not allow.

Example

import common from '@ohos.app.ability.common';
import { BusinessError } from '@ohos.base';

function setPrelaunchConfig(context: common.BaseContext): void {
  let cameraManager: camera.CameraManager = camera.getCameraManager(context);
  let cameras: Array<camera.CameraDevice> = cameraManager.getSupportedCameras();
  if (cameras && cameras.length >= 1) {
    let cameraDevice: camera.CameraDevice = cameras[0];
    if(cameraManager.isPrelaunchSupported(cameraDevice)) {
      try {
        cameraManager.setPrelaunchConfig({cameraDevice: cameraDevice});
      } catch (error) {
        let err = error as BusinessError;
        console.error(`setPrelaunchConfig error. Code: ${err.code}, message: ${err.message}`);
      }
    }
  }
}

prelaunch

prelaunch(): void

Prelaunches the camera. This API is called when a user clicks the system camera icon to start the camera application.

System API: This is a system API.

System capability: SystemCapability.Multimedia.Camera.Core

Example

import common from '@ohos.app.ability.common';
import { BusinessError } from '@ohos.base';

function preLaunch(context: common.BaseContext): void {
  let cameraManager: camera.CameraManager = camera.getCameraManager(context);
  try {
    cameraManager.prelaunch();
  } catch (error) {
    let err = error as BusinessError;
    console.error(`prelaunch error. Code: ${err.code}, message: ${err.message}`);
  }
}

createDeferredPreviewOutput

createDeferredPreviewOutput(profile: Profile): PreviewOutput

Creates a deferred PreviewOutput instance and adds it to the data stream instead of a common PreviewOutput instance during stream configuration.

System API: This is a system API.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
profileProfileYesConfiguration file of the camera preview stream.

Return value

TypeDescription
PreviewOutputReturns a PreviewOutput instance.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400101Parameter missing or parameter type incorrect.

Example

import common from '@ohos.app.ability.common';

function getDeferredPreviewOutput(context: common.BaseContext, previewProfile: camera.Profile): camera.PreviewOutput {
  const cameraManager: camera.CameraManager = camera.getCameraManager(context);
  const output: camera.PreviewOutput = cameraManager.createDeferredPreviewOutput(previewProfile);
  return output;
}

ModeManager

Implements camera mode management. Before calling any API in this class, you must use getModeManager to obtain a ModeManager instance.

getSupportedModes

getSupportedModes(device: CameraDevice): Array<CameraMode>

Obtains the modes supported by a camera device. This API returns the result synchronously.

System API: This is a system API.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
deviceCameraDeviceYesCameraDevice instance, which is obtained through getSupportedCameras.

Return value

TypeDescription
Array<CameraMode>Array of the supported modes.

Example

function getSupportedModes(modeManager: camera.ModeManager, cameraDevice: camera.CameraDevice): Array<camera.CameraMode> {
  let cameraModes: Array<camera.CameraMode> = modeManager.getSupportedModes(cameraDevice);
  return cameraModes;
}

getSupportedOutputCapability

getSupportedOutputCapability(device:CameraDevice, mode: CameraMode): CameraOutputCapability

Obtains the output capability supported by a camera device in the specified mode. This API returns the result synchronously.

System API: This is a system API.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
deviceCameraDeviceYesCameraDevice instance, which is obtained through getSupportedCameras.
modeCameraModeYesMode, which is obtained by calling getSupportedModes.

Return value

TypeDescription
CameraOutputCapabilityCamera output capability obtained.

Example

function getSupportedOutputCapability(cameraManager: camera.CameraManager, modeManager: camera.ModeManager): camera.CameraOutputCapability|undefined {
  let cameras: Array<camera.CameraDevice> = cameraManager.getSupportedCameras();
  if (cameras == undefined||cameras.length <= 0) {
    return undefined;
  }
  let cameraDevice: camera.CameraDevice = cameras[0];
  let cameraModes: Array<camera.CameraMode> = modeManager.getSupportedModes(cameraDevice);
  if (cameraModes === undefined||cameraModes.length <= 0) {
    return undefined;
  }
  let mode: camera.CameraMode = cameraModes[0];
  let cameraOutputCapability: camera.CameraOutputCapability = modeManager.getSupportedOutputCapability(cameraDevice, mode);
  return cameraOutputCapability;
}

createCaptureSession

createCaptureSession(mode: CameraMode): CaptureSession

Creates a session for a mode.

System API: This is a system API.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
modeCameraModeYesMode, which is obtained by calling getSupportedModes.

Return value

TypeDescription
CaptureSessionCaptureSession instance created.

Example

import { BusinessError } from '@ohos.base';

function createCaptureSession(cameraManager: camera.CameraManager, modeManager: camera.ModeManager): camera.CaptureSession|undefined {
  let cameras: Array<camera.CameraDevice> = cameraManager.getSupportedCameras();
  if (cameras == undefined||cameras.length <= 0) {
    return;
  }
  let cameraDevice: camera.CameraDevice = cameras[0];
  let cameraModes: Array<camera.CameraMode> = modeManager.getSupportedModes(cameraDevice);
  if (cameraModes === undefined||cameraModes.length <= 0) {
    return;
  }
  let mode: camera.CameraMode = cameraModes[0];
  let captureSession: camera.CaptureSession|undefined = undefined;
  try {
    captureSession = modeManager.createCaptureSession(mode);
  } catch (error) {
    let err = error as BusinessError;
    console.error(`The createCaptureSession call failed. error code: ${err.code}`);
  }
  return captureSession;
}

PrelaunchConfig

Defines the camera prelaunch configuration.

Currently, the configuration is used for sensor-level prelaunch. It will be used for stream-level prelaunch in a later version.

System API: This is a system API.

System capability: SystemCapability.Multimedia.Camera.Core

NameTypeMandatoryDescription
cameraDeviceCameraDeviceYesCamera object.

CameraStatusInfo

Describes the camera status information.

System capability: SystemCapability.Multimedia.Camera.Core

NameTypeMandatoryDescription
cameraCameraDeviceYesCamera object.
statusCameraStatusYesCamera status.

CameraPosition

Enumerates the camera positions.

System capability: SystemCapability.Multimedia.Camera.Core

NameValueDescription
CAMERA_POSITION_UNSPECIFIED0Unspecified position.
CAMERA_POSITION_BACK1Rear camera.
CAMERA_POSITION_FRONT2Front camera.

CameraType

Enumerates the camera types.

System capability: SystemCapability.Multimedia.Camera.Core

NameValueDescription
CAMERA_TYPE_DEFAULT0Unspecified camera type.
CAMERA_TYPE_WIDE_ANGLE1Wide camera.
CAMERA_TYPE_ULTRA_WIDE2Ultra wide camera.
CAMERA_TYPE_TELEPHOTO3Telephoto camera.
CAMERA_TYPE_TRUE_DEPTH4Camera with depth of field information.

ConnectionType

Enumerates the camera connection types.

System capability: SystemCapability.Multimedia.Camera.Core

NameValueDescription
CAMERA_CONNECTION_BUILT_IN0Built-in camera.
CAMERA_CONNECTION_USB_PLUGIN1Camera connected using USB.
CAMERA_CONNECTION_REMOTE2Remote camera.

HostDeviceType

Enumerates the remote camera types.

System API: This is a system API.

System capability: SystemCapability.Multimedia.Camera.Core

NameValueDescription
UNKNOWN_TYPE0Unknown type.
PHONE0x0ECamera of a smartphone.
TABLET0x11Camera of a tablet.

CameraDevice

Defines the camera device information.

System capability: SystemCapability.Multimedia.Camera.Core

NameTypeMandatoryDescription
cameraIdstringYesCameraDevice object.
cameraPositionCameraPositionYesCamera position.
cameraTypeCameraTypeYesCamera type.
connectionTypeConnectionTypeYesCamera connection type.
hostDeviceNamestringYesName of the remote device.
System API: This is a system API.
hostDeviceTypehostDeviceTypeYesType of the remote device.
System API: This is a system API.

Size

Enumerates the camera output capability.

System capability: SystemCapability.Multimedia.Camera.Core

NameTypeReadableWritableDescription
heightnumberYesYesImage height, in pixels.
widthnumberYesYesImage width, in pixels.

Point

Enumerates the point coordinates, which are used for focus and exposure configuration.

System capability: SystemCapability.Multimedia.Camera.Core

NameTypeMandatoryDescription
xnumberYesX coordinate of a point.
ynumberYesY coordinate of a point.

CameraFormat

Enumerates the camera output formats.

System capability: SystemCapability.Multimedia.Camera.Core

NameValueDescription
CAMERA_FORMAT_RGBA_88883RGB image.
CAMERA_FORMAT_YUV_420_SP1003YUV 420 SP image.
CAMERA_FORMAT_JPEG2000JPEG image.

CameraInput

Provides camera information used in CaptureSession.

open

open(callback: AsyncCallback<void>): void

Opens this camera. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<void>YesCallback used to return the result. If the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400107Can not use camera cause of conflict.
7400108Camera disabled cause of security reason.
7400201Camera service fatal error.

Example

import { BusinessError } from '@ohos.base';

function openCameraInput(cameraInput: camera.CameraInput): void {
  cameraInput.open((err: BusinessError) => {
    if (err) {
      console.error(`Failed to open the camera. ${err.code}`);
      return;
    }
    console.log('Callback returned with camera opened.');
  });
}

open

open(): Promise<void>

Opens this camera. This API uses a promise to return the result.

System capability: SystemCapability.Multimedia.Camera.Core

Return value

TypeDescription
Promise<void>Promise used to return the result. If the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400107Can not use camera cause of conflict.
7400108Camera disabled cause of security reason.
7400201Camera service fatal error.

Example

import { BusinessError } from '@ohos.base';

function openCameraInput(cameraInput: camera.CameraInput): void {
  cameraInput.open().then(() => {
    console.log('Promise returned with camera opened.');
  }).catch((err: BusinessError) => {
    console.error(`Failed to open the camera. ${err.code}`);
  });
}

close

close(callback: AsyncCallback<void>): void

Closes this camera. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<void>YesCallback used to return the result. If the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400201Camera service fatal error.

Example

import { BusinessError } from '@ohos.base';

function closeCameraInput(cameraInput: camera.CameraInput): void {
  cameraInput.close((err: BusinessError) => {
    if (err) {
      console.error(`Failed to close the cameras. ${err.code}`);
      return;
    }
    console.log('Callback returned with camera closed.');
  });
}

close

close(): Promise<void>

Closes this camera. This API uses a promise to return the result.

System capability: SystemCapability.Multimedia.Camera.Core

Return value

TypeDescription
Promise<void>Promise used to return the result.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400201Camera service fatal error.

Example

import { BusinessError } from '@ohos.base';

function closeCameraInput(cameraInput: camera.CameraInput): void {
  cameraInput.close().then(() => {
    console.log('Promise returned with camera closed.');
  }).catch((err: BusinessError) => {
    console.error(`Failed to close the cameras. ${err.code}`);
  });
}

on('error')

on(type: 'error', camera:CameraDevice, callback: ErrorCallback): void

Subscribes to CameraInput error events. This API uses a callback to return the result.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value is fixed at 'error'. The event can be listened for when a CameraInput instance is created. This event is triggered and the result is returned when an error occurs on the camera. For example, if the device is unavailable or a conflict occurs, the error information is returned.
cameraDeviceCameraDeviceYesCameraDevice object.
callbackErrorCallbackYesCallback used to return an error code defined in CameraErrorCode.

Example

import { BusinessError } from '@ohos.base';

function registerCameraInputError(cameraInput: camera.CameraInput, cameraDevice: camera.CameraDevice): void {
  cameraInput.on('error', cameraDevice, (error: BusinessError) => {
    console.log(`Camera input error code: ${error.code}`);
  });
}

off('error')

off(type: 'error', camera:CameraDevice, callback?: ErrorCallback): void

Unsubscribes from CameraInput error events.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value is fixed at 'error'. The event can be listened for when a CameraInput instance is created. This event is triggered and the result is returned when an error occurs on the camera. For example, if the device is unavailable or a conflict occurs, the error information is returned.
cameraDeviceCameraDeviceYesCameraDevice object.
callbackErrorCallbackNoCallback used for unsubscription. If a callback is passed in, only the subscription matching that callback is canceled.

Example


function unregisterCameraInputError(cameraInput: camera.CameraInput, cameraDevice: camera.CameraDevice): void {
  cameraInput.off('error', cameraDevice);
}

FlashMode

Enumerates the flash modes.

System capability: SystemCapability.Multimedia.Camera.Core

NameValueDescription
FLASH_MODE_CLOSE0The flash is off.
FLASH_MODE_OPEN1The flash is on.
FLASH_MODE_AUTO2The flash mode is auto, indicating that the flash fires automatically depending on the shooting conditions.
FLASH_MODE_ALWAYS_OPEN3The flash is steady on.

ExposureMode

Enumerates the exposure modes.

System capability: SystemCapability.Multimedia.Camera.Core

NameValueDescription
EXPOSURE_MODE_LOCKED0Exposure locked. The metering point cannot be set.
EXPOSURE_MODE_AUTO1Auto exposure. The metering point can be set by calling setMeteringPoint.
EXPOSURE_MODE_CONTINUOUS_AUTO2Continuous auto exposure. The metering point cannot be set.

FocusMode

Enumerates the focus modes.

System capability: SystemCapability.Multimedia.Camera.Core

NameValueDescription
FOCUS_MODE_MANUAL0Manual focus. The focal length of the camera can be manually set to change the focus position. However, the focal point cannot be set.
FOCUS_MODE_CONTINUOUS_AUTO1Continuous auto focus. The focal point cannot be set.
FOCUS_MODE_AUTO2Auto focus. The focal point can be set by calling setFocusPoint, and auto focus is performed once based on the focal point. After the auto focus operation is complete (regardless of whether the focus is successful or fails), the focus mode is locked. To enable the camera to initiate another auto focus, the application must call CONTINUOUS_AUTO again.
FOCUS_MODE_LOCKED3Focus locked. The focal point cannot be set.

FocusState

Enumerates the focus states.

System capability: SystemCapability.Multimedia.Camera.Core

NameValueDescription
FOCUS_STATE_SCAN0Focusing.
FOCUS_STATE_FOCUSED1Focused.
FOCUS_STATE_UNFOCUSED2Unfocused.

VideoStabilizationMode

Enumerates the video stabilization modes.

System capability: SystemCapability.Multimedia.Camera.Core

NameValueDescription
OFF0Video stabilization is disabled.
LOW1The basic video stabilization algorithm is used.
MIDDLE2A video stabilization algorithm with a stabilization effect better than that of the LOW type is used.
HIGH3A video stabilization algorithm with a stabilization effect better than that of the MIDDLE type is used.
AUTO4Automatic video stabilization is used.

CaptureSession

Implements a shooting session, which saves all CameraInput and CameraOutput instances required to run the camera and requests the camera to complete shooting or video recording.

beginConfig

beginConfig(): void

Starts configuration for the session.

System capability: SystemCapability.Multimedia.Camera.Core

Return value

TypeDescription
CameraErrorCodeIf the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400105Session config locked.

Example

import { BusinessError } from '@ohos.base';

function beginConfig(captureSession: camera.CaptureSession): void {
  try {
    captureSession.beginConfig();
  } catch (error) {
    // If the operation fails, error.code is returned and processed.
    let err = error as BusinessError;
    console.error(`The beginConfig call failed. error code: ${err.code}`);
  }
}

commitConfig

commitConfig(callback: AsyncCallback<void>): void

Commits the configuration for this CaptureSession instance. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<void>YesCallback used to return the result. If the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400102Operation not allow.
7400201Camera service fatal error.

Example

import { BusinessError } from '@ohos.base';

function commitConfig(captureSession: camera.CaptureSession): void {
  captureSession.commitConfig((err: BusinessError) => {
    if (err) {
      console.error(`The commitConfig call failed. error code: ${err.code}`);
      return;
    }
    console.log('Callback invoked to indicate the commit config success.');
  });
}

commitConfig

commitConfig(): Promise<void>

Commits the configuration for this CaptureSession instance. This API uses a promise to return the result.

System capability: SystemCapability.Multimedia.Camera.Core

Return value

TypeDescription
Promise<void>Promise used to return the result. If the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400102Operation not allow.
7400201Camera service fatal error.

Example

import { BusinessError } from '@ohos.base';

function commitConfig(captureSession: camera.CaptureSession): void {
  captureSession.commitConfig().then(() => {
    console.log('Promise returned to indicate the commit config success.');
  }).catch((err: BusinessError) => {
    // If the operation fails, error.code is returned and processed.
    console.error(`The commitConfig call failed. error code: ${err.code}`);
  });
}

addInput

addInput(cameraInput: CameraInput): void

Adds a CameraInput instance to the session.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
cameraInputCameraInputYesCameraInput instance to add.

Return value

TypeDescription
CameraErrorCodeIf the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400101Parameter missing or parameter type incorrect
7400102Operation not allow.

Example

import { BusinessError } from '@ohos.base';

function addInput(captureSession: camera.CaptureSession, cameraInput: camera.CameraInput): void {
  try {
    captureSession.addInput(cameraInput);
  } catch (error) {
    // If the operation fails, error.code is returned and processed.
    let err = error as BusinessError;
    console.log(`The addInput call failed. error code: ${err.code}`);
  }
}

removeInput

removeInput(cameraInput: CameraInput): void

Removes a CameraInput instance from the session.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
cameraInputCameraInputYesCameraInput instance to remove.

Return value

TypeDescription
CameraErrorCodeIf the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400101Parameter missing or parameter type incorrect
7400102Operation not allow.

Example

import { BusinessError } from '@ohos.base';

function removeInput(captureSession: camera.CaptureSession, cameraInput: camera.CameraInput): void {
  try {
    captureSession.removeInput(cameraInput);
  } catch (error) {
    // If the operation fails, error.code is returned and processed.
    let err = error as BusinessError;
    console.log(`The removeInput call failed. error code: ${err.code}`);
  }
}

addOutput

addOutput(cameraOutput: CameraOutput): void

Adds a CameraOutput instance to the session.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
cameraOutputCameraOutputYesCameraOutput instance to add.

Return value

TypeDescription
CameraErrorCodeIf the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400101Parameter missing or parameter type incorrect
7400102Operation not allow.

Example

import { BusinessError } from '@ohos.base';

function addOutput(captureSession: camera.CaptureSession, cameraOutput: camera.CameraOutput): void {
  try {
    captureSession.addOutput(cameraOutput);
  } catch (error) {
    // If the operation fails, error.code is returned and processed.
    let err = error as BusinessError;
    console.log(`The addOutput call failed. error code: ${err.code}`);
  }
}

removeOutput

removeOutput(cameraOutput: CameraOutput): void

Removes a CameraOutput instance from the session.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
cameraOutputCameraOutputYesCameraOutput instance to remove.

Return value

TypeDescription
CameraErrorCodeIf the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400101Parameter missing or parameter type incorrect
7400102Operation not allow.

Example

import { BusinessError } from '@ohos.base';

function removeOutput(captureSession: camera.CaptureSession, previewOutput: camera.PreviewOutput): void {
  try {
    captureSession.removeOutput(previewOutput);
  } catch (error) {
    // If the operation fails, error.code is returned and processed.
    let err = error as BusinessError;
    console.log(`The removeOutput call failed. error code: ${err.code}`);
  }
}

start

start(callback: AsyncCallback<void>): void

Starts this CaptureSession. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<void>YesCallback used to return the result. If the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400103Session not config.
7400201Camera service fatal error.

Example

import { BusinessError } from '@ohos.base';

function startCaptureSession(captureSession: camera.CaptureSession): void {
  captureSession.start((err: BusinessError) => {
    if (err) {
      console.error(`Failed to start the session ${err.code}`);
      return;
    }
    console.log('Callback invoked to indicate the session start success.');
  });
}

start

start(): Promise<void>

Starts this CaptureSession. This API uses a promise to return the result.

System capability: SystemCapability.Multimedia.Camera.Core

Return value

TypeDescription
Promise<void>Promise used to return the result.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400103Session not config.
7400201Camera service fatal error.

Example

import { BusinessError } from '@ohos.base';

function startCaptureSession(captureSession: camera.CaptureSession): void {
  captureSession.start().then(() => {
    console.log('Promise returned to indicate the session start success.');
  }).catch((err: BusinessError) => {
    console.error(`Failed to start the session ${err.code}`);
  });
}

stop

stop(callback: AsyncCallback<void>): void

Stops this CaptureSession. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<void>YesCallback used to return the result. If the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400201Camera service fatal error.

Example

import { BusinessError } from '@ohos.base';

function stopCaptureSession(captureSession: camera.CaptureSession): void {
  captureSession.stop((err: BusinessError) => {
    if (err) {
      console.error(`Failed to stop the session ${err.code}`);
      return;
    }
    console.log('Callback invoked to indicate the session stop success.');
  });
}

stop

stop(): Promise<void>

Stops this CaptureSession. This API uses a promise to return the result.

System capability: SystemCapability.Multimedia.Camera.Core

Return value

TypeDescription
Promise<void>Promise used to return the result. If the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400201Camera service fatal error.

Example

import { BusinessError } from '@ohos.base';

function stopCaptureSession(captureSession: camera.CaptureSession): void {
  captureSession.stop().then(() => {
    console.log('Promise returned to indicate the session stop success.');
  }).catch((err: BusinessError) => {
    console.error(`Failed to stop the session ${err.code}`);
  });
}

release

release(callback: AsyncCallback<void>): void

Releases this CaptureSession. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<void>YesCallback used to return the result. If the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400201Camera service fatal error.

Example

import { BusinessError } from '@ohos.base';

function releaseCaptureSession(captureSession: camera.CaptureSession): void {
  captureSession.release((err: BusinessError) => {
    if (err) {
      console.error(`Failed to release the CaptureSession instance ${err.code}`);
      return;
    }
    console.log('Callback invoked to indicate that the CaptureSession instance is released successfully.');
  });
}

release

release(): Promise<void>

Releases this CaptureSession. This API uses a promise to return the result.

System capability: SystemCapability.Multimedia.Camera.Core

Return value

TypeDescription
Promise<void>Promise used to return the result. If the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400201Camera service fatal error.

Example

import { BusinessError } from '@ohos.base';

function releaseCaptureSession(captureSession: camera.CaptureSession): void {
  captureSession.release().then(() => {
    console.log('Promise returned to indicate that the CaptureSession instance is released successfully.');
  }).catch((err: BusinessError) => {
    console.error(`Failed to release the CaptureSession instance ${err.code}`);
  });
}

hasFlash

hasFlash(): boolean

Checks whether the device has flash. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.Camera.Core

Return value

TypeDescription
booleanReturns true if the device has flash; returns false otherwise. If the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400103Session not config.

Example

import { BusinessError } from '@ohos.base';

function hasFlash(captureSession: camera.CaptureSession): boolean {
  let status: boolean = false;
  try {
    status = captureSession.hasFlash();
  } catch (error) {
    // If the operation fails, error.code is returned and processed.
    let err = error as BusinessError;
    console.error(`The hasFlash call failed. error code: ${err.code}`);
  }
  return status;
}

isFlashModeSupported

isFlashModeSupported(flashMode: FlashMode): boolean

Checks whether a flash mode is supported.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
flashModeFlashModeYesFlash mode.

Return value

TypeDescription
booleanReturns true if the flash mode is supported; returns false otherwise. If the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400103Session not config.

Example

import { BusinessError } from '@ohos.base';

function isFlashModeSupported(captureSession: camera.CaptureSession): boolean {
  let status: boolean = false;
  try {
    status = captureSession.isFlashModeSupported(camera.FlashMode.FLASH_MODE_AUTO);
  } catch (error) {
    // If the operation fails, error.code is returned and processed.
    let err = error as BusinessError;
    console.error(`The isFlashModeSupported call failed. error code: ${err.code}`);
  }
  return status;
}

setFlashMode

setFlashMode(flashMode: FlashMode): void

Sets a flash mode for the device.

Before the setting, do the following checks:

  1. Use hasFlash to check whether the device has flash.
  2. Use isFlashModeSupported to check whether the device supports the flash mode.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
flashModeFlashModeYesFlash mode.

Return value

TypeDescription
CameraErrorCodeIf the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400103Session not config.

Example

import { BusinessError } from '@ohos.base';

function setFlashMode(captureSession: camera.CaptureSession): void {
  try {
    captureSession.setFlashMode(camera.FlashMode.FLASH_MODE_AUTO);
  } catch (error) {
    // If the operation fails, error.code is returned and processed.
    let err = error as BusinessError;
    console.error(`The setFlashMode call failed. error code: ${err.code}`);
  }
}

getFlashMode

getFlashMode(): FlashMode

Obtains the flash mode in use.

System capability: SystemCapability.Multimedia.Camera.Core

Return value

TypeDescription
FlashModeFlash mode obtained. If the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400103Session not config.

Example

import { BusinessError } from '@ohos.base';

function getFlashMode(captureSession: camera.CaptureSession): camera.FlashMode|undefined {
  let flashMode: camera.FlashMode|undefined = undefined;
  try {
    flashMode = captureSession.getFlashMode();
  } catch (error) {
    // If the operation fails, error.code is returned and processed.
    let err = error as BusinessError;
    console.error(`The getFlashMode call failed.error code: ${err.code}`);
  }
  return flashMode;
}

isExposureModeSupported

isExposureModeSupported(aeMode: ExposureMode): boolean;

Checks whether an exposure mode is supported.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
aeModeExposureModeYesExposure mode.

Return value

TypeDescription
booleanReturns true if the exposure mode is supported; returns false otherwise. If the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400103Session not config.

Example

import { BusinessError } from '@ohos.base';

function isExposureModeSupported(captureSession: camera.CaptureSession): boolean {
  let isSupported: boolean = false;
  try {
    isSupported = captureSession.isExposureModeSupported(camera.ExposureMode.EXPOSURE_MODE_LOCKED);
  } catch (error) {
    // If the operation fails, error.code is returned and processed.
    let err = error as BusinessError;
    console.error(`The isExposureModeSupported call failed. error code: ${err.code}`);
  }
  return isSupported;
}

getExposureMode

getExposureMode(): ExposureMode

Obtains the exposure mode in use.

System capability: SystemCapability.Multimedia.Camera.Core

Return value

TypeDescription
ExposureModeExposure mode obtained. If the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400103Session not config.

Example

import { BusinessError } from '@ohos.base';

function getExposureMode(captureSession: camera.CaptureSession): camera.ExposureMode|undefined {
  let exposureMode: camera.ExposureMode|undefined = undefined;
  try {
    exposureMode = captureSession.getExposureMode();
  } catch (error) {
    // If the operation fails, error.code is returned and processed.
    let err = error as BusinessError;
    console.error(`The getExposureMode call failed. error code: ${err.code}`);
  }
  return exposureMode;
}

setExposureMode

setExposureMode(aeMode: ExposureMode): void

Sets an exposure mode for the device.

Before the setting, use isExposureModeSupported to check whether the target exposure mode is supported.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
aeModeExposureModeYesExposure mode.

Return value

TypeDescription
CameraErrorCodeIf the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400103Session not config.

Example

import { BusinessError } from '@ohos.base';

function setExposureMode(captureSession: camera.CaptureSession): void {
  try {
    captureSession.setExposureMode(camera.ExposureMode.EXPOSURE_MODE_LOCKED);
  } catch (error) {
    // If the operation fails, error.code is returned and processed.
    let err = error as BusinessError;
    console.error(`The setExposureMode call failed. error code: ${err.code}`);
  }
}

getMeteringPoint

getMeteringPoint(): Point

Obtains the metering point of the device.

System capability: SystemCapability.Multimedia.Camera.Core

Return value

TypeDescription
PointMetering point obtained. If the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400103Session not config.

Example

import { BusinessError } from '@ohos.base';

function getMeteringPoint(captureSession: camera.CaptureSession): camera.Point|undefined {
  let exposurePoint: camera.Point|undefined = undefined;
  try {
    exposurePoint = captureSession.getMeteringPoint();
  } catch (error) {
    // If the operation fails, error.code is returned and processed.
    let err = error as BusinessError;
    console.error(`The getMeteringPoint call failed. error code: ${err.code}`);
  }
  return exposurePoint;
}

setMeteringPoint

setMeteringPoint(point: Point): void

Sets the metering point, which is the center point of the metering rectangle. The metering point must be in the coordinate system (0-1), where the upper left corner is {0, 0} and the lower right corner is {1, 1}.

The coordinate system is based on the horizontal device direction with the device's charging port on the right. If the layout of the preview screen of an application is based on the vertical direction with the charging port on the lower side, the layout width and height are {w, h}, and the touch point is {x, y}, then the coordinate point after conversion is {y/h, 1-x/w}.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
exposurePointPointYesMetering point. The value range of x and y must be within [0,1]. If a value less than 0 is passed, the value 0 is used. If a value greater than 1 is passed, the value 1 is used.

Return value

TypeDescription
CameraErrorCodeIf the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400103Session not config.

Example

import { BusinessError } from '@ohos.base';

function setMeteringPoint(captureSession: camera.CaptureSession): void {
  const exposurePoint: camera.Point = {x: 1, y: 1};
  try {
    captureSession.setMeteringPoint(exposurePoint);
  } catch (error) {
    // If the operation fails, error.code is returned and processed.
    let err = error as BusinessError;
    console.error(`The setMeteringPoint call failed. error code: ${err.code}`);
  }
}

getExposureBiasRange

getExposureBiasRange(): Array<number>

Obtains the exposure compensation values of the device.

System capability: SystemCapability.Multimedia.Camera.Core

Return value

TypeDescription
Array<number>An array of compensation values. If the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400103Session not config.

Example

import { BusinessError } from '@ohos.base';

function getExposureBiasRange(captureSession: camera.CaptureSession): Array<number> {
  let biasRangeArray: Array<number> = [];
  try {
    biasRangeArray = captureSession.getExposureBiasRange();
  } catch (error) {
    // If the operation fails, error.code is returned and processed.
    let err = error as BusinessError;
    console.error(`The getExposureBiasRange call failed. error code: ${err.code}`);
  }
  return biasRangeArray;
}

setExposureBias

setExposureBias(exposureBias: number): void

Sets an exposure compensation value (EV).

Before the setting, you are advised to use getExposureBiasRange to obtain the supported values.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
exposureBiasnumberYesEV. The supported EV range can be obtained by calling getExposureBiasRange. If the value passed is not within the supported range, the nearest critical point is used. There is a step for EV. For example, if the step is 0.5 and this parameter is set to 1.2, the EV that takes effect is 1.0. If the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400103Session not config.

Example

import { BusinessError } from '@ohos.base';

function setExposureBias(captureSession: camera.CaptureSession, biasRangeArray: Array<number>): void {
  if (biasRangeArray && biasRangeArray.length > 0) {
    let exposureBias = biasRangeArray[0];
    try {
      captureSession.setExposureBias(exposureBias);
    } catch (error) {
      // If the operation fails, error.code is returned and processed.
      let err = error as BusinessError;
      console.error(`The setExposureBias call failed. error code: ${err.code}`);
    }
  }
}

getExposureValue

getExposureValue(): number

Obtains the exposure value in use.

System capability: SystemCapability.Multimedia.Camera.Core

Return value

TypeDescription
numberExposure value obtained. There is a step for EV. For example, if the step is 0.5 and this parameter is set to 1.2, the EV that takes effect is 1.0. If the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400103Session not config.

Example

import { BusinessError } from '@ohos.base';

function getExposureValue(captureSession: camera.CaptureSession): number {
  const invalid_value: number = -1;
  let exposureValue: number = invalid_value;
  try {
    exposureValue = captureSession.getExposureValue();
  } catch (error) {
    // If the operation fails, error.code is returned and processed.
    let err = error as BusinessError;
    console.error(`The getExposureValue call failed. error code: ${err.code}`);
  }
  return exposureValue;
}

isFocusModeSupported

isFocusModeSupported(afMode: FocusMode): boolean

Checks whether a focus mode is supported.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
afModeFocusModeYesFocus mode.

Return value

TypeDescription
booleanReturns true if the focus mode is supported; returns false otherwise. If the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400103Session not config.

Example

import { BusinessError } from '@ohos.base';

function isFocusModeSupported(captureSession: camera.CaptureSession): boolean {
  let status: boolean = false;
  try {
    status = captureSession.isFocusModeSupported(camera.FocusMode.FOCUS_MODE_AUTO);
  } catch (error) {
    // If the operation fails, error.code is returned and processed.
    let err = error as BusinessError;
    console.error(`The isFocusModeSupported call failed. error code: ${err.code}`);
  }
  return status;
}

setFocusMode

setFocusMode(afMode: FocusMode): void

Sets a focus mode for the device.

Before the setting, use isFocusModeSupported to check whether the focus mode is supported.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
afModeFocusModeYesFocus mode.

Return value

TypeDescription
CameraErrorCodeIf the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400103Session not config.

Example

import { BusinessError } from '@ohos.base';

function setFocusMode(captureSession: camera.CaptureSession): void {
  try {
    captureSession.setFocusMode(camera.FocusMode.FOCUS_MODE_AUTO);
  } catch (error) {
    // If the operation fails, error.code is returned and processed.
    let err = error as BusinessError;
    console.error(`The setFocusMode call failed. error code: ${err.code}`);
  }
}

getFocusMode

getFocusMode(): FocusMode

Obtains the focus mode in use.

System capability: SystemCapability.Multimedia.Camera.Core

Return value

TypeDescription
FocusModeFocus mode obtained. If the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400103Session not config.

Example

import { BusinessError } from '@ohos.base';

function getFocusMode(captureSession: camera.CaptureSession): camera.FocusMode|undefined {
  let afMode: camera.FocusMode|undefined = undefined;
  try {
    afMode = captureSession.getFocusMode();
  } catch (error) {
    // If the operation fails, error.code is returned and processed.
    let err = error as BusinessError;
    console.error(`The getFocusMode call failed. error code: ${err.code}`);
  }
  return afMode;
}

setFocusPoint

setFocusPoint(point: Point): void

Sets the focal point. The focal point must be in the coordinate system (0-1), where the upper left corner is {0, 0} and the lower right corner is {1, 1}.

The coordinate system is based on the horizontal device direction with the device's charging port on the right. If the layout of the preview screen of an application is based on the vertical direction with the charging port on the lower side, the layout width and height are {w, h}, and the touch point is {x, y}, then the coordinate point after conversion is {y/h, 1-x/w}.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
Point1PointYesFocal point. The value range of x and y must be within [0,1]. If a value less than 0 is passed, the value 0 is used. If a value greater than 1 is passed, the value 1 is used.

Return value

TypeDescription
CameraErrorCodeIf the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400103Session not config.

Example

import { BusinessError } from '@ohos.base';

function setFocusPoint(captureSession: camera.CaptureSession): void {
  const focusPoint: camera.Point = {x: 1, y: 1};
  try {
    captureSession.setFocusPoint(focusPoint);
  } catch (error) {
    // If the operation fails, error.code is returned and processed.
    let err = error as BusinessError;
    console.error(`The setFocusPoint call failed. error code: ${err.code}`);
  }
}

getFocusPoint

getFocusPoint(): Point

Obtains the focal point of the device.

System capability: SystemCapability.Multimedia.Camera.Core

Return value

TypeDescription
PointFocal point obtained. If the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400103Session not config.

Example

import { BusinessError } from '@ohos.base';

function getFocusPoint(captureSession: camera.CaptureSession): camera.Point|undefined {
  let point: camera.Point|undefined = undefined;
  try {
    point = captureSession.getFocusPoint();
  } catch (error) {
    // If the operation fails, error.code is returned and processed.
    let err = error as BusinessError;
    console.error(`The getFocusPoint call failed. error code: ${err.code}`);
  }
  return point;
}

getFocalLength

getFocalLength(): number

Obtains the focal length of the device.

System capability: SystemCapability.Multimedia.Camera.Core

Return value

TypeDescription
numberFocal length obtained. If the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400103Session not config.

Example

import { BusinessError } from '@ohos.base';

function getFocalLength(captureSession: camera.CaptureSession): number {
  const invalid_value: number = -1;
  let focalLength: number = invalid_value;
  try {
    focalLength = captureSession.getFocalLength();
  } catch (error) {
    // If the operation fails, error.code is returned and processed.
    let err = error as BusinessError;
    console.error(`The getFocalLength call failed. error code: ${err.code}`);
  }
  return focalLength;
}

getZoomRatioRange

getZoomRatioRange(): Array<number>

Obtains the supported zoom ratio range.

System capability: SystemCapability.Multimedia.Camera.Core

Return value

TypeDescription
Array<number>Callback used to return an array containing the minimum and maximum zoom ratios. If the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400103Session not config.

Example

import { BusinessError } from '@ohos.base';

function getZoomRatioRange(captureSession: camera.CaptureSession): Array<number> {
  let zoomRatioRange: Array<number> = [];
  try {
    zoomRatioRange = captureSession.getZoomRatioRange();
  } catch (error) {
    // If the operation fails, error.code is returned and processed.
    let err = error as BusinessError;
    console.error(`The getZoomRatioRange call failed. error code: ${err.code}`);
  }
  return zoomRatioRange;
}

setZoomRatio

setZoomRatio(zoomRatio: number): void

Sets a zoom ratio, with a maximum precision of two decimal places.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
zoomRationumberYesZoom ratio. The supported zoom ratio range can be obtained by calling getZoomRatioRange. If the value passed is not within the supported range, the nearest critical point is used.

Return value

TypeDescription
CameraErrorCodeIf the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400103Session not config.

Example

import { BusinessError } from '@ohos.base';

function setZoomRatio(captureSession: camera.CaptureSession, zoomRatioRange: Array<number>): void {
  if (zoomRatioRange === undefined||zoomRatioRange.length <= 0) {
    return;
  }
  let zoomRatio = zoomRatioRange[0];
  try {
    captureSession.setZoomRatio(zoomRatio);
  } catch (error) {
    // If the operation fails, error.code is returned and processed.
    let err = error as BusinessError;
    console.error(`The setZoomRatio call failed. error code: ${err.code}`);
  }
}

getZoomRatio

getZoomRatio(): number

Obtains the zoom ratio in use.

System capability: SystemCapability.Multimedia.Camera.Core

Return value

TypeDescription
numberZoom ratio obtained. If the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400103Session not config.

Example

import { BusinessError } from '@ohos.base';

function getZoomRatio(captureSession: camera.CaptureSession): number {
  const invalid_value: number = -1;
  let zoomRatio: number = invalid_value;
  try {
    zoomRatio = captureSession.getZoomRatio();
  } catch (error) {
    // If the operation fails, error.code is returned and processed.
    let err = error as BusinessError;
    console.error(`The getZoomRatio call failed. error code: ${err.code}`);
  }
  return zoomRatio;
}

isVideoStabilizationModeSupported

isVideoStabilizationModeSupported(vsMode: VideoStabilizationMode): boolean

Checks whether the specified video stabilization mode is supported.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
vsModeVideoStabilizationModeYesVideo stabilization mode.

Return value

TypeDescription
booleanReturns true if the video stabilization mode is supported; returns false otherwise. If the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400103Session not config.

Example

import { BusinessError } from '@ohos.base';

function isVideoStabilizationModeSupported(captureSession: camera.CaptureSession): boolean {
  let isSupported: boolean = false;
  try {
    isSupported = captureSession.isVideoStabilizationModeSupported(camera.VideoStabilizationMode.OFF);
  } catch (error) {
    // If the operation fails, error.code is returned and processed.
    let err = error as BusinessError;
    console.error(`The isVideoStabilizationModeSupported call failed. error code: ${err.code}`);
  }
  return isSupported;
}

getActiveVideoStabilizationMode

getActiveVideoStabilizationMode(): VideoStabilizationMode

Obtains the video stabilization mode in use.

System capability: SystemCapability.Multimedia.Camera.Core

Return value

TypeDescription
VideoStabilizationModeVideo stabilization mode obtained. If the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400103Session not config.

Example

import { BusinessError } from '@ohos.base';

function getActiveVideoStabilizationMode(captureSession: camera.CaptureSession): camera.VideoStabilizationMode|undefined {
  let vsMode: camera.VideoStabilizationMode|undefined = undefined;
  try {
    vsMode = captureSession.getActiveVideoStabilizationMode();
  } catch (error) {
    // If the operation fails, error.code is returned and processed.
    let err = error as BusinessError;
    console.error(`The getActiveVideoStabilizationMode call failed. error code: ${err.code}`);
  }
  return vsMode;
}

setVideoStabilizationMode

setVideoStabilizationMode(mode: VideoStabilizationMode): void

Sets a video stabilization mode for the device.

Before the setting, use isVideoStabilizationModeSupported to check whether the target video stabilization mode is supported.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
modeVideoStabilizationModeYesVideo stabilization mode.

Return value

TypeDescription
CameraErrorCodeIf the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400103Session not config.

Example

import { BusinessError } from '@ohos.base';

function setVideoStabilizationMode(captureSession: camera.CaptureSession): void {
  try {
    captureSession.setVideoStabilizationMode(camera.VideoStabilizationMode.OFF);
  } catch (error) {
    // If the operation fails, error.code is returned and processed.
    let err = error as BusinessError;
    console.error(`The setVideoStabilizationMode call failed. error code: ${err.code}`);
  }
}

getSupportedFilters

getSupportedFilters(): Array<number>

Obtains the supported filter types.

System API: This is a system API.

System capability: SystemCapability.Multimedia.Camera.Core

Return value

TypeDescription
Array<FilterType>Array of the supported filter types.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400103Session not config.

Example

function getSupportedFilters(portraitSession: camera.PortraitSession): Array<camera.FilterType> {
  let filterTypes: Array<camera.FilterType> = portraitSession.getSupportedFilters();
  return filterTypes;
}

setFilter

setFilter(filter: number): void

Sets a filter type.

Before the setting, use getSupportedFilters to obtain the supported filter types and check whether the target filter type is supported.

System API: This is a system API.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
filternumberYesFilter type to set.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400101Parameter missing or parameter type incorrect.
7400103Session not config.

Example

function setFilter(portraitSession: camera.PortraitSession, filterTypes: Array<camera.FilterType>): void {
  if (filterTypes === undefined||filterTypes.length <= 0) {
    return;
  }
  portraitSession.setFilter(filterTypes[0]);
}

getFilter

getFilter(): number

Obtains the filter type in use.

System API: This is a system API.

System capability: SystemCapability.Multimedia.Camera.Core

Return value

TypeDescription
numberFilter type in use. For details, see FilterType.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400103Session not config.

Example

function getFilter(portraitSession: camera.PortraitSession): number {
  let filterType: number = portraitSession.getFilter();
  return filterType;
}

getSupportedBeautyTypes

getSupportedBeautyTypes(): Array<BeautyType>

Obtains the supported beauty types.

System API: This is a system API.

System capability: SystemCapability.Multimedia.Camera.Core

Return value

TypeDescription
Array<BeautyType>Array of the supported beauty types.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400103Session not config.

Example

function getSupportedBeautyTypes(portraitSession: camera.PortraitSession): Array<camera.BeautyType> {
  let beautyTypes: Array<camera.BeautyType> = portraitSession.getSupportedBeautyTypes();
  return beautyTypes;
}

getSupportedBeautyRange

getSupportedBeautyRange(type: BeautyType): Array<number>

Obtains the levels that can be set a beauty type. The beauty levels vary according to the device type. The following table is only an example.

Input ParameterExample Return ValueReturn Value Description
AUTO[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]Beauty levels supported when type is set to AUTO. The value 0 means that beauty mode is disabled, and other positive values mean the corresponding automatic beauty levels.
SKIN_SMOOTH[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]Beauty levels supported when type is set to SKIN_SMOOTH. The value 0 means that the skin smoothing feature is disabled, and other positive values mean the corresponding skin smoothing levels.
FACE_SLENDER[0, 1, 2, 3, 4, 5]Beauty levels supported when type is set to FACE_SLENDER. The value 0 means that the face slimming feature is disabled, and other positive values mean the corresponding face slimming levels.
SKIN_TONE[-1, 16242611]Beauty levels supported when type is set to SKIN_TONE. The value -1 means that the skin tone perfection feature is disabled. Other non-negative values mean the skin tone perfection levels represented by RGB,
for example, 16242611, which is 0xF7D7B3 in hexadecimal format, where F7, D7, and B3 represent the values of the R channel, G channel, and B channel, respectively.

System API: This is a system API.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
typeBeautyTypeYesBeauty type.

Return value

TypeDescription
Array<number>Array of levels supported.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400103Session not config.

Example

function getSupportedBeautyRange(portraitSession: camera.PortraitSession): Array<number> {
  let beautyTypes: Array<camera.BeautyType> = portraitSession.getSupportedBeautyTypes();
  if (beautyTypes === undefined||beautyTypes.length <= 0) {
    return [];
  }
  let beautyLevels: Array<number> = portraitSession.getSupportedBeautyRange(beautyTypes[0]);
  return beautyLevels;
}

setBeauty

setBeauty(type: BeautyType, value: number): void

Sets a beauty type and its level. Beauty mode is turned off only when all the beauty types obtained through getSupportedBeautyTypes are disabled.

System API: This is a system API.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
typeBeautyTypeYesBeauty type.
valuenumberYesBeauty level, which can be obtained through getSupportedBeautyRange.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400103Session not config.

Example

function setBeauty(portraitSession: camera.PortraitSession): void {
  let beautyTypes: Array<camera.BeautyType> = portraitSession.getSupportedBeautyTypes();
  if (beautyTypes === undefined||beautyTypes.length <= 0) {
    return;
  }
  let beautyLevels: Array<number> = portraitSession.getSupportedBeautyRange(beautyTypes[0]);
  if (beautyLevels === undefined||beautyLevels.length <= 0) {
    return;
  }
  portraitSession.setBeauty(beautyTypes[0], beautyLevels[0]);
}

getBeauty

getBeauty(type: BeautyType): number

Obtains the level of the beauty type in use.

System API: This is a system API.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
typeBeautyTypeYesBeauty type.

Return value

NameTypeMandatoryDescription
valuenumberYesBeauty level.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400103Session not config.

Example

function getBeauty(portraitSession: camera.PortraitSession): number {
  const invalid_value: number = -1;
  let beautyTypes = portraitSession.getSupportedBeautyTypes();
  if (beautyTypes === undefined||beautyTypes.length <= 0) {
    return invalid_value;
  }
  let beautyLevels: Array<number> = portraitSession.getSupportedBeautyRange(beautyTypes[0]);
  if (beautyLevels === undefined||beautyLevels.length <= 0) {
    return invalid_value;
  }
  portraitSession.setBeauty(beautyTypes[0], beautyLevels[0]);
  let beautyLevel: number = portraitSession.getBeauty(beautyTypes[0]);
  return beautyLevel;
}

on('focusStateChange')

on(type: 'focusStateChange', callback: AsyncCallback<FocusState>): void

Subscribes to focus state events. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value is fixed at 'focusStateChange'. The event can be listened for when a session is created. This event is triggered only when the camera focus state changes in auto focus mode.
callbackAsyncCallback<FocusState>YesCallback used to return the focus state change.

Example

import { BusinessError } from '@ohos.base';

function registerFocusStateChange(captureSession: camera.CaptureSession): void {
  captureSession.on('focusStateChange', (err: BusinessError, focusState: camera.FocusState) => {
    console.log(`Focus state: ${focusState}`);
  });
}

off('focusStateChange')

off(type: 'focusStateChange', callback?: AsyncCallback<FocusState>): void

Unsubscribes from focus state events.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value is fixed at 'focusStateChange'. The event can be listened for when a session is created.
callbackAsyncCallback<FocusState>NoCallback used for unsubscription.

Example

function unregisterFocusStateChange(captureSession: camera.CaptureSession): void {
  captureSession.off('focusStateChange');
}

on('error')

on(type: 'error', callback: ErrorCallback): void

Subscribes to CaptureSession error events. This API uses a callback to return the result.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value is fixed at 'error'. The event can be listened for when a session is created. This event is triggered and the error message is returned when an error occurs during the calling of a session-related API such as beginConfig(), commitConfig(), and addInput.
callbackErrorCallbackYesCallback used to return an error code defined in CameraErrorCode.

Example

import { BusinessError } from '@ohos.base';

function registerCaptureSessionError(captureSession: camera.CaptureSession): void {
  captureSession.on('error', (error: BusinessError) => {
    console.log(`Capture session error code: ${error.code}`);
  });
}

off('error')

off(type: 'error', callback?: ErrorCallback): void

Unsubscribes from CaptureSession error events. This API uses a callback to return the result.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value is fixed at 'error'. The event can be listened for when a session is created.
callbackErrorCallbackNoCallback used for unsubscription.

Example

function unregisterCaptureSessionError(captureSession: camera.CaptureSession): void {
  captureSession.off('error');
}

PortraitSession

This class inherits from CaptureSession. It is used to set parameters of portrait mode.

getSupportedPortraitEffects

getSupportedPortraitEffects(): Array<PortraitEffect>

Obtains the supported portrait effects.

System API: This is a system API.

System capability: SystemCapability.Multimedia.Camera.Core

Return value

TypeDescription
Array<PortraitEffect >Array of the supported portrait effects.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400103Session not config.

Example

function getSupportedPortraitEffects(portraitSession: camera.PortraitSession): Array<camera.PortraitEffect> {
  let portraitEffects: Array<camera.PortraitEffect> = portraitSession.getSupportedPortraitEffects();
  return portraitEffects;
}

setPortraitEffect

setPortraitEffect(effect: PortraitEffect): void

Sets a portrait effect.

Before the setting, use getSupportedPortraitEffects to obtain the supported portrait effects and check whether the target portrait effect is supported.

System API: This is a system API.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
effectPortraitEffectYesPortrait effect, which can be obtained through getSupportedPortraitEffects.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400103Session not config.

Example

import { BusinessError } from '@ohos.base';

function setPortraitEffect(portraitSession: camera.PortraitSession, portraitEffects: Array<camera.PortraitEffect>): void {
  if (portraitEffects === undefined||portraitEffects.length <= 0) {
    return;
  }
  try {
    portraitSession.setPortraitEffect(portraitEffects[0]);
  } catch (error) {
    let err = error as BusinessError;
    console.error(`The setPortraitEffect call failed. error code: ${err.code}`);
  }
}

getPortraitEffect

getPortraitEffect(): PortraitEffect

Obtains the portrait effect in use.

System API: This is a system API.

System capability: SystemCapability.Multimedia.Camera.Core

Return value

TypeDescription
PortraitEffectPortrait effect in use.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400103Session not config.

Example

function getSupportedPortraitEffects(portraitSession: camera.PortraitSession): Array<camera.PortraitEffect> {
  let portraitEffects: Array<camera.PortraitEffect> = portraitSession.getSupportedPortraitEffects();
  return portraitEffects;
}

CameraOutput

Implements output information used in a CaptureSession. It is the base class of output.

PreviewOutput

Implements preview output. It inherits CameraOutput.

start

start(callback: AsyncCallback<void>): void

Starts to output preview streams. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<void>YesCallback used to return the result. If the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400103Session not config.

Example

import { BusinessError } from '@ohos.base';

function startPreviewOutput(previewOutput: camera.PreviewOutput): void {
  previewOutput.start((err: BusinessError) => {
    if (err) {
      console.error(`Failed to start the previewOutput. ${err.code}`);
      return;
    }
    console.log('Callback returned with previewOutput started.');
  });
}

start

start(): Promise<void>

Starts to output preview streams. This API uses a promise to return the result.

System capability: SystemCapability.Multimedia.Camera.Core

Return value

TypeDescription
Promise<void>Promise used to return the result. If the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400103Session not config.

Example

import { BusinessError } from '@ohos.base';

function startPreviewOutput(previewOutput: camera.PreviewOutput): void {
  previewOutput.start().then(() => {
    console.log('Promise returned with previewOutput started.');
  }).catch((err: BusinessError) => {
    console.log('Failed to previewOutput start '+ err.code);
  });
}

stop

stop(callback: AsyncCallback<void>): void

Stops outputting preview streams. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<void>YesCallback used to return the result.

Example

import { BusinessError } from '@ohos.base';

function stopPreviewOutput(previewOutput: camera.PreviewOutput): void {
  previewOutput.stop((err: BusinessError) => {
    if (err) {
      console.error(`Failed to stop the previewOutput. ${err.code}`);
      return;
    }
    console.log('Callback returned with previewOutput stopped.');
  })
}

stop

stop(): Promise<void>

Stops outputting preview streams. This API uses a promise to return the result.

System capability: SystemCapability.Multimedia.Camera.Core

Return value

TypeDescription
Promise<void>Promise used to return the result.

Example

import { BusinessError } from '@ohos.base';

function stopPreviewOutput(previewOutput: camera.PreviewOutput): void {
  previewOutput.stop().then(() => {
    console.log('Callback returned with previewOutput stopped.');
  }).catch((err: BusinessError) => {
    console.log('Failed to previewOutput stop '+ err.code);
  });
}

release

release(callback: AsyncCallback<void>): void

Releases output resources. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<void>YesCallback used to return the result. If the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400201Camera service fatal error.

Example

import { BusinessError } from '@ohos.base';

function releasePreviewOutput(previewOutput: camera.PreviewOutput): void {
  previewOutput.release((err: BusinessError) => {
    if (err) {
      console.error(`Failed to release the PreviewOutput instance ${err.code}`);
      return;
    }
    console.log('Callback invoked to indicate that the PreviewOutput instance is released successfully.');
  });
}

release

release(): Promise<void>

Releases output resources. This API uses a promise to return the result.

System capability: SystemCapability.Multimedia.Camera.Core

Return value

TypeDescription
Promise<void>Promise used to return the result. If the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400201Camera service fatal error.

Example

import { BusinessError } from '@ohos.base';

function releasePreviewOutput(previewOutput: camera.PreviewOutput): void {
  previewOutput.release().then(() => {
    console.log('Promise returned to indicate that the PreviewOutput instance is released successfully.');
  }).catch((err: BusinessError) => {
    console.log('Failed to previewOutput release '+ err.code);
  });
}

on('frameStart')

on(type: 'frameStart', callback: AsyncCallback<void>): void

Subscribes to preview frame start events. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value is fixed at 'frameStart'. The event can be listened for when a previewOutput instance is created. This event is triggered and returned when the bottom layer starts exposure for the first time.
callbackAsyncCallback<void>YesCallback used to return the result. The preview starts as long as this event is returned.

Example

function registerPreviewOutputFrameStart(previewOutput: camera.PreviewOutput): void {
  previewOutput.on('frameStart', () => {
    console.log('Preview frame started');
  });
}

off('frameStart')

off(type: 'frameStart', callback?: AsyncCallback<void>): void

Unsubscribes from preview frame start events.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value is fixed at 'frameStart'. The event can be listened for when a previewOutput instance is created.
callbackAsyncCallback<void>NoCallback used for unsubscription.

Example

function unregisterPreviewOutputFrameStart(previewOutput: camera.PreviewOutput): void {
  previewOutput.off('frameStart');
}

on('frameEnd')

on(type: 'frameEnd', callback: AsyncCallback<void>): void

Subscribes to preview frame end events. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value is fixed at 'frameEnd'. The event can be listened for when a previewOutput instance is created. This event is triggered and returned when the last frame of preview ends.
callbackAsyncCallback<void>YesCallback used to return the result. The preview ends as long as this event is returned.

Example

function registerPreviewOutputFrameEnd(previewOutput: camera.PreviewOutput): void {
  previewOutput.on('frameEnd', () => {
    console.log('Preview frame ended');
  });
}

off('frameEnd')

off(type: 'frameEnd', callback?: AsyncCallback<void>): void

Unsubscribes from preview frame end events.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value is fixed at 'frameEnd'. The event can be listened for when a previewOutput instance is created.
callbackAsyncCallback<void>NoCallback used for unsubscription.

Example

function unregisterPreviewOutputFrameEnd(previewOutput: camera.PreviewOutput): void {
  previewOutput.off('frameEnd');
}

on('error')

on(type: 'error', callback: ErrorCallback): void

Subscribes to PreviewOutput error events. This API uses a callback to return the result.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value is fixed at 'error'. The event can be listened for when a previewOutput instance is created. This event is triggered and the corresponding error message is returned when an error occurs during the use of a preview-related API such as start() or release().
callbackErrorCallbackYesCallback used to return an error code defined in CameraErrorCode.

Example

import { BusinessError } from '@ohos.base';

function registerPreviewOutputError(previewOutput: camera.PreviewOutput): void {
  previewOutput.on('error', (previewOutputError: BusinessError) => {
    console.log(`Preview output error code: ${previewOutputError.code}`);
  })
}

off('error')

off(type: 'error', callback?: ErrorCallback): void

Unsubscribes from PreviewOutput error events.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value is fixed at 'error'. The event can be listened for when a previewOutput instance is created.
callbackErrorCallbackNoCallback used for unsubscription.

Example

function unregisterPreviewOutputError(previewOutput: camera.PreviewOutput): void {
  previewOutput.off('error');
}

addDeferredSurface

addDeferredSurface(surfaceId: string): void

Adds a surface for delayed preview. This API can run after session.commitConfig() or session.start() is called.

System API: This is a system API.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
surfaceIdstringYesSurface ID, which is obtained from XComponent.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400101Parameter missing or parameter type incorrect

Example

import common from '@ohos.app.ability.common';

async function preview(context: common.BaseContext, cameraInfo: camera.CameraDevice, previewProfile: camera.Profile, photoProfile: camera.Profile, photoSurfaceId: string, previewSurfaceId: string): Promise<void> {
  const cameraManager: camera.CameraManager = camera.getCameraManager(context);
  const cameraInput: camera.CameraInput = cameraManager.createCameraInput(cameraInfo)
  const previewOutput: camera.PreviewOutput = cameraManager.createDeferredPreviewOutput(previewProfile);
  const photoOutput: camera.PhotoOutput = cameraManager.createPhotoOutput(photoProfile, photoSurfaceId);
  const session: camera.CaptureSession  = cameraManager.createCaptureSession();
  session.beginConfig();
  session.addInput(cameraInput);
  session.addOutput(previewOutput);
  session.addOutput(photoOutput);
  await session.commitConfig();
  await session.start();
  previewOutput.addDeferredSurface(previewSurfaceId);
}

ImageRotation

Enumerates the image rotation angles.

System capability: SystemCapability.Multimedia.Camera.Core

NameValueDescription
ROTATION_00The image rotates 0 degrees.
ROTATION_9090The image rotates 90 degrees.
ROTATION_180180The image rotates 180 degrees.
ROTATION_270270The image rotates 270 degrees.

Location

Defines geolocation information.

System capability: SystemCapability.Multimedia.Camera.Core

NameTypeMandatoryDescription
latitudenumberYesLatitude, in degrees.
longitudenumberYesLongitude, in degrees.
altitudenumberYesAltitude, in meters.

QualityLevel

Enumerates the image quality levels.

System capability: SystemCapability.Multimedia.Camera.Core

NameValueDescription
QUALITY_LEVEL_HIGH0High image quality.
QUALITY_LEVEL_MEDIUM1Medium image quality.
QUALITY_LEVEL_LOW2Low image quality.

PhotoCaptureSetting

Defines the settings for photo capture.

System capability: SystemCapability.Multimedia.Camera.Core

NameTypeMandatoryDescription
qualityQualityLevelNoPhoto quality.
rotationImageRotationNoRotation angle of the photo.
locationLocationNoGeolocation information of the photo.
mirrorbooleanNoWhether mirroring is enabled. By default, mirroring is disabled.

PhotoOutput

Implements output information used in a shooting session. This class inherits from CameraOutput.

capture

capture(callback: AsyncCallback<void>): void

Captures a photo with the default shooting parameters. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<void>YesCallback used to return the result. If the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400104Session not running.
7400201Camera service fatal error.

Example

import { BusinessError } from '@ohos.base';

function capture(photoOutput: camera.PhotoOutput): void {
  photoOutput.capture((err: BusinessError) => {
    if (err) {
      console.error(`Failed to capture the photo ${err.code}`);
      return;
    }
    console.log('Callback invoked to indicate the photo capture request success.');
  });
}

capture

capture(): Promise<void>

Captures a photo with the default shooting parameters. This API uses a promise to return the result.

System capability: SystemCapability.Multimedia.Camera.Core

Return value

TypeDescription
Promise<void>Promise used to return the result. If the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400104Session not running.
7400201Camera service fatal error.

Example

import { BusinessError } from '@ohos.base';

function capture(photoOutput: camera.PhotoOutput): void {
  photoOutput.capture().then(() => {
    console.log('Promise returned to indicate that photo capture request success.');
  }).catch((err: BusinessError) => {
    console.log('Failed to photoOutput capture '+ err.code);
  });
}

capture

capture(setting: PhotoCaptureSetting, callback: AsyncCallback<void>): void

Captures a photo with the specified shooting parameters. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
settingPhotoCaptureSettingYesShooting settings.
callbackAsyncCallback<void>YesCallback used to return the result. If the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400101Parameter missing or parameter type incorrect
7400104Session not running.
7400201Camera service fatal error.

Example

import { BusinessError } from '@ohos.base';

function capture(photoOutput: camera.PhotoOutput): void {
  let captureLocation: camera.Location = {
    latitude: 0,
    longitude: 0,
    altitude: 0,
  }
  let settings: camera.PhotoCaptureSetting = {
    quality: camera.QualityLevel.QUALITY_LEVEL_LOW,
    rotation: camera.ImageRotation.ROTATION_0,
    location: captureLocation,
    mirror: false
  }
  photoOutput.capture(settings, (err: BusinessError) => {
    if (err) {
      console.error(`Failed to capture the photo ${err.code}`);
      return;
    }
    console.log('Callback invoked to indicate the photo capture request success.');
  });
}

capture

capture(setting?: PhotoCaptureSetting): Promise<void>

Captures a photo with the specified shooting parameters. This API uses a promise to return the result.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
settingPhotoCaptureSettingNoShooting parameters. The input of undefined is processed as if no parameters were passed.

Return value

TypeDescription
Promise<void>Promise used to return the result. If the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400101Parameter missing or parameter type incorrect
7400104Session not running.
7400201Camera service fatal error.

Example

import { BusinessError } from '@ohos.base';

function capture(photoOutput: camera.PhotoOutput): void {
  let captureLocation: camera.Location = {
    latitude: 0,
    longitude: 0,
    altitude: 0,
  }
  let settings: camera.PhotoCaptureSetting = {
    quality: camera.QualityLevel.QUALITY_LEVEL_LOW,
    rotation: camera.ImageRotation.ROTATION_0,
    location: captureLocation,
    mirror: false
  }
  photoOutput.capture(settings).then(() => {
    console.log('Promise returned to indicate that photo capture request success.');
  }).catch((err: BusinessError) => {
    console.log('Failed to photoOutput capture '+ err.code);
  });
}

isMirrorSupported

isMirrorSupported(): boolean

Checks whether mirroring is supported.

System capability: SystemCapability.Multimedia.Camera.Core

Return value

TypeDescription
booleanReturns true if mirroring is supported; returns false otherwise.

Example

function isMirrorSupported(photoOutput: camera.PhotoOutput): boolean {
  let isSupported: boolean = photoOutput.isMirrorSupported();
  return isSupported;
}

release

release(callback: AsyncCallback<void>): void

Releases output resources. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<void>YesCallback used to return the result. If the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400201Camera service fatal error.

Example

import { BusinessError } from '@ohos.base';

function releasePhotoOutput(photoOutput: camera.PhotoOutput): void {
  photoOutput.release((err: BusinessError) => {
    if (err) {
      console.error(`Failed to release the PreviewOutput instance ${err.code}`);
      return;
    }
    console.log('Callback invoked to indicate that the PreviewOutput instance is released successfully.');
  });
}

release

release(): Promise<void>

Releases output resources. This API uses a promise to return the result.

System capability: SystemCapability.Multimedia.Camera.Core

Return value

TypeDescription
Promise<void>Promise used to return the result. If the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400201Camera service fatal error.

Example

import { BusinessError } from '@ohos.base';

function releasePhotoOutput(photoOutput: camera.PhotoOutput): void {
  photoOutput.release().then(() => {
    console.log('Promise returned to indicate that the PreviewOutput instance is released successfully.');
  }).catch((err: BusinessError) => {
    console.log('Failed to photoOutput release '+ err.code);
  });
}

on('captureStart')

on(type: 'captureStart', callback: AsyncCallback<number>): void

Subscribes to shooting start events. This API uses an asynchronous callback to return the capture ID.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value is fixed at 'captureStart'. The event can be listened for when a photoOutput instance is created. This event is triggered and returned when the bottom layer starts exposure each time a photo is taken.
callbackAsyncCallback<number>YesCallback used to return the capture ID.

Example

import { BusinessError } from '@ohos.base';

function registerPhotoOutputCaptureStart(photoOutput: camera.PhotoOutput): void {
  photoOutput.on('captureStart', (err: BusinessError, captureId: number) => {
    console.log(`photo capture stated, captureId : ${captureId}`);
  });
}

off('captureStart')

off(type: 'captureStart', callback?: AsyncCallback<number>): void

Unsubscribes from shooting start events.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value is fixed at 'captureStart'. The event can be listened for when a photoOutput instance is created.
callbackAsyncCallback<number>NoCallback used for unsubscription.

Example

function unregisterPhotoOutputCaptureStart(photoOutput: camera.PhotoOutput): void {
  photoOutput.off('captureStart');
}

on('frameShutter')

on(type: 'frameShutter', callback: AsyncCallback<FrameShutterInfo>): void

Subscribes to frame shutter events. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value is fixed at 'frameShutter'. The event can be listened for when a photoOutput instance is created.
callbackAsyncCallback<FrameShutterInfo>YesCallback used to return the result. A new photographing request can be delivered as long as this event is returned.

Example

import { BusinessError } from '@ohos.base';

function registerPhotoOutputFrameShutter(photoOutput: camera.PhotoOutput): void {
  photoOutput.on('frameShutter', (err: BusinessError, frameShutterInfo: camera.FrameShutterInfo) => {
    console.log(`photo capture end, captureId : ${frameShutterInfo.captureId}`);
    console.log(`Timestamp for frame : ${frameShutterInfo.timestamp}`);
  });
}

off('frameShutter')

off(type: 'frameShutter', callback?: AsyncCallback<FrameShutterInfo>): void

Unsubscribes from frame shutter events.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value is fixed at 'frameShutter'. The event can be listened for when a photoOutput instance is created.
callbackAsyncCallback<FrameShutterInfo>NoCallback used for unsubscription.

Example

function unregisterPhotoOutputFrameShutter(photoOutput: camera.PhotoOutput): void {
  photoOutput.off('frameShutter');
}

on('captureEnd')

on(type: 'captureEnd', callback: AsyncCallback<CaptureEndInfo>): void

Subscribes to shooting end events. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value is fixed at 'captureEnd'. The event can be listened for when a photoOutput instance is created. This event is triggered and the corresponding information is returned when the photographing is complete.
callbackAsyncCallback<CaptureEndInfo>YesCallback used to return the result.

Example

import { BusinessError } from '@ohos.base';

function registerPhotoOutputCaptureEnd(photoOutput: camera.PhotoOutput): void {
  photoOutput.on('captureEnd', (err: BusinessError, captureEndInfo: camera.CaptureEndInfo) => {
    console.log(`photo capture end, captureId : ${captureEndInfo.captureId}`);
    console.log(`frameCount : ${captureEndInfo.frameCount}`);
  });
}

off('captureEnd')

off(type: 'captureEnd', callback?: AsyncCallback<CaptureEndInfo>): void

Unsubscribes from shooting end events.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value is fixed at 'captureEnd'. The event can be listened for when a photoOutput instance is created.
callbackAsyncCallback<CaptureEndInfo>NoCallback used for unsubscription.

Example

function unregisterPhotoOutputCaptureEnd(photoOutput: camera.PhotoOutput): void {
  photoOutput.off('captureEnd');
}

on('error')

on(type: 'error', callback: ErrorCallback): void

Subscribes to PhotoOutput error events. This API uses a callback to return the result.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value is fixed at 'error'. The event can be listened for when a photoOutput instance is created. This event is triggered and the corresponding error message is returned when an error occurs during the calling of a photographing-related API.
callbackErrorCallbackYesCallback used to return an error code defined in CameraErrorCode.

Example

import { BusinessError } from '@ohos.base';

function registerPhotoOutputError(photoOutput: camera.PhotoOutput): void {
  photoOutput.on('error', (error: BusinessError) => {
    console.log(`Photo output error code: ${error.code}`);
  });
}

off('error')

off(type: 'error', callback?: ErrorCallback): void

Unsubscribes from PhotoOutput error events.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value is fixed at 'error'. The event can be listened for when a photoOutput instance is created.
callbackErrorCallbackNoCallback used for unsubscription.

Example

function unregisterPhotoOutputError(photoOutput: camera.PhotoOutput): void {
  photoOutput.off('error');
}

isQuickThumbnailSupported

isQuickThumbnailSupported(): boolean

Checks whether the quick thumbnail feature is supported.

This API takes effect after CaptureSession.addOutput and CaptureSession.addInput and before CaptureSession.commitConfig.

System API: This is a system API.

System capability: SystemCapability.Multimedia.Camera.Core

Return value

TypeDescription
booleanReturns whether the quick thumbnail feature is supported. The value true means that the quick thumbnail feature is supported, and false means the opposite.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400104session is not running

Example

import common from '@ohos.app.ability.common';

async function isQuickThumbnailSupported(context: common.BaseContext, photoProfile: camera.Profile, surfaceId: string): Promise<boolean> {
  let cameraManager: camera.CameraManager = camera.getCameraManager(context);
  let cameras: Array<camera.CameraDevice> = cameraManager.getSupportedCameras();
  // Create a CaptureSession instance.
  let captureSession: camera.CaptureSession = cameraManager.createCaptureSession();
  // Start configuration for the session.
  captureSession.beginConfig();
  // Add a CameraInput instance to the session.
  let cameraInput: camera.CameraInput = cameraManager.createCameraInput(cameras[0]);
  await cameraInput.open();
  captureSession.addInput(cameraInput);
  // Add the photoOutput object to the session.
  let photoOutput: camera.PhotoOutput = cameraManager.createPhotoOutput(photoProfile, surfaceId);
  captureSession.addOutput(photoOutput);
  let isSupported: boolean = photoOutput.isQuickThumbnailSupported();
  return isSupported;
}

enableQuickThumbnail

enableQuickThumbnail(enabled: boolean): void

Enables or disables the quick thumbnail feature.

This API takes effect after CaptureSession.addOutput and CaptureSession.addInput and before CaptureSession.commitConfig.

System API: This is a system API.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
enabledbooleanYesWhether to enable the quick thumbnail feature. The value true means to enable the quick thumbnail feature, and false means the opposite.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400104session is not running

Example

import common from '@ohos.app.ability.common';
import { BusinessError } from '@ohos.base';

async function enableQuickThumbnail(context: common.BaseContext, photoProfile: camera.Profile, surfaceId: string): Promise<void> {
  let cameraManager: camera.CameraManager = camera.getCameraManager(context);
  let cameras: Array<camera.CameraDevice> = cameraManager.getSupportedCameras();
  // Create a CaptureSession instance.
  let captureSession: camera.CaptureSession = cameraManager.createCaptureSession();
  // Start configuration for the session.
  captureSession.beginConfig();
  // Add a CameraInput instance to the session.
  let cameraInput: camera.CameraInput = cameraManager.createCameraInput(cameras[0]);
  await cameraInput.open();
  captureSession.addInput(cameraInput);
  // Add a PhotoOutput instance to the session.
  let photoOutput: camera.PhotoOutput = cameraManager.createPhotoOutput(photoProfile, surfaceId);
  captureSession.addOutput(photoOutput);
  let isSupported: boolean = photoOutput.isQuickThumbnailSupported();
  if (!isSupported) {
    console.log('Quick Thumbnail is not supported to be turned on.');
    return;
  }
  try {
    photoOutput.enableQuickThumbnail(true);
  } catch (error) {
    let err = error as BusinessError;
    console.error(`The enableQuickThumbnail call failed. error code: ${err.code}`);
  }
}

on('quickThumbnail')

on(type: 'quickThumbnail', callback: AsyncCallback<image.PixelMap>): void

Subscribes to quick thumbnail output events.

The listening takes effect after enableQuickThumbnail(true) is called.

System API: This is a system API.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value is fixed at 'quickThumbnail'.
callbackAsyncCallback<image.PixelMap>YesPromise that returns a PixelMap instance.

Example

import common from '@ohos.app.ability.common';
import { BusinessError } from '@ohos.base';

async function registerQuickThumbnail(context: common.BaseContext, photoProfile: camera.Profile, surfaceId: string): Promise<void> {
  let cameraManager: camera.CameraManager = camera.getCameraManager(context);
  let cameras: Array<camera.CameraDevice> = cameraManager.getSupportedCameras();
  // Create a CaptureSession instance.
  let captureSession: camera.CaptureSession = cameraManager.createCaptureSession();
  // Start configuration for the session.
  captureSession.beginConfig();
  // Add a CameraInput instance to the session.
  let cameraInput: camera.CameraInput = cameraManager.createCameraInput(cameras[0]);
  await cameraInput.open();
  captureSession.addInput(cameraInput);
  // Add a PhotoOutput instance to the session.
  let photoOutput: camera.PhotoOutput = cameraManager.createPhotoOutput(photoProfile, surfaceId);
  captureSession.addOutput(photoOutput);
  let isSupported: boolean = photoOutput.isQuickThumbnailSupported();
  if (!isSupported) {
    console.log('Quick Thumbnail is not supported to be turned on.');
    return;
  }
  try {
    photoOutput.enableQuickThumbnail(true);
  } catch (error) {
    let err = error as BusinessError;
    console.error(`The enableQuickThumbnail call failed. error code: ${err.code}`);
  }

  photoOutput.on('quickThumbnail', (err: BusinessError, pixelMap: image.PixelMap) => {
    if (err||pixelMap === undefined) {
      console.error('photoOutput on thumbnail failed');
      return;
    }
    // Display or save the PixelMap instance.
    // do something
  })
}

off('quickThumbnail')

off(type: 'quickThumbnail', callback?: AsyncCallback<image.PixelMap>): void

Unsubscribes from quick thumbnail output events.

System API: This is a system API.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value is fixed at 'quickThumbnail'.
callbackAsyncCallback<image.PixelMap>NoCallback used for unsubscription.

Example

function unregisterQuickThumbnail(photoOutput: camera.PhotoOutput): void {
  photoOutput.off('quickThumbnail');
}

FrameShutterInfo

Defines the frame shutter information.

System capability: SystemCapability.Multimedia.Camera.Core

NameTypeMandatoryDescription
captureIdnumberYesID of this capture action.
timestampnumberYesTimestamp when the frame shutter event is triggered.

CaptureEndInfo

Defines the capture end information.

System capability: SystemCapability.Multimedia.Camera.Core

NameTypeMandatoryDescription
captureIdnumberYesID of this capture action.
frameCountnumberYesNumber of frames captured.

VideoOutput

Implements output information used in a video recording session. This class inherits from CameraOutput.

start

start(callback: AsyncCallback<void>): void

Starts video recording. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<void>YesCallback used to return the result. If the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400103Session not config.
7400201Camera service fatal error.

Example

import { BusinessError } from '@ohos.base';

function startVideoOutput(videoOutput: camera.VideoOutput): void {
  videoOutput.start((err: BusinessError) => {
    if (err) {
      console.error(`Failed to start the video output ${err.code}`);
      return;
    }
    console.log('Callback invoked to indicate the video output start success.');
  });
}

start

start(): Promise<void>

Starts video recording. This API uses a promise to return the result.

System capability: SystemCapability.Multimedia.Camera.Core

Return value

TypeDescription
Promise<void>Promise used to return the result. If the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400103Session not config.
7400201Camera service fatal error.

Example

import { BusinessError } from '@ohos.base';

function startVideoOutput(videoOutput: camera.VideoOutput): void {
  videoOutput.start().then(() => {
    console.log('Promise returned to indicate that start method execution success.');
  }).catch((err: BusinessError) => {
    console.log('Failed to videoOutput start '+ err.code);
  });
}

stop

stop(callback: AsyncCallback<void>): void

Stops video recording. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<void>YesCallback used to return the result.

Example

import { BusinessError } from '@ohos.base';

function stopVideoOutput(videoOutput: camera.VideoOutput): void {
  videoOutput.stop((err: BusinessError) => {
    if (err) {
      console.error(`Failed to stop the video output ${err.code}`);
      return;
    }
    console.log('Callback invoked to indicate the video output stop success.');
  });
}

stop

stop(): Promise<void>

Stops video recording. This API uses a promise to return the result.

System capability: SystemCapability.Multimedia.Camera.Core

Return value

TypeDescription
Promise<void>Promise used to return the result.

Example

import { BusinessError } from '@ohos.base';

function stopVideoOutput(videoOutput: camera.VideoOutput): void {
  videoOutput.stop().then(() => {
    console.log('Promise returned to indicate that stop method execution success.');
  }).catch((err: BusinessError) => {
    console.log('Failed to videoOutput stop '+ err.code);
  });
}

release

release(callback: AsyncCallback<void>): void

Releases output resources. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<void>YesCallback used to return the result. If the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400201Camera service fatal error.

Example

import { BusinessError } from '@ohos.base';

function releaseVideoOutput(videoOutput: camera.VideoOutput): void {
  videoOutput.release((err: BusinessError) => {
    if (err) {
      console.error(`Failed to release the PreviewOutput instance ${err.code}`);
      return;
    }
    console.log('Callback invoked to indicate that the videoOutput instance is released successfully.');
  });
}

release

release(): Promise<void>

Releases output resources. This API uses a promise to return the result.

System capability: SystemCapability.Multimedia.Camera.Core

Return value

TypeDescription
Promise<void>Promise used to return the result. If the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400201Camera service fatal error.

Example

import { BusinessError } from '@ohos.base';

function releaseVideoOutput(videoOutput: camera.VideoOutput): void {
  videoOutput.release().then(() => {
    console.log('Promise returned to indicate that the videoOutput instance is released successfully.');
  }).catch((err: BusinessError) => {
    console.log('Failed to videoOutput release '+ err.code);
  });
}

on('frameStart')

on(type: 'frameStart', callback: AsyncCallback<void>): void

Subscribes to video recording start events. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value is fixed at 'frameStart'. The event can be listened for when a videoOutput instance is created. The event is triggered and the corresponding information is returned when the bottom layer starts exposure for the first time.
callbackAsyncCallback<void>YesCallback used to return the result. The recording starts as long as this event is returned.

Example

function registerVideoOutputFrameStart(videoOutput: camera.VideoOutput): void {
  videoOutput.on('frameStart', () => {
    console.log('Video frame started');
  });
}

off('frameStart')

off(type: 'frameStart', callback?: AsyncCallback<void>): void

Unsubscribes from video recording start events.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value is fixed at 'frameStart'. The event can be listened for when a videoOutput instance is created.
callbackAsyncCallback<void>NoCallback used for unsubscription.

Example

function unregisterVideoOutputFrameStart(videoOutput: camera.VideoOutput): void {
  videoOutput.off('frameStart');
}

on('frameEnd')

on(type: 'frameEnd', callback: AsyncCallback<void>): void

Subscribes to video recording stop events. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value is fixed at 'frameEnd'. The event can be listened for when a videoOutput instance is created. This event is triggered and returned when the last frame of recording is complete.
callbackAsyncCallback<void>YesCallback used to return the result. The recording ends as long as this event is returned.

Example

function registerVideoOutputFrameEnd(videoOutput: camera.VideoOutput): void {
  videoOutput.on('frameEnd', () => {
    console.log('Video frame ended');
  });
}

off('frameEnd')

off(type: 'frameEnd', callback?: AsyncCallback<void>): void

Unsubscribes from video recording stop events.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value is fixed at 'frameEnd'. The event can be listened for when a videoOutput instance is created.
callbackAsyncCallback<void>NoCallback used for unsubscription.

Example

function unregisterVideoOutputFrameEnd(videoOutput: camera.VideoOutput): void {
  videoOutput.off('frameEnd');
}

on('error')

on(type: 'error', callback: ErrorCallback): void

Subscribes to VideoOutput error events. This API uses a callback to return the result.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value is fixed at 'error'. The event can be listened for when a videoOutput instance is created. This event is triggered and the corresponding error message is returned when an error occurs during the calling of a recording-related API such as start() and release().
callbackErrorCallbackYesCallback used to return an error code defined in CameraErrorCode.

Example

import { BusinessError } from '@ohos.base';

function registerVideoOutputError(videoOutput: camera.VideoOutput): void {
  videoOutput.on('error', (error: BusinessError) => {
    console.log(`Video output error code: ${error.code}`);
  });
}

off('error')

off(type: 'error', callback?: ErrorCallback): void

Unsubscribes from VideoOutput error events.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value is fixed at 'error'. The event can be listened for when a videoOutput instance is created.
callbackErrorCallbackNoCallback used for unsubscription.

Example

function unregisterVideoOutputError(videoOutput: camera.VideoOutput): void {
  videoOutput.off('error');
}

MetadataOutput

Implements metadata streams. It inherits CameraOutput.

start

start(callback: AsyncCallback<void>): void

Starts to output metadata. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<void>YesCallback used to return the result. If the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400103Session not config.
7400201Camera service fatal error.

Example

import { BusinessError } from '@ohos.base';

function startMetadataOutput(metadataOutput: camera.MetadataOutput): void {
  metadataOutput.start((err: BusinessError) => {
    if (err) {
      console.error(`Failed to start metadataOutput. ${err.code}`);
      return;
    }
    console.log('Callback returned with metadataOutput started.');
  });
}

start

start(): Promise<void>

Starts to output metadata. This API uses a promise to return the result.

System capability: SystemCapability.Multimedia.Camera.Core

Return value

TypeDescription
Promise<void>Promise used to return the result. If the operation fails, an error code defined in CameraErrorCode is returned.

Error codes

For details about the error codes, see Camera Error Codes.

IDError Message
7400103Session not config.
7400201Camera service fatal error.

Example

import { BusinessError } from '@ohos.base';

function startMetadataOutput(metadataOutput: camera.MetadataOutput): void {
  metadataOutput.start().then(() => {
    console.log('Callback returned with metadataOutput started.');
  }).catch((err: BusinessError) => {
    console.log('Failed to metadataOutput start '+ err.code);
  });
}

stop

stop(callback: AsyncCallback<void>): void

Stops outputting metadata. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<void>YesCallback used to return the result.

Example

import { BusinessError } from '@ohos.base';

function stopMetadataOutput(metadataOutput: camera.MetadataOutput): void {
  metadataOutput.stop((err: BusinessError) => {
    if (err) {
      console.error(`Failed to stop the metadataOutput. ${err.code}`);
      return;
    }
    console.log('Callback returned with metadataOutput stopped.');
  })
}

stop

stop(): Promise<void>

Stops outputting metadata. This API uses a promise to return the result.

System capability: SystemCapability.Multimedia.Camera.Core

Return value

TypeDescription
Promise<void>Promise used to return the result.

Example

import { BusinessError } from '@ohos.base';

function stopMetadataOutput(metadataOutput: camera.MetadataOutput): void {
  metadataOutput.stop().then(() => {
    console.log('Callback returned with metadataOutput stopped.');
  }).catch((err: BusinessError) => {
    console.log('Failed to metadataOutput stop '+ err.code);
  });
}

on('metadataObjectsAvailable')

on(type: 'metadataObjectsAvailable', callback: AsyncCallback<Array<MetadataObject>>): void

Subscribes to events indicating available metadata objects. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value is fixed at 'metadataObjectsAvailable'. The event can be listened for when a metadataOutput instance is created. This event is triggered and the corresponding metadata is returned when valid metadata is detected.
callbackCallback<Array<MetadataObject>>YesCallback used to return the metadata.

Example

import { BusinessError } from '@ohos.base';

function registerMetadataObjectsAvailable(metadataOutput: camera.MetadataOutput): void {
  metadataOutput.on('metadataObjectsAvailable', (err: BusinessError, metadataObjectArr: Array<camera.MetadataObject>) => {
    console.log(`metadata output metadataObjectsAvailable`);
  });
}

off('metadataObjectsAvailable')

off(type: 'metadataObjectsAvailable', callback?: AsyncCallback<Array<MetadataObject>>): void

Unsubscribes from events indicating available metadata objects.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value is fixed at 'metadataObjectsAvailable'. The event can be listened for when a metadataOutput instance is created.
callbackCallback<Array<MetadataObject>>NoCallback used for unsubscription.

Example

function unregisterMetadataObjectsAvailable(metadataOutput: camera.MetadataOutput): void {
  metadataOutput.off('metadataObjectsAvailable');
}

on('error')

on(type: 'error', callback: ErrorCallback): void

Subscribes to metadata error events. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value is fixed at 'error'. The event can be listened for when a metadataOutput instance is created. This event is triggered and the corresponding error message is returned when an error occurs during the calling of a metadata-related API such as start() and release().
callbackErrorCallbackYesCallback used to return an error code defined in CameraErrorCode.

Example

import { BusinessError } from '@ohos.base';

function registerMetadataOutputError(metadataOutput: camera.MetadataOutput): void {
  metadataOutput.on('error', (metadataOutputError: BusinessError) => {
    console.log(`Metadata output error code: ${metadataOutputError.code}`);
  });
}

off('error')

off(type: 'error', callback?: ErrorCallback): void

Unsubscribes from metadata error events.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value is fixed at 'error'. The event can be listened for when a metadataOutput instance is created.
callbackErrorCallbackNoCallback used for unsubscription.

Example

function unregisterMetadataOutputError(metadataOutput: camera.MetadataOutput): void {
  metadataOutput.off('error');
}

MetadataObjectType

Enumerates the metadata object types.

System capability: SystemCapability.Multimedia.Camera.Core

NameValueDescription
FACE_DETECTION0Face detection. The detection point must be in the coordinate system (0-1), where the upper left corner is {0, 0} and the lower right corner is {1, 1}.
The coordinate system is based on the horizontal device direction with the device's charging port on the right.
If the layout of a preview screen of an application is based on the vertical direction with the charging port on the lower side,
the layout width and height are {w, h} and the return point is {x, y}, then the coordinate point after conversion is {1-y, x}.

Rect

Defines a rectangle.

System capability: SystemCapability.Multimedia.Camera.Core

NameTypeMandatoryDescription
topLeftXnumberYesX-axis coordinate of the upper left corner of the rectangle.
topLeftYnumberYesY-axis coordinate of the upper left corner of the rectangle.
widthnumberYesWidth of the rectangle.
heightnumberYesHeight of the rectangle.

MetadataObject

Implements camera metadata, which is the data source of CameraInput. The metadata is obtained through metadataOutput.on('metadataObjectsAvailable').

System capability: SystemCapability.Multimedia.Camera.Core

NameTypeMandatoryDescription
typeMetadataObjectTypeNoMetadata data type. Only face recognition is supported.
timestampnumberNoCurrent timestamp, in milliseconds.
boundingBoxRectNoMetadata rectangle.

你可能感兴趣的鸿蒙文章

harmony 鸿蒙APIs

harmony 鸿蒙System Common Events (To Be Deprecated Soon)

harmony 鸿蒙System Common Events

harmony 鸿蒙API Reference Document Description

harmony 鸿蒙Enterprise Device Management Overview (for System Applications Only)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager)

harmony 鸿蒙@ohos.distributedBundle (Distributed Bundle Management)

harmony 鸿蒙@ohos.bundle (Bundle)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (EnterpriseAdminExtensionAbility)

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/af2829dc0fb241d8832c9c7d4953056d