• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Interface (Zoom)
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
13Zoom inherits from [ZoomQuery](arkts-apis-camera-ZoomQuery.md).
14
15It provides APIs related to zoom operations.
16
17## Modules to Import
18
19```ts
20import { camera } from '@kit.CameraKit';
21```
22
23## setZoomRatio<sup>11+</sup>
24
25setZoomRatio(zoomRatio: number): void
26
27Sets a zoom ratio, with a maximum precision of two decimal places.
28
29**Atomic service API**: This API can be used in atomic services since API version 19.
30
31**System capability**: SystemCapability.Multimedia.Camera.Core
32
33**Parameters**
34
35| Name      | Type                 | Mandatory| Description                                                                                                                             |
36| --------- | -------------------- | ---- |---------------------------------------------------------------------------------------------------------------------------------|
37| zoomRatio | number               | Yes  | Zoom ratio. The supported zoom ratio range can be obtained by calling [getZoomRatioRange](arkts-apis-camera-ZoomQuery.md#getzoomratiorange11). If the value passed in is not within the supported range, the value within the precision range is retained.<br>It takes some time for the zoom ratio to take effect at the bottom layer. To obtain the correct zoom ratio, you need to wait for one to two frames.|
38
39**Error codes**
40
41For details about the error codes, see [Camera Error Codes](errorcode-camera.md).
42
43| ID        | Error Message       |
44| --------------- | --------------- |
45| 7400103                |  Session not config.                                   |
46
47**Example**
48
49```ts
50import { BusinessError } from '@kit.BasicServicesKit';
51
52function setZoomRatio(photoSession: camera.PhotoSession, zoomRatioRange: Array<number>): void {
53  if (zoomRatioRange === undefined || zoomRatioRange.length <= 0) {
54    return;
55  }
56  let zoomRatio = zoomRatioRange[0];
57  try {
58    photoSession.setZoomRatio(zoomRatio);
59  } catch (error) {
60    // If the operation fails, error.code is returned and processed.
61    let err = error as BusinessError;
62    console.error(`The setZoomRatio call failed. error code: ${err.code}`);
63  }
64}
65```
66
67## getZoomRatio<sup>11+</sup>
68
69getZoomRatio(): number
70
71Obtains the zoom ratio in use.
72
73**Atomic service API**: This API can be used in atomic services since API version 19.
74
75**System capability**: SystemCapability.Multimedia.Camera.Core
76
77**Return value**
78
79| Type       | Description                         |
80| ---------- | ----------------------------- |
81| number    | Zoom ratio obtained. If the operation fails, an error code defined in [CameraErrorCode](arkts-apis-camera-e.md#cameraerrorcode) is returned.|
82
83**Error codes**
84
85For details about the error codes, see [Camera Error Codes](errorcode-camera.md).
86
87| ID        | Error Message       |
88| --------------- | --------------- |
89| 7400103                |  Session not config.                                   |
90| 7400201                |  Camera service fatal error.                           |
91
92**Example**
93
94```ts
95import { BusinessError } from '@kit.BasicServicesKit';
96
97function getZoomRatio(photoSession: camera.PhotoSession): number {
98  const invalidValue: number = -1;
99  let zoomRatio: number = invalidValue;
100  try {
101    zoomRatio = photoSession.getZoomRatio();
102  } catch (error) {
103    // If the operation fails, error.code is returned and processed.
104    let err = error as BusinessError;
105    console.error(`The getZoomRatio call failed. error code: ${err.code}`);
106  }
107  return zoomRatio;
108}
109```
110
111## setSmoothZoom<sup>11+</sup>
112
113setSmoothZoom(targetRatio: number, mode?: SmoothZoomMode): void
114
115Sets smooth zoom.
116
117**Atomic service API**: This API can be used in atomic services since API version 19.
118
119**System capability**: SystemCapability.Multimedia.Camera.Core
120
121**Parameters**
122
123| Name      | Type           | Mandatory| Description              |
124| ------------ | -------------- | ---- | ----------------- |
125| targetRatio  | number         | Yes  | Target zoom ratio.     |
126| mode         | [SmoothZoomMode](arkts-apis-camera-e.md#smoothzoommode11) | No  | Smooth zoom mode.     |
127
128**Example**
129
130```ts
131import { BusinessError } from '@kit.BasicServicesKit';
132
133function setSmoothZoom(sessionExtendsZoom: camera.Zoom, targetZoomRatio: number, mode: camera.SmoothZoomMode): void {
134  sessionExtendsZoom.setSmoothZoom(targetZoomRatio, mode);
135}
136```
137