Interface (CameraManager)
CameraManager implements camera management. Before calling any API in CameraManager, you must use getCameraManager to obtain a CameraManager instance.
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 '@kit.CameraKit';
getSupportedCameras
getSupportedCameras(): Array<CameraDevice>
Obtains the supported camera devices. This API returns the result synchronously.
Atomic service API: This API can be used in atomic services since API version 19.
System capability: SystemCapability.Multimedia.Camera.Core
Return value
| Type | Description |
|---|---|
| Array<CameraDevice> | Array of camera devices supported. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
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;
}
getSupportedSceneModes11+
getSupportedSceneModes(camera: CameraDevice): Array<SceneMode>
Obtains the scene modes supported by a camera device. This API returns the result synchronously.
Atomic service API: This API can be used in atomic services since API version 19.
System capability: SystemCapability.Multimedia.Camera.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| camera | CameraDevice | Yes | CameraDevice instance, which is obtained through getSupportedCameras. Error code 7400101 is returned if the input parameter is invalid. |
Return value
| Type | Description |
|---|---|
| Array<SceneMode> | Array of scene modes supported. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
function getSupportedSceneModes(cameraManager: camera.CameraManager, camera: camera.CameraDevice): Array<camera.SceneMode> {
let modes: Array<camera.SceneMode> = [];
try {
modes = cameraManager.getSupportedSceneModes(camera);
} catch (error) {
let err = error as BusinessError;
console.error(`The getSupportedSceneModes call failed. error code: ${err.code}`);
}
return modes;
}
getSupportedOutputCapability11+
getSupportedOutputCapability(camera: CameraDevice, mode: SceneMode): CameraOutputCapability
Obtains the output capability supported by a camera device in a given scene mode. This API returns the result synchronously.
Atomic service API: This API can be used in atomic services since API version 19.
System capability: SystemCapability.Multimedia.Camera.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| camera | CameraDevice | Yes | CameraDevice instance, which is obtained through getSupportedCameras. |
| mode | SceneMode | Yes | Scene mode, which is obtained through getSupportedSceneModes. |
Return value
| Type | Description |
|---|---|
| CameraOutputCapability | Camera output capability obtained. |
Example
function getSupportedOutputCapability(camera: camera.CameraDevice, cameraManager: camera.CameraManager, sceneMode: camera.SceneMode): camera.CameraOutputCapability {
let cameraOutputCapability: camera.CameraOutputCapability = cameraManager.getSupportedOutputCapability(camera, sceneMode);
return cameraOutputCapability;
}
getSupportedFullOutputCapability23+
getSupportedFullOutputCapability(camera: CameraDevice, mode: SceneMode): CameraOutputCapability
Obtains the complete output capabilities supported by a specified camera in a specified mode, including YUV, HEIF, and HDR.
NOTE
Before using YUV, HEIF, or HDR, you need to explicitly call this method to ensure that the complete output capabilities are obtained.
Model restriction: This API can be used only in the stage model.
Atomic service API: This API can be used in atomic services since API version 23.
System capability: SystemCapability.Multimedia.Camera.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| camera | CameraDevice | Yes | CameraDevice instance, which is obtained through getSupportedCameras. |
| mode | SceneMode | Yes | Scene mode, which is obtained through getSupportedSceneModes. |
Return value
| Type | Description |
|---|---|
| CameraOutputCapability | Camera output capability obtained. |
Example
import { camera } from '@kit.CameraKit';
function getSupportedFullOutputCapability(camera: camera.CameraDevice, cameraManager: camera.CameraManager, sceneMode: camera.SceneMode): camera.CameraOutputCapability {
let cameraOutputCapability: camera.CameraOutputCapability = cameraManager.getSupportedFullOutputCapability(camera, sceneMode);
return cameraOutputCapability;
}
isCameraMuted
isCameraMuted(): boolean
Checks whether this camera is muted.
Atomic service API: This API can be used in atomic services since API version 19.
System capability: SystemCapability.Multimedia.Camera.Core
Return value
| Type | Description |
|---|---|
| boolean | Check result for whether the camera is muted. true if muted, false otherwise. |
Example
function isCameraMuted(cameraManager: camera.CameraManager): boolean {
let isMuted: boolean = cameraManager.isCameraMuted();
return isMuted;
}
createCameraInput
createCameraInput(camera: CameraDevice): CameraInput
Creates a CameraInput instance with the specified CameraDevice instance. This API returns the result synchronously.
Before calling this API, call getSupportedCameras to obtain the list of supported camera devices, select the camera device that meets the requirements based on the actual usage scenario, and then create the CameraInput instance.
Required permissions: ohos.permission.CAMERA
Atomic service API: This API can be used in atomic services since API version 19.
System capability: SystemCapability.Multimedia.Camera.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| camera | CameraDevice | Yes | CameraDevice instance, which is obtained through getSupportedCameras. |
Return value
| Type | Description |
|---|---|
| CameraInput | CameraInput 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.
| ID | Error Message |
|---|---|
| 7400101 | Parameter missing or parameter type incorrect. |
| 7400102 | Operation not allowed. |
| 7400201 | Camera service fatal error. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
function createCameraInput(camera: camera.CameraDevice, cameraManager: camera.CameraManager): camera.CameraInput | undefined {
let cameraInput: camera.CameraInput | undefined = undefined;
try {
cameraInput = cameraManager.createCameraInput(camera);
} 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.
Before calling this API, specify the camera position and type based on the usage scenario. For example, open the front camera for the selfie feature
Required permissions: ohos.permission.CAMERA
Atomic service API: This API can be used in atomic services since API version 19.
System capability: SystemCapability.Multimedia.Camera.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| position | CameraPosition | Yes | Camera position. You need to obtain the supported camera object by calling getSupportedCameras and then obtain the device position information based on the returned camera object. |
| type | CameraType | Yes | Camera type. You need to obtain the supported camera object by calling getSupportedCameras and then obtain the camera type based on the returned camera object. |
Return value
| Type | Description |
|---|---|
| CameraInput | CameraInput 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.
| ID | Error Message |
|---|---|
| 7400101 | Parameter missing or parameter type incorrect. |
| 7400102 | Operation not allowed. |
| 7400201 | Camera service fatal error. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
function createCameraInput(camera: camera.CameraDevice, cameraManager: camera.CameraManager): camera.CameraInput | undefined {
let position: camera.CameraPosition = camera.cameraPosition;
let type: camera.CameraType = camera.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.error(`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.
Atomic service API: This API can be used in atomic services since API version 19.
System capability: SystemCapability.Multimedia.Camera.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| profile | Profile | Yes | Supported preview profile, which is obtained through getSupportedOutputCapability. |
| surfaceId | string | Yes | Surface ID, which is obtained from XComponent or ImageReceiver. |
Return value
| Type | Description |
|---|---|
| PreviewOutput | PreviewOutput 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.
| ID | Error Message |
|---|---|
| 7400101 | Parameter missing or parameter type incorrect. |
| 7400201 | Camera service fatal error. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
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.error(`The createPreviewOutput call failed. error code: ${err.code}`);
}
return previewOutput;
}
createPreviewOutput12+
createPreviewOutput(surfaceId: string): PreviewOutput
Creates a PreviewOutput instance without configuration. This API returns the result synchronously. It must be used with preconfig.
Atomic service API: This API can be used in atomic services since API version 19.
System capability: SystemCapability.Multimedia.Camera.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| surfaceId | string | Yes | Surface ID, which is obtained from XComponent or ImageReceiver. |
Return value
| Type | Description |
|---|---|
| PreviewOutput | PreviewOutput 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.
| ID | Error Message |
|---|---|
| 7400101 | Parameter missing or parameter type incorrect. |
| 7400201 | Camera service fatal error. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
function createPreviewOutput(cameraManager: camera.CameraManager, surfaceId: string): camera.PreviewOutput | undefined {
let previewOutput: camera.PreviewOutput | undefined = undefined;
try {
previewOutput = cameraManager.createPreviewOutput(surfaceId);
} catch (error) {
// If the operation fails, error.code is returned and processed.
let err = error as BusinessError;
console.error(`The createPreviewOutput call failed. error code: ${err.code}`);
}
return previewOutput;
}
createDeferredPreviewOutput24+
createDeferredPreviewOutput(profile: Profile): PreviewOutput
Creates a deferred PreviewOutput instance and adds it, instead of a common PreviewOutput instance, to the data stream during stream configuration.
Atomic service API: This API can be used in atomic services since API version 24.
System capability: SystemCapability.Multimedia.Camera.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| profile | Profile | Yes | Supported preview profile, which is obtained through getSupportedOutputCapability. |
Return value
| Type | Description |
|---|---|
| PreviewOutput | PreviewOutput 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.
| ID | Error Message |
|---|---|
| 7400101 | Parameter missing or parameter type incorrect. |
| 7400201 | Camera service fatal error. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
function createPreviewOutput(cameraOutputCapability: camera.CameraOutputCapability, cameraManager: camera.CameraManager): camera.PreviewOutput | undefined {
let profile: camera.Profile = cameraOutputCapability.previewProfiles[0];
let previewOutput: camera.PreviewOutput | undefined = undefined;
try {
previewOutput = cameraManager.createDeferredPreviewOutput(profile);
} catch (error) {
// If the operation fails, error.code is returned and processed.
let err = error as BusinessError;
console.error(`The createPreviewOutput call failed. error code: ${err.code}`);
}
return previewOutput;
}
createPhotoOutput11+
createPhotoOutput(profile?: Profile): PhotoOutput
Creates a PhotoOutput instance. This API returns the result synchronously.
Atomic service API: This API can be used in atomic services since API version 19.
System capability: SystemCapability.Multimedia.Camera.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| profile | Profile | No | Supported photo profile, which is obtained through getSupportedOutputCapability. In API version 11, this parameter is mandatory. Starting from API version 12, it will overwrite the preconfigured parameters passed in through preconfig. |
Return value
| Type | Description |
|---|---|
| PhotoOutput | PhotoOutput 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.
| ID | Error Message |
|---|---|
| 7400101 | Parameter missing or parameter type incorrect. |
| 7400201 | Camera service fatal error. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
function createPhotoOutput(cameraOutputCapability: camera.CameraOutputCapability, cameraManager: camera.CameraManager): camera.PhotoOutput | undefined {
let profile: camera.Profile = cameraOutputCapability.photoProfiles[0];
let photoOutput: camera.PhotoOutput | undefined = undefined;
try {
photoOutput = cameraManager.createPhotoOutput(profile);
} catch (error) {
// If the operation fails, error.code is returned and processed.
let err = error as BusinessError;
console.error(`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.
In video recording mode, if SDR or HDR VIVID is enabled, the camera format and color space must be configured according to the relationships specified in the table below. Configurations that do not match the table will cause issues such as preview exceptions.
| SDR/HDR Photo Capture | CameraFormat | ColorSpace |
|---|---|---|
| SDR | CAMERA_FORMAT_YUV_420_SP | BT709_LIMIT |
| HDR_VIVID | CAMERA_FORMAT_YCRCB_P010 CAMERA_FORMAT_YCBCR_P010 |
BT2020_HLG_LIMIT BT2020_HLG_FULL |
Atomic service API: This API can be used in atomic services since API version 19.
System capability: SystemCapability.Multimedia.Camera.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| profile | VideoProfile | Yes | Supported video profile, which is obtained through getSupportedOutputCapability. |
| surfaceId | string | Yes | Surface ID, which is obtained from AVRecorder. |
Return value
| Type | Description |
|---|---|
| VideoOutput | VideoOutput 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.
| ID | Error Message |
|---|---|
| 7400101 | Parameter missing or parameter type incorrect. |
| 7400201 | Camera service fatal error. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
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.error(`The createVideoOutput call failed. error code: ${err.code}`);
}
return videoOutput;
}
createVideoOutput12+
createVideoOutput(surfaceId: string): VideoOutput
Creates a VideoOutput instance without configuration. This API returns the result synchronously. It must be used with preconfig.
Atomic service API: This API can be used in atomic services since API version 19.
System capability: SystemCapability.Multimedia.Camera.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| surfaceId | string | Yes | Surface ID, which is obtained from AVRecorder. |
Return value
| Type | Description |
|---|---|
| VideoOutput | VideoOutput 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.
| ID | Error Message |
|---|---|
| 7400101 | Parameter missing or parameter type incorrect. |
| 7400201 | Camera service fatal error. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
function createVideoOutput(cameraManager: camera.CameraManager, surfaceId: string): camera.VideoOutput | undefined {
let videoOutput: camera.VideoOutput | undefined = undefined;
try {
videoOutput = cameraManager.createVideoOutput(surfaceId);
} catch (error) {
// If the operation fails, error.code is returned and processed.
let err = error as BusinessError;
console.error(`The createVideoOutput call failed. error code: ${err.code}`);
}
return videoOutput;
}
createMetadataOutput
createMetadataOutput(metadataObjectTypes: Array<MetadataObjectType>): MetadataOutput
Creates a MetadataOutput instance. This API returns the result synchronously.
Atomic service API: This API can be used in atomic services since API version 19.
System capability: SystemCapability.Multimedia.Camera.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| metadataObjectTypes | Array<MetadataObjectType> | Yes | Metadata object types, which are obtained through getSupportedOutputCapability. |
Return value
| Type | Description |
|---|---|
| MetadataOutput | MetadataOutput 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.
| ID | Error Message |
|---|---|
| 7400101 | Parameter missing or parameter type incorrect. |
| 7400201 | Camera service fatal error. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
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.error(`createMetadataOutput error. error code: ${err.code}`);
}
}
createSession11+
createSession<T extends Session>(mode: SceneMode): T
Creates a Session instance with a given scene mode. This API returns the result synchronously.
Atomic service API: This API can be used in atomic services since API version 19.
System capability: SystemCapability.Multimedia.Camera.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| mode | SceneMode | Yes | Scene mode. The API does not take effect if the input parameter is invalid (for example, the value is out of range, null, or undefined). |
Return value
| Type | Description |
|---|---|
| T | Session 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.
| ID | Error Message |
|---|---|
| 7400101 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3.Parameter verification failed. |
| 7400201 | Camera service fatal error. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
function createSession(cameraManager: camera.CameraManager, mode: camera.SceneMode): camera.Session | undefined {
let photoSession: camera.PhotoSession | undefined = undefined;
try {
photoSession = cameraManager.createSession(mode) as camera.PhotoSession;
} catch (error) {
// If the operation fails, error.code is returned and processed.
let err = error as BusinessError;
console.error(`createCaptureSession error. error code: ${err.code}`);
}
return photoSession;
}
on('cameraStatus')
on(type: 'cameraStatus', callback: AsyncCallback<CameraStatusInfo>): void
Subscribes to camera status events. This API uses an asynchronous callback to return the result.
NOTE
Currently, you cannot use off() to unregister the callback in the callback method of on().
Atomic service API: This API can be used in atomic services since API version 19.
System capability: SystemCapability.Multimedia.Camera.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| type | string | Yes | Event 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 camera device is enabled or disabled. |
| callback | AsyncCallback<CameraStatusInfo> | Yes | Callback used to return the camera status change. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
function callback(err: BusinessError, cameraStatusInfo: camera.CameraStatusInfo): void {
if (err !== undefined && err.code !== 0) {
console.error('cameraStatus with errorCode = ' + err.code);
return;
}
console.info(`camera : ${cameraStatusInfo.camera.cameraId}`);
console.info(`status: ${cameraStatusInfo.status}`);
}
function registerCameraStatus(cameraManager: camera.CameraManager): void {
cameraManager.on('cameraStatus', callback);
}
off('cameraStatus')
off(type: 'cameraStatus', callback?: AsyncCallback<CameraStatusInfo>): void
Unsubscribes from camera status events. This API uses an asynchronous callback to return the result.
Atomic service API: This API can be used in atomic services since API version 19.
System capability: SystemCapability.Multimedia.Camera.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| type | string | Yes | Event type. The value is fixed at 'cameraStatus'. The event can be listened for when a CameraManager instance is obtained. |
| callback | AsyncCallback<CameraStatusInfo> | No | Callback used to return the result. If this parameter is specified, the subscription to the specified event with the specified callback is canceled. (The callback object cannot be an anonymous function.) Otherwise, the subscriptions to the specified event with all the callbacks are canceled. |
Example
function unregisterCameraStatus(cameraManager: camera.CameraManager): void {
cameraManager.off('cameraStatus');
}
on('foldStatusChange')12+
on(type: 'foldStatusChange', callback: AsyncCallback<FoldStatusInfo>): void
Subscribes to fold status change events of the foldable device. This API uses an asynchronous callback to return the result.
NOTE
Currently, you cannot use off() to unregister the callback in the callback method of on().
Atomic service API: This API can be used in atomic services since API version 19.
System capability: SystemCapability.Multimedia.Camera.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| type | string | Yes | Event type. The value is fixed at 'foldStatusChange'. The event is triggered when the fold state of the foldable device changes. |
| callback | AsyncCallback<FoldStatusInfo> | Yes | Callback used to return the fold state information about the foldable device. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
function callback(err: BusinessError, foldStatusInfo: camera.FoldStatusInfo): void {
if (err !== undefined && err.code !== 0) {
console.error('foldStatusChange with errorCode = ' + err.code);
return;
}
console.info(`camera length: ${foldStatusInfo.supportedCameras.length}`);
console.info(`foldStatus: ${foldStatusInfo.foldStatus}`);
}
function registerFoldStatusChange(cameraManager: camera.CameraManager): void {
cameraManager.on('foldStatusChange', callback);
}
off('foldStatusChange')12+
off(type: 'foldStatusChange', callback?: AsyncCallback<FoldStatusInfo>): void
Unsubscribes from fold state change events of the foldable device.
Atomic service API: This API can be used in atomic services since API version 19.
System capability: SystemCapability.Multimedia.Camera.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| type | string | Yes | Event type. The value is fixed at 'foldStatusChange'. The event is triggered when the fold state of the foldable device changes. |
| callback | AsyncCallback<FoldStatusInfo> | No | Callback used to return the fold state information about the foldable device. If this parameter is specified, the subscription to the specified event with the specified callback is canceled. (The callback object cannot be an anonymous function.) Otherwise, the subscriptions to the specified event with all the callbacks are canceled. |
Example
function unregisterFoldStatusChange(cameraManager: camera.CameraManager): void {
cameraManager.off('foldStatusChange');
}
isTorchSupported11+
isTorchSupported(): boolean
Checks whether the camera device supports the flashlight.
Atomic service API: This API can be used in atomic services since API version 19.
System capability: SystemCapability.Multimedia.Camera.Core
Return value
| Type | Description |
|---|---|
| boolean | Whether the device supports the flashlight. true if supported, false otherwise. If false is returned, isTorchModeSupported, getTorchMode, setTorchMode, isTorchLevelControlSupported, and setTorchModeOnWithLevel do not take effect. If the API call fails, undefined is returned. |
Example
function isTorchSupported(cameraManager: camera.CameraManager): boolean {
let isSupported = cameraManager.isTorchSupported();
return isSupported;
}
isTorchModeSupported11+
isTorchModeSupported(mode: TorchMode): boolean
Checks whether a flashlight mode is supported.
Atomic service API: This API can be used in atomic services since API version 19.
System capability: SystemCapability.Multimedia.Camera.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| mode | TorchMode | Yes | Flashlight mode. If the input parameter is null or undefined, it is treated as 0 and the flashlight is turned off. |
Return value
| Type | Description |
|---|---|
| boolean | Check result for the support of the flashlight mode. true if supported, false otherwise. If the API call fails, undefined is returned. |
Example
function isTorchModeSupported(cameraManager: camera.CameraManager, torchMode: camera.TorchMode): boolean {
let isSupported = cameraManager.isTorchModeSupported(torchMode);
return isSupported;
}
getTorchMode11+
getTorchMode(): TorchMode
Obtains the flashlight mode of this camera device.
Atomic service API: This API can be used in atomic services since API version 19.
System capability: SystemCapability.Multimedia.Camera.Core
Return value
| Type | Description |
|---|---|
| TorchMode | Flashlight mode. |
Example
function getTorchMode(cameraManager: camera.CameraManager): camera.TorchMode | undefined {
let torchMode: camera.TorchMode | undefined = undefined;
torchMode = cameraManager.getTorchMode();
return torchMode;
}
setTorchMode11+
setTorchMode(mode: TorchMode): void
Sets the flashlight mode.
Atomic service API: This API can be used in atomic services since API version 19.
System capability: SystemCapability.Multimedia.Camera.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| mode | TorchMode | Yes | Flashlight mode. If the input parameter is null or undefined, it is treated as 0 and the flashlight is turned off. |
Error codes
For details about the error codes, see Camera Error Codes.
| ID | Error Message |
|---|---|
| 7400102 | Operation not allowed. |
| 7400201 | Camera service fatal error. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
function setTorchMode(cameraManager: camera.CameraManager, torchMode: camera.TorchMode): void {
try {
cameraManager.setTorchMode(torchMode);
} catch (error) {
// If the operation fails, error.code is returned and processed.
let err = error as BusinessError;
console.error(`The setTorchMode call failed. error code: ${err.code}`);
}
}
on('torchStatusChange')11+
on(type: 'torchStatusChange', callback: AsyncCallback<TorchStatusInfo>): void
Subscribes to flashlight status change events. This API uses an asynchronous callback to return the result.
NOTE
Currently, you cannot use off() to unregister the callback in the callback method of on().
Atomic service API: This API can be used in atomic services since API version 19.
System capability: SystemCapability.Multimedia.Camera.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| type | string | Yes | Event type. The value is fixed at 'torchStatusChange'. The event can be listened for when a CameraManager instance is obtained. Currently, this event is triggered only in the following scenarios: The flashlight is turned on or turned off, or becomes unavailable or available. |
| callback | AsyncCallback<TorchStatusInfo> | Yes | Callback used to return the flashlight status. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
function callback(err: BusinessError, torchStatusInfo: camera.TorchStatusInfo): void {
if (err !== undefined && err.code !== 0) {
console.error(`Callback Error, errorCode: ${err.code}`);
return;
}
console.info(`onTorchStatusChange, isTorchAvailable: ${torchStatusInfo.isTorchAvailable}, isTorchActive: ${torchStatusInfo.isTorchActive}, level: ${torchStatusInfo.torchLevel}`);
}
function registerTorchStatusChange(cameraManager: camera.CameraManager): void {
cameraManager.on('torchStatusChange', callback);
}
off('torchStatusChange')11+
off(type: 'torchStatusChange', callback?: AsyncCallback<TorchStatusInfo>): void
Unsubscribes from flashlight status change events. This API uses an asynchronous callback to return the result.
Atomic service API: This API can be used in atomic services since API version 19.
System capability: SystemCapability.Multimedia.Camera.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| type | string | Yes | Event type. The value is fixed at 'torchStatusChange'. The event can be listened for when a CameraManager instance is obtained. |
| callback | AsyncCallback<TorchStatusInfo> | No | Callback used to return the result. If this parameter is specified, the subscription to the specified event with the specified callback is canceled. (The callback object cannot be an anonymous function.) Otherwise, the subscriptions to the specified event with all the callbacks are canceled. |
Example
function unregisterTorchStatusChange(cameraManager: camera.CameraManager): void {
cameraManager.off('torchStatusChange');
}
isTorchLevelControlSupported
isTorchLevelControlSupported(): boolean
Checks whether the device supports flashlight brightness control.
Since: 26.0.0
Model restriction: This API can be used only in the stage model.
Atomic service API: This API can be used in atomic services since API version 26.
System capability: SystemCapability.Multimedia.Camera.Core
Return value
| Type | Description |
|---|---|
| boolean | Whether the device supports flashlight brightness control. Returns true if supported, false if not. If the API call fails, undefined is returned. |
Example
function isTorchLevelControlSupported(cameraManager: camera.CameraManager): boolean {
let isSupported = cameraManager.isTorchLevelControlSupported();
return isSupported;
}
SetTorchModeOnWithLevel
SetTorchModeOnWithLevel(torchLevel: number): void
Sets the specified brightness level for the flashlight.
Since: 26.0.0
Model restriction: This API can be used only in the stage model.
Atomic service API: This API can be used in atomic services since API version 26.
System capability: SystemCapability.Multimedia.Camera.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| torchLevel | number | Yes | Flashlight brightness level. The value range is [0.0, 1.0] (0.0 indicates the darkest, and 1.0 indicates the brightest). |
Error codes
For details about the error codes, see Camera Error Codes.
| ID | Error Message |
|---|---|
| 7400201 | Camera service fatal error. |
| 7400102 | Operation not allowed. |
Example
function SetTorchModeOnWithLevel(cameraManager: camera.CameraManager, torchLevel: number): void {
cameraManager.setTorchModeOnWithLevel(torchLevel);
return ;
}
getCameraDevice18+
getCameraDevice(position: CameraPosition, type: CameraType): CameraDevice
Obtains the specified camera based on the camera position and type.
Obtains the camera lens of the specified CameraPosition and CameraType. If the returned result is undefined, the camera lens is not found on the current device.
Atomic service API: This API can be used in atomic services since API version 19.
System capability: SystemCapability.Multimedia.Camera.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| position | CameraPosition | Yes | Camera position. |
| type | CameraType | Yes | Camera type. |
Return value
| Type | Description |
|---|---|
| CameraDevice | Camera obtained. |
Error codes
For details about the error codes, see Camera Error Codes.
| ID | Error Message |
|---|---|
| 7400201 | Camera service fatal error. |
Example
import { camera } from '@kit.CameraKit';
import { BusinessError } from '@kit.BasicServicesKit';
function getCameraDevice(cameraManager: camera.CameraManager, position: camera.CameraPosition, type: camera.CameraType): void {
try {
let curCameraDev: camera.CameraDevice | undefined = undefined;
curCameraDev = cameraManager.getCameraDevice(position, type);
} catch (error) {
// If the operation fails, an error code is returned and processed.
let err = error as BusinessError;
console.error(`The getCameraDevice call failed. error code: ${err.code}`);
}
}
getCameraDevices23+
getCameraDevices(position: CameraPosition, types: Array<CameraType>, connectType: ConnectionType): Array<CameraDevice>
Obtains the list of cameras that meet the search criteria based on the camera position, camera types, and connection type.
Atomic service API: This API can be used in atomic services since API version 23.
System capability: SystemCapability.Multimedia.Camera.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| position | CameraPosition | Yes | Camera position. |
| types | Array<CameraType> | Yes | Array of camera types. |
| connectType | ConnectionType | Yes | Camera connection type. |
Return value
| Type | Description |
|---|---|
| Array<CameraDevice> | Array of cameras that meet the search criteria. |
Error codes
For details about the error codes, see Camera Error Codes.
| ID | Error Message |
|---|---|
| 7400201 | Camera service fatal error. |
Example
import { camera } from '@kit.CameraKit';
import { BusinessError } from '@kit.BasicServicesKit';
function getCameraDevices(cameraManager: camera.CameraManager, position: camera.CameraPosition, types: Array<camera.CameraType>, connectType: camera.ConnectionType): void {
try {
let cameraDevs: Array<camera.CameraDevice> = [];
cameraDevs = cameraManager.getCameraDevices(position, types, connectType);
} catch (error) {
// If the operation fails, an error code is returned and processed.
let err = error as BusinessError;
console.error(`The getCameraDevices call failed. error code: ${err.code}`);
}
}
getCameraConcurrentInfos18+
getCameraConcurrentInfos(cameras: Array<CameraDevice>): Array<CameraConcurrentInfo>
Obtains the concurrency information of the specified cameras. If the return value is an empty array, concurrency is not supported.
Atomic service API: This API can be used in atomic services since API version 19.
System capability: SystemCapability.Multimedia.Camera.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| cameras | Array<CameraDevice> | Yes | Array of CameraDevice objects. You are advised to use the front and rear cameras obtained by calling getCameraDevice. |
Return value
| Type | Description |
|---|---|
| Array<CameraConcurrentInfo> | Array of concurrency information corresponding to the provided CameraDevice objects, with a one-to-one mapping. |
Error codes
For details about the error codes, see Camera Error Codes.
| ID | Error Message |
|---|---|
| 7400201 | Camera service fatal error. |
Example
import { camera } from '@kit.CameraKit';
import { BusinessError } from '@kit.BasicServicesKit';
function getCameraConcurrentInfos(cameraManager: camera.CameraManager,
cameraDeviceArray: Array<camera.CameraDevice>): Array<camera.CameraConcurrentInfo> {
let cameraConcurrentInfos: Array<camera.CameraConcurrentInfo> = [];
try {
cameraConcurrentInfos = cameraManager.getCameraConcurrentInfos(cameraDeviceArray);
} catch (error) {
// If the operation fails, an error code is returned and processed.
let err = error as BusinessError;
console.error(`The getCameraConcurrentInfos call failed. error code: ${err.code}`);
}
return cameraConcurrentInfos;
}
getSupportedOutputCapability(deprecated)
getSupportedOutputCapability(camera: CameraDevice): CameraOutputCapability
Obtains the output capability supported by a camera device. This API returns the result synchronously.
NOTE This API is supported since API version 10 and deprecated since API version 11. You are advised to use getSupportedOutputCapability instead.
System capability: SystemCapability.Multimedia.Camera.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| camera | CameraDevice | Yes | CameraDevice instance, which is obtained through getSupportedCameras. An error code is returned if the input parameter is invalid. |
Return value
| Type | Description |
|---|---|
| CameraOutputCapability | Camera output capability obtained. |
Example
function getSupportedOutputCapability(camera: camera.CameraDevice, cameraManager: camera.CameraManager): camera.CameraOutputCapability {
let cameraOutputCapability: camera.CameraOutputCapability = cameraManager.getSupportedOutputCapability(camera);
return cameraOutputCapability;
}
createPhotoOutput(deprecated)
createPhotoOutput(profile: Profile, surfaceId: string): PhotoOutput
Creates a PhotoOutput instance. This API returns the result synchronously.
NOTE
- This API is supported since API version 10 and deprecated since API version 11. You are advised to use createPhotoOutput instead.
- This API can only be used to create a PhotoOutput object in JPEG format.
System capability: SystemCapability.Multimedia.Camera.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| profile | Profile | Yes | Supported photo profile, which is obtained through getSupportedOutputCapability. |
| surfaceId | string | Yes | Surface ID, which is obtained from ImageReceiver. |
Return value
| Type | Description |
|---|---|
| PhotoOutput | PhotoOutput 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.
| ID | Error Message |
|---|---|
| 7400101 | Parameter missing or parameter type incorrect. |
import { BusinessError } from '@kit.BasicServicesKit';
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.error(`The createPhotoOutput call failed. error code: ${err.code}`);
}
return photoOutput;
}
createCaptureSession(deprecated)
createCaptureSession(): CaptureSession
Creates a CaptureSession instance. This API returns the result synchronously.
NOTE This API is supported since API version 10 and deprecated since API version 11. You are advised to use createSession instead.
System capability: SystemCapability.Multimedia.Camera.Core
Return value
| Type | Description |
|---|---|
| CaptureSession | CaptureSession 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.
| ID | Error Message |
|---|---|
| 7400201 | Camera service fatal error. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
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.error(`createCaptureSession error. error code: ${err.code}`);
}
return captureSession;
}