• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Interface (PhotoSession)
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
13PhotoSession inherits from [Session](arkts-apis-camera-Session.md), [Flash](arkts-apis-camera-Flash.md), [AutoExposure](arkts-apis-camera-AutoExposure.md), [WhiteBalance](arkts-apis-camera-WhiteBalance.md), [Focus](arkts-apis-camera-Focus.md), [Zoom](arkts-apis-camera-Zoom.md), [ColorManagement](arkts-apis-camera-ColorManagement.md), [AutoDeviceSwitch](arkts-apis-camera-AutoDeviceSwitch.md), and [Macro](arkts-apis-camera-Macro.md).
14
15It implements a photo session, which provides operations on the flash, exposure, white balance, focus, zoom, color space, and macro mode.
16
17> **NOTE**
18>
19> This class is provided for the default photo mode. It is used to take standard photos. It supports multiple photo formats and resolutions, which are suitable for most daily photo capture scenarios.
20
21## Modules to Import
22
23```ts
24import { camera } from '@kit.CameraKit';
25```
26
27## canPreconfig<sup>12+</sup>
28
29canPreconfig(preconfigType: PreconfigType, preconfigRatio?: PreconfigRatio): boolean
30
31Checks whether this session supports a preconfigured resolution.
32
33**Atomic service API**: This API can be used in atomic services since API version 19.
34
35**System capability**: SystemCapability.Multimedia.Camera.Core
36
37**Parameters**
38
39| Name           | Type                                 | Mandatory | Description             |
40|----------------|-------------------------------------|-----|-----------------|
41| preconfigType  | [PreconfigType](arkts-apis-camera-e.md#preconfigtype12)   | Yes  | Resolution type.     |
42| preconfigRatio | [PreconfigRatio](arkts-apis-camera-e.md#preconfigratio12) | No  | Aspect ratio. The default value is 4:3. |
43
44**Return value**
45
46| Type     | Description                                     |
47|---------|-----------------------------------------|
48| boolean | **true**: The preconfigured resolution is supported.<br>**false**: The preconfigured resolution is not supported.|
49
50**Error codes**
51
52For details about the error codes, see [Camera Error Codes](errorcode-camera.md).
53
54| ID  | Error Message                       |
55|---------|-----------------------------|
56| 7400201 | Camera service fatal error. |
57
58**Example**
59
60```ts
61function testCanPreconfig(photoSession: camera.PhotoSession, preconfigType: camera.PreconfigType,
62  preconfigRatio: camera.PreconfigRatio): void {
63  try {
64    let result = photoSession.canPreconfig(preconfigType, preconfigRatio);
65    console.info(`canPreconfig ${preconfigType} ${preconfigRatio} result is : ${result}`);
66  } catch (error) {
67    let err = error as BusinessError;
68    console.error(`The canPreconfig call failed. error code: ${err.code}`);
69  }
70}
71```
72
73## preconfig<sup>12+</sup>
74
75preconfig(preconfigType: PreconfigType, preconfigRatio?: PreconfigRatio): void
76
77Preconfigures this session.
78
79**Atomic service API**: This API can be used in atomic services since API version 19.
80
81**System capability**: SystemCapability.Multimedia.Camera.Core
82
83**Parameters**
84
85| Name           | Type                                 | Mandatory | Description             |
86|----------------|-------------------------------------|-----|-----------------|
87| preconfigType  | [PreconfigType](arkts-apis-camera-e.md#preconfigtype12)   | Yes  | Resolution type.     |
88| preconfigRatio | [PreconfigRatio](arkts-apis-camera-e.md#preconfigratio12) | No  | Aspect ratio. The default value is 4:3. |
89
90**Error codes**
91
92For details about the error codes, see [Camera Error Codes](errorcode-camera.md).
93
94| ID  | Error Message                       |
95|---------|-----------------------------|
96| 7400201 | Camera service fatal error. |
97
98**Example**
99
100```ts
101function testPreconfig(photoSession: camera.PhotoSession, preconfigType: camera.PreconfigType,
102  preconfigRatio: camera.PreconfigRatio): void {
103  try {
104    photoSession.preconfig(preconfigType, preconfigRatio);
105    console.info(`preconfig ${preconfigType} ${preconfigRatio} success`);
106  } catch (error) {
107    let err = error as BusinessError;
108    console.error(`The preconfig call failed. error code: ${err.code}`);
109  }
110}
111```
112
113## on('error')<sup>11+</sup>
114
115on(type: 'error', callback: ErrorCallback): void
116
117Subscribes to PhotoSession error events. This API uses an asynchronous callback to return the result.
118
119> **NOTE**
120>
121> Currently, you cannot use **off()** to unregister the callback in the callback method of **on()**.
122
123**Atomic service API**: This API can be used in atomic services since API version 19.
124
125**System capability**: SystemCapability.Multimedia.Camera.Core
126
127**Parameters**
128
129| Name    | Type                                                         | Mandatory| Description                          |
130| -------- | ----------------------------------------------------------- | ---- | ------------------------------ |
131| type     | string                                                      | Yes  | Event type. The value is fixed at **'error'**. The event can be listened for when a session is created. This event is triggered and the error message is returned when an error occurs during the calling of a session-related API such as [beginConfig](arkts-apis-camera-Session.md#beginconfig11), [commitConfig](arkts-apis-camera-Session.md#commitconfig11), and [addInput](arkts-apis-camera-Session.md#addinput11).|
132| callback | [ErrorCallback](../apis-basic-services-kit/js-apis-base.md#errorcallback)| Yes  | Callback used to return an error code defined in [CameraErrorCode](arkts-apis-camera-e.md#cameraerrorcode).       |
133
134**Example**
135
136```ts
137import { BusinessError } from '@kit.BasicServicesKit';
138
139function callback(err: BusinessError): void {
140  console.error(`Photo session error code: ${err.code}`);
141}
142
143function registerSessionError(photoSession: camera.PhotoSession): void {
144  photoSession.on('error', callback);
145}
146```
147
148## off('error')<sup>11+</sup>
149
150off(type: 'error', callback?: ErrorCallback): void
151
152Unsubscribes from PhotoSession error events. This API uses a callback to return the result.
153
154**Atomic service API**: This API can be used in atomic services since API version 19.
155
156**System capability**: SystemCapability.Multimedia.Camera.Core
157
158**Parameters**
159
160| Name    | Type                           | Mandatory| Description                          |
161| -------- | -------------------------------- | ---- | ------------------------------ |
162| type     | string                           | Yes  | Event type. The value is fixed at **'error'**. The event can be listened for when a session is created.|
163| callback | [ErrorCallback](../apis-basic-services-kit/js-apis-base.md#errorcallback)| No  | Callback used to return the result. If this parameter is specified, the subscription to the specified event with the specified callback is canceled. (The callback object cannot be an anonymous function.) Otherwise, the subscriptions to the specified event with all the callbacks are canceled.|
164
165**Example**
166
167```ts
168function unregisterSessionError(photoSession: camera.PhotoSession): void {
169  photoSession.off('error');
170}
171```
172
173## on('focusStateChange')<sup>11+</sup>
174
175on(type: 'focusStateChange', callback: AsyncCallback\<FocusState\>): void
176
177Subscribes to focus state change events. This API uses an asynchronous callback to return the result.
178
179> **NOTE**
180>
181> Currently, you cannot use **off()** to unregister the callback in the callback method of **on()**.
182
183**Atomic service API**: This API can be used in atomic services since API version 19.
184
185**System capability**: SystemCapability.Multimedia.Camera.Core
186
187**Parameters**
188
189| Name    | Type                   | Mandatory| Description                      |
190| -------- | ---------------- | ---- | ------------------------ |
191| type     | string                                    | Yes  | Event type. The value is fixed at **'focusStateChange'**. The event can be listened for when a session is created. This event is triggered only when the camera focus state changes in auto focus mode.|
192| callback | AsyncCallback\<[FocusState](arkts-apis-camera-e.md#focusstate)\> | Yes  | Callback used to return the focus state change. |
193
194**Example**
195
196```ts
197import { BusinessError } from '@kit.BasicServicesKit';
198
199function callback(err: BusinessError, focusState: camera.FocusState): void {
200  if (err !== undefined && err.code !== 0) {
201    console.error(`Callback Error, errorCode: ${err.code}`);
202    return;
203  }
204  console.info(`Focus state: ${focusState}`);
205}
206
207function registerFocusStateChange(photoSession: camera.PhotoSession): void {
208  photoSession.on('focusStateChange', callback);
209}
210```
211
212## off('focusStateChange')<sup>11+</sup>
213
214off(type: 'focusStateChange', callback?: AsyncCallback\<FocusState\>): void
215
216Unsubscribes from focus state change events.
217
218**Atomic service API**: This API can be used in atomic services since API version 19.
219
220**System capability**: SystemCapability.Multimedia.Camera.Core
221
222**Parameters**
223
224| Name    | Type                                     | Mandatory| Description                      |
225| -------- | ----------------------------------------- | ---- | ------------------------ |
226| type     | string                                    | Yes  | Event type. The value is fixed at **'focusStateChange'**. The event can be listened for when a session is created.|
227| callback | AsyncCallback\<[FocusState](arkts-apis-camera-e.md#focusstate)\> | No  | Callback used to return the result. If this parameter is specified, the subscription to the specified event with the specified callback is canceled. (The callback object cannot be an anonymous function.) Otherwise, the subscriptions to the specified event with all the callbacks are canceled.|
228
229**Example**
230
231```ts
232function unregisterFocusStateChange(photoSession: camera.PhotoSession): void {
233  photoSession.off('focusStateChange');
234}
235```
236
237## on('smoothZoomInfoAvailable')<sup>11+</sup>
238
239on(type: 'smoothZoomInfoAvailable', callback: AsyncCallback\<SmoothZoomInfo\>): void
240
241Subscribes to smooth zoom state change events. This API uses an asynchronous callback to return the result.
242
243> **NOTE**
244>
245> Currently, you cannot use **off()** to unregister the callback in the callback method of **on()**.
246
247**Atomic service API**: This API can be used in atomic services since API version 19.
248
249**System capability**: SystemCapability.Multimedia.Camera.Core
250
251**Parameters**
252
253| Name    | Type                  | Mandatory| Description                      |
254| -------- | ----------------------- | ---- | ------------------------ |
255| type     | string                  | Yes  | Event type. The value is fixed at **'smoothZoomInfoAvailable'**. The event can be listened for when a session is created.|
256| callback | AsyncCallback\<[SmoothZoomInfo](arkts-apis-camera-i.md#smoothzoominfo11)\> | Yes  | Callback used to return the smooth zoom state change. |
257
258**Example**
259
260```ts
261import { BusinessError } from '@kit.BasicServicesKit';
262
263function callback(err: BusinessError, smoothZoomInfo: camera.SmoothZoomInfo): void {
264  if (err !== undefined && err.code !== 0) {
265    console.error(`Callback Error, errorCode: ${err.code}`);
266    return;
267  }
268  console.info(`The duration of smooth zoom: ${smoothZoomInfo.duration}`);
269}
270
271function registerSmoothZoomInfo(photoSession: camera.PhotoSession): void {
272  photoSession.on('smoothZoomInfoAvailable', callback);
273}
274```
275
276## off('smoothZoomInfoAvailable')<sup>11+</sup>
277
278off(type: 'smoothZoomInfoAvailable', callback?: AsyncCallback\<SmoothZoomInfo\>): void
279
280Unsubscribes from smooth zoom state change events.
281
282**Atomic service API**: This API can be used in atomic services since API version 19.
283
284**System capability**: SystemCapability.Multimedia.Camera.Core
285
286**Parameters**
287
288| Name    | Type                                     | Mandatory| Description                      |
289| -------- | ----------------------------------------- | ---- | ------------------------ |
290| type     | string              | Yes  | Event type. The value is fixed at **'smoothZoomInfoAvailable'**. The event can be listened for when a session is created.|
291| callback | AsyncCallback\<[SmoothZoomInfo](arkts-apis-camera-i.md#smoothzoominfo11)\> | No  | Callback used to return the result. If this parameter is specified, the subscription to the specified event with the specified callback is canceled. (The callback object cannot be an anonymous function.) Otherwise, the subscriptions to the specified event with all the callbacks are canceled.|
292
293**Example**
294
295```ts
296function unregisterSmoothZoomInfo(photoSession: camera.PhotoSession): void {
297  photoSession.off('smoothZoomInfoAvailable');
298}
299```
300
301## on('autoDeviceSwitchStatusChange')<sup>13+</sup>
302
303on(type: 'autoDeviceSwitchStatusChange', callback: AsyncCallback\<AutoDeviceSwitchStatus\>): void
304
305Subscribes to automatic camera switch status change events. This API uses an asynchronous callback to return the result.
306
307> **NOTE**
308>
309> Currently, you cannot use **off()** to unregister the callback in the callback method of **on()**.
310
311**Atomic service API**: This API can be used in atomic services since API version 19.
312
313**System capability**: SystemCapability.Multimedia.Camera.Core
314
315**Parameters**
316
317| Name    | Type                                                                  | Mandatory| Description                                                    |
318| -------- |----------------------------------------------------------------------| ---- |--------------------------------------------------------|
319| type     | string                                                               | Yes  | Event type. The value is fixed at **'autoDeviceSwitchStatusChange'**. The event can be listened for when a session is created.|
320| callback | AsyncCallback\<[AutoDeviceSwitchStatus](arkts-apis-camera-i.md#autodeviceswitchstatus13)\> | Yes  | Callback function, which is used to obtain the status of automatic camera switch.                                 |
321
322**Example**
323
324```ts
325import { BusinessError } from '@kit.BasicServicesKit';
326
327function callback(err: BusinessError, autoDeviceSwitchStatus: camera.AutoDeviceSwitchStatus): void {
328  if (err !== undefined && err.code !== 0) {
329    console.error(`Callback Error, errorCode: ${err.code}`);
330    return;
331  }
332  console.info(`isDeviceSwitched: ${autoDeviceSwitchStatus.isDeviceSwitched}, isDeviceCapabilityChanged: ${autoDeviceSwitchStatus.isDeviceCapabilityChanged}`);
333}
334
335function registerAutoDeviceSwitchStatus(photoSession: camera.PhotoSession): void {
336  photoSession.on('autoDeviceSwitchStatusChange', callback);
337}
338```
339
340## off('autoDeviceSwitchStatusChange')<sup>13+</sup>
341
342off(type: 'autoDeviceSwitchStatusChange', callback?: AsyncCallback\<AutoDeviceSwitchStatus\>): void
343
344Unsubscribes from automatic camera switch status change events.
345
346**Atomic service API**: This API can be used in atomic services since API version 19.
347
348**System capability**: SystemCapability.Multimedia.Camera.Core
349
350**Parameters**
351
352| Name    | Type                                          | Mandatory| Description                      |
353| -------- |----------------------------------------------| ---- | ------------------------ |
354| type     | string                                       | Yes  | Event type. The value is fixed at **'autoDeviceSwitchStatusChange'**. The event can be listened for when a session is created.|
355| callback | AsyncCallback\<[AutoDeviceSwitchStatus](arkts-apis-camera-i.md#autodeviceswitchstatus13)\> | No  | Callback used to return the result. If this parameter is specified, the subscription to the specified event with the specified callback is canceled. (The callback object cannot be an anonymous function.) Otherwise, the subscriptions to the specified event with all the callbacks are canceled.|
356
357**Example**
358
359```ts
360function unregisterSmoothZoomInfo(photoSession: camera.PhotoSession): void {
361  photoSession.off('autoDeviceSwitchStatusChange');
362}
363```
364
365## on('systemPressureLevelChange')<sup>20+</sup>
366
367on(type: 'systemPressureLevelChange', callback: AsyncCallback\<SystemPressureLevel\>): void
368
369Subscribes to system pressure level change events. This API uses an asynchronous callback to return the result.
370
371> **NOTE**
372>
373> Currently, you cannot use **off()** to unregister the callback in the callback method of **on()**.
374
375**Atomic service API**: This API can be used in atomic services since API version 20.
376
377**System capability**: SystemCapability.Multimedia.Camera.Core
378
379**Parameters**
380
381| Name | Type                                                     | Mandatory| Description                                                             |
382|---------|----------------------------------------------------------|------|-------------------------------------------------------------------|
383|type     | string                                                   | Yes  | Event type. The value is fixed at **'systemPressureLevelChange'**. The event can be listened for when a session is created.|
384|callback | AsyncCallback\<[SystemPressureLevel](arkts-apis-camera-e.md#systempressurelevel20)\> | Yes| Callback used to return the current system pressure level.|
385
386**Example**
387
388```ts
389import { BusinessError } from '@kit.BasicServicesKit';
390
391function callback(err: BusinessError, systemPressureLevel: camera.SystemPressureLevel): void {
392  if (err !== undefined && err.code !== 0) {
393    console.error(`Callback Error, errorCode: ${err.code}`);
394    return;
395  }
396  console.info(`systemPressureLevel: ${systemPressureLevel}`);
397}
398
399function registerSystemPressureLevelChangeCallback(photoSession: camera.PhotoSession): void {
400    photoSession.on('systemPressureLevelChange', callback);
401}
402```
403
404## off('systemPressureLevelChange')<sup>20+</sup>
405
406off(type: 'systemPressureLevelChange', callback?: AsyncCallback\<SystemPressureLevel\>): void
407
408Unsubscribes from system pressure level change events.
409
410**Atomic service API**: This API can be used in atomic services since API version 20.
411
412**System capability**: SystemCapability.Multimedia.Camera.Core
413
414**Parameters**
415
416| Name   | Type                          | Mandatory   | Description                                                            |
417|----------|--------------------------------|---------|------------------------------------------------------------------|
418| type     | string                         | Yes     | Event type. The value is fixed at **'systemPressureLevelChange'**. The event can be listened for when a session is created.|
419| callback | AsyncCallback\<[SystemPressureLevel](arkts-apis-camera-e.md#systempressurelevel20)\> | No| Callback used to return the result. If this parameter is specified, the subscription to the specified event with the specified callback is canceled. (The callback object cannot be an anonymous function.) Otherwise, the subscriptions to the specified event with all the callbacks are canceled.|
420
421**Example**
422
423```ts
424function unregisterSystemPressureLevelChangeCallback(photoSession: camera.PhotoSession): void {
425  photoSession.off('systemPressureLevelChange');
426}
427```
428