openharmony 鸿蒙 arkts-apis-camera-Focus

2026-08-25 浏览 (1)

Interface (Focus)

Focus inherits from FocusQuery.

It provides APIs related to focus operations.

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';

setFocusMode11+

setFocusMode(afMode: FocusMode): void

Sets a focus mode.

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

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

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

NameTypeMandatoryDescription
afModeFocusModeYesFocus mode. If the input parameter is null or undefined, it is treated as 0 and manual focus is used.

Error codes

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

IDError Message
7400103Session not config.

Example

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

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

getFocusMode11+

getFocusMode(): FocusMode

Obtains the focus mode in use.

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

System capability: SystemCapability.Multimedia.Camera.Core

Return value

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

Error codes

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

IDError Message
7400103Session not config.

Example

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

setFocusPoint11+

setFocusPoint(point: Point): void

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

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

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

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

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

Error codes

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

IDError Message
7400103Session not config.

Example

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

getFocusPoint11+

getFocusPoint(): Point

Obtains the focal point in use.

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

System capability: SystemCapability.Multimedia.Camera.Core

Return value

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

Error codes

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

IDError Message
7400103Session not config.

Example

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

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

getFocalLength11+

getFocalLength(): number

Obtains the focal length in use.

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

System capability: SystemCapability.Multimedia.Camera.Core

Return value

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

Error codes

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

IDError Message
7400103Session not config.

Example

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

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

你可能感兴趣的鸿蒙文章

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/RZRPdRaG