openharmony 鸿蒙 arkts-apis-camera-Session

2026-08-25 浏览 (1)

Interface (Session)

Session implements a session, which saves all CameraInput and CameraOutput instances required to run the camera and requests the camera to take a photo or record a video.

NOTE

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

Modules to Import

import { camera } from '@kit.CameraKit';

beginConfig11+

beginConfig(): void

Starts configuration for the session.

Atomic service API: This API can be used in atomic services since API version 19.

System capability: SystemCapability.Multimedia.Camera.Core

Error codes

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

IDError Message
7400105Session config locked.
7400201Camera service fatal error.

Example

import { BusinessError } from '@kit.BasicServicesKit';

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

commitConfig11+

commitConfig(callback: AsyncCallback<void>): void

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

Atomic service API: This API can be used in atomic services since API version 19.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<void>YesCallback used to return the result. If the configuration is successfully committed, err is undefined; otherwise, err is an error object with an error code defined in CameraErrorCode. For example, if the aspect ratio of the preview stream is different from that of the video output stream, error code 7400201 is returned.

Error codes

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

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

Example

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<void>

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

Atomic service API: This API can be used in atomic services since API version 19.

System capability: SystemCapability.Multimedia.Camera.Core

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

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

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

Example

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) => {
    // If the operation fails, error.code is returned and processed.
    console.error(`The commitConfig call failed. error code: ${error.code}`);
  });
}

canAddInput11+

canAddInput(cameraInput: CameraInput): boolean

Checks whether a CameraInput instance can be added to this session. This API must be called after beginConfig and before commitConfig.

Atomic service API: This API can be used in atomic services since API version 19.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
cameraInputCameraInputYesCameraInput instance to add. The API does not take effect if the input parameter is invalid (for example, the value is out of range, null, or undefined).

Return value

TypeDescription
booleanCheck result for adding the CameraInput instance. true if it can be added, false otherwise.

Example

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

Adds a CameraInput instance to this session.

Atomic service API: This API can be used in atomic services since API version 19.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
cameraInputCameraInputYesCameraInput instance to add.

Error codes

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

IDError Message
7400101Parameter missing or parameter type incorrect.
7400102Operation not allowed.
7400201Camera service fatal error.

Example

import { BusinessError } from '@kit.BasicServicesKit';

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

removeInput11+

removeInput(cameraInput: CameraInput): void

Removes a CameraInput instance from this session. This API must be called after beginConfig and before commitConfig.

Atomic service API: This API can be used in atomic services since API version 19.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
cameraInputCameraInputYesCameraInput instance to remove.

Error codes

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

IDError Message
7400101Parameter missing or parameter type incorrect.
7400102Operation not allowed.
7400201Camera service fatal error.

Example

import { BusinessError } from '@kit.BasicServicesKit';

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

canAddOutput11+

canAddOutput(cameraOutput: CameraOutput): boolean

Determines whether a CameraOutput instance can be added to this session. This API must be called after addInput and before commitConfig.

Atomic service API: This API can be used in atomic services since API version 19.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
cameraOutputCameraOutputYesCameraOutput instance to add. The API does not take effect if the input parameter is invalid (for example, the value is out of range, null, or undefined).

Return value

TypeDescription
booleanCheck result for adding the CameraOutput instance. true if it can be added, false otherwise.

Example

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

Adds a CameraOutput instance to this session.

Atomic service API: This API can be used in atomic services since API version 19.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
cameraOutputCameraOutputYesCameraOutput instance to add.

Error codes

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

IDError Message
7400101Parameter missing or parameter type incorrect.
7400102Operation not allowed.
7400201Camera service fatal error.

Example

import { BusinessError } from '@kit.BasicServicesKit';

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

removeOutput11+

removeOutput(cameraOutput: CameraOutput): void

Removes a CameraOutput instance from this session.

Atomic service API: This API can be used in atomic services since API version 19.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
cameraOutputCameraOutputYesCameraOutput instance to remove.

Error codes

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

IDError Message
7400101Parameter missing or parameter type incorrect.
7400102Operation not allowed.
7400201Camera service fatal error.

Example

import { BusinessError } from '@kit.BasicServicesKit';

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

start11+

start(callback: AsyncCallback<void>): void

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

Atomic service API: This API can be used in atomic services since API version 19.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<void>YesCallback used to return the result. If the session starts successfully, err is undefined; otherwise, err is an error object with an error code defined in CameraErrorCode.

Error codes

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

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

Example

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<void>

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

Atomic service API: This API can be used in atomic services since API version 19.

System capability: SystemCapability.Multimedia.Camera.Core

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

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

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

Example

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>): void

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

Atomic service API: This API can be used in atomic services since API version 19.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<void>YesCallback used to return the result. If the session stops successfully, err is undefined; otherwise, err is an error object with an error code defined in CameraErrorCode.

Error codes

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

IDError Message
7400201Camera service fatal error.

Example

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<void>

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

Atomic service API: This API can be used in atomic services since API version 19.

System capability: SystemCapability.Multimedia.Camera.Core

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

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

IDError Message
7400201Camera service fatal error.

Example

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>): void

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

Atomic service API: This API can be used in atomic services since API version 19.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<void>YesCallback used to return the result. If the session is released successfully, err is undefined; otherwise, err is an error object with an error code defined in CameraErrorCode.

Error codes

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

IDError Message
7400201Camera service fatal error.

Example

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<void>

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

Atomic service API: This API can be used in atomic services since API version 19.

System capability: SystemCapability.Multimedia.Camera.Core

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

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

IDError Message
7400201Camera service fatal error.

Example

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}.`);
  });
}

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 capi-oh-camera-capturesession-callbacks

openharmony 鸿蒙 arkts-apis-camera-OIS

openharmony 鸿蒙 capi-oh-camera-camera-devicequeryinfo

openharmony 鸿蒙 capi-oh-camera-camera-frameraterange

openharmony 鸿蒙 capi-oh-camera-camera-outputcapability

openharmony 鸿蒙 arkts-apis-camera-Photo

openharmony 鸿蒙 capi-oh-camera-camera-profile

openharmony 鸿蒙 capi-oh-camera-camera-manager

openharmony 鸿蒙 capi-oh-camera-previewoutput-callbacks

openharmony 鸿蒙 arkts-apis-camera-ManualExposureQuery

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