# @ohos.multimedia.camera (相机管理)
本模块为开发者提供一套简单且易于理解的相机服务接口,开发者通过调用接口可以开发相机应用。应用通过访问和操作相机硬件,实现基础操作,如预览、拍照和录像;还可以通过接口组合完成更多操作,如控制闪光灯和曝光时间、对焦或调焦等。
> **说明:**
>
> 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
## 导入模块
```ts
import { camera } from '@kit.CameraKit';
```
## camera.getCameraManager
getCameraManager(context: Context): CameraManager
获取相机管理器实例,同步返回结果。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------------------------------- | ---- | ---------------------------- |
| context | [Context](../apis-ability-kit/js-apis-inner-application-baseContext.md) | 是 | 应用上下文。 |
**返回值:**
| 类型 | 说明 |
| ----------------------------------------------- | ---------------------------- |
| [CameraManager](#cameramanager) | 相机管理器。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400101 | Parameter missing or parameter type incorrect. |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
import { common } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
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;
}
```
## CameraDevice
相机设备信息。
**系统能力:** SystemCapability.Multimedia.Camera.Core
| 名称 | 类型 | 只读 | 可选 | 说明 |
| -------------- | --------------------------------- | ---- | ---- |---------- |
| cameraId | string | 是 | 否 | 相机id。|
| cameraPosition | [CameraPosition](#cameraposition) | 是 | 否 | 相机位置。 |
| cameraType | [CameraType](#cameratype) | 是 | 否 | 相机类型。 |
| connectionType | [ConnectionType](#connectiontype) | 是 | 否 | 相机连接类型。 |
| cameraOrientation12+ | number | 是 | 否 | 镜头的安装角度,不会随着屏幕旋转而改变,取值范围为0°-360°。 |
## CameraPosition
枚举,相机位置。
**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
**系统能力:** SystemCapability.Multimedia.Camera.Core
| 名称 | 值 | 说明 |
| --------------------------- | ---- |-----------------------------------------------------------------|
| CAMERA_POSITION_UNSPECIFIED | 0 | 相对于设备屏幕没有固定的朝向的相机。 |
| CAMERA_POSITION_BACK | 1 | 后置相机。 |
| CAMERA_POSITION_FRONT | 2 | 前置相机。 |
| CAMERA_POSITION_FOLD_INNER(deprecated) | 3 | 折叠态相机。
从API version 11开始支持,从API version 12开始废弃。 |
## CameraType
枚举,相机类型。
**系统能力:** SystemCapability.Multimedia.Camera.Core
| 名称 | 值 | 说明 |
| ----------------------- | ---- | -------------- |
| CAMERA_TYPE_DEFAULT | 0 | 相机类型未指定。 |
| CAMERA_TYPE_WIDE_ANGLE | 1 | 广角相机。 |
| CAMERA_TYPE_ULTRA_WIDE | 2 | 超广角相机。 |
| CAMERA_TYPE_TELEPHOTO | 3 | 长焦相机。 |
| CAMERA_TYPE_TRUE_DEPTH | 4 | 带景深信息的相机。 |
## ConnectionType
枚举,相机连接类型。
**系统能力:** SystemCapability.Multimedia.Camera.Core
| 名称 | 值 | 说明 |
| ---------------------------- | ---- | ------------- |
| CAMERA_CONNECTION_BUILT_IN | 0 | 内置相机。 |
| CAMERA_CONNECTION_USB_PLUGIN | 1 | USB连接的相机。 |
| CAMERA_CONNECTION_REMOTE | 2 | 远程连接的相机。 |
## CameraStatus
枚举,相机状态。
**系统能力:** SystemCapability.Multimedia.Camera.Core
| 名称 | 值 | 说明 |
| ------------------------- | ---- | ------------ |
| CAMERA_STATUS_APPEAR | 0 | 新的相机出现。 |
| CAMERA_STATUS_DISAPPEAR | 1 | 相机被移除。 |
| CAMERA_STATUS_AVAILABLE | 2 | 相机可用。 |
| CAMERA_STATUS_UNAVAILABLE | 3 | 相机不可用。 |
## FoldStatus12+
枚举,折叠机折叠状态。
**系统能力:** SystemCapability.Multimedia.Camera.Core
| 名称 | 值 | 说明 |
| ------------------------- | ---- | ------------ |
| NON_FOLDABLE | 0 | 表示当前设备不可折叠。 |
| EXPANDED | 1 | 表示当前设备折叠状态为完全展开。 |
| FOLDED | 2 | 表示当前设备折叠状态为折叠。 |
## CameraStatusInfo
相机管理器回调返回的接口实例,表示相机状态信息。
**系统能力:** SystemCapability.Multimedia.Camera.Core
| 名称 | 类型 | 只读 | 可选 | 说明 |
| ------ | ----------------------------- | --------- |------------ | ---------- |
| camera | [CameraDevice](#cameradevice) | 否 | 否 | 相机信息。 |
| status | [CameraStatus](#camerastatus) | 否 | 否 | 相机状态。 |
## FoldStatusInfo12+
相机管理器回调返回的接口实例,表示折叠机折叠状态信息。
**系统能力:** SystemCapability.Multimedia.Camera.Core
| 名称 | 类型 | 只读 | 可选 | 说明 |
| ------ | ----------------------------- | --------- |------------ | ---------- |
| supportedCameras | [Array](#cameradevice) | 否 | 否 | 当前折叠状态所支持的相机信息列表。 |
| foldStatus | [FoldStatus](#foldstatus12) | 否 | 否 | 折叠屏折叠状态。 |
## Profile
相机配置信息项。
**系统能力:** SystemCapability.Multimedia.Camera.Core
| 名称 | 类型 | 只读 | 可选 | 说明 |
| -------- | ----------------------------- |---- | ---- | ------------- |
| format | [CameraFormat](#cameraformat) | 是 | 否 | 输出格式。 |
| size | [Size](#size) | 是 | 否 | 分辨率。
设置的是相机分辨率宽高,非实际出图宽高。 |
## FrameRateRange
帧率范围。
**系统能力:** SystemCapability.Multimedia.Camera.Core
| 名称 | 类型 | 只读 | 可选 | 说明 |
| -------- | ----------------------------- |----- |---| -------------- |
| min | number | 是 | 否 | 最小帧率。 |
| max | number | 是 | 否 | 最大帧率。 |
## VideoProfile
视频配置信息项,继承[Profile](#profile)。
**系统能力:** SystemCapability.Multimedia.Camera.Core
| 名称 | 类型 | 只读 | 可选 | 说明 |
| ------------------------- | ----------------------------------------- | --- | ---- |----------- |
| frameRateRange | [FrameRateRange](#frameraterange) | 是 | 否 | 帧率范围,fps(frames per second)。 |
## CameraOutputCapability
相机输出能力项。
**系统能力:** SystemCapability.Multimedia.Camera.Core
| 名称 | 类型 | 只读 | 可选 | 说明 |
| ----------------------------- | --------------------------------------------------- | ---- | ---- |-------------------|
| previewProfiles | Array\<[Profile](#profile)\> | 是 | 否 | 支持的预览配置信息集合。 |
| photoProfiles | Array\<[Profile](#profile)\> | 是 | 否 | 支持的拍照配置信息集合。 |
| videoProfiles | Array\<[VideoProfile](#videoprofile)\> | 是 | 否 | 支持的录像配置信息集合。 |
| supportedMetadataObjectTypes | Array\<[MetadataObjectType](#metadataobjecttype)\> | 是 | 否 | 支持的metadata流类型信息集合。 |
## SceneMode11+
枚举,相机支持模式。
**系统能力:** SystemCapability.Multimedia.Camera.Core
| 名称 | 值 | 说明 |
|----------------------------|---------|---------------------------------------------|
| NORMAL_PHOTO | 1 | 普通拍照模式。详情见[PhotoSession](#photosession11) |
| NORMAL_VIDEO | 2 | 普通录像模式。详情见[VideoSession](#videosession11) |
| SECURE_PHOTO12+ | 12 | 安全相机模式。详情见[SecureSession](#securesession12) |
## CameraErrorCode
相机错误码。
接口使用不正确以及on接口监听error状态返回。
**系统能力:** SystemCapability.Multimedia.Camera.Core
| 名称 | 值 | 说明 |
| ------------------------- | ---- | ------------ |
| INVALID_ARGUMENT | 7400101 | 参数缺失或者参数类型不对。 |
| OPERATION_NOT_ALLOWED | 7400102 | 操作流程不对,不允许。 |
| SESSION_NOT_CONFIG | 7400103 | session 未配置返回。 |
| SESSION_NOT_RUNNING | 7400104 | session 未运行返回。 |
| SESSION_CONFIG_LOCKED | 7400105 | session 配置已锁定返回。 |
| DEVICE_SETTING_LOCKED | 7400106 | 设备设置已锁定返回。 |
| CONFLICT_CAMERA | 7400107 | 设备重复打开返回。 |
| DEVICE_DISABLED | 7400108 | 安全原因摄像头被禁用。 |
| DEVICE_PREEMPTED | 7400109 | 相机被抢占导致无法使用。 |
| UNRESOLVED_CONFLICTS_WITH_CURRENT_CONFIGURATIONS12+ | 7400110 | 与当前配置存在冲突。 |
| SERVICE_FATAL_ERROR | 7400201 | 相机服务错误返回。 |
## CameraManager
相机管理器类,使用前需要通过[getCameraManager](#cameragetcameramanager)获取相机管理实例。
### getSupportedCameras
getSupportedCameras(): Array\
获取支持指定的相机设备对象,同步返回结果。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| ----------------------------------------------- | ---------------------------- |
| Array\<[CameraDevice](#cameradevice)> | 相机设备列表。 |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function getSupportedCameras(cameraManager: camera.CameraManager): Array {
let cameras: Array = [];
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\
获取指定的相机设备对象支持的模式,同步返回结果。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------------ |--------------------------------------------------------------- | -- | -------------------------- |
| camera | [CameraDevice](#cameradevice) | 是 | 相机设备,通过 [getSupportedCameras](#getsupportedcameras) 接口获取。传参异常时,会返回错误码。 |
**返回值:**
| 类型 | 说明 |
| ----------------------------------------------- | ---------------------------- |
| Array\<[SceneMode](#scenemode11)> | 相机支持的模式列表。 |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function getSupportedSceneModes(cameraManager: camera.CameraManager, camera: camera.CameraDevice): Array {
let modes: Array = [];
try {
modes = cameraManager.getSupportedSceneModes(camera);
} catch (error) {
let err = error as BusinessError;
console.error(`The getSupportedSceneModes call failed. error code: ${err.code}`);
}
return modes;
}
```
### getSupportedOutputCapability(deprecated)
getSupportedOutputCapability(camera: CameraDevice): CameraOutputCapability
查询相机设备支持的输出能力,同步返回结果。
> **说明:**
> 从 API version 10开始支持,从API version 11开始废弃。建议使用[getSupportedOutputCapability](#getsupportedoutputcapability11)替代。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------------ |--------------------------------------------------------------- | -- | -------------------------- |
| camera | [CameraDevice](#cameradevice) | 是 | 相机设备,通过 [getSupportedCameras](#getsupportedcameras) 接口获取。传参异常时,会返回错误码。 |
**返回值:**
| 类型 | 说明 |
| ----------------------------------------------- | ---------------------------- |
| [CameraOutputCapability](#cameraoutputcapability) | 相机输出能力。 |
**示例:**
```ts
function getSupportedOutputCapability(camera: camera.CameraDevice, cameraManager: camera.CameraManager): camera.CameraOutputCapability {
let cameraOutputCapability: camera.CameraOutputCapability = cameraManager.getSupportedOutputCapability(camera);
return cameraOutputCapability;
}
```
### getSupportedOutputCapability11+
getSupportedOutputCapability(camera: CameraDevice, mode: SceneMode): CameraOutputCapability
查询相机设备在模式下支持的输出能力,同步返回结果。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------------ |--------------------------------------------------------------- | -- | -------------------------- |
| camera | [CameraDevice](#cameradevice) | 是 | 相机设备,通过 [getSupportedCameras](#getsupportedcameras) 接口获取。 |
| mode | [SceneMode](#scenemode11) | 是 | 相机模式,通过 [getSupportedSceneModes](#getsupportedscenemodes11) 接口获取。 |
**返回值:**
| 类型 | 说明 |
| ----------------------------------------------- | ---------------------------- |
| [CameraOutputCapability](#cameraoutputcapability) | 相机输出能力。 |
**示例:**
```ts
function getSupportedOutputCapability(camera: camera.CameraDevice, cameraManager: camera.CameraManager, sceneMode: camera.SceneMode): camera.CameraOutputCapability {
let cameraOutputCapability: camera.CameraOutputCapability = cameraManager.getSupportedOutputCapability(camera, sceneMode);
return cameraOutputCapability;
}
```
### isCameraMuted
isCameraMuted(): boolean
查询相机当前的禁用状态(禁用/未禁用)。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| ---------- | -------------------------------------------- |
| boolean | 返回true表示相机被禁用,返回false表示相机未被禁用。 |
**示例:**
```ts
function isCameraMuted(cameraManager: camera.CameraManager): boolean {
let isMuted: boolean = cameraManager.isCameraMuted();
return isMuted;
}
```
### createCameraInput
createCameraInput(camera: CameraDevice): CameraInput
使用CameraDevice对象创建CameraInput实例,同步返回结果。
**需要权限:** ohos.permission.CAMERA
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------------------- | ---- |--------------------------------------------------|
| camera | [CameraDevice](#cameradevice) | 是 | CameraDevice对象,通过 [getSupportedCameras](#getsupportedcameras) 接口获取。 |
**返回值:**
| 类型 | 说明 |
| ---------- | ----------------------------- |
| [CameraInput](#camerainput) | CameraInput实例。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400101 | Parameter missing or parameter type incorrect. |
| 7400102 | Operation not allowed. |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
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) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The createCameraInput call failed. error code: ${err.code}`);
}
return cameraInput;
}
```
### createCameraInput
createCameraInput(position: CameraPosition, type: CameraType): CameraInput
根据相机位置和类型创建CameraInput实例,同步返回结果。
**需要权限:** ohos.permission.CAMERA
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------------------- | ---- | --------------------------------- |
| position | [CameraPosition](#cameraposition) | 是 | 相机位置,通过 [getSupportedCameras](#getsupportedcameras) 接口获取设备,然后获取设备位置信息。 |
| type | [CameraType](#cameratype) | 是 | 相机类型,通过 [getSupportedCameras](#getsupportedcameras) 接口获取设备,然后获取设备类型信息。 |
**返回值:**
| 类型 | 说明 |
| ---------- | ----------------------------- |
| [CameraInput](#camerainput) | CameraInput实例。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400101 | Parameter missing or parameter type incorrect. |
| 7400102 | Operation not allowed. |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
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) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The createCameraInput call failed. error code: ${err.code}`);
}
return cameraInput;
}
```
### createPreviewOutput
createPreviewOutput(profile: Profile, surfaceId: string): PreviewOutput
创建预览输出对象,同步返回结果。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------------------------------- | ---- | ------------------------------- |
| profile | [Profile](#profile) | 是 | 支持的预览配置信息,通过[getSupportedOutputCapability](#getsupportedoutputcapability11)接口获取。|
| surfaceId| string | 是 | 从[XComponent](../apis-arkui/arkui-ts/ts-basic-components-xcomponent.md)或者[ImageReceiver](../apis-image-kit/js-apis-image.md#imagereceiver9)组件获取的surfaceId。|
**返回值:**
| 类型 | 说明 |
| ---------- | ----------------------------- |
| [PreviewOutput](#previewoutput) | PreviewOutput实例。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400101 | Parameter missing or parameter type incorrect. |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
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) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The createPreviewOutput call failed. error code: ${err.code}`);
}
return previewOutput;
}
```
### createPreviewOutput12+
createPreviewOutput(surfaceId: string): PreviewOutput
创建无配置信息的预览输出对象,同步返回结果。该接口需配合[preconfig](#preconfig12)一起使用。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------------------------------- | ---- | ------------------------------- |
| surfaceId| string | 是 | 从[XComponent](../apis-arkui/arkui-ts/ts-basic-components-xcomponent.md)或者[ImageReceiver](../apis-image-kit/js-apis-image.md#imagereceiver9)组件获取的surfaceId。|
**返回值:**
| 类型 | 说明 |
| ---------- | ----------------------------- |
| [PreviewOutput](#previewoutput) | PreviewOutput实例。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
|---------|------------------------------------------------|
| 7400101 | Parameter missing or parameter type incorrect. |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
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) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The createPreviewOutput call failed. error code: ${err.code}`);
}
return previewOutput;
}
```
### createPhotoOutput(deprecated)
createPhotoOutput(profile: Profile, surfaceId: string): PhotoOutput
创建拍照输出对象,同步返回结果。
> **说明:**
> 从 API version 10开始支持,从API version 11开始废弃。建议使用[createPhotoOutput](#createphotooutput11)替代。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------------------- | ---- | ----------------------------------- |
| profile | [Profile](#profile) | 是 | 支持的拍照配置信息,通过[getSupportedOutputCapability](#getsupportedoutputcapability11)接口获取。|
| surfaceId| string | 是 | 从[ImageReceiver](../apis-image-kit/js-apis-image.md#imagereceiver9)获取的surfaceId。|
**返回值:**
| 类型 | 说明 |
| ---------- | ----------------------------- |
| [PhotoOutput](#photooutput) | PhotoOutput实例。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400101 | Parameter missing or parameter type incorrect. |
```ts
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) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The createPhotoOutput call failed. error code: ${err.code}`);
}
return photoOutput;
}
```
### createPhotoOutput11+
createPhotoOutput(profile?: Profile): PhotoOutput
创建拍照输出对象,同步返回结果。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------------------- |----| ----------------------------------- |
| profile | [Profile](#profile) | 否 | 支持的拍照配置信息,通过[getSupportedOutputCapability](#getsupportedoutputcapability11)接口获取。
API 11时,该参数必填;从API version 12开始,如果使用[preconfig](#preconfig12)进行预配置,传入profile参数会覆盖preconfig的预配置参数。|
**返回值:**
| 类型 | 说明 |
| ---------- | ----------------------------- |
| [PhotoOutput](#photooutput) | PhotoOutput实例。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
|----------|------------------------------------------------|
| 7400101 | Parameter missing or parameter type incorrect. |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
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) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The createPhotoOutput call failed. error code: ${err.code}`);
}
return photoOutput;
}
```
### createVideoOutput
createVideoOutput(profile: VideoProfile, surfaceId: string): VideoOutput
创建录像输出对象,同步返回结果。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------------------- | ---- | ------------------------------ |
| profile | [VideoProfile](#videoprofile) | 是 | 支持的录像配置信息,通过[getSupportedOutputCapability](#getsupportedoutputcapability11)接口获取。 |
| surfaceId| string | 是 | 从[AVRecorder](../apis-media-kit/js-apis-media.md#avrecorder9)获取的surfaceId。|
**返回值:**
| 类型 | 说明 |
| ---------- | ----------------------------- |
| [VideoOutput](#videooutput) | VideoOutput实例。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400101 | Parameter missing or parameter type incorrect. |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
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) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The createVideoOutput call failed. error code: ${err.code}`);
}
return videoOutput;
}
```
### createVideoOutput12+
createVideoOutput(surfaceId: string): VideoOutput
创建无配置信息的录像输出对象,同步返回结果。该接口需配合[preconfig](#preconfig12-1)功能一起使用。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
|-----------|--------|-------|----------------------------------------------------------------------------|
| surfaceId | string | 是 | 从[AVRecorder](../apis-media-kit/js-apis-media.md#avrecorder9)获取的surfaceId。 |
**返回值:**
| 类型 | 说明 |
| ---------- | ----------------------------- |
| [VideoOutput](#videooutput) | VideoOutput实例。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
|----------|------------------------------------------------|
| 7400101 | Parameter missing or parameter type incorrect. |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
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) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The createVideoOutput call failed. error code: ${err.code}`);
}
return videoOutput;
}
```
### createMetadataOutput
createMetadataOutput(metadataObjectTypes: Array\): MetadataOutput
创建metadata流输出对象,同步返回结果。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------------------- | -------------------------------------------------- | --- | ---------------------------- |
| metadataObjectTypes | Array\<[MetadataObjectType](#metadataobjecttype)\> | 是 | metadata流类型信息,通过[getSupportedOutputCapability](#getsupportedoutputcapability11)接口获取。 |
**返回值:**
| 类型 | 说明 |
| ---------- | ----------------------------- |
| [MetadataOutput](#metadataoutput) | MetadataOutput实例。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400101 | Parameter missing or parameter type incorrect. |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function createMetadataOutput(cameraManager: camera.CameraManager, cameraOutputCapability: camera.CameraOutputCapability): void {
let metadataObjectTypes: Array = cameraOutputCapability.supportedMetadataObjectTypes;
let metadataOutput: camera.MetadataOutput | undefined = undefined;
try {
metadataOutput = cameraManager.createMetadataOutput(metadataObjectTypes);
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`createMetadataOutput error. error code: ${err.code}`);
}
}
```
### createCaptureSession(deprecated)
createCaptureSession(): CaptureSession
创建CaptureSession实例,同步返回结果。
> **说明:**
>从 API version 10开始支持,从API version 11开始废弃。建议使用[createSession](#createsession11)替代。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| ---------- | ----------------------------- |
| [CaptureSession](#capturesessiondeprecated) | CaptureSession实例。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
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) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`createCaptureSession error. error code: ${err.code}`);
}
return captureSession;
}
```
### createSession11+
createSession\(mode: SceneMode): T
创建指定SceneMode的Session实例,同步返回结果。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -----------------| ---- | --------- |
| mode | [SceneMode](#scenemode11) | 是 | 相机支持的模式。传参异常(如超出范围、传入null、未定义等),实际接口不会生效。 |
**返回值:**
| 类型 | 说明 |
| ---------- | ----------------------------- |
| [T extends Session](#session11) | Session实例。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- |------------------------------------------------------------------------------------------------------------------------------------------------|
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3.Parameter verification failed. |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
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) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`createCaptureSession error. error code: ${err.code}`);
}
return photoSession;
}
```
### on('cameraStatus')
on(type: 'cameraStatus', callback: AsyncCallback\): void
相机设备状态回调,通过注册回调函数获取相机的状态变化。使用callback异步回调。
> **说明:**
>
> 当前注册监听接口,不支持在on监听的回调方法里,调用off注销回调。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -----------------| ---- | --------- |
| type | string | 是 | 监听事件,固定为'cameraStatus'。cameraManager对象获取成功后可监听。目前只支持对设备打开或者关闭会触发该事件并返回对应信息。 |
| callback | AsyncCallback\<[CameraStatusInfo](#camerastatusinfo)\> | 是 | 回调函数,用于获取镜头状态变化信息。 | |
**示例:**
```ts
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\): void
相机设备状态注销回调,通过注销回调函数取消获取相机的状态变化。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -----------------| ---- | --------- |
| type | string | 是 | 监听事件,固定为'cameraStatus'。cameraManager对象获取成功后可监听。 |
| callback | AsyncCallback\<[CameraStatusInfo](#camerastatusinfo)\> | 否 | 回调函数,如果指定参数则取消对应callback(callback对象不可是匿名函数),否则取消所有callback。 |
**示例:**
```ts
function unregisterCameraStatus(cameraManager: camera.CameraManager): void {
cameraManager.off('cameraStatus');
}
```
### on('foldStatusChange')12+
on(type: 'foldStatusChange', callback: AsyncCallback\): void
开启折叠设备折叠状态变化的监听。使用callback异步回调。
> **说明:**
>
> 当前注册监听接口,不支持在on监听的回调方法里,调用off注销回调。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -----------------| ---- | --------- |
| type | string | 是 | 监听事件,固定为'foldStatusChange'。表示折叠设备折叠状态发生变化。 |
| callback | AsyncCallback\<[FoldStatusInfo](#foldstatusinfo12)\> | 是 | 回调函数。返回折叠设备折叠信息。 |
**示例:**
```ts
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\): void
关闭折叠设备折叠状态变化的监听。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -----------------| ---- | --------- |
| type | string | 是 | 监听事件,固定为'foldStatusChange'。表示折叠设备折叠状态发生变化。 |
| callback | AsyncCallback\<[FoldStatusInfo](#foldstatusinfo12)\> | 否 | 回调函数,返回折叠设备折叠信息。如果指定参数则取消对应callback(callback对象不可是匿名函数),否则取消所有callback。 |
**示例:**
```ts
function unregisterFoldStatusChange(cameraManager: camera.CameraManager): void {
cameraManager.off('foldStatusChange');
}
```
### isTorchSupported11+
isTorchSupported(): boolean
检测设备是否支持手电筒。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| ---------- | ----------------------------- |
| boolean | 返回true表示设备支持手电筒。 |
**示例:**
```ts
function isTorchSupported(cameraManager: camera.CameraManager): boolean {
let isSupported = cameraManager.isTorchSupported();
return isSupported;
}
```
### isTorchModeSupported11+
isTorchModeSupported(mode: TorchMode): boolean
检测是否支持设置的手电筒模式。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------- | ---- | --------- |
| mode | [TorchMode](#torchmode11) | 是 | 手电筒模式。传参为null或者undefined,作为0处理,手电筒关闭。 |
**返回值:**
| 类型 | 说明 |
| ---------- | ----------------------------- |
| boolean | 返回true表示设备支持设置的手电筒模式。 |
**示例:**
```ts
function isTorchModeSupported(cameraManager: camera.CameraManager, torchMode: camera.TorchMode): boolean {
let isSupported = cameraManager.isTorchModeSupported(torchMode);
return isSupported;
}
```
### getTorchMode11+
getTorchMode(): TorchMode
获取当前设备手电筒模式。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| ---------- | ----------------------------- |
| [TorchMode](#torchmode11) | 返回设备当前手电筒模式。 |
**示例:**
```ts
function getTorchMode(cameraManager: camera.CameraManager): camera.TorchMode | undefined {
let torchMode: camera.TorchMode | undefined = undefined;
torchMode = cameraManager.getTorchMode();
return torchMode;
}
```
### setTorchMode11+
setTorchMode(mode: TorchMode): void
设置设备手电筒模式。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------- | ---- | --------- |
| mode | [TorchMode](#torchmode11) | 是 | 手电筒模式。传参为null或者undefined,作为0处理,手电筒关闭。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400101 | Parameter missing or parameter type incorrect. |
| 7400102 | Operation not allowed. |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function setTorchMode(cameraManager: camera.CameraManager, torchMode: camera.TorchMode): void {
try {
cameraManager.setTorchMode(torchMode);
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The setTorchMode call failed. error code: ${err.code}`);
}
}
```
### on('torchStatusChange')11+
on(type: 'torchStatusChange', callback: AsyncCallback\): void
手电筒状态变化回调,通过注册回调函数获取手电筒状态变化。使用callback异步回调。
> **说明:**
>
> 当前注册监听接口,不支持在on监听的回调方法里,调用off注销回调。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------- | ---- | --------- |
| type | string | 是 | 监听事件,固定为'torchStatusChange'。cameraManager对象获取成功后可监听。目前只支持手电筒打开,手电筒关闭,手电筒不可用,手电筒恢复可用会触发该事件并返回对应信息。 |
| callback | AsyncCallback\ | 是 | 回调函数,用于获取手电筒状态变化信息。 |
**示例:**
```ts
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\): void
手电筒状态变化注销回调,通过注销回调函数取消获取手电筒状态变化。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------- | ---- | --------- |
| type | string | 是 | 监听事件,固定为'torchStatusChange'。cameraManager对象获取成功后可监听。 |
| callback | AsyncCallback\ | 否 | 回调函数,如果指定参数则取消对应callback(callback对象不可是匿名函数),否则取消所有callback。 |
**示例:**
```ts
function unregisterTorchStatusChange(cameraManager: camera.CameraManager): void {
cameraManager.off('torchStatusChange');
}
```
## TorchMode11+
枚举,手电筒模式。
**系统能力:** SystemCapability.Multimedia.Camera.Core
| 名称 | 值 | 说明 |
| ---------------------------- | ---- | ------------- |
| OFF | 0 | 常关模式。 |
| ON | 1 | 常开模式。 |
| AUTO | 2 | 自动模式。 |
## TorchStatusInfo11+
手电筒回调返回的接口实例,表示手电筒状态信息。
**系统能力:** SystemCapability.Multimedia.Camera.Core
| 名称 | 类型 | 只读 | 可选 | 说明 |
| ---------------- | ---------- | ---- | ---- | ----------- |
| isTorchAvailable | boolean | 是 | 否 | 手电筒是否可用。|
| isTorchActive | boolean | 是 | 否 | 手电筒是否被激活。 |
| torchLevel | number | 是 | 否 | 手电筒亮度等级。取值范围为[0,1],越靠近1,亮度越大。 |
## Size
输出能力查询。
**系统能力:** SystemCapability.Multimedia.Camera.Core
| 名称 | 类型 | 只读 | 可选 | 说明 |
| ------ | ------ | ---- |-----| ------------ |
| height | number | 否 | 否 | 图像尺寸高(像素)。 |
| width | number | 否 | 否 | 图像尺寸宽(像素)。 |
## Point
点坐标用于对焦、曝光配置。
**系统能力:** SystemCapability.Multimedia.Camera.Core
| 名称 | 类型 | 只读 | 可选 | 说明 |
| ------ | ------ | ------ | ------ | ------------ |
| x | number | 否 | 否 | 点的x坐标。 |
| y | number | 否 | 否 | 点的y坐标。 |
## CameraFormat
枚举,输出格式。
**系统能力:** SystemCapability.Multimedia.Camera.Core
| 名称 | 值 | 说明 |
| ----------------------- | --------- | ------------ |
| CAMERA_FORMAT_RGBA_8888 | 3 | RGBA_888格式的图片。 |
| CAMERA_FORMAT_YUV_420_SP| 1003 | YUV_420_SP格式的图片。 |
| CAMERA_FORMAT_JPEG | 2000 | JPEG格式的图片。 |
| CAMERA_FORMAT_YCBCR_P01011+ | 2001 | YCBCR_P010格式的图片。 |
| CAMERA_FORMAT_YCRCB_P01011+ | 2002 | YCRCB_P010格式的图片。 |
| CAMERA_FORMAT_HEIC13+ | 2003 | HEIF格式的图片。 |
## VideoCodecType13+
枚举,视频编码类型。
**系统能力:** SystemCapability.Multimedia.Camera.Core
| 名称 | 值 | 说明 |
|------|------|-------------|
| AVC | 0 | 视频编码类型AVC。 |
| HEVC | 1 | 视频编码类型HEVC。 |
## CameraInput
相机设备输入对象。
会话中[Session](#session11)使用的相机信息。
### open
open(callback: AsyncCallback\): void
打开相机,通过注册回调函数获取状态。使用callback异步回调。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------------- | ---- | ------------------- |
| callback | AsyncCallback\ | 是 | 回调函数,用于获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400107 | Can not use camera cause of conflict. |
| 7400108 | Camera disabled cause of security reason. |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function openCameraInput(cameraInput: camera.CameraInput): void {
cameraInput.open((err: BusinessError) => {
if (err) {
console.error(`Failed to open the camera, error code: ${err.code}.`);
return;
}
console.info('Callback returned with camera opened.');
});
}
```
### open
open(): Promise\
打开相机,通过Promise获取相机的状态。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| -------------- | ----------------------- |
| Promise\ | 无返回结果的Promise对象。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
|---------|-------------------------------------------|
| 7400102 | Operation not allowed. |
| 7400107 | Can not use camera cause of conflict. |
| 7400108 | Camera disabled cause of security reason. |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function openCameraInput(cameraInput: camera.CameraInput): void {
cameraInput.open().then(() => {
console.info('Promise returned with camera opened.');
}).catch((error: BusinessError) => {
console.error(`Failed to open the camera, error code: ${error.code}.`);
});
}
```
### open12+
open(isSecureEnabled: boolean): Promise\
打开相机,获取安全相机的句柄。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------------- | ---- |-------------------------------------------------------------------------|
| isSecureEnabled | boolean | 是 | 是否使能以安全的方式打开相机。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**返回值:**
| 类型 | 说明 |
| -------------- | ----------------------- |
| Promise\ | 使用Promise的方式获取打开相机句柄。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400107 | Can not use camera cause of conflict. |
| 7400108 | Camera disabled cause of security reason. |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function openCameraInput(cameraInput: camera.CameraInput): void {
cameraInput.open(true).then(() => {
console.info('Promise returned with camera opened.');
}).catch((error: BusinessError) => {
console.error(`Failed to open the camera, error code: ${error.code}.`);
});
}
```
### close
close(callback: AsyncCallback\\): void
关闭相机,通过注册回调函数获取状态。使用callback异步回调。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------------- | ---- | -------------------- |
| callback | AsyncCallback\ | 是 | 回调函数,用于获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function closeCameraInput(cameraInput: camera.CameraInput): void {
cameraInput.close((err: BusinessError) => {
if (err) {
console.error(`Failed to close the cameras, error code: ${err.code}.`);
return;
}
console.info('Callback returned with camera closed.');
});
}
```
### close
close(): Promise\
关闭相机,通过Promise获取状态。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| -------------- | ----------------------- |
| Promise\ | 无返回结果的Promise对象。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function closeCameraInput(cameraInput: camera.CameraInput): void {
cameraInput.close().then(() => {
console.info('Promise returned with camera closed.');
}).catch((error: BusinessError) => {
console.error(`Failed to close the cameras, error code: ${error.code}.`);
});
}
```
### on('error')
on(type: 'error', camera: CameraDevice, callback: ErrorCallback): void
监听CameraInput的错误事件,通过注册回调函数获取结果。使用callback异步回调。
> **说明:**
>
> 当前注册监听接口,不支持在on监听的回调方法里,调用off注销回调。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------------------------- | --- | ------------------------------------------- |
| type | string | 是 | 监听事件,固定为'error',CameraInput对象创建成功可监听。相机设备出错情况下可触发该事件并返回结果,比如设备不可用或者冲突等返回对应错误信息。 |
| camera | [CameraDevice](#cameradevice) | 是 | CameraDevice对象。 |
| callback | [ErrorCallback](../apis-basic-services-kit/js-apis-base.md#errorcallback) | 是 | 回调函数,用于获取结果。返回错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function callback(err: BusinessError): void {
console.error(`Camera input error code: ${err.code}`);
}
function registerCameraInputError(cameraInput: camera.CameraInput, camera: camera.CameraDevice): void {
cameraInput.on('error', camera, callback);
}
```
### off('error')
off(type: 'error', camera: CameraDevice, callback?: ErrorCallback): void
注销监听CameraInput的错误事件。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------------------------- | --- | ------------------------------------------- |
| type | string | 是 | 监听事件,固定为'error',CameraInput对象创建成功可监听。相机设备出错情况下可触发该事件并返回结果,比如设备不可用或者冲突等返回对应错误信息。 |
| camera | [CameraDevice](#cameradevice) | 是 | CameraDevice对象。 |
| callback | [ErrorCallback](../apis-basic-services-kit/js-apis-base.md#errorcallback) | 否 | 回调函数,如果指定参数则取消对应callback(callback对象不可是匿名函数),否则取消所有callback。 |
**示例:**
```ts
function unregisterCameraInputError(cameraInput: camera.CameraInput, camera: camera.CameraDevice): void {
cameraInput.off('error', camera);
}
```
## CameraOutput
会话中[Session](#session11)使用的输出信息,output的基类。
### release
release(callback: AsyncCallback\): void
释放输出资源,通过注册回调函数获取结果。使用callback异步回调。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------------- | ---- | ------------------- |
| callback | AsyncCallback\ | 是 | 回调函数,用于获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function releasePreviewOutput(previewOutput: camera.PreviewOutput): void {
previewOutput.release((err: BusinessError) => {
if (err) {
console.error(`Failed to release the Preview output instance ${err.code}`);
return;
}
console.info('Callback invoked to indicate that the preview output instance is released successfully.');
});
}
function releaseVideoOutput(videoOutput: camera.VideoOutput): void {
videoOutput.release((err: BusinessError) => {
if (err) {
console.error(`Failed to release the video output instance ${err.code}`);
return;
}
console.info('Callback invoked to indicate that the video output instance is released successfully.');
});
}
```
### release
release(): Promise\
释放输出资源,通过Promise获取结果。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| -------------- | ----------------------- |
| Promise\ | 无返回结果的Promise对象。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function releasePreviewOutput(previewOutput: camera.PreviewOutput): void {
previewOutput.release().then(() => {
console.info('Promise returned to indicate that the preview output instance is released successfully.');
}).catch((error: BusinessError) => {
console.error(`Failed to preview output release, error code: ${error.code}`);
});
}
function releaseVideoOutput(videoOutput: camera.VideoOutput): void {
videoOutput.release().then(() => {
console.info('Promise returned to indicate that the video output instance is released successfully.');
}).catch((error: BusinessError) => {
console.error(`Failed to video output release, error code: ${error.code}`);
});
}
```
## PreviewOutput
预览输出类。继承[CameraOutput](#cameraoutput)。
### start(deprecated)
start(callback: AsyncCallback\): void
开始输出预览流,通过注册回调函数获取结果。使用callback异步回调。
> **说明:**
>从 API version 10开始支持,从API version 11开始废弃。建议使用[Session.start](#start11)替代。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------------- | ---- | -------------------- |
| callback | AsyncCallback\ | 是 | 回调函数,用于获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function startPreviewOutput(previewOutput: camera.PreviewOutput): void {
previewOutput.start((err: BusinessError) => {
if (err) {
console.error(`Failed to start the preview output, error code: ${err.code}.`);
return;
}
console.info('Callback returned with preview output started.');
});
}
```
### start(deprecated)
start(): Promise\
开始输出预览流,通过Promise获取结果。
> **说明:**
>从 API version 10开始支持,从API version 11开始废弃。建议使用[Session.start](#start11-1)替代。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| -------------- |-------------------|
| Promise\ | 无返回结果的Promise对象。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function startPreviewOutput(previewOutput: camera.PreviewOutput): void {
previewOutput.start().then(() => {
console.info('Promise returned with preview output started.');
}).catch((error: BusinessError) => {
console.error(`Failed to preview output start, error code: ${error.code}.`);
});
}
```
### stop(deprecated)
stop(callback: AsyncCallback\): void
停止输出预览流,通过注册回调函数获取结果。使用callback异步回调。
> **说明:**
>从 API version 10开始支持,从API version 11开始废弃。建议使用[Session.stop](#stop11)替代。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------------- | ---- | -------------------- |
| callback | AsyncCallback\ | 是 | 回调函数,用于获取结果。 |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function stopPreviewOutput(previewOutput: camera.PreviewOutput): void {
previewOutput.stop((err: BusinessError) => {
if (err) {
console.error(`Failed to stop the preview output, error code: ${err.code}.`);
return;
}
console.info('Returned with preview output stopped.');
})
}
```
### stop(deprecated)
stop(): Promise\
停止输出预览流,通过Promise获取结果。
> **说明:**
>从 API version 10开始支持,从API version 11开始废弃。建议使用[Session.stop](#stop11-1)替代。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| -------------- | ------------------------ |
| Promise\ | 无返回结果的Promise对象。 |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function stopPreviewOutput(previewOutput: camera.PreviewOutput): void {
previewOutput.stop().then(() => {
console.info('Callback returned with preview output stopped.');
}).catch((error: BusinessError) => {
console.error(`Failed to preview output stop, error code: ${error.code}.`);
});
}
```
### on('frameStart')
on(type: 'frameStart', callback: AsyncCallback\): void
监听预览帧启动,通过注册回调函数获取结果。使用callback异步回调。
> **说明:**
>
> 当前注册监听接口,不支持在on监听的回调方法里,调用off注销回调。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------------- | ---- | --------------------------------------- |
| type | string | 是 | 监听事件,固定为'frameStart',previewOutput创建成功可监听。底层第一次开始曝光时触发该事件并返回。 |
| callback | AsyncCallback\ | 是 | 回调函数,用于获取结果。只要有该事件返回就证明预览开始。 |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function callback(err: BusinessError): void {
if (err !== undefined && err.code !== 0) {
console.error(`Callback Error, errorCode: ${err.code}`);
return;
}
console.info('Preview frame started');
}
function registerPreviewOutputFrameStart(previewOutput: camera.PreviewOutput): void {
previewOutput.on('frameStart', callback);
}
```
### off('frameStart')
off(type: 'frameStart', callback?: AsyncCallback\): void
注销监听预览帧启动。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------------- | ---- | --------------------------------------- |
| type | string | 是 | 监听事件,固定为'frameStart',previewOutput创建成功可监听。 |
| callback | AsyncCallback\ | 否 | 回调函数,如果指定参数则取消对应callback(callback对象不可是匿名函数),否则取消所有callback。 |
**示例:**
```ts
function unregisterPreviewOutputFrameStart(previewOutput: camera.PreviewOutput): void {
previewOutput.off('frameStart');
}
```
### on('frameEnd')
on(type: 'frameEnd', callback: AsyncCallback\): void
监听预览帧结束,通过注册回调函数获取结果。使用callback异步回调。
> **说明:**
>
> 当前注册监听接口,不支持在on监听的回调方法里,调用off注销回调。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------------- | ---- | ------------------------------------- |
| type | string | 是 | 监听事件,固定为'frameEnd',previewOutput创建成功可监听。预览完全结束最后一帧时触发该事件并返回。 |
| callback | AsyncCallback\ | 是 | 回调函数,用于获取结果。只要有该事件返回就证明预览结束。 |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function callback(err: BusinessError): void {
if (err !== undefined && err.code !== 0) {
console.error(`Callback Error, errorCode: ${err.code}`);
return;
}
console.info('Preview frame ended');
}
function registerPreviewOutputFrameEnd(previewOutput: camera.PreviewOutput): void {
previewOutput.on('frameEnd', callback);
}
```
### off('frameEnd')
off(type: 'frameEnd', callback?: AsyncCallback\): void
注销监听预览帧结束。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------------- | ---- | ------------------------------------- |
| type | string | 是 | 监听事件,固定为'frameEnd',previewOutput创建成功可监听。 |
| callback | AsyncCallback\ | 否 | 回调函数,如果指定参数则取消对应callback(callback对象不可是匿名函数),否则取消所有callback。 |
**示例:**
```ts
function unregisterPreviewOutputFrameEnd(previewOutput: camera.PreviewOutput): void {
previewOutput.off('frameEnd');
}
```
### on('error')
on(type: 'error', callback: ErrorCallback): void
监听预览输出的错误事件,通过注册回调函数获取结果。使用callback异步回调。
> **说明:**
>
> 当前注册监听接口,不支持在on监听的回调方法里,调用off注销回调。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------| ---- | ------------------------ |
| type | string | 是 | 监听事件,固定为'error',previewOutput创建成功可监听。预览接口使用错误时触发该事件,比如调用[Session.start](#start11-1),[CameraOutput.release](#release-1)等接口发生错误时返回对应错误信息。 |
| callback | [ErrorCallback](../apis-basic-services-kit/js-apis-base.md#errorcallback) | 是 | 回调函数,用于获取错误信息。返回错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function callback(previewOutputError: BusinessError): void {
console.error(`Preview output error code: ${previewOutputError.code}`);
}
function registerPreviewOutputError(previewOutput: camera.PreviewOutput): void {
previewOutput.on('error', callback)
}
```
### off('error')
off(type: 'error', callback?: ErrorCallback): void
注销监听预览输出的错误事件。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------| ---- | ------------------------ |
| type | string | 是 | 监听事件,固定为'error',previewOutput创建成功可监听。 |
| callback | [ErrorCallback](../apis-basic-services-kit/js-apis-base.md#errorcallback) | 否 | 回调函数,如果指定参数则取消对应callback(callback对象不可是匿名函数),否则取消所有callback。 |
**示例:**
```ts
function unregisterPreviewOutputError(previewOutput: camera.PreviewOutput): void {
previewOutput.off('error');
}
```
### getSupportedFrameRates12+
getSupportedFrameRates(): Array\
查询支持的帧率范围。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| ------------- | ------------ |
| Array<[FrameRateRange](#frameraterange)> | 支持的帧率范围列表 |
**示例:**
```ts
function getSupportedFrameRates(previewOutput: camera.PreviewOutput): Array {
let supportedFrameRatesArray: Array = previewOutput.getSupportedFrameRates();
return supportedFrameRatesArray;
}
```
### setFrameRate12+
setFrameRate(minFps: number, maxFps: number): void
设置预览流帧率范围,设置的范围必须在支持的帧率范围内。
进行设置前,可通过[getSupportedFrameRates](#getsupportedframerates12)查询支持的帧率范围。
> **说明:**
> 仅在[PhotoSession](#photosession11)或[VideoSession](#videosession11)模式下支持。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------| ---- | ------------------------ |
| minFps | number | 是 | 最小帧率。 |
| maxFps | number | 是 | 最大帧率,当传入的最小值大于最大值时,传参异常,接口不生效。|
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400101 | Parameter missing or parameter type incorrect. |
| 7400110 | Unresolved conflicts with current configurations. |
**示例:**
```ts
function setFrameRateRange(previewOutput: camera.PreviewOutput, frameRateRange: Array): void {
previewOutput.setFrameRate(frameRateRange[0], frameRateRange[1]);
}
```
### getActiveFrameRate12+
getActiveFrameRate(): FrameRateRange
获取已设置的帧率范围。
使用[setFrameRate](#setframerate12)对预览流设置过帧率后可查询。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| ------------- | ------------ |
| [FrameRateRange](#frameraterange) | 帧率范围 |
**示例:**
```ts
function getActiveFrameRate(previewOutput: camera.PreviewOutput): camera.FrameRateRange {
let activeFrameRate: camera.FrameRateRange = previewOutput.getActiveFrameRate();
return activeFrameRate;
}
```
### getActiveProfile12+
getActiveProfile(): Profile
获取当前生效的配置信息。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| ------------- |-----------|
| [Profile](#profile) | 当前生效的配置信息 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
|---------|------------------------------|
| 7400201 | Camera service fatal error. |
**示例:**
```ts
function testGetActiveProfile(previewOutput: camera.PreviewOutput): camera.Profile | undefined {
let activeProfile: camera.Profile | undefined = undefined;
try {
activeProfile = previewOutput.getActiveProfile();
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The previewOutput.getActiveProfile call failed. error code: ${err.code}`);
}
return activeProfile;
}
```
### getPreviewRotation12+
getPreviewRotation(displayRotation: number): ImageRotation
获取预览旋转角度。
- 设备自然方向:设备默认使用方向,手机为竖屏(充电口向下)。
- 相机镜头角度:值等于相机图像顺时针旋转到设备自然方向的角度,手机后置相机传感器是横屏安装的,所以需要顺时针旋转90度到设备自然方向。
- 屏幕显示方向:需要屏幕显示的图片左上角为第一个像素点为坐标原点。锁屏时与自然方向一致。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------| ---- | ------------------------ |
| displayRotation | number | 是 | 显示设备的屏幕旋转角度,通过[display.getDefaultDisplaySync](../apis-arkui/js-apis-display.md#displaygetdefaultdisplaysync9)获得。 |
**返回值:**
| 类型 | 说明 |
| ------------- |-----------|
| [ImageRotation](#imagerotation) | 获取预览旋转角度。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
|---------|------------------------------|
| 7400101 | Parameter missing or parameter type incorrect. |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
function testGetPreviewRotation(previewOutput: camera.PreviewOutput, imageRotation : camera.ImageRotation): camera.ImageRotation {
let previewRotation: camera.ImageRotation = camera.ImageRotation.ROTATION_0;
try {
previewRotation = previewOutput.getPreviewRotation(imageRotation);
console.log(`Preview rotation is: ${previewRotation}`);
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The previewOutput.getPreviewRotation call failed. error code: ${err.code}`);
}
return previewRotation;
}
```
### setPreviewRotation12+
setPreviewRotation(previewRotation: ImageRotation, isDisplayLocked?: boolean): void
设置预览旋转角度。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------| ---- | ------------------------ |
| previewRotation | [ImageRotation](#imagerotation) | 是 | 预览旋转角度 |
| isDisplayLocked | boolean | 否 | 是否旋转锁定 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
|---------|------------------------------|
| 7400101 | Parameter missing or parameter type incorrect. |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
function testSetPreviewRotation(previewOutput: camera.PreviewOutput, previewRotation : camera.ImageRotation, isDisplayLocked: boolean): void {
try {
previewOutput.setPreviewRotation(previewRotation, isDisplayLocked);
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The previewOutput.setPreviewRotation call failed. error code: ${err.code}`);
}
return;
}
```
## ImageRotation
枚举,图片旋转角度。
**系统能力:** SystemCapability.Multimedia.Camera.Core
| 名称 | 值 | 说明 |
| ------------ | ---- | ------------- |
| ROTATION_0 | 0 | 图片旋转0度。 |
| ROTATION_90 | 90 | 图片旋转90度。 |
| ROTATION_180 | 180 | 图片旋转180度。 |
| ROTATION_270 | 270 | 图片旋转270度。 |
## Location
图片地理位置信息。
**系统能力:** SystemCapability.Multimedia.Camera.Core
| 名称 | 类型 | 只读 | 可选 |说明 |
| ------------ | ------ | ---- |-----|------------ |
| latitude | number | 否 | 否 |纬度(度)。 |
| longitude | number | 否 | 否 |经度(度)。 |
| altitude | number | 否 | 否 |海拔(米)。 |
## QualityLevel
枚举,图片质量。
**系统能力:** SystemCapability.Multimedia.Camera.Core
| 名称 | 值 | 说明 |
| -------------------- | ---- | ------------ |
| QUALITY_LEVEL_HIGH | 0 | 图片质量高。 |
| QUALITY_LEVEL_MEDIUM | 1 | 图片质量中等。 |
| QUALITY_LEVEL_LOW | 2 | 图片质量差。 |
## PhotoCaptureSetting
拍摄照片的设置。
**系统能力:** SystemCapability.Multimedia.Camera.Core
| 名称 | 类型 | 只读 | 可选 | 说明 |
| -------- | ------------------------------- | ---- | ---- |----------------------------------------------------------------------|
| quality | [QualityLevel](#qualitylevel) | 否 | 是 | 图片质量(默认低)。 |
| rotation | [ImageRotation](#imagerotation) | 否 | 是 | 图片旋转角度(默认0度,顺时针旋转)。 |
| location | [Location](#location) | 否 | 是 | 图片地理位置信息(默认以设备硬件信息为准)。 |
| mirror | boolean | 否 | 是 | 镜像使能开关(默认关)。使用之前需要使用[isMirrorSupported](#ismirrorsupported)进行判断是否支持。 |
## Photo11+
全质量图对象。
### 属性
**系统能力:** SystemCapability.Multimedia.Camera.Core
| 名称 | 类型 | 必填 | 说明 |
| ------ | ----------------------------- | -------------- | ---------- |
| main11+ | [image.Image](../apis-image-kit/js-apis-image.md#image9) | 是 | 全质量图Image。 |
### release11+
release(): Promise\
释放输出资源,通过Promise获取结果。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| -------------- | ----------------------- |
| Promise\ | 无返回结果的Promise对象。 |
**示例:**
```ts
async function releasePhoto(photo: camera.Photo): Promise {
await photo.release();
}
```
## PhotoOutput
拍照会话中使用的输出信息,继承[CameraOutput](#cameraoutput)。
### capture
capture(callback: AsyncCallback\): void
以默认设置触发一次拍照,通过注册回调函数获取结果。使用callback异步回调。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------------- | ---- | ------------------- |
| callback | AsyncCallback\ | 是 | 回调函数,用于获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400104 | Session not running. |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function capture(photoOutput: camera.PhotoOutput): void {
photoOutput.capture((err: BusinessError) => {
if (err) {
console.error(`Failed to capture the photo, error code: ${err.code}.`);
return;
}
console.info('Callback invoked to indicate the photo capture request success.');
});
}
```
### capture
capture(): Promise\
以默认设置触发一次拍照,通过Promise获取结果。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| -------------- | ------------------------ |
| Promise\ | 无返回结果的Promise对象。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400104 | Session not running. |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function capture(photoOutput: camera.PhotoOutput): void {
photoOutput.capture().then(() => {
console.info('Promise returned to indicate that photo capture request success.');
}).catch((error: BusinessError) => {
console.error(`Failed to photo output capture, error code: ${error.code}.`);
});
}
```
### capture
capture(setting: PhotoCaptureSetting, callback: AsyncCallback\): void
以指定参数触发一次拍照,通过注册回调函数获取结果。使用callback异步回调。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------------------- | ---- | -------------------- |
| setting | [PhotoCaptureSetting](#photocapturesetting) | 是 | 拍照设置。 |
| callback | AsyncCallback\ | 是 | 回调函数,用于获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400101 | Parameter missing or parameter type incorrect. |
| 7400104 | Session not running. |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
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, error code: ${err.code}.`);
return;
}
console.info('Callback invoked to indicate the photo capture request success.');
});
}
```
### capture
capture(setting: PhotoCaptureSetting): Promise\
以指定参数触发一次拍照,通过Promise获取结果。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------------------------------------------- | ---- | -------- |
| setting | [PhotoCaptureSetting](#photocapturesetting) | 是 | 拍照设置,传入undefined类型数据按默认无参处理。 |
**返回值:**
| 类型 | 说明 |
| -------------- | ------------------------ |
| Promise\ | 无返回结果的Promise对象。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400101 | Parameter missing or parameter type incorrect. |
| 7400104 | Session not running. |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
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.info('Promise returned to indicate that photo capture request success.');
}).catch((error: BusinessError) => {
console.error(`Failed to photo output capture, error code: ${error.code}.`);
});
}
```
### on('photoAvailable')11+
on(type: 'photoAvailable', callback: AsyncCallback\): void
注册监听全质量图上报。使用callback异步回调。
> **说明:**
>
> 当前注册监听接口,不支持在on监听的回调方法里,调用off注销回调。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------- | --- | ------------------------------------ |
| type | string | 是 | 监听事件,固定为'photoAvailable',photoOutput创建成功后可监听。 |
| callback | AsyncCallback\<[Photo](#photo11)\> | 是 | 回调函数,用于监听全质量图上报。 |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
import { image } from '@kit.ImageKit';
function callback(err: BusinessError, photo: camera.Photo): void {
if (err !== undefined && err.code !== 0) {
console.error(`Callback Error, errorCode: ${err.code}`);
return;
}
let mainImage: image.Image = photo.main;
}
function registerPhotoOutputPhotoAvailable(photoOutput: camera.PhotoOutput): void {
photoOutput.on('photoAvailable', callback);
}
```
### off('photoAvailable')11+
off(type: 'photoAvailable', callback?: AsyncCallback\): void
注销监听全质量图上报。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ------------------------------------------ |
| type | string | 是 | 监听事件,固定为'photoAvailable',photoOutput创建成功后可监听。 |
| callback | AsyncCallback\<[Photo](#photo11)\> | 否 | 回调函数,如果指定参数则取消对应callback(callback对象不可是匿名函数),否则取消所有callback。 |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
import { image } from '@kit.ImageKit';
function callback(err: BusinessError, photo: camera.Photo): void {
if (err !== undefined && err.code !== 0) {
console.error(`Callback Error, errorCode: ${err.code}`);
return;
}
let mainImage: image.Image = photo.main;
}
function unRegisterPhotoOutputPhotoAvailable(photoOutput: camera.PhotoOutput): void {
photoOutput.off('photoAvailable', callback);
}
```
### on('captureStartWithInfo')11+
on(type: 'captureStartWithInfo', callback: AsyncCallback\): void
监听拍照开始,通过注册回调函数获取[CaptureStartInfo](#capturestartinfo11)。使用callback异步回调。
> **说明:**
>
> 当前注册监听接口,不支持在on监听的回调方法里,调用off注销回调。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------- | --- | ------------------------------------ |
| type | string | 是 | 监听事件,固定为'captureStartWithInfo',photoOutput创建成功后可监听。 |
| callback | AsyncCallback\<[CaptureStartInfo](#capturestartinfo11)\> | 是 | 使用callback的方式获取Capture ID。|
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function callback(err: BusinessError, captureStartInfo: camera.CaptureStartInfo): void {
if (err !== undefined && err.code !== 0) {
console.error(`Callback Error, errorCode: ${err.code}`);
return;
}
console.info(`photo capture started, captureStartInfo : ${captureStartInfo}`);
}
function registerCaptureStartWithInfo(photoOutput: camera.PhotoOutput): void {
photoOutput.on('captureStartWithInfo', callback);
}
```
### off('captureStartWithInfo')11+
off(type: 'captureStartWithInfo', callback?: AsyncCallback\): void
注销监听拍照。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ------------------------------------------ |
| type | string | 是 | 监听事件,固定为'captureStartWithInfo',photoOutput创建成功后可监听。 |
| callback | AsyncCallback\<[CaptureStartInfo](#capturestartinfo11)\> | 否 | 回调函数,如果指定参数则取消对应callback(callback对象不可是匿名函数),否则取消所有callback。 |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function unRegisterCaptureStartWithInfo(photoOutput: camera.PhotoOutput): void {
photoOutput.off('captureStartWithInfo');
}
```
### isMovingPhotoSupported12+
isMovingPhotoSupported(): boolean
查询是否支持动态照片拍摄。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| -------------- | ----------------------- |
| boolean | 返回是否支持动态照片拍照,true表示支持,false表示不支持。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| -------------- | --------------- |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function isMovingPhotoSupported(photoOutput: camera.PhotoOutput): boolean {
let isSupported: boolean = false;
try {
isSupported = photoOutput.isMovingPhotoSupported();
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The isMovingPhotoSupported call failed. error code: ${err.code}`);
}
return isSupported;
}
```
### enableMovingPhoto12+
enableMovingPhoto(enabled: boolean): void
使能动态照片拍照。
**需要权限:** ohos.permission.MICROPHONE
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ------------------------------------------ |
| enabled | boolean | 是 | true为开启动态照片,false为关闭动态照片。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| -------- |------------------------------------------------|
| 201 | permission denied. |
| 7400101 | Parameter missing or parameter type incorrect. |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function enableMovingPhoto(photoOutput: camera.PhotoOutput): void {
try {
photoOutput.enableMovingPhoto(true);
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The enableMovingPhoto call failed. error code: ${err.code}`);
}
}
```
### on('photoAssetAvailable')12+
on(type: 'photoAssetAvailable', callback: AsyncCallback\): void
注册监听photoAsset上报。使用callback异步回调。
> **说明:**
>
> 当前注册监听接口,不支持在on监听的回调方法里,调用off注销回调。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------- | --- | ------------------------------------ |
| type | string | 是 | 监听事件,固定为'photoAssetAvailable',photoOutput创建成功后可监听。 |
| callback | AsyncCallback\<[photoAccessHelper.PhotoAsset](../apis-media-library-kit/js-apis-photoAccessHelper.md#photoasset)\> | 是 | 回调函数,用于监听photoAsset上报。 |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
import { photoAccessHelper } from '@kit.MediaLibraryKit';
function photoAssetAvailableCallback(err: BusinessError, photoAsset: photoAccessHelper.PhotoAsset): void {
if (err) {
console.info(`photoAssetAvailable error: ${JSON.stringify(err)}.`);
return;
}
console.info('photoOutPutCallBack photoAssetAvailable');
// 开发者可通过photoAsset获取图片相关信息
}
function onPhotoOutputPhotoAssetAvailable(photoOutput: camera.PhotoOutput): void {
photoOutput.on('photoAssetAvailable', photoAssetAvailableCallback);
}
```
### off('photoAssetAvailable')12+
off(type: 'photoAssetAvailable', callback?: AsyncCallback\): void
解注册photoAsset上报。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------- |-----|----------------------------------------------------------------------------|
| type | string | 是 | 监听事件,固定为'photoAssetAvailable',photoOutput创建成功后可监听。 |
| callback | AsyncCallback\<[photoAccessHelper.PhotoAsset](../apis-media-library-kit/js-apis-photoAccessHelper.md#photoasset)\> | 否 | 需要解监听的回调方法。如果callback不为空且与此对应的监听方法一致,不为匿名方法,则解注册该方法;如果callback为空,则解监听所有回调。 |
**示例:**
```ts
function offPhotoOutputPhotoAssetAvailable(photoOutput: camera.PhotoOutput): void {
photoOutput.off('photoAssetAvailable');
}
```
### isMirrorSupported
isMirrorSupported(): boolean
查询是否支持镜像拍照。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| -------------- | ----------------------- |
| boolean | 返回是否支持镜像拍照,true表示支持,false表示不支持。 |
**示例:**
```ts
function isMirrorSupported(photoOutput: camera.PhotoOutput): boolean {
let isSupported: boolean = photoOutput.isMirrorSupported();
return isSupported;
}
```
### enableMirror13+
enableMirror(enabled: boolean): void
是否启用镜像拍照。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
|----------| ---------------------- | ---- |---------------------------|
| enabled | boolean | 是 | true为开启镜像拍照,false为关闭镜像拍照。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| -------- |------------------------------------------------|
| 7400101 | Parameter missing or parameter type incorrect. |
| 7400103 | Session not config. |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function enableMirror(photoOutput: camera.PhotoOutput): void {
try {
photoOutput.enableMirror(true);
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The enableMirror call failed. error code: ${err.code}`);
}
}
```
### getSupportedMovingPhotoVideoCodecTypes13+
getSupportedMovingPhotoVideoCodecTypes(): Array\
查询支持的动态照片短视频编码类型。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| -------------- |-------------------|
| Array\<[VideoCodecType](#videocodectype13)\> | 支持的动态照片短视频编码类型列表。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
function getSupportedMovingPhotoVideoCodecType(photoOutput: camera.PhotoOutput): Array {
let supportedVideoCodecTypesArray: Array = photoOutput.getSupportedMovingPhotoVideoCodecTypes();
return supportedVideoCodecTypesArray;
}
```
### setMovingPhotoVideoCodecType13+
setMovingPhotoVideoCodecType(codecType: VideoCodecType): void
设置动态照片短视频编码类型。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------------- |-------------------------------------|-------| ------------ |
| codecType | [VideoCodecType](#videocodectype13) | 是 |获取动态照片短视频编码类型 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
function setMovingPhotoVideoCodecTypes(photoOutput: camera.PhotoOutput, videoCodecType: camera.VideoCodecType): void {
photoOutput.setMovingPhotoVideoCodecType(videoCodecType);
}
```
### on('captureStart')(deprecated)
on(type: 'captureStart', callback: AsyncCallback\): void
监听拍照开始,通过注册回调函数获取Capture ID。使用callback异步回调。
> **说明:**
> 从 API version 10开始支持,从API version 11开始废弃。建议使用[on('captureStartWithInfo')](#oncapturestartwithinfo11)替代。
>
> 当前注册监听接口,不支持在on监听的回调方法里,调用off注销回调。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ------------------------------------------ |
| type | string | 是 | 监听事件,固定为'captureStart',photoOutput创建成功后可监听。每次拍照,底层开始曝光时触发该事件并返回。 |
| callback | AsyncCallback\ | 是 | 使用callback的方式获取Capture ID。 |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function callback(err: BusinessError, captureId: number): void {
if (err !== undefined && err.code !== 0) {
console.error(`Callback Error, errorCode: ${err.code}`);
return;
}
console.info(`photo capture started, captureId : ${captureId}`);
}
function registerPhotoOutputCaptureStart(photoOutput: camera.PhotoOutput): void {
photoOutput.on('captureStart', callback);
}
```
### off('captureStart')(deprecated)
off(type: 'captureStart', callback?: AsyncCallback\): void
注销监听拍照开始。
> **说明:**
> 从 API version 10开始支持,从API version 11开始废弃。建议使用[off('captureStartWithInfo')](#offcapturestartwithinfo11)替代。
>
> 当前注册监听接口,不支持在on监听的回调方法里,调用off注销回调。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ------------------------------------------ |
| type | string | 是 | 监听事件,固定为'captureStart',photoOutput创建成功后可监听。 |
| callback | AsyncCallback\ | 否 | 回调函数,如果指定参数则取消对应callback(callback对象不可是匿名函数),否则取消所有callback。 |
**示例:**
```ts
function unregisterPhotoOutputCaptureStart(photoOutput: camera.PhotoOutput): void {
photoOutput.off('captureStart');
}
```
### on('frameShutter')
on(type: 'frameShutter', callback: AsyncCallback\): void
监听拍照帧输出捕获,通过注册回调函数获取结果。使用callback异步回调。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------- | --- | ------------------------------------ |
| type | string | 是 | 监听事件,固定为'frameShutter',photoOutput创建成功后可监听。 |
| callback | AsyncCallback\<[FrameShutterInfo](#frameshutterinfo)\> | 是 | 回调函数,用于获取相关信息。该回调返回意味着可以再次下发拍照请求。 |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function callback(err: BusinessError, frameShutterInfo: camera.FrameShutterInfo): void {
if (err !== undefined && err.code !== 0) {
console.error(`Callback Error, errorCode: ${err.code}`);
return;
}
console.info(`CaptureId for frame : ${frameShutterInfo.captureId}`);
console.info(`Timestamp for frame : ${frameShutterInfo.timestamp}`);
}
function registerPhotoOutputFrameShutter(photoOutput: camera.PhotoOutput): void {
photoOutput.on('frameShutter', callback);
}
```
### off('frameShutter')
off(type: 'frameShutter', callback?: AsyncCallback\): void
注销监听拍照帧输出捕获。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------- | --- | ------------------------------------ |
| type | string | 是 | 监听事件,固定为'frameShutter',photoOutput创建成功后可监听。 |
| callback | AsyncCallback\<[FrameShutterInfo](#frameshutterinfo)\> | 否 | 回调函数,如果指定参数则取消对应callback(callback对象不可是匿名函数),否则取消所有callback。 |
**示例:**
```ts
function unregisterPhotoOutputFrameShutter(photoOutput: camera.PhotoOutput): void {
photoOutput.off('frameShutter');
}
```
### on('captureEnd')
on(type: 'captureEnd', callback: AsyncCallback\): void
监听拍照结束,通过注册回调函数获取结果。使用callback异步回调。
> **说明:**
>
> 当前注册监听接口,不支持在on监听的回调方法里,调用off注销回调。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------- | ---- | ---------------------------------------- |
| type | string | 是 | 监听事件,固定为'captureEnd',photoOutput创建成功后可监听。拍照完全结束可触发该事件发生并返回相应信息。 |
| callback | AsyncCallback\<[CaptureEndInfo](#captureendinfo)\> | 是 | 回调函数,用于获取相关信息。 |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function callback(err: BusinessError, captureEndInfo: camera.CaptureEndInfo): void {
if (err !== undefined && err.code !== 0) {
console.error(`Callback Error, errorCode: ${err.code}`);
return;
}
console.info(`photo capture end, captureId : ${captureEndInfo.captureId}`);
console.info(`frameCount : ${captureEndInfo.frameCount}`);
}
function registerPhotoOutputCaptureEnd(photoOutput: camera.PhotoOutput): void {
photoOutput.on('captureEnd', callback);
}
```
### off('captureEnd')
off(type: 'captureEnd', callback?: AsyncCallback\): void
注销监听拍照结束。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------- | ---- | ---------------------------------------- |
| type | string | 是 | 监听事件,固定为'captureEnd',photoOutput创建成功后可监听。 |
| callback | AsyncCallback\<[CaptureEndInfo](#captureendinfo)\> | 否 | 回调函数,如果指定参数则取消对应callback(callback对象不可是匿名函数),否则取消所有callback。 |
**示例:**
```ts
function unregisterPhotoOutputCaptureEnd(photoOutput: camera.PhotoOutput): void {
photoOutput.off('captureEnd');
}
```
### on('frameShutterEnd')12+
on(type: 'frameShutterEnd', callback: AsyncCallback\): void
监听拍照曝光结束捕获,通过注册回调函数获取结果。使用callback异步回调。
> **说明:**
>
> 当前注册监听接口,不支持在on监听的回调方法里,调用off注销回调。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| type | string | 是 | 监听事件,固定为'frameShutterEnd',photoOutput创建成功后可监听。 |
| callback | AsyncCallback\<[FrameShutterEndInfo](#frameshutterendinfo12)\> | 是 | 回调函数,用于获取相关信息。该回调返回表示拍照曝光结束。 |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function callback(err: BusinessError, frameShutterEndInfo: camera.FrameShutterEndInfo): void {
if (err !== undefined && err.code !== 0) {
console.error(`Callback Error, errorCode: ${err.code}`);
return;
}
console.info(`CaptureId for frame : ${frameShutterEndInfo.captureId}`);
}
function registerPhotoOutputFrameShutterEnd(photoOutput: camera.PhotoOutput): void {
photoOutput.on('frameShutterEnd', callback);
}
```
### off('frameShutterEnd')12+
off(type: 'frameShutterEnd', callback?: AsyncCallback\): void
注销监听拍照帧输出捕获。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| type | string | 是 | 监听事件,固定为'frameShutterEnd',photoOutput创建成功后可监听。 |
| callback | AsyncCallback\<[FrameShutterEndInfo](#frameshutterendinfo12)\> | 否 | 回调函数,如果指定参数则取消对应callback(callback对象不可是匿名函数),否则取消所有callback。 |
**示例:**
```ts
function unregisterPhotoOutputFrameShutterEnd(photoOutput: camera.PhotoOutput): void {
photoOutput.off('frameShutterEnd');
}
```
### on('captureReady')12+
on(type: 'captureReady', callback: AsyncCallback\): void
监听可拍下一张,通过注册回调函数获取结果。使用callback异步回调。
> **说明:**
>
> 当前注册监听接口,不支持在on监听的回调方法里,调用off注销回调。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------- | ---- | ------------------------------------------------------------ |
| type | string | 是 | 监听事件,固定为'captureReady',photoOutput创建成功后可监听。当下一张可拍时可触发该事件发生并返回相应信息。 |
| callback | AsyncCallback\ | 是 | 回调函数,用于获取相关信息。 |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function callback(err: BusinessError): void {
if (err !== undefined && err.code !== 0) {
console.error(`Callback Error, errorCode: ${err.code}`);
return;
}
console.info(`photo capture ready`);
}
function registerPhotoOutputCaptureReady(photoOutput: camera.PhotoOutput): void {
photoOutput.on('captureReady', callback);
}
```
### off('captureReady')12+
off(type: 'captureReady', callback?: AsyncCallback\): void
注销监听可拍下一张。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------------------------------------- | ---- | ------------------------------------------------------------ |
| type | string | 是 | 监听事件,固定为'captureReady',photoOutput创建成功后可监听。 |
| callback | AsyncCallback\<[CaptureReadyInfo](#captureendinfo)\> | 否 | 回调函数,如果指定参数则取消对应callback(callback对象不可是匿名函数),否则取消所有callback。 |
**示例:**
```ts
function unregisterPhotoOutputCaptureReady(photoOutput: camera.PhotoOutput): void {
photoOutput.off('captureReady');
}
```
### on('estimatedCaptureDuration')12+
on(type: 'estimatedCaptureDuration', callback: AsyncCallback\): void
监听预估的拍照时间,通过注册回调函数获取结果。使用callback异步回调。
> **说明:**
>
> 当前注册监听接口,不支持在on监听的回调方法里,调用off注销回调。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ------------------------------------------------------------ |
| type | string | 是 | 监听事件,固定为'estimatedCaptureDuration',photoOutput创建成功后可监听。拍照完全结束可触发该事件发生并返回相应信息。 |
| callback | AsyncCallback\ | 是 | 回调函数,用于获取预估的单次拍照底层出sensor采集帧时间,如果上报-1,代表没有预估时间。 |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function callback(err: BusinessError, duration: number): void {
if (err !== undefined && err.code !== 0) {
console.error(`Callback Error, errorCode: ${err.code}`);
return;
}
console.info(`photo estimated capture duration : ${duration}`);
}
function registerPhotoOutputEstimatedCaptureDuration(photoOutput: camera.PhotoOutput): void {
photoOutput.on('estimatedCaptureDuration', callback);
}
```
### off('estimatedCaptureDuration')12+
off(type: 'estimatedCaptureDuration', callback?: AsyncCallback\): void
注销监听预估的拍照时间。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | ------------------------------------------------------------ |
| type | string | 是 | 监听事件,固定为'estimatedCaptureDuration',photoOutput创建成功后可监听。 |
| callback | AsyncCallback\ | 否 | 回调函数,如果指定参数则取消对应callback(callback对象不可是匿名函数),否则取消所有callback。 |
**示例:**
```ts
function unregisterPhotoOutputEstimatedCaptureDuration(photoOutput: camera.PhotoOutput): void {
photoOutput.off('estimatedCaptureDuration');
}
```
### on('error')
on(type: 'error', callback: ErrorCallback): void
监听拍照输出发生错误,通过注册回调函数获取结果。使用callback异步回调。
> **说明:**
>
> 当前注册监听接口,不支持在on监听的回调方法里,调用off注销回调。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------- | ---- | ----------------------------------- |
| type | string | 是 | 监听事件,固定为'error',photoOutput创建成功后可监听。拍照接口调用时出现错误触发该事件并返回错误信息。 |
| callback | [ErrorCallback](../apis-basic-services-kit/js-apis-base.md#errorcallback) | 是 | 回调函数,用于获取错误信息。返回错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function callback(err: BusinessError): void {
console.error(`Photo output error code: ${err.code}`);
}
function registerPhotoOutputError(photoOutput: camera.PhotoOutput): void {
photoOutput.on('error', callback);
}
```
### off('error')
off(type: 'error', callback?: ErrorCallback): void
注销监听拍照输出发生错误。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------- | ---- | ----------------------------------- |
| type | string | 是 | 监听事件,固定为'error',photoOutput创建成功后可监听。 |
| callback | [ErrorCallback](../apis-basic-services-kit/js-apis-base.md#errorcallback) | 否 | 回调函数,如果指定参数则取消对应callback(callback对象不可是匿名函数),否则取消所有callback。 |
**示例:**
```ts
function unregisterPhotoOutputError(photoOutput: camera.PhotoOutput): void {
photoOutput.off('error');
}
```
### getActiveProfile12+
getActiveProfile(): Profile
获取当前生效的配置信息。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| ------------- |-----------|
| [Profile](#profile) | 当前生效的配置信息 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
|---------|------------------------------|
| 7400201 | Camera service fatal error. |
**示例:**
```ts
function testGetActiveProfile(photoOutput: camera.PhotoOutput): camera.Profile | undefined {
let activeProfile: camera.Profile | undefined = undefined;
try {
activeProfile = photoOutput.getActiveProfile();
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The photoOutput.getActiveProfile call failed. error code: ${err.code}`);
}
return activeProfile;
}
```
### getPhotoRotation12+
getPhotoRotation(deviceDegree: number): ImageRotation
获取拍照旋转角度。
- 设备自然方向:设备默认使用方向,手机为竖屏(充电口向下)。
- 相机镜头角度:值等于相机图像顺时针旋转到设备自然方向的角度,手机后置相机传感器是横屏安装的,所以需要顺时针旋转90度到设备自然方向。
- 屏幕显示方向:需要屏幕显示的图片左上角为第一个像素点为坐标原点。锁屏时与自然方向一致。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------| ---- | ------------------------ |
| deviceDegree | number | 是 | 设备旋转角度 |
**返回值:**
| 类型 | 说明 |
| ------------- |-----------|
| [ImageRotation](#imagerotation) | 获取拍照旋转角度。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
|---------|------------------------------|
| 7400101 | Parameter missing or parameter type incorrect. |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
function testGetPhotoRotation(photoOutput: camera.PhotoOutput, deviceDegree : number): camera.ImageRotation {
let photoRotation: camera.ImageRotation = camera.ImageRotation.ROTATION_0;
try {
photoRotation = photoOutput.getPhotoRotation(deviceDegree);
console.log(`Photo rotation is: ${photoRotation}`);
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The photoOutput.getPhotoRotation call failed. error code: ${err.code}`);
}
return photoRotation;
}
```
## FrameShutterInfo
拍照帧输出信息。
**系统能力:** SystemCapability.Multimedia.Camera.Core
| 名称 | 类型 | 只读 | 可选 | 说明 |
| --------- | ------ | ---- | ---- | ---------- |
| captureId | number | 否 | 否 | 拍照的ID。 |
| timestamp | number | 否 | 否 | 快门时间戳。 |
## FrameShutterEndInfo12+
拍照曝光结束信息。
**系统能力:** SystemCapability.Multimedia.Camera.Core
| 名称 | 类型 | 只读 | 可选 | 说明 |
| --------- | ------ | ---- | ---- | ---------- |
| captureId | number | 否 | 否 | 拍照的ID。 |
## CaptureStartInfo11+
拍照开始信息。
**系统能力:** SystemCapability.Multimedia.Camera.Core
| 名称 | 类型 | 只读 | 可选 | 说明 |
| ---------- | ------ | ---- | ---- | --------- |
| captureId | number | 否 | 否 | 拍照的ID。 |
| time | number | 否 | 否 | 预估的单次拍照底层出sensor采集帧时间,如果上报-1,代表没有预估时间。 |
## CaptureEndInfo
拍照停止信息。
**系统能力:** SystemCapability.Multimedia.Camera.Core
| 名称 | 类型 | 只读 | 可选 | 说明 |
| ---------- | ------ | ---- | ---- | ---------|
| captureId | number | 否 | 否 | 拍照的ID。 |
| frameCount | number | 否 | 否 | 帧数。 |
## AutoDeviceSwitchStatus13+
自动切换镜头状态信息。
**系统能力:** SystemCapability.Multimedia.Camera.Core
| 名称 | 类型 | 只读 | 可选 | 说明 |
| ---------- |---------| ---- | ---- |-------------------------|
| isDeviceSwitched | boolean | 否 | 否 | 自动切换镜头是否成功。 |
| isDeviceCapabilityChanged | boolean | 否 | 否 | 自动切换镜头成功后,其镜头能力值是否发生改变。 |
## VideoOutput
录像会话中使用的输出信息,继承[CameraOutput](#cameraoutput)。
### start
start(callback: AsyncCallback\): void
启动录制,通过注册回调函数获取结果。使用callback异步回调。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------------- | ---- | -------------------- |
| callback | AsyncCallback\ | 是 | 回调函数,用于获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config. |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function startVideoOutput(videoOutput: camera.VideoOutput): void {
videoOutput.start((err: BusinessError) => {
if (err) {
console.error(`Failed to start the video output, error code: ${err.code}.`);
return;
}
console.info('Callback invoked to indicate the video output start success.');
});
}
```
### start
start(): Promise\
启动录制,通过Promise获取结果。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| -------------- | ----------------------- |
| Promise\ | 无返回结果的Promise对象。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config. |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function startVideoOutput(videoOutput: camera.VideoOutput): void {
videoOutput.start().then(() => {
console.info('Promise returned to indicate that start method execution success.');
}).catch((error: BusinessError) => {
console.error(`Failed to video output start, error code: ${error.code}.`);
});
}
```
### stop
stop(callback: AsyncCallback\): void
结束录制,通过注册回调函数获取结果。使用callback异步回调。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------------- | ---- | ------------------------ |
| callback | AsyncCallback\ | 是 | 回调函数,用于获取结果。 |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function stopVideoOutput(videoOutput: camera.VideoOutput): void {
videoOutput.stop((err: BusinessError) => {
if (err) {
console.error(`Failed to stop the video output, error code: ${err.code}.`);
return;
}
console.info('Callback invoked to indicate the video output stop success.');
});
}
```
### stop
stop(): Promise\
结束录制,通过Promise获取结果。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| -------------- | ----------------------- |
| Promise\ | 无返回结果的Promise对象。 |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function stopVideoOutput(videoOutput: camera.VideoOutput): void {
videoOutput.stop().then(() => {
console.info('Promise returned to indicate that stop method execution success.');
}).catch((error: BusinessError) => {
console.error(`Failed to video output stop, error code: ${error.code}.`);
});
}
```
### on('frameStart')
on(type: 'frameStart', callback: AsyncCallback\): void
监听录像开始,通过注册回调函数获取结果。使用callback异步回调。
> **说明:**
>
> 当前注册监听接口,不支持在on监听的回调方法里,调用off注销回调。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------------- | ---- | ----------------------------------------- |
| type | string | 是 | 监听事件,固定为'frameStart',videoOutput创建成功后可监听。底层第一次曝光时触发该事件并返回。 |
| callback | AsyncCallback\ | 是 | 回调函数,用于获取结果。 只要有该事件返回就证明录像开始。 |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function callback(err: BusinessError): void {
if (err !== undefined && err.code !== 0) {
console.error(`Callback Error, errorCode: ${err.code}`);
return;
}
console.info('Video frame started');
}
function registerVideoOutputFrameStart(videoOutput: camera.VideoOutput): void {
videoOutput.on('frameStart', callback);
}
```
### off('frameStart')
off(type: 'frameStart', callback?: AsyncCallback\): void
注销监听录像开始。
> **说明:**
>
> 当前注册监听接口,不支持在on监听的回调方法里,调用off注销回调。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------------- | ---- | ----------------------------------------- |
| type | string | 是 | 监听事件,固定为'frameStart',videoOutput创建成功后可监听。 |
| callback | AsyncCallback\ | 否 | 回调函数,如果指定参数则取消对应callback(callback对象不可是匿名函数),否则取消所有callback。 |
**示例:**
```ts
function unregisterVideoOutputFrameStart(videoOutput: camera.VideoOutput): void {
videoOutput.off('frameStart');
}
```
### on('frameEnd')
on(type: 'frameEnd', callback: AsyncCallback\): void
监听录像结束,通过注册回调函数获取结果。使用callback异步回调。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------------- | ---- | ------------------------------------------ |
| type | string | 是 | 监听事件,固定为'frameEnd',videoOutput创建成功后可监听。录像完全结束最后一帧时触发该事件并返回。 |
| callback | AsyncCallback\ | 是 | 回调函数,用于获取结果。 只要有该事件返回就证明录像结束。 |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function callback(err: BusinessError): void {
if (err !== undefined && err.code !== 0) {
console.error(`Callback Error, errorCode: ${err.code}`);
return;
}
console.info('Video frame ended');
}
function registerVideoOutputFrameEnd(videoOutput: camera.VideoOutput): void {
videoOutput.on('frameEnd', callback);
}
```
### off('frameEnd')
off(type: 'frameEnd', callback?: AsyncCallback\): void
注销监听录像结束。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------------- | ---- | ------------------------------------------ |
| type | string | 是 | 监听事件,固定为'frameEnd',videoOutput创建成功后可监听。 |
| callback | AsyncCallback\ | 否 | 回调函数,如果指定参数则取消对应callback(callback对象不可是匿名函数),否则取消所有callback。 |
**示例:**
```ts
function unregisterVideoOutputFrameEnd(videoOutput: camera.VideoOutput): void {
videoOutput.off('frameEnd');
}
```
### on('error')
on(type: 'error', callback: ErrorCallback): void
监听录像输出发生错误,通过注册回调函数获取结果。使用callback异步回调。
> **说明:**
>
> 当前注册监听接口,不支持在on监听的回调方法里,调用off注销回调。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------- | ---- | -------------------------------------- |
| type | string | 是 | 监听事件,固定为'error',videoOutput创建成功后可监听。录像接口调用出现错误时触发该事件并返回对应错误码,比如调用[start](#start-1),[CameraOutput.release](#release-1)接口时出现错误返回对应错误信息。 |
| callback | [ErrorCallback](../apis-basic-services-kit/js-apis-base.md#errorcallback) | 是 | 回调函数,用于获取错误信息。返回错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function callback(err: BusinessError): void {
console.error(`Video output error code: ${err.code}`);
}
function registerVideoOutputError(videoOutput: camera.VideoOutput): void {
videoOutput.on('error', callback);
}
```
### off('error')
off(type: 'error', callback?: ErrorCallback): void
注销监听录像输出发生错误。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------- | ---- | ----------------------------------- |
| type | string | 是 | 监听事件,固定为'error',photoOutput创建成功后可监听。 |
| callback | [ErrorCallback](../apis-basic-services-kit/js-apis-base.md#errorcallback) | 否 | 回调函数,如果指定参数则取消对应callback(callback对象不可是匿名函数),否则取消所有callback。 |
**示例:**
```ts
function unregisterVideoOutputError(videoOutput: camera.VideoOutput): void {
videoOutput.off('error');
}
```
### getSupportedFrameRates12+
getSupportedFrameRates(): Array\
查询支持的帧率范围。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| ------------- | ------------ |
| Array<[FrameRateRange](#frameraterange)> | 支持的帧率范围列表 |
**示例:**
```ts
function getSupportedFrameRates(videoOutput: camera.VideoOutput): Array {
let supportedFrameRatesArray: Array = videoOutput.getSupportedFrameRates();
return supportedFrameRatesArray;
}
```
### setFrameRate12+
setFrameRate(minFps: number, maxFps: number): void
设置录像流帧率范围,设置的范围必须在支持的帧率范围内。
进行设置前,可通过[getSupportedFrameRates](#getsupportedframerates12-1)查询支持的帧率范围。
> **说明:**
> 仅在[PhotoSession](#photosession11)或[VideoSession](#videosession11)模式下支持。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------| ---- | ------------------------ |
| minFps | number | 是 | 最小帧率。 |
| maxFps | number | 是 | 最大帧率。当传入的最小值大于最大值时,传参异常,接口不生效。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400101 | Parameter missing or parameter type incorrect. |
| 7400110 | Unresolved conflicts with current configurations. |
**示例:**
```ts
function setFrameRateRange(videoOutput: camera.VideoOutput, frameRateRange: Array): void {
videoOutput.setFrameRate(frameRateRange[0], frameRateRange[1]);
}
```
### getActiveFrameRate12+
getActiveFrameRate(): FrameRateRange
获取已设置的帧率范围。
使用[setFrameRate](#setframerate12-1)对录像流设置过帧率后可查询。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| ------------- | ------------ |
| [FrameRateRange](#frameraterange) | 帧率范围 |
**示例:**
```ts
function getActiveFrameRate(videoOutput: camera.VideoOutput): camera.FrameRateRange {
let activeFrameRate: camera.FrameRateRange = videoOutput.getActiveFrameRate();
return activeFrameRate;
}
```
### getActiveProfile12+
getActiveProfile(): VideoProfile
获取当前生效的配置信息。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| ------------- |-----------|
| [VideoProfile](#videoprofile) | 当前生效的配置信息 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
|---------|------------------------------|
| 7400201 | Camera service fatal error. |
**示例:**
```ts
function testGetActiveProfile(videoOutput: camera.VideoOutput): camera.Profile | undefined {
let activeProfile: camera.VideoProfile | undefined = undefined;
try {
activeProfile = videoOutput.getActiveProfile();
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The videoOutput.getActiveProfile call failed. error code: ${err.code}`);
}
return activeProfile;
}
```
### getVideoRotation12+
getVideoRotation(deviceDegree: number): ImageRotation
获取录像旋转角度。
- 设备自然方向:设备默认使用方向,手机为竖屏(充电口向下)。
- 相机镜头角度:值等于相机图像顺时针旋转到设备自然方向的角度,手机后置相机传感器是横屏安装的,所以需要顺时针旋转90度到设备自然方向。
- 屏幕显示方向:需要屏幕显示的图片左上角为第一个像素点为坐标原点。锁屏时与自然方向一致。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------| ---- | ------------------------ |
| deviceDegree | number | 是 | 设备旋转角度 |
**返回值:**
| 类型 | 说明 |
| ------------- |-----------|
| [ImageRotation](#imagerotation) | 获取录像旋转角度。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
|---------|------------------------------|
| 7400101 | Parameter missing or parameter type incorrect. |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
function testGetVideoRotation(videoOutput: camera.VideoOutput, deviceDegree : number): camera.ImageRotation {
let videoRotation: camera.ImageRotation = camera.ImageRotation.ROTATION_0;
try {
videoRotation = videoOutput.getVideoRotation(deviceDegree);
console.log(`Video rotation is: ${videoRotation}`);
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The videoOutput.getVideoRotation call failed. error code: ${err.code}`);
}
return videoRotation;
}
```
## MetadataOutput
metadata流。继承[CameraOutput](#cameraoutput)。
### start
start(callback: AsyncCallback\): void
开始输出metadata,通过注册回调函数获取结果。使用callback异步回调。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------------------- | ---- | ------------------- |
| callback | AsyncCallback\ | 是 | 回调函数,用于获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config. |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function startMetadataOutput(metadataOutput: camera.MetadataOutput): void {
metadataOutput.start((err: BusinessError) => {
if (err) {
console.error(`Failed to start metadata output, error code: ${err.code}.`);
return;
}
console.info('Callback returned with metadata output started.');
});
}
```
### start
start(): Promise\
开始输出metadata,通过Promise获取结果。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| ---------------------- | ------------------------ |
| Promise\ | 无返回结果的Promise对象。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config. |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function startMetadataOutput(metadataOutput: camera.MetadataOutput): void {
metadataOutput.start().then(() => {
console.info('Callback returned with metadata output started.');
}).catch((error: BusinessError) => {
console.error(`Failed to metadata output stop, error code: ${error.code}`);
});
}
```
### stop
stop(callback: AsyncCallback\): void
停止输出metadata,通过注册回调函数获取结果。使用callback异步回调。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------------------- | ---- | ------------------- |
| callback | AsyncCallback\ | 是 | 回调函数,用于获取结果。 |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function stopMetadataOutput(metadataOutput: camera.MetadataOutput): void {
metadataOutput.stop((err: BusinessError) => {
if (err) {
console.error(`Failed to stop the metadata output, error code: ${err.code}.`);
return;
}
console.info('Callback returned with metadata output stopped.');
})
}
```
### stop
stop(): Promise\
停止输出metadata,通过Promise获取结果。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| ---------------------- | --------------------------- |
| Promise\ | 无返回结果的Promise对象。 |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function stopMetadataOutput(metadataOutput: camera.MetadataOutput): void {
metadataOutput.stop().then(() => {
console.info('Callback returned with metadata output stopped.');
}).catch((error: BusinessError) => {
console.error(`Failed to metadata output stop, error code: ${error.code}`);
});
}
```
### on('metadataObjectsAvailable')
on(type: 'metadataObjectsAvailable', callback: AsyncCallback\\>): void
监听检测到的metadata对象,通过注册回调函数获取结果。使用callback异步回调。
> **说明:**
>
> 当前注册监听接口,不支持在on监听的回调方法里,调用off注销回调。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------- | ---- | ------------------------------------ |
| type | string | 是 | 监听事件,固定为'metadataObjectsAvailable',metadataOutput创建成功后可监听。检测到有效的metadata数据时触发该事件发生并返回相应的metadata数据。 |
| callback | AsyncCallback\\> | 是 | 回调函数,用于获取metadata数据。 |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function callback(err: BusinessError, metadataObjectArr: Array): void {
if (err !== undefined && err.code !== 0) {
console.error(`Callback Error, errorCode: ${err.code}`);
return;
}
console.info('metadata output metadataObjectsAvailable');
}
function registerMetadataObjectsAvailable(metadataOutput: camera.MetadataOutput): void {
metadataOutput.on('metadataObjectsAvailable', callback);
}
```
### off('metadataObjectsAvailable')
off(type: 'metadataObjectsAvailable', callback?: AsyncCallback\\>): void
注销监听检测到的metadata对象。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------- | ---- | ------------------------------------ |
| type | string | 是 | 监听事件,固定为'metadataObjectsAvailable',metadataOutput创建成功后可监听。 |
| callback | AsyncCallback\\> | 否 | 回调函数,如果指定参数则取消对应callback(callback对象不可是匿名函数),否则取消所有callback。 |
**示例:**
```ts
function unregisterMetadataObjectsAvailable(metadataOutput: camera.MetadataOutput): void {
metadataOutput.off('metadataObjectsAvailable');
}
```
### on('error')
on(type: 'error', callback: ErrorCallback): void
监听metadata流的错误,通过注册回调函数获取结果。使用callback异步回调。
> **说明:**
>
> 当前注册监听接口,不支持在on监听的回调方法里,调用off注销回调。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------- | ---- | --------------------------------------- |
| type | string | 是 | 监听事件,固定为'error',metadataOutput创建成功后可监听。metadata接口使用错误时触发该事件并返回对应错误码,比如调用[start](#start-3),[CameraOutput.release](#release-1)接口时发生错误返回对应错误信息。 |
| callback | [ErrorCallback](../apis-basic-services-kit/js-apis-base.md#errorcallback) | 是 | 回调函数,用于获取错误信息。返回错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function callback(metadataOutputError: BusinessError): void {
console.error(`Metadata output error code: ${metadataOutputError.code}`);
}
function registerMetadataOutputError(metadataOutput: camera.MetadataOutput): void {
metadataOutput.on('error', callback);
}
```
### off('error')
off(type: 'error', callback?: ErrorCallback): void
注销监听metadata流的错误。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------- | ---- | --------------------------------------- |
| type | string | 是 | 监听事件,固定为'error',metadataOutput创建成功后可监听。 |
| callback | [ErrorCallback](../apis-basic-services-kit/js-apis-base.md#errorcallback) | 否 | 回调函数,如果指定参数则取消对应callback(callback对象不可是匿名函数),否则取消所有callback。 |
**示例:**
```ts
function unregisterMetadataOutputError(metadataOutput: camera.MetadataOutput): void {
metadataOutput.off('error');
}
```
## MetadataObjectType
枚举,metadata流。
**系统能力:** SystemCapability.Multimedia.Camera.Core
| 名称 | 值 | 说明 |
| ------------------------- | ---- | ----------------- |
| FACE_DETECTION | 0 | metadata对象类型,用于人脸检测。
检测点应在0-1坐标系内,该坐标系左上角为(0,0),右下角为(1,1)。
此坐标系以设备充电口在右侧时的横向设备方向为基准。
例如应用的预览界面布局以设备充电口在下侧时的竖向方向为基准,
布局宽高为(w,h), 返回点为(x,y),则转换后的坐标点为(1-y,x)。 |
## Rect
矩形定义。
**系统能力:** SystemCapability.Multimedia.Camera.Core
| 名称 | 类型 | 只读 | 可选 | 说明 |
| -------- | ------ | ------ |-----| --------------------- |
| topLeftX | number | 否 | 否 | 矩形区域左上角x坐标。 |
| topLeftY | number | 否 | 否 | 矩形区域左上角y坐标。 |
| width | number | 否 | 否 | 矩形宽,相对值,范围[0, 1]。 |
| height | number | 否 | 否 | 矩形高,相对值,范围[0, 1]。 |
## MetadataObject
相机元能力信息,[CameraInput](#camerainput)相机信息中的数据来源,通过metadataOutput.on('metadataObjectsAvailable')接口获取。
**系统能力:** SystemCapability.Multimedia.Camera.Core
| 名称 | 类型 | 只读 | 可选 |说明 |
| ----------- | ------------------------------------------- | ---- | ---- | ----------------- |
| type | [MetadataObjectType](#metadataobjecttype) | 是 | 否 | metadata 类型。 |
| timestamp | number | 是 | 否 | 当前时间戳(毫秒)。|
| boundingBox | [Rect](#rect) | 是 | 否 | metadata 区域框。 |
## FlashMode
枚举,闪光灯模式。
**系统能力:** SystemCapability.Multimedia.Camera.Core
| 名称 | 值 | 说明 |
| ---------------------- | ---- | ---------- |
| FLASH_MODE_CLOSE | 0 | 闪光灯关闭。 |
| FLASH_MODE_OPEN | 1 | 闪光灯打开。 |
| FLASH_MODE_AUTO | 2 | 自动闪光灯。 |
| FLASH_MODE_ALWAYS_OPEN | 3 | 闪光灯常亮。 |
## ExposureMode
枚举,曝光模式。
**系统能力:** SystemCapability.Multimedia.Camera.Core
| 名称 | 值 | 说明 |
| ----------------------------- | ---- | ----------- |
| EXPOSURE_MODE_LOCKED | 0 | 锁定曝光模式。不支持曝光区域中心点设置。 |
| EXPOSURE_MODE_AUTO | 1 | 自动曝光模式。支持曝光区域中心点设置,可以使用[AutoExposure.setMeteringPoint](#setmeteringpoint11)设置曝光区域中心点。 |
| EXPOSURE_MODE_CONTINUOUS_AUTO | 2 | 连续自动曝光。不支持曝光区域中心点设置。 |
## FocusMode
枚举,焦距模式。
**系统能力:** SystemCapability.Multimedia.Camera.Core
| 名称 | 值 | 说明 |
| -------------------------- | ---- | ------------ |
| FOCUS_MODE_MANUAL | 0 | 手动对焦。通过手动修改相机焦距来改变对焦位置,不支持对焦点设置。 |
| FOCUS_MODE_CONTINUOUS_AUTO | 1 | 连续自动对焦。不支持对焦点设置。 |
| FOCUS_MODE_AUTO | 2 | 自动对焦。支持对焦点设置,可以使用[Focus.setFocusPoint](#setfocuspoint11)设置对焦点,根据对焦点执行一次自动对焦。 |
| FOCUS_MODE_LOCKED | 3 | 对焦锁定。不支持对焦点设置。 |
## FocusState
枚举,焦距状态。
**系统能力:** SystemCapability.Multimedia.Camera.Core
| 名称 | 值 | 说明 |
| --------------------- | ---- | --------- |
| FOCUS_STATE_SCAN | 0 | 触发对焦。 |
| FOCUS_STATE_FOCUSED | 1 | 对焦成功。 |
| FOCUS_STATE_UNFOCUSED | 2 | 未完成对焦。 |
## VideoStabilizationMode
枚举,视频防抖模式。
**系统能力:** SystemCapability.Multimedia.Camera.Core
| 名称 | 值 | 说明 |
| --------- | ---- | ------------ |
| OFF | 0 | 关闭视频防抖功能。 |
| LOW | 1 | 使用基础防抖算法。 |
| MIDDLE | 2 | 使用防抖效果一般的防抖算法,防抖效果优于LOW类型。 |
| HIGH | 3 | 使用防抖效果最好的防抖算法,防抖效果优于MIDDLE类型。 |
| AUTO | 4 | 自动进行选择。 |
## Session11+
会话类,保存一次相机运行所需要的所有资源[CameraInput](#camerainput)、[CameraOutput](#cameraoutput),并向相机设备申请完成相机功能(录像,拍照)。
### beginConfig11+
beginConfig(): void
开始配置会话。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400105 | Session config locked. |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function beginConfig(session: camera.Session): void {
try {
session.beginConfig();
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The beginConfig call failed. error code: ${err.code}`);
}
}
```
### commitConfig11+
commitConfig(callback: AsyncCallback\): void
提交配置信息,通过注册回调函数获取结果。使用callback异步回调。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------------- | ---- | -------------------- |
| callback | AsyncCallback\ | 是 | 回调函数,用于获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400102 | Operation not allowed. |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function commitConfig(session: camera.Session): void {
session.commitConfig((err: BusinessError) => {
if (err) {
console.error(`The commitConfig call failed. error code: ${err.code}`);
return;
}
console.info('Callback invoked to indicate the commit config success.');
});
}
```
### commitConfig11+
commitConfig(): Promise\
提交配置信息,通过Promise获取结果。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| -------------- | ------------------------ |
| Promise\ | 无返回结果的Promise对象。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400102 | Operation not allowed. |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function commitConfig(session: camera.Session): void {
session.commitConfig().then(() => {
console.info('Promise returned to indicate the commit config success.');
}).catch((error: BusinessError) => {
// 失败返回错误码error.code并处理
console.error(`The commitConfig call failed. error code: ${error.code}`);
});
}
```
### canAddInput11+
canAddInput(cameraInput: CameraInput): boolean
判断当前cameraInput是否可以添加到session中。当前函数需要在[beginConfig](#beginconfig11)和[commitConfig](#commitconfig11-1)之间生效。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----------- | --------------------------- | ---- | ------------------------ |
| cameraInput | [CameraInput](#camerainput) | 是 | 需要添加的CameraInput实例。传参异常(如超出范围、传入null、未定义等),实际接口不会生效。 |
**返回值:**
| 类型 | 说明 |
| -------------- | ------------------------ |
| boolean | 返回true表示支持添加当前cameraInput,返回false表示不支持添加。 |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function canAddInput(session: camera.Session, cameraInput: camera.CameraInput): void {
let canAdd: boolean = session.canAddInput(cameraInput);
console.info(`The input canAddInput: ${canAdd}`);
}
```
### addInput11+
addInput(cameraInput: CameraInput): void
把[CameraInput](#camerainput)加入到会话。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----------- | --------------------------- | ---- | ------------------------ |
| cameraInput | [CameraInput](#camerainput) | 是 | 需要添加的CameraInput实例。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400101 | Parameter missing or parameter type incorrect. |
| 7400102 | Operation not allowed. |
| 7400103 | Session not config. |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function addInput(session: camera.Session, cameraInput: camera.CameraInput): void {
try {
session.addInput(cameraInput);
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The addInput call failed. error code: ${err.code}`);
}
}
```
### removeInput11+
removeInput(cameraInput: CameraInput): void
移除[CameraInput](#camerainput)。当前函数需要在[beginConfig](#beginconfig11)和[commitConfig](#commitconfig11-1)之间生效。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----------- | --------------------------- | ---- | ------------------------ |
| cameraInput | [CameraInput](#camerainput) | 是 | 需要移除的CameraInput实例。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400101 | Parameter missing or parameter type incorrect. |
| 7400102 | Operation not allowed. |
| 7400103 | Session not config. |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function removeInput(session: camera.Session, cameraInput: camera.CameraInput): void {
try {
session.removeInput(cameraInput);
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The removeInput call failed. error code: ${err.code}`);
}
}
```
### canAddOutput11+
canAddOutput(cameraOutput: CameraOutput): boolean
判断当前cameraOutput是否可以添加到session中。当前函数需要在[addInput](#addinput11)和[commitConfig](#commitconfig11-1)之间生效。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----------- | --------------------------- | ---- | ------------------------ |
| cameraOutput | [CameraOutput](#cameraoutput) | 是 | 需要添加的CameraOutput实例。传参异常(如超出范围、传入null、未定义等),实际接口不会生效。 |
**返回值:**
| 类型 | 说明 |
| -------------- | ------------------------ |
| boolean | 是否可以添加当前cameraOutput到session中。 |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function canAddOutput(session: camera.Session, cameraOutput: camera.CameraOutput): void {
let canAdd: boolean = session.canAddOutput(cameraOutput);
console.info(`This addOutput can add: ${canAdd}`);
}
```
### addOutput11+
addOutput(cameraOutput: CameraOutput): void
把[CameraOutput](#cameraoutput)加入到会话。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------------- | ------------------------------- | ---- | ------------------------ |
| cameraOutput | [CameraOutput](#cameraoutput) | 是 | 需要添加的CameraOutput实例。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400101 | Parameter missing or parameter type incorrect. |
| 7400102 | Operation not allowed. |
| 7400103 | Session not config. |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function addOutput(session: camera.Session, cameraOutput: camera.CameraOutput): void {
try {
session.addOutput(cameraOutput);
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The addOutput call failed. error code: ${err.code}`);
}
}
```
### removeOutput11+
removeOutput(cameraOutput: CameraOutput): void
从会话中移除[CameraOutput](#cameraoutput)。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------------- | ------------------------------- | ---- | ------------------------ |
| cameraOutput | [CameraOutput](#cameraoutput) | 是 | 需要移除的CameraOutput实例。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400101 | Parameter missing or parameter type incorrect. |
| 7400102 | Operation not allowed. |
| 7400103 | Session not config. |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function removeOutput(session: camera.Session, previewOutput: camera.PreviewOutput): void {
try {
session.removeOutput(previewOutput);
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The removeOutput call failed. error code: ${err.code}`);
}
}
```
### start11+
start(callback: AsyncCallback\): void
开始会话工作,通过注册回调函数获取结果。使用callback异步回调。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------------- | ---- | -------------------- |
| callback | AsyncCallback\ | 是 | 回调函数,用于获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400102 | Operation not allowed. |
| 7400103 | Session not config. |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function startCaptureSession(session: camera.Session): void {
session.start((err: BusinessError) => {
if (err) {
console.error(`Failed to start the session, error code: ${err.code}.`);
return;
}
console.info('Callback invoked to indicate the session start success.');
});
}
```
### start11+
start(): Promise\
开始会话工作,通过Promise获取结果。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| -------------- | ------------------------ |
| Promise\ | 无返回结果的Promise对象。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400102 | Operation not allowed. |
| 7400103 | Session not config. |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function startCaptureSession(session: camera.Session): void {
session.start().then(() => {
console.info('Promise returned to indicate the session start success.');
}).catch((error: BusinessError) => {
console.error(`Failed to start the session, error code: ${error.code}.`);
});
}
```
### stop11+
stop(callback: AsyncCallback\): void
停止会话工作,通过注册回调函数获取结果。使用callback异步回调。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------------- | ---- | ------------------- |
| callback | AsyncCallback\ | 是 | 回调函数,用于获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function stopCaptureSession(session: camera.Session): void {
session.stop((err: BusinessError) => {
if (err) {
console.error(`Failed to stop the session, error code: ${err.code}.`);
return;
}
console.info('Callback invoked to indicate the session stop success.');
});
}
```
### stop11+
stop(): Promise\
停止会话工作,通过Promise获取结果。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| -------------- |-------------------|
| Promise\ | 无返回结果的Promise对象。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function stopCaptureSession(session: camera.Session): void {
session.stop().then(() => {
console.info('Promise returned to indicate the session stop success.');
}).catch((error: BusinessError) => {
console.error(`Failed to stop the session, error code: ${error.code}.`);
});
}
```
### release11+
release(callback: AsyncCallback\): void
释放会话资源,通过注册回调函数获取结果。使用callback异步回调。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------------- | ---- | -------------------- |
| callback | AsyncCallback\ | 是 | 回调函数,用于获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function releaseCaptureSession(session: camera.Session): void {
session.release((err: BusinessError) => {
if (err) {
console.error(`Failed to release the session instance, error code: ${err.code}.`);
return;
}
console.info('Callback invoked to indicate that the session instance is released successfully.');
});
}
```
### release11+
release(): Promise\
释放会话资源,通过Promise获取结果。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| -------------- | ------------------------ |
| Promise\ | 无返回结果的Promise对象。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function releaseCaptureSession(session: camera.Session): void {
session.release().then(() => {
console.info('Promise returned to indicate that the session instance is released successfully.');
}).catch((error: BusinessError) => {
console.error(`Failed to release the session instance, error code: ${error.code}.`);
});
}
```
## Flash11+
Flash extends [FlashQuery](#flashquery12)
闪光灯类,对设备闪光灯操作。
### setFlashMode11+
setFlashMode(flashMode: FlashMode): void
设置闪光灯模式。
进行设置之前,需要先检查:
1. 设备是否支持闪光灯,可使用方法[hasFlash](#hasflash11)。
2. 设备是否支持指定的闪光灯模式,可使用方法[isFlashModeSupported](#isflashmodesupported11)。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| --------- | ----------------------- | ---- | --------------------- |
| flashMode | [FlashMode](#flashmode) | 是 | 指定闪光灯模式。传参为null或者undefined,作为0处理,闪光灯关闭。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function setFlashMode(photoSession: camera.PhotoSession): void {
try {
photoSession.setFlashMode(camera.FlashMode.FLASH_MODE_AUTO);
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The setFlashMode call failed. error code: ${err.code}`);
}
}
```
### getFlashMode11+
getFlashMode(): FlashMode
获取当前设备的闪光灯模式。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| ---------- | ----------------------------- |
| [FlashMode](#flashmode) | 获取当前设备的闪光灯模式。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function getFlashMode(photoSession: camera.PhotoSession): camera.FlashMode | undefined {
let flashMode: camera.FlashMode | undefined = undefined;
try {
flashMode = photoSession.getFlashMode();
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The getFlashMode call failed.error code: ${err.code}`);
}
return flashMode;
}
```
## FlashQuery12+
提供了查询设备的闪光灯状态和模式的能力。
### hasFlash11+
hasFlash(): boolean
检测是否有闪光灯,通过注册回调函数获取结果。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| ---------- | ----------------------------- |
| boolean | 返回true表示设备支持闪光灯,false表示不支持。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config, only throw in session usage. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function hasFlash(photoSession: camera.PhotoSession): boolean {
let status: boolean = false;
try {
status = photoSession.hasFlash();
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The hasFlash call failed. error code: ${err.code}`);
}
return status;
}
```
### isFlashModeSupported11+
isFlashModeSupported(flashMode: FlashMode): boolean
检测闪光灯模式是否支持。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| --------- | ----------------------- | ---- | --------------------------------- |
| flashMode | [FlashMode](#flashmode) | 是 | 指定闪光灯模式。传参为null或者undefined,作为0处理,闪光灯关闭。 |
**返回值:**
| 类型 | 说明 |
| ---------- | ----------------------------- |
| boolean | 返回true表示支持该闪光灯模式,false表示不支持。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config, only throw in session usage. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function isFlashModeSupported(photoSession: camera.PhotoSession): boolean {
let status: boolean = false;
try {
status = photoSession.isFlashModeSupported(camera.FlashMode.FLASH_MODE_AUTO);
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The isFlashModeSupported call failed. error code: ${err.code}`);
}
return status;
}
```
## AutoExposure11+
AutoExposure extends [AutoExposureQuery](#autoexposurequery12)
自动曝光类,对设备自动曝光(AE)操作。
### getExposureMode11+
getExposureMode(): ExposureMode
获取当前曝光模式。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| ---------- | ----------------------------- |
| [ExposureMode](#exposuremode) | 获取当前曝光模式。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function getExposureMode(photoSession: camera.PhotoSession): camera.ExposureMode | undefined {
let exposureMode: camera.ExposureMode | undefined = undefined;
try {
exposureMode = photoSession.getExposureMode();
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The getExposureMode call failed. error code: ${err.code}`);
}
return exposureMode;
}
```
### setExposureMode11+
setExposureMode(aeMode: ExposureMode): void
设置曝光模式。进行设置之前,需要先检查设备是否支持指定的曝光模式,可使用方法[isExposureModeSupported](#isexposuremodesupported11)。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------------------------| ---- | ----------------------- |
| aeMode | [ExposureMode](#exposuremode) | 是 | 曝光模式。传参为null或者undefined,作为0处理,曝光锁定。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function setExposureMode(photoSession: camera.PhotoSession): void {
try {
photoSession.setExposureMode(camera.ExposureMode.EXPOSURE_MODE_LOCKED);
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The setExposureMode call failed. error code: ${err.code}`);
}
}
```
### getMeteringPoint11+
getMeteringPoint(): Point
查询曝光区域中心点。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| ---------- | ----------------------------- |
| [Point](#point) | 获取当前曝光点。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function getMeteringPoint(photoSession: camera.PhotoSession): camera.Point | undefined {
let exposurePoint: camera.Point | undefined = undefined;
try {
exposurePoint = photoSession.getMeteringPoint();
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The getMeteringPoint call failed. error code: ${err.code}`);
}
return exposurePoint;
}
```
### setMeteringPoint11+
setMeteringPoint(point: Point): void
设置曝光区域中心点,曝光点应在0-1坐标系内,该坐标系左上角为{0,0},右下角为{1,1}。
此坐标系是以设备充电口在右侧时的横向设备方向为基准的,例如应用的预览界面布局以
设备充电口在下侧时的竖向方向为基准,布局宽高为{w,h},且触碰点为{x,y},
则转换后的坐标点为{y/h,1-x/w}。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------------- | -------------------------------| ---- | ------------------- |
| point | [Point](#point) | 是 | 曝光点,x、y设置范围应在[0,1]之内,超过范围,如果小于0设置0,大于1设置1。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function setMeteringPoint(photoSession: camera.PhotoSession): void {
const point: camera.Point = {x: 1, y: 1};
try {
photoSession.setMeteringPoint(point);
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The setMeteringPoint call failed. error code: ${err.code}`);
}
}
```
### setExposureBias11+
setExposureBias(exposureBias: number): void
设置曝光补偿,曝光补偿值(EV)。
进行设置之前,建议先通过方法[getExposureBiasRange](#getexposurebiasrange11)查询支持的范围。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------------------------| ---- |-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| exposureBias | number | 是 | 曝光补偿,[getExposureBiasRange](#getexposurebiasrange11)查询支持的范围,如果设置超过支持范围的值,自动匹配到就近临界点。
曝光补偿存在步长,如步长为0.5。则设置1.2时,获取到实际生效曝光补偿为1.0。
接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400102 | Operation not allowed. |
| 7400103 | Session not config. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function setExposureBias(photoSession: camera.PhotoSession, biasRangeArray: Array): void {
if (biasRangeArray && biasRangeArray.length > 0) {
let exposureBias = biasRangeArray[0];
try {
photoSession.setExposureBias(exposureBias);
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The setExposureBias call failed. error code: ${err.code}`);
}
}
}
```
### getExposureValue11+
getExposureValue(): number
查询当前曝光值。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| ---------- | ----------------------------- |
| number | 获取曝光值。曝光补偿存在步长,如步长为0.5。则设置1.2时,获取到实际生效曝光补偿为1.0。
接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function getExposureValue(photoSession: camera.PhotoSession): number {
const invalidValue: number = -1;
let exposureValue: number = invalidValue;
try {
exposureValue = photoSession.getExposureValue();
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The getExposureValue call failed. error code: ${err.code}`);
}
return exposureValue;
}
```
## AutoExposureQuery12+
提供了针对设备的自动曝光特性提供了一系列查询功能。
### isExposureModeSupported11+
isExposureModeSupported(aeMode: ExposureMode): boolean
检测曝光模式是否支持。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------------------------| ---- | ----------------------------- |
| aeMode | [ExposureMode](#exposuremode) | 是 | 曝光模式。传参为null或者undefined,作为0处理,曝光锁定。 |
**返回值:**
| 类型 | 说明 |
| ---------- | ----------------------------- |
| boolean | 获取是否支持曝光模式。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config, only throw in session usage. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function isExposureModeSupported(photoSession: camera.PhotoSession): boolean {
let isSupported: boolean = false;
try {
isSupported = photoSession.isExposureModeSupported(camera.ExposureMode.EXPOSURE_MODE_LOCKED);
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The isExposureModeSupported call failed. error code: ${err.code}`);
}
return isSupported;
}
```
### getExposureBiasRange11+
getExposureBiasRange(): Array\
查询曝光补偿范围。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| ---------- | ----------------------------- |
| Array\ | 获取补偿范围的数组。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config, only throw in session usage. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function getExposureBiasRange(photoSession: camera.PhotoSession): Array {
let biasRangeArray: Array = [];
try {
biasRangeArray = photoSession.getExposureBiasRange();
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The getExposureBiasRange call failed. error code: ${err.code}`);
}
return biasRangeArray;
}
```
## Focus11+
Focus extends [FocusQuery](#focusquery12)
对焦类,对设备对焦操作。
### setFocusMode11+
setFocusMode(afMode: FocusMode): void
设置对焦模式。
进行设置之前,需要先检查设备是否支持指定的焦距模式,可使用方法[isFocusModeSupported](#isfocusmodesupported11)。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | ------------------- |
| afMode | [FocusMode](#focusmode) | 是 | 指定的焦距模式。传参为null或者undefined,作为0处理,手动对焦模式。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function setFocusMode(photoSession: camera.PhotoSession): void {
try {
photoSession.setFocusMode(camera.FocusMode.FOCUS_MODE_AUTO);
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The setFocusMode call failed. error code: ${err.code}`);
}
}
```
### getFocusMode11+
getFocusMode(): FocusMode
获取当前的对焦模式。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| ---------- | ----------------------------- |
| [FocusMode](#focusmode) | 获取当前设备的焦距模式。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function getFocusMode(photoSession: camera.PhotoSession): camera.FocusMode | undefined {
let afMode: camera.FocusMode | undefined = undefined;
try {
afMode = photoSession.getFocusMode();
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The getFocusMode call failed. error code: ${err.code}`);
}
return afMode;
}
```
### setFocusPoint11+
setFocusPoint(point: Point): void
设置焦点,焦点应在0-1坐标系内,该坐标系左上角为{0,0},右下角为{1,1}。
此坐标系是以设备充电口在右侧时的横向设备方向为基准的,例如应用的预览界面布局以
设备充电口在下侧时的竖向方向为基准,布局宽高为{w,h},且触碰点为{x,y},
则转换后的坐标点为{y/h,1-x/w}。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | ------------------- |
| point | [Point](#point) | 是 | 焦点。x、y设置范围应在[0,1]之内,超过范围,如果小于0设置0,大于1设置1。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function setFocusPoint(photoSession: camera.PhotoSession): void {
const focusPoint: camera.Point = {x: 1, y: 1};
try {
photoSession.setFocusPoint(focusPoint);
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The setFocusPoint call failed. error code: ${err.code}`);
}
}
```
### getFocusPoint11+
getFocusPoint(): Point
查询焦点。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| ---------- | ----------------------------- |
| [Point](#point) | 用于获取当前焦点。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function getFocusPoint(photoSession: camera.PhotoSession): camera.Point | undefined {
let point: camera.Point | undefined = undefined;
try {
point = photoSession.getFocusPoint();
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The getFocusPoint call failed. error code: ${err.code}`);
}
return point;
}
```
### getFocalLength11+
getFocalLength(): number
查询焦距值。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| ---------- | ----------------------------- |
| number | 用于获取当前焦距,单位mm。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function getFocalLength(photoSession: camera.PhotoSession): number {
const invalidValue: number = -1;
let focalLength: number = invalidValue;
try {
focalLength = photoSession.getFocalLength();
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The getFocalLength call failed. error code: ${err.code}`);
}
return focalLength;
}
```
## FocusQuery12+
提供了查询是否支持当前对焦模式的方法。
### isFocusModeSupported11+
isFocusModeSupported(afMode: FocusMode): boolean
检测对焦模式是否支持。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | -------------------------------- |
| afMode | [FocusMode](#focusmode) | 是 | 指定的焦距模式。传参为null或者undefined,作为0处理,手动对焦模式。 |
**返回值:**
| 类型 | 说明 |
| ---------- | ----------------------------- |
| boolean | 返回true表示支持该焦距模式,false表示不支持。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config, only throw in session usage. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function isFocusModeSupported(photoSession: camera.PhotoSession): boolean {
let status: boolean = false;
try {
status = photoSession.isFocusModeSupported(camera.FocusMode.FOCUS_MODE_AUTO);
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The isFocusModeSupported call failed. error code: ${err.code}`);
}
return status;
}
```
## SmoothZoomMode11+
平滑变焦模式。
**系统能力:** SystemCapability.Multimedia.Camera.Core
| 名称 | 值 | 说明 |
| ------------ | ---- | -------------- |
| NORMAL | 0 | 贝塞尔曲线模式。 |
## SmoothZoomInfo11+
平滑变焦参数信息。
**系统能力:** SystemCapability.Multimedia.Camera.Core
| 名称 | 类型 | 只读 | 可选 | 说明 |
| -------- | ---------- | -------- | -------- | ---------- |
| duration | number | 否 | 否 | 平滑变焦总时长,单位ms。 |
## Zoom11+
Zoom extends [ZoomQuery](#zoomquery12)
变焦类,对设备变焦操作。
### setZoomRatio11+
setZoomRatio(zoomRatio: number): void
设置变焦比,变焦精度最高为小数点后两位,如果设置超过支持的精度范围,则只保留精度范围内数值。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| --------- | -------------------- | ---- |---------------------------------------------------------------------------------------------------------------------------------|
| zoomRatio | number | 是 | 可变焦距比,通过[getZoomRatioRange](#getzoomratiorange11)获取支持的变焦范围,如果设置超过支持范围的值,则只保留精度范围内数值。
设置可变焦距比到底层生效需要一定时间,获取正确设置的可变焦距比需要等待1~2帧的时间。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function setZoomRatio(photoSession: camera.PhotoSession, zoomRatioRange: Array): void {
if (zoomRatioRange === undefined || zoomRatioRange.length <= 0) {
return;
}
let zoomRatio = zoomRatioRange[0];
try {
photoSession.setZoomRatio(zoomRatio);
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The setZoomRatio call failed. error code: ${err.code}`);
}
}
```
### getZoomRatio11+
getZoomRatio(): number
获取当前的变焦比。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| ---------- | ----------------------------- |
| number | 获取当前的变焦比结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config. |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function getZoomRatio(photoSession: camera.PhotoSession): number {
const invalidValue: number = -1;
let zoomRatio: number = invalidValue;
try {
zoomRatio = photoSession.getZoomRatio();
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The getZoomRatio call failed. error code: ${err.code}`);
}
return zoomRatio;
}
```
### setSmoothZoom11+
setSmoothZoom(targetRatio: number, mode?: SmoothZoomMode): void
触发平滑变焦。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------------ | -------------- | ---- | ----------------- |
| targetRatio | number | 是 | 目标值。 |
| mode | [SmoothZoomMode](#smoothzoommode11) | 否 | 模式。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function setSmoothZoom(sessionExtendsZoom: camera.Zoom, targetZoomRatio: number, mode: camera.SmoothZoomMode): void {
try {
sessionExtendsZoom.setSmoothZoom(targetZoomRatio, mode);
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The setSmoothZoom call failed. error code: ${err.code}`);
}
}
```
## ZoomQuery12+
提供了与设备的缩放相关的查询功能,包括获取支持的缩放比例范围。
### getZoomRatioRange11+
getZoomRatioRange(): Array\
获取支持的变焦范围。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| ---------- | ----------------------------- |
| Array\ | 用于获取可变焦距比范围,返回的数组包括其最小值和最大值。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config, only throw in session usage. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function getZoomRatioRange(photoSession: camera.PhotoSession): Array {
let zoomRatioRange: Array = [];
try {
zoomRatioRange = photoSession.getZoomRatioRange();
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The getZoomRatioRange call failed. error code: ${err.code}`);
}
return zoomRatioRange;
}
```
## Stabilization11+
Stabilization extends [StabilizationQuery](#stabilizationquery12)
提供设备在录像模式下设置视频防抖的操作。
> **说明:**
>
> 需要会话中有录像流([VideoOutput](#videooutput))的前提下,才可以对视频进行防抖设置,
> 其中[VideoStabilizationMode](#videostabilizationmode)中的枚举HIGH需要在[Profile](#profile)的分辨率为1920*1080的场景下生效。
### getActiveVideoStabilizationMode11+
getActiveVideoStabilizationMode(): VideoStabilizationMode
查询当前正在使用的视频防抖模式。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| ---------- |-------------|
| [VideoStabilizationMode](#videostabilizationmode) | 视频防抖是否正在使用。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function getActiveVideoStabilizationMode(videoSession: camera.VideoSession): camera.VideoStabilizationMode | undefined {
let vsMode: camera.VideoStabilizationMode | undefined = undefined;
try {
vsMode = videoSession.getActiveVideoStabilizationMode();
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The getActiveVideoStabilizationMode call failed. error code: ${err.code}`);
}
return vsMode;
}
```
### setVideoStabilizationMode11+
setVideoStabilizationMode(mode: VideoStabilizationMode): void
设置视频防抖模式。需要先检查设备是否支持对应的防抖模式,可以通过[isVideoStabilizationModeSupported](#isvideostabilizationmodesupported11)方法判断所设置的模式是否支持。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------------------------- | ---- | --------------------- |
| mode | [VideoStabilizationMode](#videostabilizationmode) | 是 | 需要设置的视频防抖模式。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function setVideoStabilizationMode(videoSession: camera.VideoSession): void {
try {
videoSession.setVideoStabilizationMode(camera.VideoStabilizationMode.OFF);
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The setVideoStabilizationMode call failed. error code: ${err.code}`);
}
}
```
## StabilizationQuery12+
提供了查询设备在录像模式下是否支持对应的视频防抖模式的能力。
### isVideoStabilizationModeSupported11+
isVideoStabilizationModeSupported(vsMode: VideoStabilizationMode): boolean
查询是否支持指定的视频防抖模式。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------------------------- | ---- | ------------------------------ |
| vsMode | [VideoStabilizationMode](#videostabilizationmode) | 是 | 视频防抖模式。 |
**返回值:**
| 类型 | 说明 |
| ---------- | ----------------------------- |
| boolean | 返回视频防抖模式是否支持,true表示支持,false表示不支持。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config, only throw in session usage. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function isVideoStabilizationModeSupported(videoSession: camera.VideoSession): boolean {
let isSupported: boolean = false;
try {
isSupported = videoSession.isVideoStabilizationModeSupported(camera.VideoStabilizationMode.OFF);
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The isVideoStabilizationModeSupported call failed. error code: ${err.code}`);
}
return isSupported;
}
```
## CaptureSession(deprecated)
拍照会话类,保存一次相机运行所需要的所有资源[CameraInput](#camerainput)、[CameraOutput](#cameraoutput),并向相机设备申请完成相机功能(录像,拍照)。
> **说明:**
>从 API version 10开始支持,从API version 11开始废弃。建议使用[PhotoSession](#photosession11)、[VideoSession](#videosession11)替代。
### beginConfig(deprecated)
beginConfig(): void
开始配置会话。
> **说明:**
>从 API version 10开始支持,从API version 11开始废弃。建议使用[Session.beginConfig](#beginconfig11)替代。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400105 | Session config locked. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function beginConfig(captureSession: camera.CaptureSession): void {
try {
captureSession.beginConfig();
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The beginConfig call failed. error code: ${err.code}`);
}
}
```
### commitConfig(deprecated)
commitConfig(callback: AsyncCallback\): void
提交配置信息,通过注册回调函数获取结果。使用callback异步回调。
> **说明:**
>从 API version 10开始支持,从API version 11开始废弃。建议使用[Session.commitConfig](#commitconfig11)替代。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------------- | ---- | -------------------- |
| callback | AsyncCallback\ | 是 | 回调函数,用于获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400102 | Operation not allowed. |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function commitConfig(captureSession: camera.CaptureSession): void {
captureSession.commitConfig((err: BusinessError) => {
if (err) {
console.error(`The commitConfig call failed. error code: ${err.code}`);
return;
}
console.info('Callback invoked to indicate the commit config success.');
});
}
```
### commitConfig(deprecated)
commitConfig(): Promise\
提交配置信息,通过Promise获取结果。
> **说明:**
>从 API version 10开始支持,从API version 11开始废弃。建议使用[Session.commitConfig](#commitconfig11-1)替代。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| -------------- |-------------------|
| Promise\ | 无返回结果的Promise对象。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400102 | Operation not allowed. |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function commitConfig(captureSession: camera.CaptureSession): void {
captureSession.commitConfig().then(() => {
console.info('Promise returned to indicate the commit config success.');
}).catch((error: BusinessError) => {
// 失败返回错误码error.code并处理
console.error(`The commitConfig call failed. error code: ${error.code}`);
});
}
```
### addInput(deprecated)
addInput(cameraInput: CameraInput): void
把[CameraInput](#camerainput)加入到会话。
> **说明:**
>从 API version 10开始支持,从API version 11开始废弃。建议使用[Session.addInput](#addinput11)替代。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----------- | --------------------------- | ---- | ------------------------ |
| cameraInput | [CameraInput](#camerainput) | 是 | 需要添加的CameraInput实例。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
|---------|-----------------------------------------------|
| 7400101 | Parameter missing or parameter type incorrect. |
| 7400102 | Operation not allowed. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function addInput(captureSession: camera.CaptureSession, cameraInput: camera.CameraInput): void {
try {
captureSession.addInput(cameraInput);
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The addInput call failed. error code: ${err.code}`);
}
}
```
### removeInput(deprecated)
removeInput(cameraInput: CameraInput): void
移除[CameraInput](#camerainput)。
> **说明:**
>从 API version 10开始支持,从API version 11开始废弃。建议使用[Session.removeInput](#removeinput11)替代。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----------- | --------------------------- | ---- | ------------------------ |
| cameraInput | [CameraInput](#camerainput) | 是 | 需要移除的CameraInput实例。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400101 | Parameter missing or parameter type incorrect. |
| 7400102 | Operation not allowed. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function removeInput(captureSession: camera.CaptureSession, cameraInput: camera.CameraInput): void {
try {
captureSession.removeInput(cameraInput);
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The removeInput call failed. error code: ${err.code}`);
}
}
```
### addOutput(deprecated)
addOutput(cameraOutput: CameraOutput): void
把[CameraOutput](#cameraoutput)加入到会话。
> **说明:**
>从 API version 10开始支持,从API version 11开始废弃。建议使用[Session.addOutput](#addoutput11)替代。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------------- | ------------------------------- | ---- | ------------------------ |
| cameraOutput | [CameraOutput](#cameraoutput) | 是 | 需要添加的CameraOutput实例。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400101 | Parameter missing or parameter type incorrect. |
| 7400102 | Operation not allowed. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function addOutput(captureSession: camera.CaptureSession, cameraOutput: camera.CameraOutput): void {
try {
captureSession.addOutput(cameraOutput);
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The addOutput call failed. error code: ${err.code}`);
}
}
```
### removeOutput(deprecated)
removeOutput(cameraOutput: CameraOutput): void
从会话中移除[CameraOutput](#cameraoutput)。
> **说明:**
>从 API version 10开始支持,从API version 11开始废弃。建议使用[Session.removeOutput](#removeoutput11)替代。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------------- | ------------------------------- | ---- | ------------------------ |
| cameraOutput | [CameraOutput](#cameraoutput) | 是 | 需要移除的CameraOutput实例。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400101 | Parameter missing or parameter type incorrect. |
| 7400102 | Operation not allowed. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function removeOutput(captureSession: camera.CaptureSession, previewOutput: camera.PreviewOutput): void {
try {
captureSession.removeOutput(previewOutput);
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The removeOutput call failed. error code: ${err.code}`);
}
}
```
### start(deprecated)
start(callback: AsyncCallback\): void
开始会话工作,通过注册回调函数获取结果。使用callback异步回调。
> **说明:**
>从 API version 10开始支持,从API version 11开始废弃。建议使用[Session.start](#start11)替代。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------------- | ---- | -------------------- |
| callback | AsyncCallback\ | 是 | 回调函数,用于获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config. |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function startCaptureSession(captureSession: camera.CaptureSession): void {
captureSession.start((err: BusinessError) => {
if (err) {
console.error(`Failed to start the session, error code: ${err.code}.`);
return;
}
console.info('Callback invoked to indicate the session start success.');
});
}
```
### start(deprecated)
start(): Promise\
开始会话工作,通过Promise获取结果。
> **说明:**
>从 API version 10开始支持,从API version 11开始废弃。建议使用[Session.start](#start11-1)替代。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| -------------- | ------------------------ |
| Promise\ | 无返回结果的Promise对象。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config. |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function startCaptureSession(captureSession: camera.CaptureSession): void {
captureSession.start().then(() => {
console.info('Promise returned to indicate the session start success.');
}).catch((err: BusinessError) => {
console.error(`Failed to start the session, error code: ${err.code}.`);
});
}
```
### stop(deprecated)
stop(callback: AsyncCallback\): void
停止会话工作,通过注册回调函数获取结果。使用callback异步回调。
> **说明:**
>从 API version 10开始支持,从API version 11开始废弃。建议使用[Session.stop](#stop11)替代。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------------- | ---- | ------------------- |
| callback | AsyncCallback\ | 是 | 回调函数,用于获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function stopCaptureSession(captureSession: camera.CaptureSession): void {
captureSession.stop((err: BusinessError) => {
if (err) {
console.error(`Failed to stop the session, error code: ${err.code}.`);
return;
}
console.info('Callback invoked to indicate the session stop success.');
});
}
```
### stop(deprecated)
stop(): Promise\
停止会话工作,通过Promise获取结果。
> **说明:**
>从 API version 10开始支持,从API version 11开始废弃。建议使用[Session.stop](#stop11-1)替代。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| -------------- | ----------------------- |
| Promise\ | 无返回结果的Promise对象。。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function stopCaptureSession(captureSession: camera.CaptureSession): void {
captureSession.stop().then(() => {
console.info('Promise returned to indicate the session stop success.');
}).catch((err: BusinessError) => {
console.error(`Failed to stop the session, error code: ${err.code}.`);
});
}
```
### release(deprecated)
release(callback: AsyncCallback\): void
释放会话资源,通过注册回调函数获取结果。使用callback异步回调。
> **说明:**
>从 API version 10开始支持,从API version 11开始废弃。建议使用[Session.release](#release11-1)替代。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------------- | ---- | -------------------- |
| callback | AsyncCallback\ | 是 | 回调函数,用于获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function releaseCaptureSession(captureSession: camera.CaptureSession): void {
captureSession.release((err: BusinessError) => {
if (err) {
console.error(`Failed to release the CaptureSession instance, error code: ${err.code}.`);
return;
}
console.info('Callback invoked to indicate that the CaptureSession instance is released successfully.');
});
}
```
### release(deprecated)
release(): Promise\
释放会话资源,通过Promise获取结果。
> **说明:**
>从 API version 10开始支持,从API version 11开始废弃。建议使用[Session.release](#release11-2)替代。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| -------------- | ------------------------ |
| Promise\ | 无返回结果的Promise对象。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function releaseCaptureSession(captureSession: camera.CaptureSession): void {
captureSession.release().then(() => {
console.info('Promise returned to indicate that the CaptureSession instance is released successfully.');
}).catch((err: BusinessError) => {
console.error(`Failed to release the CaptureSession instance, error code: ${err.code}.`);
});
}
```
### hasFlash(deprecated)
hasFlash(): boolean
检测是否有闪光灯。
> **说明:**
>从 API version 10开始支持,从API version 11开始废弃。建议使用[Flash.hasFlash](#hasflash11)替代。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| ---------- | ----------------------------- |
| boolean | 返回true表示设备支持闪光灯,false表示不支持。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function hasFlash(captureSession: camera.CaptureSession): boolean {
let status: boolean = false;
try {
status = captureSession.hasFlash();
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The hasFlash call failed. error code: ${err.code}`);
}
return status;
}
```
### isFlashModeSupported(deprecated)
isFlashModeSupported(flashMode: FlashMode): boolean
检测闪光灯模式是否支持。
> **说明:**
>从 API version 10开始支持,从API version 11开始废弃。建议使用[Flash.isFlashModeSupported](#isflashmodesupported11)替代。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| --------- | ----------------------- | ---- | --------------------------------- |
| flashMode | [FlashMode](#flashmode) | 是 | 指定闪光灯模式。 |
**返回值:**
| 类型 | 说明 |
| ---------- | ----------------------------- |
| boolean | 返回true表示支持该闪光灯模式,false表示不支持。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function isFlashModeSupported(captureSession: camera.CaptureSession): boolean {
let status: boolean = false;
try {
status = captureSession.isFlashModeSupported(camera.FlashMode.FLASH_MODE_AUTO);
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The isFlashModeSupported call failed. error code: ${err.code}`);
}
return status;
}
```
### setFlashMode(deprecated)
setFlashMode(flashMode: FlashMode): void
设置闪光灯模式。
进行设置之前,需要先检查:
1. 设备是否支持闪光灯,可使用方法[hasFlash](#hasflashdeprecated)。
2. 设备是否支持指定的闪光灯模式,可使用方法[isFlashModeSupported](#isflashmodesupporteddeprecated)。
> **说明:**
>从 API version 10开始支持,从API version 11开始废弃。建议使用[Flash.setFlashMode](#setflashmode11)替代。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| --------- | ----------------------- | ---- | -------------------- |
| flashMode | [FlashMode](#flashmode) | 是 | 指定闪光灯模式。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function setFlashMode(captureSession: camera.CaptureSession): void {
try {
captureSession.setFlashMode(camera.FlashMode.FLASH_MODE_AUTO);
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The setFlashMode call failed. error code: ${err.code}`);
}
}
```
### getFlashMode(deprecated)
getFlashMode(): FlashMode
获取当前设备的闪光灯模式。
> **说明:**
>从 API version 10开始支持,从API version 11开始废弃。建议使用[Flash.getFlashMode](#getflashmode11)替代。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| ---------- | ----------------------------- |
| [FlashMode](#flashmode) | 获取当前设备的闪光灯模式。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function getFlashMode(captureSession: camera.CaptureSession): camera.FlashMode | undefined {
let flashMode: camera.FlashMode | undefined = undefined;
try {
flashMode = captureSession.getFlashMode();
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The getFlashMode call failed.error code: ${err.code}`);
}
return flashMode;
}
```
### isExposureModeSupported(deprecated)
isExposureModeSupported(aeMode: ExposureMode): boolean
检测曝光模式是否支持。
> **说明:**
>从 API version 10开始支持,从API version 11开始废弃。建议使用[AutoExposure.isExposureModeSupported](#isexposuremodesupported11)替代。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------------------------| ---- | ----------------------------- |
| aeMode | [ExposureMode](#exposuremode) | 是 | 曝光模式。 |
**返回值:**
| 类型 | 说明 |
| ---------- | ----------------------------- |
| boolean | 获取是否支持曝光模式。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function isExposureModeSupported(captureSession: camera.CaptureSession): boolean {
let isSupported: boolean = false;
try {
isSupported = captureSession.isExposureModeSupported(camera.ExposureMode.EXPOSURE_MODE_LOCKED);
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The isExposureModeSupported call failed. error code: ${err.code}`);
}
return isSupported;
}
```
### getExposureMode(deprecated)
getExposureMode(): ExposureMode
获取当前曝光模式。
> **说明:**
>从 API version 10开始支持,从API version 11开始废弃。建议使用[AutoExposure.getExposureMode](#getexposuremode11)替代。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| ---------- | ----------------------------- |
| [ExposureMode](#exposuremode) | 获取当前曝光模式。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function getExposureMode(captureSession: camera.CaptureSession): camera.ExposureMode | undefined {
let exposureMode: camera.ExposureMode | undefined = undefined;
try {
exposureMode = captureSession.getExposureMode();
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The getExposureMode call failed. error code: ${err.code}`);
}
return exposureMode;
}
```
### setExposureMode(deprecated)
setExposureMode(aeMode: ExposureMode): void
设置曝光模式。进行设置之前,需要先检查设备是否支持指定的曝光模式,可使用方法[isExposureModeSupported](#isexposuremodesupporteddeprecated)。
> **说明:**
>从 API version 10开始支持,从API version 11开始废弃。建议使用[AutoExposure.setExposureMode](#setexposuremode11)替代。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------------------------| ---- | ----------------------- |
| aeMode | [ExposureMode](#exposuremode) | 是 | 曝光模式。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function setExposureMode(captureSession: camera.CaptureSession): void {
try {
captureSession.setExposureMode(camera.ExposureMode.EXPOSURE_MODE_LOCKED);
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The setExposureMode call failed. error code: ${err.code}`);
}
}
```
### getMeteringPoint(deprecated)
getMeteringPoint(): Point
查询曝光区域中心点。
> **说明:**
>从 API version 10开始支持,从API version 11开始废弃。建议使用[AutoExposure.getMeteringPoint](#getmeteringpoint11)替代。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| ---------- | ----------------------------- |
| [Point](#point) | 获取当前曝光点。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function getMeteringPoint(captureSession: camera.CaptureSession): camera.Point | undefined {
let exposurePoint: camera.Point | undefined = undefined;
try {
exposurePoint = captureSession.getMeteringPoint();
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The getMeteringPoint call failed. error code: ${err.code}`);
}
return exposurePoint;
}
```
### setMeteringPoint(deprecated)
setMeteringPoint(point: Point): void
设置曝光区域中心点,曝光点应在0-1坐标系内,该坐标系左上角为{0,0},右下角为{1,1}。
此坐标系是以设备充电口在右侧时的横向设备方向为基准的,例如应用的预览界面布局以
设备充电口在下侧时的竖向方向为基准,布局宽高为{w,h},且触碰点为{x,y},
则转换后的坐标点为{y/h,1-x/w}。
> **说明:**
>从 API version 10开始支持,从API version 11开始废弃。建议使用[AutoExposure.setMeteringPoint](#setmeteringpoint11)替代。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------------- | -------------------------------| ---- | ------------------- |
| point | [Point](#point) | 是 | 曝光点,x,y设置范围应在[0,1]之内,超过范围,如果小于0设置0,大于1设置1。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function setMeteringPoint(captureSession: camera.CaptureSession): void {
const point: camera.Point = {x: 1, y: 1};
try {
captureSession.setMeteringPoint(point);
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The setMeteringPoint call failed. error code: ${err.code}`);
}
}
```
### getExposureBiasRange(deprecated)
getExposureBiasRange(): Array\
查询曝光补偿范围。
> **说明:**
>从 API version 10开始支持,从API version 11开始废弃。建议使用[AutoExposure.getExposureBiasRange](#getexposurebiasrange11)替代。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| ---------- | ----------------------------- |
| Array\ | 获取补偿范围的数组。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function getExposureBiasRange(captureSession: camera.CaptureSession): Array {
let biasRangeArray: Array = [];
try {
biasRangeArray = captureSession.getExposureBiasRange();
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The getExposureBiasRange call failed. error code: ${err.code}`);
}
return biasRangeArray;
}
```
### setExposureBias(deprecated)
setExposureBias(exposureBias: number): void
设置曝光补偿,曝光补偿值(EV)。
进行设置之前,建议先通过方法[getExposureBiasRange](#getexposurebiasrangedeprecated)查询支持的范围。
> **说明:**
>从 API version 10开始支持,从API version 11开始废弃。建议使用[AutoExposure.setExposureBias](#setexposurebias11)替代。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------------------------|-----|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| exposureBias | number | 是 | 曝光补偿,[getExposureBiasRange](#getexposurebiasrange11)查询支持的范围,如果设置超过支持范围的值,自动匹配到就近临界点。曝光补偿存在步长,如步长为0.5。则设置1.2时,获取到实际生效曝光补偿为1.0。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。传参为null或者undefined,作为0处理,曝光补偿设置0。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function setExposureBias(captureSession: camera.CaptureSession, biasRangeArray: Array): void {
if (biasRangeArray && biasRangeArray.length > 0) {
let exposureBias = biasRangeArray[0];
try {
captureSession.setExposureBias(exposureBias);
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The setExposureBias call failed. error code: ${err.code}`);
}
}
}
```
### getExposureValue(deprecated)
getExposureValue(): number
查询当前曝光值。
> **说明:**
>从 API version 10开始支持,从API version 11开始废弃。建议使用[AutoExposure.getExposureValue](#getexposurevalue11)替代。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| ---------- | ----------------------------- |
| number | 获取曝光值。曝光补偿存在步长,如步长为0.5。则设置1.2时,获取到实际生效曝光补偿为1.0。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function getExposureValue(captureSession: camera.CaptureSession): number {
const invalidValue: number = -1;
let exposureValue: number = invalidValue;
try {
exposureValue = captureSession.getExposureValue();
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The getExposureValue call failed. error code: ${err.code}`);
}
return exposureValue;
}
```
### isFocusModeSupported(deprecated)
isFocusModeSupported(afMode: FocusMode): boolean
检测对焦模式是否支持。
> **说明:**
>从 API version 10开始支持,从API version 11开始废弃。建议使用[Focus.isFocusModeSupported](#isfocusmodesupported11)替代。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | -------------------------------- |
| afMode | [FocusMode](#focusmode) | 是 | 指定的焦距模式。 |
**返回值:**
| 类型 | 说明 |
| ---------- | ----------------------------- |
| boolean | 返回true表示支持该焦距模式,false表示不支持。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function isFocusModeSupported(captureSession: camera.CaptureSession): boolean {
let status: boolean = false;
try {
status = captureSession.isFocusModeSupported(camera.FocusMode.FOCUS_MODE_AUTO);
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The isFocusModeSupported call failed. error code: ${err.code}`);
}
return status;
}
```
### setFocusMode(deprecated)
setFocusMode(afMode: FocusMode): void
设置对焦模式。
进行设置之前,需要先检查设备是否支持指定的焦距模式,可使用方法[isFocusModeSupported](#isfocusmodesupporteddeprecated)。
> **说明:**
>从 API version 10开始支持,从API version 11开始废弃。建议使用[Focus.setFocusMode](#setfocusmode11)替代。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | ------------------- |
| afMode | [FocusMode](#focusmode) | 是 | 指定的焦距模式。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function setFocusMode(captureSession: camera.CaptureSession): void {
try {
captureSession.setFocusMode(camera.FocusMode.FOCUS_MODE_AUTO);
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The setFocusMode call failed. error code: ${err.code}`);
}
}
```
### getFocusMode(deprecated)
getFocusMode(): FocusMode
获取当前的对焦模式。
> **说明:**
>从 API version 10开始支持,从API version 11开始废弃。建议使用[Focus.getFocusMode](#getfocusmode11)替代。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| ---------- | ----------------------------- |
| [FocusMode](#focusmode) | 获取当前设备的焦距模式。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function getFocusMode(captureSession: camera.CaptureSession): camera.FocusMode | undefined {
let afMode: camera.FocusMode | undefined = undefined;
try {
afMode = captureSession.getFocusMode();
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The getFocusMode call failed. error code: ${err.code}`);
}
return afMode;
}
```
### setFocusPoint(deprecated)
setFocusPoint(point: Point): void
设置焦点,焦点应在0-1坐标系内,该坐标系左上角为{0,0},右下角为{1,1}。
此坐标系是以设备充电口在右侧时的横向设备方向为基准的,例如应用的预览界面布局以
设备充电口在下侧时的竖向方向为基准,布局宽高为{w,h},且触碰点为{x,y},
则转换后的坐标点为{y/h,1-x/w}。
> **说明:**
>从 API version 10开始支持,从API version 11开始废弃。建议使用[Focus.setFocusPoint](#setfocuspoint11)替代。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
|-------| ----------------------- |-----| ------------------- |
| point | [Point](#point) | 是 | 焦点。x,y设置范围应在[0,1]之内,超过范围,如果小于0设置0,大于1设置1。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function setFocusPoint(captureSession: camera.CaptureSession): void {
const focusPoint: camera.Point = {x: 1, y: 1};
try {
captureSession.setFocusPoint(focusPoint);
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The setFocusPoint call failed. error code: ${err.code}`);
}
}
```
### getFocusPoint(deprecated)
getFocusPoint(): Point
查询焦点。
> **说明:**
>从 API version 10开始支持,从API version 11开始废弃。建议使用[Focus.getFocusPoint](#getfocuspoint11)替代。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| ---------- | ----------------------------- |
| [Point](#point) | 用于获取当前焦点。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function getFocusPoint(captureSession: camera.CaptureSession): camera.Point | undefined {
let point: camera.Point | undefined = undefined;
try {
point = captureSession.getFocusPoint();
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The getFocusPoint call failed. error code: ${err.code}`);
}
return point;
}
```
### getFocalLength(deprecated)
getFocalLength(): number
查询焦距值。
> **说明:**
>从 API version 10开始支持,从API version 11开始废弃。建议使用[Focus.getFocalLength](#getfocallength11)替代。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| ---------- | ----------------------------- |
| number | 用于获取当前焦距。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function getFocalLength(captureSession: camera.CaptureSession): number {
const invalidValue: number = -1;
let focalLength: number = invalidValue;
try {
focalLength = captureSession.getFocalLength();
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The getFocalLength call failed. error code: ${err.code}`);
}
return focalLength;
}
```
### getZoomRatioRange(deprecated)
getZoomRatioRange(): Array\
获取支持的变焦范围。
> **说明:**
>从 API version 10开始支持,从API version 11开始废弃。建议使用[Zoom.getZoomRatioRange](#getzoomratiorange11)替代。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| ---------- | ----------------------------- |
| Array\ | 用于获取可变焦距比范围,返回的数组包括其最小值和最大值。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function getZoomRatioRange(captureSession: camera.CaptureSession): Array {
let zoomRatioRange: Array = [];
try {
zoomRatioRange = captureSession.getZoomRatioRange();
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The getZoomRatioRange call failed. error code: ${err.code}`);
}
return zoomRatioRange;
}
```
### setZoomRatio(deprecated)
setZoomRatio(zoomRatio: number): void
设置变焦比,变焦精度最高为小数点后两位,如果设置超过支持的精度范围,则只保留精度范围内数值。
> **说明:**
>从 API version 10开始支持,从API version 11开始废弃。建议使用[Zoom.setZoomRatio](#setzoomratio11)替代。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| --------- | -------------------- |-----| ------------------- |
| zoomRatio | number | 是 | 可变焦距比,通过[getZoomRatioRange](#getzoomratiorange11)获取支持的变焦范围,如果设置超过支持范围的值,则只保留精度范围内数值。传参为null或者undefined,作为0处理,变焦设置最小值。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function setZoomRatio(captureSession: camera.CaptureSession, zoomRatioRange: Array): void {
if (zoomRatioRange === undefined || zoomRatioRange.length <= 0) {
return;
}
let zoomRatio = zoomRatioRange[0];
try {
captureSession.setZoomRatio(zoomRatio);
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The setZoomRatio call failed. error code: ${err.code}`);
}
}
```
### getZoomRatio(deprecated)
getZoomRatio(): number
获取当前的变焦比。
> **说明:**
>从 API version 10开始支持,从API version 11开始废弃。建议使用[Zoom.getZoomRatio](#getzoomratio11)替代。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| ---------- | ----------------------------- |
| number | 获取当前的变焦比结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function getZoomRatio(captureSession: camera.CaptureSession): number {
const invalidValue: number = -1;
let zoomRatio: number = invalidValue;
try {
zoomRatio = captureSession.getZoomRatio();
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The getZoomRatio call failed. error code: ${err.code}`);
}
return zoomRatio;
}
```
### isVideoStabilizationModeSupported(deprecated)
isVideoStabilizationModeSupported(vsMode: VideoStabilizationMode): boolean
查询是否支持指定的视频防抖模式。
> **说明:**
>从 API version 10开始支持,从API version 11开始废弃。建议使用[Stabilization.isVideoStabilizationModeSupported](#isvideostabilizationmodesupported11)替代。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------------------------- | ---- | ------------------------------ |
| vsMode | [VideoStabilizationMode](#videostabilizationmode) | 是 | 视频防抖模式。传参为null或者undefined,作为0处理,超级防抖模式关闭。 |
**返回值:**
| 类型 | 说明 |
| ---------- | ----------------------------- |
| boolean | 返回视频防抖模式是否支持,true表示支持,false表示不支持。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function isVideoStabilizationModeSupported(captureSession: camera.CaptureSession): boolean {
let isSupported: boolean = false;
try {
isSupported = captureSession.isVideoStabilizationModeSupported(camera.VideoStabilizationMode.OFF);
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The isVideoStabilizationModeSupported call failed. error code: ${err.code}`);
}
return isSupported;
}
```
### getActiveVideoStabilizationMode(deprecated)
getActiveVideoStabilizationMode(): VideoStabilizationMode
查询当前正在使用的视频防抖模式。
> **说明:**
>从 API version 10开始支持,从API version 11开始废弃。建议使用[Stabilization.getActiveVideoStabilizationMode](#getactivevideostabilizationmode11)替代。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| ---------- | ----------------------------- |
| [VideoStabilizationMode](#videostabilizationmode) | 视频防抖是否正在使用。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function getActiveVideoStabilizationMode(captureSession: camera.CaptureSession): camera.VideoStabilizationMode | undefined {
let vsMode: camera.VideoStabilizationMode | undefined = undefined;
try {
vsMode = captureSession.getActiveVideoStabilizationMode();
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The getActiveVideoStabilizationMode call failed. error code: ${err.code}`);
}
return vsMode;
}
```
### setVideoStabilizationMode(deprecated)
setVideoStabilizationMode(mode: VideoStabilizationMode): void
设置视频防抖模式。需要先检查设备是否支持对应的防抖模式,可以通过[isVideoStabilizationModeSupported](#isvideostabilizationmodesupporteddeprecated)方法判断所设置的模式是否支持。
> **说明:**
>从 API version 10开始支持,从API version 11开始废弃。建议使用[Stabilization.setVideoStabilizationMode](#setvideostabilizationmode11)替代。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------------------------- | ---- | --------------------- |
| mode | [VideoStabilizationMode](#videostabilizationmode) | 是 | 需要设置的视频防抖模式。传参为null或者undefined,作为0处理,超级防抖模式关闭。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function setVideoStabilizationMode(captureSession: camera.CaptureSession): void {
try {
captureSession.setVideoStabilizationMode(camera.VideoStabilizationMode.OFF);
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The setVideoStabilizationMode call failed. error code: ${err.code}`);
}
}
```
### on('focusStateChange')(deprecated)
on(type: 'focusStateChange', callback: AsyncCallback\): void
监听相机聚焦的状态变化,通过注册回调函数获取结果。使用callback异步回调。
> **说明:**
> 从 API version 10开始支持,从API version 11开始废弃。建议使用[VideoSession.on('focusStateChange')](#onfocusstatechange11-1)替代。
>
> 当前注册监听接口,不支持在on监听的回调方法里,调用off注销回调。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------------------------- | ---- | ------------------------ |
| type | string | 是 | 监听事件,固定为'focusStateChange',session 创建成功可监听。仅当自动对焦模式时,且相机对焦状态发生改变时可触发该事件。 |
| callback | AsyncCallback\<[FocusState](#focusstate)\> | 是 | 回调函数,用于获取当前对焦状态。 |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function registerFocusStateChange(captureSession: camera.CaptureSession): void {
captureSession.on('focusStateChange', (err: BusinessError, focusState: camera.FocusState) => {
if (err !== undefined && err.code !== 0) {
console.error(`Callback Error, errorCode: ${err.code}`);
return;
}
console.info(`Focus state: ${focusState}`);
});
}
```
### off('focusStateChange')(deprecated)
off(type: 'focusStateChange', callback?: AsyncCallback\): void
注销监听相机聚焦的状态变化。
> **说明:**
>从 API version 10开始支持,从API version 11开始废弃。建议使用[VideoSession.off('focusStateChange')](#offfocusstatechange11-1)替代。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------------------------- | ---- | ------------------------ |
| type | string | 是 | 监听事件,固定为'focusStateChange',session 创建成功可监听。|
| callback | AsyncCallback\<[FocusState](#focusstate)\> | 否 | 回调函数,如果指定参数则取消对应callback(callback对象不可是匿名函数),否则取消所有callback。 |
**示例:**
```ts
function unregisterFocusStateChange(captureSession: camera.CaptureSession): void {
captureSession.off('focusStateChange');
}
```
### on('error')(deprecated)
on(type: 'error', callback: ErrorCallback): void
监听拍照会话的错误事件,通过注册回调函数获取结果。使用callback异步回调。
> **说明:**
>
> 当前注册监听接口,不支持在on监听的回调方法里,调用off注销回调。
> **说明:**
>从 API version 10开始支持,从API version 11开始废弃。建议使用[VideoSession.on('error')](#onerror11-1)替代。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- |--------------------------------------------------------------------------| ---- | ------------------------------ |
| type | string | 是 | 监听事件,固定为'error',session创建成功之后可监听该接口。session调用相关接口出现错误时会触发该事件,比如调用[beginConfig](#beginconfigdeprecated),[commitConfig](#commitconfigdeprecated-1),[addInput](#addinputdeprecated)等接口发生错误时返回错误信息。 |
| callback | [ErrorCallback](../apis-basic-services-kit/js-apis-base.md#errorcallback) | 是 | 回调函数,用于获取错误信息。返回错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function registerCaptureSessionError(captureSession: camera.CaptureSession): void {
captureSession.on('error', (error: BusinessError) => {
console.error(`Capture session error code: ${error.code}`);
});
}
```
### off('error')(deprecated)
off(type: 'error', callback?: ErrorCallback): void
注销监听拍照会话的错误事件,通过注册回调函数获取结果。
> **说明:**
>从 API version 10开始支持,从API version 11开始废弃。建议使用[VideoSession.off('error')](#offerror11-1)替代。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------------------------------------------- | ---- | ------------------------------ |
| type | string | 是 | 监听事件,固定为'error',session创建成功之后可监听该接口。 |
| callback | [ErrorCallback](../apis-basic-services-kit/js-apis-base.md#errorcallback)| 否 | 回调函数,如果指定参数则取消对应callback(callback对象不可是匿名函数),否则取消所有callback。 |
**示例:**
```ts
function unregisterCaptureSessionError(captureSession: camera.CaptureSession): void {
captureSession.off('error');
}
```
## ColorManagementQuery12+
色彩管理类,用于查询色彩空间参数。
### getSupportedColorSpaces12+
getSupportedColorSpaces(): Array\
获取支持的色彩空间列表。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| ----------------------------------------------- | ---------------------------- |
| Array<[colorSpaceManager.ColorSpace](../apis-arkgraphics2d/js-apis-colorSpaceManager.md#colorspace)>| 支持的色彩空间列表。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config, only throw in session usage. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
import { colorSpaceManager } from '@kit.ArkGraphics2D';
function getSupportedColorSpaces(session: camera.PhotoSession): Array {
let colorSpaces: Array = [];
try {
colorSpaces = session.getSupportedColorSpaces();
} catch (error) {
let err = error as BusinessError;
console.error(`The getSupportedColorSpaces call failed. error code: ${err.code}`);
}
return colorSpaces;
}
```
## ColorManagement12+
ColorManagement extends [ColorManagementQuery](#colormanagementquery12)
色彩管理类,继承自[ColorManagementQuery](#colormanagementquery12),用于设置色彩空间参数。
### setColorSpace12+
setColorSpace(colorSpace: colorSpaceManager.ColorSpace): void
设置色彩空间。可以先通过[getSupportedColorSpaces](#getsupportedcolorspaces12)获取当前设备所支持的ColorSpaces。
**P3广色域与HDR高动态范围成像**
应用可以下发不同的色彩空间(ColorSpace)参数来支持P3广色域以及HDR的功能。
当应用不主动设置色彩空间时,拍照以及录像模式默认为HDR拍摄效果。
在拍照模式下设置HDR高显效果可直接支持P3色域。
应用针对不同模式使能HDR效果以及设置的色彩空间可参考下表。
**录像模式:**
| SDR/HRD拍摄 | CameraFormat | ColorSpace |
|--------------------|--------------------------|------------------|
| SDR | CAMERA_FORMAT_YUV_420_SP | BT709_LIMIT |
| HDR_VIVID(Default) | CAMERA_FORMAT_YCRCB_P010 | BT2020_HLG_LIMIT |
**拍照模式:**
| SDR/HRD拍摄 | ColorSpace |
|--------------|------------|
| SDR | SRGB |
| HDR(Default) | DISPLAY_P3 |
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------------ |---------------------- | -- | -------------------------- |
| colorSpace | [colorSpaceManager.ColorSpace](../apis-arkgraphics2d/js-apis-colorSpaceManager.md#colorspace) | 是 | 色彩空间,通过[getSupportedColorSpaces](#getsupportedcolorspaces12)接口获取。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400101 | Parameter missing or parameter type incorrect. |
| 7400102 | The colorSpace does not match the format. |
| 7400103 | Session not config. |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
import { colorSpaceManager } from '@kit.ArkGraphics2D';
function setColorSpace(session: camera.PhotoSession, colorSpaces: Array): void {
if (colorSpaces === undefined || colorSpaces.length <= 0) {
return;
}
try {
session.setColorSpace(colorSpaces[0]);
} catch (error) {
let err = error as BusinessError;
console.error(`The setColorSpace call failed, error code: ${err.code}`);
}
}
```
### getActiveColorSpace12+
getActiveColorSpace(): colorSpaceManager.ColorSpace
获取当前设置的色彩空间。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| ----------------------------------------------- | ---------------------------- |
| [colorSpaceManager.ColorSpace](../apis-arkgraphics2d/js-apis-colorSpaceManager.md#colorspace) | 当前设置的色彩空间。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400103 | Session not config. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
import { colorSpaceManager } from '@kit.ArkGraphics2D';
function getActiveColorSpace(session: camera.PhotoSession): colorSpaceManager.ColorSpace | undefined {
let colorSpace: colorSpaceManager.ColorSpace | undefined = undefined;
try {
colorSpace = session.getActiveColorSpace();
} catch (error) {
let err = error as BusinessError;
console.error(`The getActiveColorSpace call failed. error code: ${err.code}`);
}
return colorSpace;
}
```
## AutoDeviceSwitchQuery13+
自动切换镜头查询类,用于查询设备是否支持自动切换镜头。
### isAutoDeviceSwitchSupported13+
isAutoDeviceSwitchSupported(): boolean
查询设备是否支持自动切换镜头能力。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**返回值:**
| 类型 | 说明 |
| ----------------------------------------------- |-------------|
| boolean | 是否支持自动切换镜头。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- |---------------------------------------------------|
| 7400103 | Session not config, only throw in session usage. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function isAutoDeviceSwitchSupported(session: camera.PhotoSession): boolean {
let isSupported = false;
try {
isSupported = session.isAutoDeviceSwitchSupported();
} catch (error) {
let err = error as BusinessError;
console.error(`The isAutoDeviceSwitchSupported call failed, error code: ${err.code}`);
}
return isSupported;
}
```
## AutoDeviceSwitch13+
AutoDeviceSwitch extends [AutoDeviceSwitchQuery](#autodeviceswitchquery13)
自动切换镜头类,继承自[AutoDeviceSwitchQuery](#autodeviceswitchquery13),用于使能或去使能自动切换镜头。
使用建议:自动切换镜头功能由系统自动完成输入设备切换、会话配置和参数接续,
如系统发现镜头切换时,两颗镜头的变焦范围不一致,则会通过[AutoDeviceSwitchStatus](#autodeviceswitchstatus13)中的isDeviceCapabilityChanged字段告知应用,
但仍需要应用自己处理UX的变更(如变焦范围的调整,需要重新通过[getZoomRatioRange](#getzoomratiorange11)接口获取数据并更新UX),
因此更适用于极简UX交换的场景。
### enableAutoDeviceSwitch13+
enableAutoDeviceSwitch(enabled: boolean): void
使能或去使能自动切换镜头。可以先通过[isAutoDeviceSwitchSupported](#isautodeviceswitchsupported13)获取当前设备是否支持自动切换镜头。
> **说明:**
> 该接口仅用于有多个前置镜头的折叠设备,在不同的折叠状态下可自动切换到当前可使用的前置镜头。无法实现前后置镜头的切换。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----------- |---------------------- |---| -------------------------- |
| enabled | boolean | 是 | 使能或去使能自动切换镜头。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
|----------|------------------------------------------------------------------------------------------------------------------------------------------------|
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameters verification failed. |
| 7400102 | Operation not allowed. |
| 7400103 | Session not config. |
| 7400201 | Camera service fatal error. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function enableAutoDeviceSwitch(session: camera.PhotoSession, isEnable: boolean): void {
try {
session.enableAutoDeviceSwitch(isEnable);
} catch (error) {
let err = error as BusinessError;
console.error(`The enableAutoDeviceSwitch call failed, error code: ${err.code}`);
}
}
```
## PreconfigType12+
枚举,提供预配置的类型。
**系统能力:** SystemCapability.Multimedia.Camera.Core
| 名称 | 值 | 说明 |
|-------------------------|---|------------|
| PRECONFIG_720P | 0 | 720P预配置。 |
| PRECONFIG_1080P | 1 | 1080P预配置。 |
| PRECONFIG_4K | 2 | 4K预配置。 |
| PRECONFIG_HIGH_QUALITY | 3 | 高质量预配置。 |
## PreconfigRatio12+
枚举,提供预配置的分辨率比例。
**系统能力:** SystemCapability.Multimedia.Camera.Core
| 名称 | 值 | 说明 |
|--------------------------|---|---------|
| PRECONFIG_RATIO_1_1 | 0 | 1:1画幅。 |
| PRECONFIG_RATIO_4_3 | 1 | 4:3画幅。 |
| PRECONFIG_RATIO_16_9 | 2 | 16:9画幅。 |
## PhotoSession11+
PhotoSession extends [Session](#session11), [Flash](#flash11), [AutoExposure](#autoexposure11), [Focus](#focus11), [Zoom](#zoom11), [ColorManagement](#colormanagement12), [AutoDeviceSwitch](#autodeviceswitch13)
普通拍照模式会话类,提供了对闪光灯、曝光、对焦、变焦、色彩空间的操作。
> **说明:**
>
> 默认的拍照模式,用于拍摄标准照片。支持多种照片格式和分辨率,适合大多数日常拍摄场景。
### canPreconfig12+
canPreconfig(preconfigType: PreconfigType, preconfigRatio?: PreconfigRatio): boolean
查询当前Session是否支持指定的预配置类型。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
|----------------|-------------------------------------|-----|-----------------|
| preconfigType | [PreconfigType](#preconfigtype12) | 是 | 指定配置预期分辨率。 |
| preconfigRatio | [PreconfigRatio](#preconfigratio12) | 否 | 可选画幅比例,默认为4:3。 |
**返回值:**
| 类型 | 说明 |
|---------|-----------------------------------------|
| boolean | true: 支持指定预配值类型。
false: 不支持指定预配值类型。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
|---------|-----------------------------|
| 7400201 | Camera service fatal error. |
**示例:**
```ts
function testCanPreconfig(photoSession: camera.PhotoSession, preconfigType: camera.PreconfigType,
preconfigRatio: camera.PreconfigRatio): void {
try {
let result = photoSession.canPreconfig(preconfigType, preconfigRatio);
console.info(`canPreconfig ${preconfigType} ${preconfigRatio} result is : ${result}`);
} catch (error) {
let err = error as BusinessError;
console.error(`The canPreconfig call failed. error code: ${err.code}`);
}
}
```
### preconfig12+
preconfig(preconfigType: PreconfigType, preconfigRatio?: PreconfigRatio): void
对当前Session进行预配置。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
|----------------|-------------------------------------|-----|-----------------|
| preconfigType | [PreconfigType](#preconfigtype12) | 是 | 指定配置预期分辨率。 |
| preconfigRatio | [PreconfigRatio](#preconfigratio12) | 否 | 可选画幅比例,默认为4:3。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
|---------|-----------------------------|
| 7400201 | Camera service fatal error. |
**示例:**
```ts
function testPreconfig(photoSession: camera.PhotoSession, preconfigType: camera.PreconfigType,
preconfigRatio: camera.PreconfigRatio): void {
try {
photoSession.preconfig(preconfigType, preconfigRatio);
console.info(`preconfig ${preconfigType} ${preconfigRatio} success`);
} catch (error) {
let err = error as BusinessError;
console.error(`The preconfig call failed. error code: ${err.code}`);
}
}
```
### on('error')11+
on(type: 'error', callback: ErrorCallback): void
监听普通拍照会话的错误事件,通过注册回调函数获取结果。使用callback异步回调。
> **说明:**
>
> 当前注册监听接口,不支持在on监听的回调方法里,调用off注销回调。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------------------------------------------- | ---- | ------------------------------ |
| type | string | 是 | 监听事件,固定为'error',session创建成功之后可监听该接口。session调用相关接口出现错误时会触发该事件,比如调用[beginConfig](#beginconfig11),[commitConfig](#commitconfig11-1),[addInput](#addinput11)等接口发生错误时返回错误信息。 |
| callback | [ErrorCallback](../apis-basic-services-kit/js-apis-base.md#errorcallback)| 是 | 回调函数,用于获取错误信息。返回错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function callback(err: BusinessError): void {
console.error(`Photo session error code: ${err.code}`);
}
function registerSessionError(photoSession: camera.PhotoSession): void {
photoSession.on('error', callback);
}
```
### off('error')11+
off(type: 'error', callback?: ErrorCallback): void
注销监听普通拍照会话的错误事件,通过注册回调函数获取结果。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------------------------- | ---- | ------------------------------ |
| type | string | 是 | 监听事件,固定为'error',session创建成功之后可监听该接口。 |
| callback | [ErrorCallback](../apis-basic-services-kit/js-apis-base.md#errorcallback)| 否 | 回调函数,如果指定参数则取消对应callback(callback对象不可是匿名函数),否则取消所有callback。 |
**示例:**
```ts
function unregisterSessionError(photoSession: camera.PhotoSession): void {
photoSession.off('error');
}
```
### on('focusStateChange')11+
on(type: 'focusStateChange', callback: AsyncCallback\): void
监听相机聚焦的状态变化,通过注册回调函数获取结果。使用callback异步回调。
> **说明:**
>
> 当前注册监听接口,不支持在on监听的回调方法里,调用off注销回调。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------- | ---- | ------------------------ |
| type | string | 是 | 监听事件,固定为'focusStateChange',session创建成功可监听。仅当自动对焦模式时,且相机对焦状态发生改变时可触发该事件。 |
| callback | AsyncCallback\<[FocusState](#focusstate)\> | 是 | 回调函数,用于获取当前对焦状态。 |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function callback(err: BusinessError, focusState: camera.FocusState): void {
if (err !== undefined && err.code !== 0) {
console.error(`Callback Error, errorCode: ${err.code}`);
return;
}
console.info(`Focus state: ${focusState}`);
}
function registerFocusStateChange(photoSession: camera.PhotoSession): void {
photoSession.on('focusStateChange', callback);
}
```
### off('focusStateChange')11+
off(type: 'focusStateChange', callback?: AsyncCallback\): void
注销监听相机聚焦的状态变化。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------------------------- | ---- | ------------------------ |
| type | string | 是 | 监听事件,固定为'focusStateChange',session创建成功可监听。 |
| callback | AsyncCallback\<[FocusState](#focusstate)\> | 否 | 回调函数,如果指定参数则取消对应callback(callback对象不可是匿名函数),否则取消所有callback。 |
**示例:**
```ts
function unregisterFocusStateChange(photoSession: camera.PhotoSession): void {
photoSession.off('focusStateChange');
}
```
### on('smoothZoomInfoAvailable')11+
on(type: 'smoothZoomInfoAvailable', callback: AsyncCallback\): void
监听相机平滑变焦的状态变化,通过注册回调函数获取结果。使用callback异步回调。
> **说明:**
>
> 当前注册监听接口,不支持在on监听的回调方法里,调用off注销回调。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | ------------------------ |
| type | string | 是 | 监听事件,固定为'smoothZoomInfoAvailable',session创建成功可监听。|
| callback | AsyncCallback\<[SmoothZoomInfo](#smoothzoominfo11)\> | 是 | 回调函数,用于获取当前平滑变焦状态。 |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function callback(err: BusinessError, smoothZoomInfo: camera.SmoothZoomInfo): void {
if (err !== undefined && err.code !== 0) {
console.error(`Callback Error, errorCode: ${err.code}`);
return;
}
console.info(`The duration of smooth zoom: ${smoothZoomInfo.duration}`);
}
function registerSmoothZoomInfo(photoSession: camera.PhotoSession): void {
photoSession.on('smoothZoomInfoAvailable', callback);
}
```
### off('smoothZoomInfoAvailable')11+
off(type: 'smoothZoomInfoAvailable', callback?: AsyncCallback\): void
注销监听相机平滑变焦的状态变化。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------------------------- | ---- | ------------------------ |
| type | string | 是 | 监听事件,固定为'smoothZoomInfoAvailable',session创建成功可监听。|
| callback | AsyncCallback\<[SmoothZoomInfo](#smoothzoominfo11)\> | 否 | 回调函数,如果指定参数则取消对应callback(callback对象不可是匿名函数),否则取消所有callback。 |
**示例:**
```ts
function unregisterSmoothZoomInfo(photoSession: camera.PhotoSession): void {
photoSession.off('smoothZoomInfoAvailable');
}
```
### on('autoDeviceSwitchStatusChange')13+
on(type: 'autoDeviceSwitchStatusChange', callback: AsyncCallback\): void
监听相机自动切换镜头状态变化,通过注册回调函数获取结果。使用callback异步回调。
> **说明:**
>
> 当前注册监听接口,不支持在on监听的回调方法里,调用off注销回调。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- |----------------------------------------------------------------------| ---- |--------------------------------------------------------|
| type | string | 是 | 监听事件,固定为'autoDeviceSwitchStatusChange',session创建成功可监听。 |
| callback | AsyncCallback\<[AutoDeviceSwitchStatus](#autodeviceswitchstatus13)\> | 是 | 回调函数,用于获取当前自动切换镜头的状态。 |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function callback(err: BusinessError, autoDeviceSwitchStatus: camera.AutoDeviceSwitchStatus): void {
if (err !== undefined && err.code !== 0) {
console.error(`Callback Error, errorCode: ${err.code}`);
return;
}
console.info(`isDeviceSwitched: ${autoDeviceSwitchStatus.isDeviceSwitched}, isDeviceCapabilityChanged: ${autoDeviceSwitchStatus.isDeviceCapabilityChanged}`);
}
function registerAutoDeviceSwitchStatus(photoSession: camera.PhotoSession): void {
photoSession.on('autoDeviceSwitchStatusChange', callback);
}
```
### off('autoDeviceSwitchStatusChange')13+
off(type: 'autoDeviceSwitchStatusChange', callback?: AsyncCallback\): void
注销监听相机自动切换镜头状态变化。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- |----------------------------------------------| ---- | ------------------------ |
| type | string | 是 | 监听事件,固定为'autoDeviceSwitchStatusChange',session创建成功可监听。|
| callback | AsyncCallback\<[AutoDeviceSwitchStatus](#autodeviceswitchstatus13)\> | 否 | 回调函数,如果指定参数则取消对应callback(callback对象不可是匿名函数),否则取消所有callback。 |
**示例:**
```ts
function unregisterSmoothZoomInfo(photoSession: camera.PhotoSession): void {
photoSession.off('autoDeviceSwitchStatusChange');
}
```
## QualityPrioritization14+
枚举,录像质量优先级。
**系统能力:** SystemCapability.Multimedia.Camera.Core
| 名称 | 值 | 说明 |
| ------------- | --- | ---------- |
| HIGH_QUALITY | 0 | 高录像质量。 |
| POWER_BALANCE | 1 | 功耗平衡的录像质量。 |
## VideoSession11+
VideoSession extends [Session](#session11), [Flash](#flash11), [AutoExposure](#autoexposure11), [Focus](#focus11), [Zoom](#zoom11), [Stabilization](#stabilization11), [ColorManagement](#colormanagement12), [AutoDeviceSwitch](#autodeviceswitch13)
普通录像模式会话类,提供了对闪光灯、曝光、对焦、变焦、视频防抖、色彩空间的操作。
> **说明:**
>
> 默认的视频录制模式,适用于一般场景。支持720P、1080p等多种分辨率的录制,可选择不同帧率(如30fps、60fps)。
### canPreconfig12+
canPreconfig(preconfigType: PreconfigType, preconfigRatio?: PreconfigRatio): boolean
查询当前Session是否支持指定的与配置类型。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
|----------------|-------------------------------------|-----|-----------------|
| preconfigType | [PreconfigType](#preconfigtype12) | 是 | 指定配置预期分辨率。 |
| preconfigRatio | [PreconfigRatio](#preconfigratio12) | 否 | 可选画幅比例,默认为16:9。 |
**返回值:**
| 类型 | 说明 |
|---------|-----------------------------------------|
| boolean | true: 支持指定预配值类型。
false: 不支持指定预配值类型。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
|---------|-----------------------------|
| 7400201 | Camera service fatal error. |
**示例:**
```ts
function testCanPreconfig(videoSession: camera.VideoSession, preconfigType: camera.PreconfigType,
preconfigRatio: camera.PreconfigRatio): void {
try {
let result = videoSession.canPreconfig(preconfigType, preconfigRatio);
console.info(`canPreconfig ${preconfigType} ${preconfigRatio} result is : ${result}`);
} catch (error) {
let err = error as BusinessError;
console.error(`The canPreconfig call failed. error code: ${err.code}`);
}
}
```
### preconfig12+
preconfig(preconfigType: PreconfigType, preconfigRatio?: PreconfigRatio): void
对当前Session进行预配置。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
|----------------|-------------------------------------|-----|-----------------|
| preconfigType | [PreconfigType](#preconfigtype12) | 是 | 指定配置预期分辨率。 |
| preconfigRatio | [PreconfigRatio](#preconfigratio12) | 否 | 可选画幅比例,默认为16:9。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
|---------|-----------------------------|
| 7400201 | Camera service fatal error. |
**示例:**
```ts
function testPreconfig(videoSession: camera.VideoSession, preconfigType: camera.PreconfigType,
preconfigRatio: camera.PreconfigRatio): void {
try {
videoSession.preconfig(preconfigType, preconfigRatio);
console.info(`preconfig ${preconfigType} ${preconfigRatio} success`);
} catch (error) {
let err = error as BusinessError;
console.error(`The preconfig call failed. error code: ${err.code}`);
}
}
```
### on('error')11+
on(type: 'error', callback: ErrorCallback): void
监听普通录像会话的错误事件,通过注册回调函数获取结果。使用callback异步回调。
> **说明:**
>
> 当前注册监听接口,不支持在on监听的回调方法里,调用off注销回调。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------ | ---- | ------------------------------ |
| type | string | 是 | 监听事件,固定为'error',session创建成功之后可监听该接口。session调用相关接口出现错误时会触发该事件,比如调用[beginConfig](#beginconfig11),[commitConfig](#commitconfig11-1),[addInput](#addinput11)等接口发生错误时返回错误信息。 |
| callback | [ErrorCallback](../apis-basic-services-kit/js-apis-base.md#errorcallback) | 是 | 回调函数,用于获取错误信息。返回错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function callback(err: BusinessError): void {
console.error(`Video session error code: ${err.code}`);
}
function registerSessionError(videoSession: camera.VideoSession): void {
videoSession.on('error', callback);
}
```
### off('error')11+
off(type: 'error', callback?: ErrorCallback): void
注销监听普通录像会话的错误事件,通过注册回调函数获取结果。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | ------------------------------ |
| type | string | 是 | 监听事件,固定为'error',session创建成功之后可监听该接口。 |
| callback | [ErrorCallback](../apis-basic-services-kit/js-apis-base.md#errorcallback)| 否 | 回调函数,如果指定参数则取消对应callback(callback对象不可是匿名函数),否则取消所有callback。 |
**示例:**
```ts
function unregisterSessionError(videoSession: camera.VideoSession): void {
videoSession.off('error');
}
```
### on('focusStateChange')11+
on(type: 'focusStateChange', callback: AsyncCallback\): void
监听相机聚焦的状态变化,通过注册回调函数获取结果。使用callback异步回调。
> **说明:**
>
> 当前注册监听接口,不支持在on监听的回调方法里,调用off注销回调。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------- | ---- | ------------------------ |
| type | string | 是 | 监听事件,固定为'focusStateChange',session创建成功可监听。仅当自动对焦模式时,且相机对焦状态发生改变时可触发该事件。 |
| callback | AsyncCallback\<[FocusState](#focusstate)\> | 是 | 回调函数,用于获取当前对焦状态。 |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function callback(err: BusinessError, focusState: camera.FocusState): void {
if (err !== undefined && err.code !== 0) {
console.error(`Callback Error, errorCode: ${err.code}`);
return;
}
console.info(`Focus state: ${focusState}`);
}
function registerFocusStateChange(videoSession: camera.VideoSession): void {
videoSession.on('focusStateChange', callback);
}
```
### off('focusStateChange')11+
off(type: 'focusStateChange', callback?: AsyncCallback\): void
注销监听相机聚焦的状态变化。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------------------------- | ---- | ------------------------ |
| type | string | 是 | 监听事件,固定为'focusStateChange',session创建成功可监听。 |
| callback | AsyncCallback\<[FocusState](#focusstate)\> | 否 | 回调函数,如果指定参数则取消对应callback(callback对象不可是匿名函数),否则取消所有callback。 |
**示例:**
```ts
function unregisterFocusStateChange(videoSession: camera.VideoSession): void {
videoSession.off('focusStateChange');
}
```
### on('smoothZoomInfoAvailable')11+
on(type: 'smoothZoomInfoAvailable', callback: AsyncCallback\): void
监听相机平滑变焦的状态变化,通过注册回调函数获取结果。使用callback异步回调。
> **说明:**
>
> 当前注册监听接口,不支持在on监听的回调方法里,调用off注销回调。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | ------------------------ |
| type | string | 是 | 监听事件,固定为'smoothZoomInfoAvailable',session创建成功可监听。|
| callback | AsyncCallback\<[SmoothZoomInfo](#smoothzoominfo11)\> | 是 | 回调函数,用于获取当前平滑变焦状态。 |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function callback(err: BusinessError, smoothZoomInfo: camera.SmoothZoomInfo): void {
if (err !== undefined && err.code !== 0) {
console.error(`Callback Error, errorCode: ${err.code}`);
return;
}
console.info(`The duration of smooth zoom: ${smoothZoomInfo.duration}`);
}
function registerSmoothZoomInfo(videoSession: camera.VideoSession): void {
videoSession.on('smoothZoomInfoAvailable', callback);
}
```
### off('smoothZoomInfoAvailable')11+
off(type: 'smoothZoomInfoAvailable', callback?: AsyncCallback\): void
注销监听相机平滑变焦的状态变化。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------------------------- | ---- | ------------------------ |
| type | string | 是 | 监听事件,固定为'smoothZoomInfoAvailable',session创建成功可监听。|
| callback | AsyncCallback\<[SmoothZoomInfo](#smoothzoominfo11)\> | 否 | 回调函数,如果指定参数则取消对应callback(callback对象不可是匿名函数),否则取消所有callback。 |
**示例:**
```ts
function unregisterSmoothZoomInfo(videoSession: camera.VideoSession): void {
videoSession.off('smoothZoomInfoAvailable');
}
```
### on('autoDeviceSwitchStatusChange')13+
on(type: 'autoDeviceSwitchStatusChange', callback: AsyncCallback\): void
监听相机自动切换镜头状态变化,通过注册回调函数获取结果。使用callback异步回调。
> **说明:**
>
> 当前注册监听接口,不支持在on监听的回调方法里,调用off注销回调。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- |----------------------------------------------------------------------| ---- | ------------------------ |
| type | string | 是 | 监听事件,固定为'autoDeviceSwitchStatusChange',session创建成功可监听。|
| callback | AsyncCallback\<[AutoDeviceSwitchStatus](#autodeviceswitchstatus13)\> | 是 | 回调函数,用于获取当前自动切换镜头的状态。 |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function callback(err: BusinessError, autoDeviceSwitchStatus: camera.AutoDeviceSwitchStatus): void {
if (err !== undefined && err.code !== 0) {
console.error(`Callback Error, errorCode: ${err.code}`);
return;
}
console.info(`isDeviceSwitched: ${autoDeviceSwitchStatus.isDeviceSwitched}, isDeviceCapabilityChanged: ${autoDeviceSwitchStatus.isDeviceCapabilityChanged}`);
}
function registerAutoDeviceSwitchStatus(videoSession: camera.VideoSession): void {
videoSession.on('autoDeviceSwitchStatusChange', callback);
}
```
### off('autoDeviceSwitchStatusChange')13+
off(type: 'autoDeviceSwitchStatusChange', callback?: AsyncCallback\): void
注销监听相机自动切换镜头状态变化。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- |----------------------------------------------| ---- | ------------------------ |
| type | string | 是 | 监听事件,固定为'autoDeviceSwitchStatusChange',session创建成功可监听。|
| callback | AsyncCallback\<[AutoDeviceSwitchStatus](#autodeviceswitchstatus13)\> | 否 | 回调函数,如果指定参数则取消对应callback(callback对象不可是匿名函数),否则取消所有callback。 |
**示例:**
```ts
function unregisterSmoothZoomInfo(videoSession: camera.VideoSession): void {
videoSession.off('autoDeviceSwitchStatusChange');
}
```
### setQualityPrioritization14+
setQualityPrioritization(quality : QualityPrioritization) : void;
设置录像质量优先级。
> **说明:**
> 默认为高质量,设置为功耗平衡将降低录像质量以减少功耗。实际功耗收益因平台而异。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------------------------------------------------- | ---- | ------------------------------------------ |
| quality | [QualityPrioritization](#qualityprioritization14) | 是 | 需要设置的视频质量优先级(默认为高质量)。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| -------- | ----------------------------------------------------------------------------------------------------------------------------------------------- |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
| 7400103 | Session not config. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function setQualityPrioritization(videoSession: camera.VideoSession): void {
try {
videoSession.setQualityPrioritization(camera.QualityPrioritization.POWER_BALANCE);
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The setQualityPrioritization call failed. error code: ${err.code}`);
}
}
```
## SecureSession12+
SecureSession extends [Session](#session11), [Flash](#flash11), [AutoExposure](#autoexposure11), [Focus](#focus11), [Zoom](#zoom11)
安全模式会话类,提供了对闪光灯、曝光、对焦、变焦的操作。
> **说明:**
>
> 通过[createSession](#createsession11)接口传入[SceneMode](#scenemode11)为SECURE_PHOTO模式创建一个安全模式的会话。该模式开放给人脸识别、银行等有安全诉求的应用,需要结合安全TA使用,支持同时出普通预览流和安全流的业务场景。
>
安全TA:可用于图片处理,它具备验证服务器下发数据的验签能力、图片签名、解析及组装tlv逻辑的能力,还具备密钥读取、创建及操作能力。
### addSecureOutput12+
addSecureOutput(previewOutput: PreviewOutput): void
把其中一条[PreviewOutput](#previewoutput)标记成安全输出。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------------- | ------------------------------- | ---- |---------------|
| previewOutput | [PreviewOutput](#previewoutput) | 是 | 需要标记成安全输出的预览流,传参异常时,会返回错误码。 |
**错误码:**
以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
| 错误码ID | 错误信息 |
| --------------- | --------------- |
| 7400101 | Parameter missing or parameter type incorrect. |
| 7400102 | Operation not allowed. |
| 7400103 | Session not config. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function addSecureOutput(session: camera.SecureSession, previewOutput: camera.PreviewOutput): void {
try {
session.addSecureOutput(previewOutput);
} catch (error) {
// 失败返回错误码error.code并处理
let err = error as BusinessError;
console.error(`The addOutput call failed. error code: ${err.code}`);
}
}
```
### on('error')12+
on(type: 'error', callback: ErrorCallback): void
监听安全相机会话的错误事件,通过注册回调函数获取结果。使用callback异步回调。
> **说明:**
>
> 当前注册监听接口,不支持在on监听的回调方法里,调用off注销回调。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------ | ---- | ------------------------------ |
| type | string | 是 | 监听事件,固定为'error',session创建成功之后可监听该接口。session调用相关接口出现错误时会触发该事件,比如调用[beginConfig](#beginconfig11),[commitConfig](#commitconfig11-1),[addInput](#addinput11)等接口发生错误时返回错误信息。 |
| callback | [ErrorCallback](../apis-basic-services-kit/js-apis-base.md#errorcallback) | 是 | 回调函数,用于获取错误信息。返回错误码,错误码类型[CameraErrorCode](#cameraerrorcode)。 |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function callback(err: BusinessError): void {
console.error(`Video session error code: ${err.code}`);
}
function registerSessionError(secureSession: camera.SecureSession): void {
secureSession.on('error', callback);
}
```
### off('error')12+
off(type: 'error', callback?: ErrorCallback): void
注销监听安全相机会话的错误事件,通过注册回调函数获取结果。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | ------------------------------ |
| type | string | 是 | 监听事件,固定为'error',session创建成功之后可监听该接口。 |
| callback | [ErrorCallback](../apis-basic-services-kit/js-apis-base.md#errorcallback)| 否 | 回调函数,如果指定参数则取消对应callback(callback对象不可是匿名函数),否则取消所有callback。 |
**示例:**
```ts
function unregisterSessionError(secureSession: camera.SecureSession): void {
secureSession.off('error');
}
```
### on('focusStateChange')12+
on(type: 'focusStateChange', callback: AsyncCallback\): void
监听相机聚焦的状态变化,通过注册回调函数获取结果。使用callback异步回调。
> **说明:**
>
> 当前注册监听接口,不支持在on监听的回调方法里,调用off注销回调。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------- | ---- | ------------------------ |
| type | string | 是 | 监听事件,固定为'focusStateChange',session创建成功可监听。仅当自动对焦模式时,且相机对焦状态发生改变时可触发该事件。 |
| callback | AsyncCallback\<[FocusState](#focusstate)\> | 是 | 回调函数,用于获取当前对焦状态。 |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
function callback(err: BusinessError, focusState: camera.FocusState): void {
if (err !== undefined && err.code !== 0) {
console.error(`Callback Error, errorCode: ${err.code}`);
return;
}
console.info(`Focus state: ${focusState}`);
}
function registerFocusStateChange(secureSession: camera.SecureSession): void {
secureSession.on('focusStateChange', callback);
}
```
### off('focusStateChange')12+
off(type: 'focusStateChange', callback?: AsyncCallback\): void
注销监听相机聚焦的状态变化。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------------------------- | ---- | ------------------------ |
| type | string | 是 | 监听事件,固定为'focusStateChange',session创建成功可监听。 |
| callback | AsyncCallback\<[FocusState](#focusstate)\> | 否 | 回调函数,如果指定参数则取消对应callback(callback对象不可是匿名函数),否则取消所有callback。 |
**示例:**
```ts
function unregisterFocusStateChange(secureSession: camera.SecureSession): void {
secureSession.off('focusStateChange');
}
```