• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Interface (Focus)
2<!--Kit: Camera Kit-->
3<!--Subsystem: Multimedia-->
4<!--Owner: @qano-->
5<!--Designer: @leo_ysl-->
6<!--Tester: @xchaosioda-->
7<!--Adviser: @zengyawen-->
8
9> **说明:**
10>
11> - 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
12> - 本Interface首批接口从API version 11开始支持。
13
14Focus 继承自 [FocusQuery](arkts-apis-camera-FocusQuery.md)。
15
16对焦类,对设备对焦操作。
17
18## 导入模块
19
20```ts
21import { camera } from '@kit.CameraKit';
22```
23
24## setFocusMode<sup>11+</sup>
25
26setFocusMode(afMode: FocusMode): void
27
28设置对焦模式。
29
30进行设置之前,需要先检查设备是否支持指定的焦距模式,可使用方法[isFocusModeSupported](arkts-apis-camera-FocusQuery.md#isfocusmodesupported11)。
31
32**原子化服务API:** 从API version 19开始,该接口支持在原子化服务中使用。
33
34**系统能力:** SystemCapability.Multimedia.Camera.Core
35
36**参数:**
37
38| 参数名      | 类型                     | 必填 | 说明                 |
39| -------- | ----------------------- | ---- | ------------------- |
40| afMode   | [FocusMode](arkts-apis-camera-e.md#focusmode) | 是   | 指定的焦距模式。传参为null或者undefined,作为0处理,手动对焦模式。       |
41
42**错误码:**
43
44以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
45
46| 错误码ID         | 错误信息        |
47| --------------- | --------------- |
48| 7400103                |  Session not config.                                   |
49
50**示例:**
51
52```ts
53import { BusinessError } from '@kit.BasicServicesKit';
54
55function setFocusMode(photoSession: camera.PhotoSession): void {
56  try {
57    photoSession.setFocusMode(camera.FocusMode.FOCUS_MODE_AUTO);
58  } catch (error) {
59    // 失败返回错误码error.code并处理。
60    let err = error as BusinessError;
61    console.error(`The setFocusMode call failed. error code: ${err.code}`);
62  }
63}
64```
65
66## getFocusMode<sup>11+</sup>
67
68getFocusMode(): FocusMode
69
70获取当前的对焦模式。
71
72**原子化服务API:** 从API version 19开始,该接口支持在原子化服务中使用。
73
74**系统能力:** SystemCapability.Multimedia.Camera.Core
75
76**返回值:**
77
78| 类型        | 说明                          |
79| ---------- | ----------------------------- |
80| [FocusMode](arkts-apis-camera-e.md#focusmode)   | 获取当前设备的焦距模式。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](arkts-apis-camera-e.md#cameraerrorcode)。 |
81
82**错误码:**
83
84以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
85
86| 错误码ID         | 错误信息        |
87| --------------- | --------------- |
88| 7400103                |  Session not config.                                   |
89
90**示例:**
91
92```ts
93import { BusinessError } from '@kit.BasicServicesKit';
94
95function getFocusMode(photoSession: camera.PhotoSession): camera.FocusMode | undefined {
96  let afMode: camera.FocusMode | undefined = undefined;
97  try {
98    afMode = photoSession.getFocusMode();
99  } catch (error) {
100    // 失败返回错误码error.code并处理。
101    let err = error as BusinessError;
102    console.error(`The getFocusMode call failed. error code: ${err.code}`);
103  }
104  return afMode;
105}
106```
107
108## setFocusPoint<sup>11+</sup>
109
110setFocusPoint(point: Point): void
111
112设置焦点,焦点应在0-1坐标系内,该坐标系左上角为{0,0},右下角为{1,1}。
113
114此坐标系是以设备充电口在右侧时的横向设备方向为基准的,例如应用的预览界面布局以设备充电口在下侧时的竖向方向为基准,布局宽高为{w,h},且触碰点为{x,y},则转换后的坐标点为{y/h,1-x/w}。
115
116**原子化服务API:** 从API version 19开始,该接口支持在原子化服务中使用。
117
118**系统能力:** SystemCapability.Multimedia.Camera.Core
119
120**参数:**
121
122| 参数名      | 类型                     | 必填 | 说明                 |
123| -------- | ----------------------- | ---- | ------------------- |
124| point    | [Point](arkts-apis-camera-i.md#point)         | 是   | 焦点。x、y设置范围应在[0,1]之内,超过范围,如果小于0设置0,大于1设置1。   |
125
126**错误码:**
127
128以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
129
130| 错误码ID         | 错误信息        |
131| --------------- | --------------- |
132| 7400103                |  Session not config.                                   |
133
134**示例:**
135
136```ts
137import { BusinessError } from '@kit.BasicServicesKit';
138
139function setFocusPoint(photoSession: camera.PhotoSession): void {
140  const focusPoint: camera.Point = {x: 1, y: 1};
141  try {
142    photoSession.setFocusPoint(focusPoint);
143  } catch (error) {
144    // 失败返回错误码error.code并处理。
145    let err = error as BusinessError;
146    console.error(`The setFocusPoint call failed. error code: ${err.code}`);
147  }
148}
149```
150
151## getFocusPoint<sup>11+</sup>
152
153getFocusPoint(): Point
154
155查询当前的焦点。
156
157**原子化服务API:** 从API version 19开始,该接口支持在原子化服务中使用。
158
159**系统能力:** SystemCapability.Multimedia.Camera.Core
160
161**返回值:**
162
163| 类型        | 说明                          |
164| ---------- | ----------------------------- |
165| [Point](arkts-apis-camera-i.md#point)    | 用于获取当前的焦点。接口调用失败会返回相应错误码,错误码类型为[CameraErrorCode](arkts-apis-camera-e.md#cameraerrorcode)。 |
166
167**错误码:**
168
169以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
170
171| 错误码ID         | 错误信息        |
172| --------------- | --------------- |
173| 7400103                |  Session not config.                                   |
174
175**示例:**
176
177```ts
178import { BusinessError } from '@kit.BasicServicesKit';
179
180function getFocusPoint(photoSession: camera.PhotoSession): camera.Point | undefined {
181  let point: camera.Point | undefined = undefined;
182  try {
183    point = photoSession.getFocusPoint();
184  } catch (error) {
185    // 失败返回错误码error.code并处理。
186    let err = error as BusinessError;
187    console.error(`The getFocusPoint call failed. error code: ${err.code}`);
188  }
189  return point;
190}
191```
192
193## getFocalLength<sup>11+</sup>
194
195getFocalLength(): number
196
197查询当前的焦距值。
198
199**原子化服务API:** 从API version 19开始,该接口支持在原子化服务中使用。
200
201**系统能力:** SystemCapability.Multimedia.Camera.Core
202
203**返回值:**
204
205| 类型        | 说明                          |
206| ---------- | ----------------------------- |
207| number    | 用于获取当前焦距,单位mm。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](arkts-apis-camera-e.md#cameraerrorcode)。 |
208
209**错误码:**
210
211以下错误码的详细介绍请参见[Camera错误码](errorcode-camera.md)。
212
213| 错误码ID         | 错误信息        |
214| --------------- | --------------- |
215| 7400103                |  Session not config.                                   |
216
217**示例:**
218
219```ts
220import { BusinessError } from '@kit.BasicServicesKit';
221
222function getFocalLength(photoSession: camera.PhotoSession): number {
223  const invalidValue: number = -1;
224  let focalLength: number = invalidValue;
225  try {
226    focalLength = photoSession.getFocalLength();
227  } catch (error) {
228    // 失败返回错误码error.code并处理。
229    let err = error as BusinessError;
230    console.error(`The getFocalLength call failed. error code: ${err.code}`);
231  }
232  return focalLength;
233}
234```
235