1# Interface (Focus) 2<!--Kit: Camera Kit--> 3<!--Subsystem: Multimedia--> 4<!--Owner: @qano--> 5<!--SE: @leo_ysl--> 6<!--TSE: @xchaosioda--> 7 8> **NOTE** 9> 10> - 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. 11> - The initial APIs of this interface are supported since API version 11. 12 13Focus inherits from [FocusQuery](arkts-apis-camera-FocusQuery.md). 14 15It provides APIs related to focus operations. 16 17## Modules to Import 18 19```ts 20import { camera } from '@kit.CameraKit'; 21``` 22 23## setFocusMode<sup>11+</sup> 24 25setFocusMode(afMode: FocusMode): void 26 27Sets a focus mode. 28 29Before the setting, call [isFocusModeSupported](arkts-apis-camera-FocusQuery.md#isfocusmodesupported11) to check whether the focus mode is supported. 30 31**Atomic service API**: This API can be used in atomic services since API version 19. 32 33**System capability**: SystemCapability.Multimedia.Camera.Core 34 35**Parameters** 36 37| Name | Type | Mandatory| Description | 38| -------- | ----------------------- | ---- | ------------------- | 39| afMode | [FocusMode](arkts-apis-camera-e.md#focusmode) | Yes | Focus mode. If the input parameter is null or undefined, it is treated as 0 and manual focus is used. | 40 41**Error codes** 42 43For details about the error codes, see [Camera Error Codes](errorcode-camera.md). 44 45| ID | Error Message | 46| --------------- | --------------- | 47| 7400103 | Session not config. | 48 49**Example** 50 51```ts 52import { BusinessError } from '@kit.BasicServicesKit'; 53 54function setFocusMode(photoSession: camera.PhotoSession): void { 55 try { 56 photoSession.setFocusMode(camera.FocusMode.FOCUS_MODE_AUTO); 57 } catch (error) { 58 // If the operation fails, error.code is returned and processed. 59 let err = error as BusinessError; 60 console.error(`The setFocusMode call failed. error code: ${err.code}`); 61 } 62} 63``` 64 65## getFocusMode<sup>11+</sup> 66 67getFocusMode(): FocusMode 68 69Obtains the focus mode in use. 70 71**Atomic service API**: This API can be used in atomic services since API version 19. 72 73**System capability**: SystemCapability.Multimedia.Camera.Core 74 75**Return value** 76 77| Type | Description | 78| ---------- | ----------------------------- | 79| [FocusMode](arkts-apis-camera-e.md#focusmode) | Focus mode obtained. If the operation fails, an error code defined in [CameraErrorCode](arkts-apis-camera-e.md#cameraerrorcode) is returned.| 80 81**Error codes** 82 83For details about the error codes, see [Camera Error Codes](errorcode-camera.md). 84 85| ID | Error Message | 86| --------------- | --------------- | 87| 7400103 | Session not config. | 88 89**Example** 90 91```ts 92import { BusinessError } from '@kit.BasicServicesKit'; 93 94function getFocusMode(photoSession: camera.PhotoSession): camera.FocusMode | undefined { 95 let afMode: camera.FocusMode | undefined = undefined; 96 try { 97 afMode = photoSession.getFocusMode(); 98 } catch (error) { 99 // If the operation fails, error.code is returned and processed. 100 let err = error as BusinessError; 101 console.error(`The getFocusMode call failed. error code: ${err.code}`); 102 } 103 return afMode; 104} 105``` 106 107## setFocusPoint<sup>11+</sup> 108 109setFocusPoint(point: Point): void 110 111Sets the focal point. The focal point must be in the coordinate system (0-1), where the upper left corner is {0, 0} and the lower right corner is {1, 1}. 112 113The 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}. 114 115**Atomic service API**: This API can be used in atomic services since API version 19. 116 117**System capability**: SystemCapability.Multimedia.Camera.Core 118 119**Parameters** 120 121| Name | Type | Mandatory| Description | 122| -------- | ----------------------- | ---- | ------------------- | 123| point | [Point](arkts-apis-camera-i.md#point) | Yes | Focal 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. | 124 125**Error codes** 126 127For details about the error codes, see [Camera Error Codes](errorcode-camera.md). 128 129| ID | Error Message | 130| --------------- | --------------- | 131| 7400103 | Session not config. | 132 133**Example** 134 135```ts 136import { BusinessError } from '@kit.BasicServicesKit'; 137 138function setFocusPoint(photoSession: camera.PhotoSession): void { 139 const focusPoint: camera.Point = {x: 1, y: 1}; 140 try { 141 photoSession.setFocusPoint(focusPoint); 142 } catch (error) { 143 // If the operation fails, error.code is returned and processed. 144 let err = error as BusinessError; 145 console.error(`The setFocusPoint call failed. error code: ${err.code}`); 146 } 147} 148``` 149 150## getFocusPoint<sup>11+</sup> 151 152getFocusPoint(): Point 153 154Obtains the focal point in use. 155 156**Atomic service API**: This API can be used in atomic services since API version 19. 157 158**System capability**: SystemCapability.Multimedia.Camera.Core 159 160**Return value** 161 162| Type | Description | 163| ---------- | ----------------------------- | 164| [Point](arkts-apis-camera-i.md#point) | Focal point obtained. If the operation fails, an error code defined in [CameraErrorCode](arkts-apis-camera-e.md#cameraerrorcode) is returned.| 165 166**Error codes** 167 168For details about the error codes, see [Camera Error Codes](errorcode-camera.md). 169 170| ID | Error Message | 171| --------------- | --------------- | 172| 7400103 | Session not config. | 173 174**Example** 175 176```ts 177import { BusinessError } from '@kit.BasicServicesKit'; 178 179function getFocusPoint(photoSession: camera.PhotoSession): camera.Point | undefined { 180 let point: camera.Point | undefined = undefined; 181 try { 182 point = photoSession.getFocusPoint(); 183 } catch (error) { 184 // If the operation fails, error.code is returned and processed. 185 let err = error as BusinessError; 186 console.error(`The getFocusPoint call failed. error code: ${err.code}`); 187 } 188 return point; 189} 190``` 191 192## getFocalLength<sup>11+</sup> 193 194getFocalLength(): number 195 196Obtains the focal length in use. 197 198**Atomic service API**: This API can be used in atomic services since API version 19. 199 200**System capability**: SystemCapability.Multimedia.Camera.Core 201 202**Return value** 203 204| Type | Description | 205| ---------- | ----------------------------- | 206| number | Focal length, in mm. If the operation fails, an error code defined in [CameraErrorCode](arkts-apis-camera-e.md#cameraerrorcode) is returned.| 207 208**Error codes** 209 210For details about the error codes, see [Camera Error Codes](errorcode-camera.md). 211 212| ID | Error Message | 213| --------------- | --------------- | 214| 7400103 | Session not config. | 215 216**Example** 217 218```ts 219import { BusinessError } from '@kit.BasicServicesKit'; 220 221function getFocalLength(photoSession: camera.PhotoSession): number { 222 const invalidValue: number = -1; 223 let focalLength: number = invalidValue; 224 try { 225 focalLength = photoSession.getFocalLength(); 226 } catch (error) { 227 // If the operation fails, error.code is returned and processed. 228 let err = error as BusinessError; 229 console.error(`The getFocalLength call failed. error code: ${err.code}`); 230 } 231 return focalLength; 232} 233``` 234