• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Taking Photos in Portrait Mode
2
3## How to Develop
4
5Portrait mode depends on the mode manager. After obtaining the mode management capability, you can create photo streams.
6
7Mode management, as an enhancement to **CameraManager**, is used to manage advanced camera features. The figure below illustrates the mode management development process.
8
9![portraitgraphing Development Process](figures/portrait-capture-development-process.png)
10
11## Sample Code
12There are different [types of contexts](../application-models/application-context-stage.md).
13```ts
14import camera from '@ohos.multimedia.camera';
15import { BusinessError } from '@ohos.base';
16import image from '@ohos.multimedia.image';
17import featureAbility from '@ohos.ability.featureAbility';
18
19async function cameraModeCase(context: featureAbility.Context, surfaceId: string): Promise<void> {
20  // Create a CameraManager instance.
21  let cameraManager: camera.CameraManager = camera.getCameraManager(context);
22  if (!cameraManager) {
23    console.error("camera.getCameraManager error");
24    return;
25  }
26  // Create a ModeManager instance.
27  let modeManager: camera.ModeManager = camera.getModeManager(context);
28  if (!cameraManager) {
29    console.error("camera.getModeManager error");
30    return;
31  }
32  // Listen for camera status changes.
33  cameraManager.on('cameraStatus', (err: BusinessError, cameraStatusInfo: camera.CameraStatusInfo) => {
34    console.info(`camera : ${cameraStatusInfo.camera.cameraId}`);
35    console.info(`status: ${cameraStatusInfo.status}`);
36  });
37  // Obtain the camera list.
38  let cameraArray: Array<camera.CameraDevice> = cameraManager.getSupportedCameras();
39  if (cameraArray.length <= 0) {
40    console.error("cameraManager.getSupportedCameras error");
41    return;
42  }
43
44  for (let index = 0; index < cameraArray.length; index++) {
45    console.info('cameraId : ' + cameraArray[index].cameraId); // Obtain the camera ID.
46    console.info('cameraPosition : ' + cameraArray[index].cameraPosition); // Obtain the camera position.
47    console.info('cameraType : ' + cameraArray[index].cameraType); // Obtain the camera type.
48    console.info('connectionType : ' + cameraArray[index].connectionType); // Obtain the camera connection type.
49  }
50
51  // Obtain the mode list.
52  let cameraModeArray: Array<camera.CameraMode> = modeManager.getSupportedModes(cameraArray[0]);
53  if (cameraModeArray.length <= 0) {
54    console.error("modeManager.getSupportedModes error");
55    return;
56  }
57  // Create a camera input stream.
58  let cameraInput: camera.CameraInput | undefined = undefined;
59  try {
60    cameraInput = cameraManager.createCameraInput(cameraArray[0]);
61  } catch (error) {
62    let err = error as BusinessError;
63    console.error('Failed to createCameraInput errorCode = ' + err.code);
64  }
65  // Listen for camera input errors.
66  let cameraDevice: camera.CameraDevice = cameraArray[0];
67  if (cameraInput === undefined) {
68    return;
69  }
70  cameraInput.on('error', cameraDevice, (error: BusinessError) => {
71    console.info(`Camera input error code: ${error.code}`);
72  });
73
74  // Open the camera.
75  await cameraInput.open();
76
77  // Obtain the output stream capabilities supported by the camera in the current mode.
78  let cameraOutputCap: camera.CameraOutputCapability = modeManager.getSupportedOutputCapability(cameraArray[0], cameraModeArray[0]);
79  if (!cameraOutputCap) {
80    console.error("modeManager.getSupportedOutputCapability error");
81    return;
82  }
83  console.info("outputCapability: " + JSON.stringify(cameraOutputCap));
84
85  let previewProfilesArray: Array<camera.Profile> = cameraOutputCap.previewProfiles;
86  if (!previewProfilesArray) {
87    console.error("createOutput previewProfilesArray == null || undefined");
88  }
89
90  let photoProfilesArray: Array<camera.Profile> = cameraOutputCap.photoProfiles;
91  if (!photoProfilesArray) {
92    console.error("createOutput photoProfilesArray == null || undefined");
93  }
94
95  // Create a preview output stream. For details about the surfaceId parameter, see the XComponent. The preview stream uses the surface provided by the XComponent.
96  let previewOutput: camera.PreviewOutput | undefined = undefined;
97  try {
98    previewOutput = cameraManager.createPreviewOutput(previewProfilesArray[0], surfaceId);
99  } catch (error) {
100    let err = error as BusinessError;
101    console.error("Failed to create the PreviewOutput instance. error code:" + err.code);
102  }
103  if (previewOutput === undefined) {
104    return;
105  }
106  // Listen for preview output errors.
107  previewOutput.on('error', (error: BusinessError) => {
108    console.info(`Preview output error code: ${error.code}`);
109  });
110  // Create an ImageReceiver instance and set photographing parameters. Wherein, the resolution must be one of the photographing resolutions supported by the current device, which are obtained from photoProfilesArray.
111  let imageReceiver: image.ImageReceiver = image.createImageReceiver(1920, 1080, 4, 8);
112  // Obtain the surface ID for displaying photos.
113  let photoSurfaceId: string = await imageReceiver.getReceivingSurfaceId();
114  // Create a photo output stream.
115  let photoOutput: camera.PhotoOutput | undefined = undefined;
116  try {
117    photoOutput = cameraManager.createPhotoOutput(photoProfilesArray[0], photoSurfaceId);
118  } catch (error) {
119    let err = error as BusinessError;
120    console.error('Failed to createPhotoOutput errorCode = ' + err.code);
121  }
122  if (photoOutput === undefined) {
123    return;
124  }
125  // Create a portrait session.
126  let portraitSession: camera.CaptureSession | undefined = undefined;
127  try {
128    portraitSession = modeManager.createCaptureSession(cameraModeArray[0]);
129  } catch (error) {
130    let err = error as BusinessError;
131    console.error('Failed to create the CaptureSession instance. errorCode = ' + err.code);
132  }
133  if (portraitSession === undefined) {
134    return;
135  }
136  // Listen for portrait session errors.
137  portraitSession.on('error', (error: BusinessError) => {
138    console.info(`Capture session error code: ${error.code}`);
139  });
140
141  // Start configuration for the session.
142  try {
143    portraitSession.beginConfig();
144  } catch (error) {
145    let err = error as BusinessError;
146    console.error('Failed to beginConfig. errorCode = ' + err.code);
147  }
148
149  // Add the camera input stream to the session.
150  try {
151    portraitSession.addInput(cameraInput);
152  } catch (error) {
153    let err = error as BusinessError;
154    console.error('Failed to addInput. errorCode = ' + err.code);
155  }
156
157  // Add the preview output stream to the session.
158  try {
159    portraitSession.addOutput(previewOutput);
160  } catch (error) {
161    let err = error as BusinessError;
162    console.error('Failed to addOutput(previewOutput). errorCode = ' + err.code);
163  }
164
165  // Add the photo output stream to the session.
166  try {
167    portraitSession.addOutput(photoOutput);
168  } catch (error) {
169    let err = error as BusinessError;
170    console.error('Failed to addOutput(photoOutput). errorCode = ' + err.code);
171  }
172
173  // Commit the session configuration.
174  await portraitSession.commitConfig();
175
176  // Start the session.
177  await portraitSession.start().then(() => {
178    console.info('Promise returned to indicate the session start success.');
179  })
180
181  // Obtain the supported beauty types.
182  let beautyTypes: Array<camera.BeautyType> = [];
183  try {
184    beautyTypes = portraitSession.getSupportedBeautyTypes();
185  } catch (error) {
186    let err = error as BusinessError;
187    console.error('Failed to get the beauty types. errorCode = ' + err.code);
188  }
189  if (beautyTypes.length <= 0) {
190    return;
191  }
192  // Obtain the beautify levels of the beauty type.
193  let beautyRanges: Array<number> = [];
194  try {
195    beautyRanges = portraitSession.getSupportedBeautyRange(beautyTypes[0]);
196  } catch (error) {
197    let err = error as BusinessError;
198    console.error('Failed to get the beauty types ranges. errorCode = ' + err.code);
199  }
200  if (beautyRanges.length <= 0) {
201    return;
202  }
203  // Set the beauty type and its level.
204  try {
205    portraitSession.setBeauty(beautyTypes[0], beautyRanges[0]);
206  } catch (error) {
207    let err = error as BusinessError;
208    console.error('Failed to set the beauty type value. errorCode = ' + err.code);
209  }
210  // Obtain the beauty level in use.
211  let beautyLevel: number = -1;
212  try {
213    beautyLevel = portraitSession.getBeauty(beautyTypes[0]);
214  } catch (error) {
215    let err = error as BusinessError;
216    console.error('Failed to get the beauty type value. errorCode = ' + err.code);
217  }
218
219  if (beautyLevel === -1) {
220    return;
221  }
222  // Obtain the supported filter types.
223  let filterTypes: Array<camera.FilterType> = [];
224  try {
225    filterTypes = portraitSession.getSupportedFilters();
226  } catch (error) {
227    let err = error as BusinessError;
228    console.error('Failed to get the filter types. errorCode = ' + err.code);
229  }
230  if (filterTypes.length <= 0) {
231    return;
232  }
233  // Set a filter type.
234  try {
235    portraitSession.setFilter(filterTypes[0]);
236  } catch (error) {
237    let err = error as BusinessError;
238    console.error('Failed to set the filter type value. errorCode = ' + err.code);
239  }
240  // Obtain the filter type in use.
241  let filter: number = -1;
242  try {
243    filter = portraitSession.getFilter();
244  } catch (error) {
245    let err = error as BusinessError;
246    console.error('Failed to get the filter type value. errorCode = ' + err.code);
247  }
248  if (filter === -1) {
249    return;
250  }
251
252  // Obtain the supported portrait types.
253  let portraitTypes: Array<camera.PortraitEffect> = [];
254  try {
255    let portraitSession1: camera.PortraitSession = portraitSession as camera.PortraitSession;
256    portraitTypes = portraitSession1.getSupportedPortraitEffects();
257  } catch (error) {
258    let err = error as BusinessError;
259    console.error('Failed to get the portrait effects types. errorCode = ' + err.code);
260  }
261  if (portraitTypes.length <= 0) {
262    return;
263  }
264  // Set a portrait type.
265  try {
266    let portraitSession1: camera.PortraitSession = portraitSession as camera.PortraitSession;
267    portraitSession1.setPortraitEffect(portraitTypes[0]);
268  } catch (error) {
269    let err = error as BusinessError;
270    console.error('Failed to set the portrait effects value. errorCode = ' + err.code);
271  }
272  // Obtain the portrait type in use.
273  let effect: camera.PortraitEffect | undefined = undefined;
274  try {
275    let portraitSession1: camera.PortraitSession = portraitSession as camera.PortraitSession;
276    effect = portraitSession1.getPortraitEffect();
277  } catch (error) {
278    let err = error as BusinessError;
279    console.error('Failed to get the portrait effects value. errorCode = ' + err.code);
280  }
281
282  let captureSettings: camera.PhotoCaptureSetting = {
283    quality: camera.QualityLevel.QUALITY_LEVEL_HIGH,
284    rotation: camera.ImageRotation.ROTATION_0,
285    mirror: false
286  };
287  // Use the current photographing settings to take photos.
288  photoOutput.capture(captureSettings, async (err: BusinessError) => {
289    if (err) {
290      console.error('Failed to capture the photo ${err.message}');
291      return;
292    }
293    console.info('Callback invoked to indicate the photo capture request success.');
294  });
295  // Stop the session.
296  portraitSession.stop();
297
298  // Release the camera input stream.
299  cameraInput.close();
300
301  // Release the preview output stream.
302  previewOutput.release();
303
304  // Release the photo output stream.
305  photoOutput.release();
306
307  // Release the session.
308  portraitSession.release();
309
310  // Set the session to null.
311  portraitSession = undefined;
312}
313```
314