# Interface (Session)
> **说明:**
>
> - 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
> - 本Interface首批接口从API version 11开始支持。
会话类,保存一次相机运行所需要的所有资源[CameraInput](arkts-apis-camera-CameraInput.md)、[CameraOutput](arkts-apis-camera-CameraOutput.md),并向相机设备申请完成相机功能(录像,拍照)。
## 导入模块
```ts
import { camera } from '@kit.CameraKit';
```
## beginConfig11+
beginConfig(): void
开始配置会话。
**原子化服务API:** 从API version 19开始,该接口支持在原子化服务中使用。
**系统能力:** 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异步回调。
**原子化服务API:** 从API version 19开始,该接口支持在原子化服务中使用。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------------- | ---- | -------------------- |
| callback | AsyncCallback\ | 是 | 回调函数。当提交配置信息成功,err为undefined,否则为错误对象。错误码类型[CameraErrorCode](arkts-apis-camera-e.md#cameraerrorcode),比如预览流与录像输出流的分辨率的宽高比不一致,会返回7400201。 |
**错误码:**
以下错误码的详细介绍请参见[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异步回调。
**原子化服务API:** 从API version 19开始,该接口支持在原子化服务中使用。
**系统能力:** 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)之间生效。
**原子化服务API:** 从API version 19开始,该接口支持在原子化服务中使用。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----------- | --------------------------- | ---- | ------------------------ |
| cameraInput | [CameraInput](arkts-apis-camera-CameraInput.md) | 是 | 需要添加的CameraInput实例。传参异常(如超出范围、传入null、未定义等),实际接口不会生效。 |
**返回值:**
| 类型 | 说明 |
| -------------- | ------------------------ |
| boolean | 判断当前cameraInput是否可以添加到session中。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](arkts-apis-camera-CameraInput.md)加入到会话。
**原子化服务API:** 从API version 19开始,该接口支持在原子化服务中使用。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----------- | --------------------------- | ---- | ------------------------ |
| cameraInput | [CameraInput](arkts-apis-camera-CameraInput.md) | 是 | 需要添加的CameraInput实例。 |
**错误码:**
以下错误码的详细介绍请参见[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 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](arkts-apis-camera-CameraInput.md)。当前函数需要在[beginConfig](#beginconfig11)和[commitConfig](#commitconfig11-1)之间生效。
**原子化服务API:** 从API version 19开始,该接口支持在原子化服务中使用。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----------- | --------------------------- | ---- | ------------------------ |
| cameraInput | [CameraInput](arkts-apis-camera-CameraInput.md) | 是 | 需要移除的CameraInput实例。 |
**错误码:**
以下错误码的详细介绍请参见[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 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)之间生效。
**原子化服务API:** 从API version 19开始,该接口支持在原子化服务中使用。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----------- | --------------------------- | ---- | ------------------------ |
| cameraOutput | [CameraOutput](arkts-apis-camera-CameraOutput.md) | 是 | 需要添加的CameraOutput实例。传参异常(如超出范围、传入null、未定义等),实际接口不会生效。 |
**返回值:**
| 类型 | 说明 |
| -------------- | ------------------------ |
| boolean | 是否可以添加当前cameraOutput到session中,true为可添加,false为不可添加。 |
**示例:**
```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](arkts-apis-camera-CameraOutput.md)加入到会话。
**原子化服务API:** 从API version 19开始,该接口支持在原子化服务中使用。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------------- | ------------------------------- | ---- | ------------------------ |
| cameraOutput | [CameraOutput](arkts-apis-camera-CameraOutput.md) | 是 | 需要添加的CameraOutput实例。 |
**错误码:**
以下错误码的详细介绍请参见[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 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](arkts-apis-camera-CameraOutput.md)。
**原子化服务API:** 从API version 19开始,该接口支持在原子化服务中使用。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------------- | ------------------------------- | ---- | ------------------------ |
| cameraOutput | [CameraOutput](arkts-apis-camera-CameraOutput.md) | 是 | 需要移除的CameraOutput实例。 |
**错误码:**
以下错误码的详细介绍请参见[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 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异步回调。
**原子化服务API:** 从API version 19开始,该接口支持在原子化服务中使用。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------------- | ---- | -------------------- |
| callback | AsyncCallback\ | 是 | 回调函数。当开始会话工作成功,err为undefined,否则为错误对象。错误码类型[CameraErrorCode](arkts-apis-camera-e.md#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异步回调。
**原子化服务API:** 从API version 19开始,该接口支持在原子化服务中使用。
**系统能力:** 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异步回调。
**原子化服务API:** 从API version 19开始,该接口支持在原子化服务中使用。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------------- | ---- | ------------------- |
| callback | AsyncCallback\ | 是 | 回调函数。当停止会话工作成功,err为undefined,否则为错误对象。错误码类型[CameraErrorCode](arkts-apis-camera-e.md#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异步回调。
**原子化服务API:** 从API version 19开始,该接口支持在原子化服务中使用。
**系统能力:** 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异步回调。
**原子化服务API:** 从API version 19开始,该接口支持在原子化服务中使用。
**系统能力:** SystemCapability.Multimedia.Camera.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------------- | ---- | -------------------- |
| callback | AsyncCallback\ | 是 | 回调函数。当释放会话资源成功,err为undefined,否则为错误对象。错误码类型[CameraErrorCode](arkts-apis-camera-e.md#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异步回调。
**原子化服务API:** 从API version 19开始,该接口支持在原子化服务中使用。
**系统能力:** 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}.`);
});
}
```