You can use the camera module to develop basic camera functions, including previewing, photographing, and video recording.
With the APIs provided by the **Camera** module, you can access and operate camera devices and develop new functions. Common operations include preview, photographing, and video recording. You can also implement flash control, exposure time control, focus mode control, zooming control, and many others.
Before calling camera APIs, be familiar with the following concepts:
-**Static camera capabilities**: A series of parameters used to describe inherent capabilities of a camera, such as orientation and supported resolution.
-**Physical camera**: An independent camera device. The physical camera ID is a string that uniquely identifies a physical camera.
-**Asynchronous operation**: To prevent the UI thread from being blocked, most **Camera** calls are asynchronous. Each API provides the callback and promise functions.
## How to Develop
...
...
@@ -12,152 +18,298 @@ For details about the APIs, see [Camera Management](../reference/apis/js-apis-ca
### Full-Process Scenario
The full process includes creating an instance, setting parameters, managing sessions, taking photos, recording videos, and releasing resources.
The full process includes applying for permissions, creating an instance, setting parameters, managing sessions, taking photos, recording videos, and releasing resources.
#### Applying for Permissions
You must apply for the permission for your application to access the camera device and other functions. The following table lists camera-related permissions.
| Permission| Attribute Value |
| -------- | ------------------------------ |
| Camera| ohos.permission.CAMERA |
| Call recording| ohos.permission.MICROPHONE |
| Storage| ohos.permission.WRITE_MEDIA |
| Read| ohos.permission.READ_MEDIA |
| Location| ohos.permission.MEDIA_LOCATION |
The method for creating an XComponent is also provided. For details, see [XComponent Creation](#xcomponent-creation).
The code snippet is as follows:
For details about the APIs used to save images, see [Image Processing](../reference/apis/js-apis-image.md).
You must create an independent **CameraManager** instance before performing camera operations. If this operation fails, the camera may be occupied or unusable. If the camera is occupied, wait until it is released. You can call **getSupportedCameras()** to obtain the list of cameras supported by the current device. The list stores all camera IDs of the current device. If the list is not empty, each ID in the list can be used to create an independent camera instance. If the list is empty, no camera is available for the current device and subsequent operations cannot be performed. The camera has preview, shooting, video recording, and metadata streams. You can use **getSupportedOutputCapability()** to obtain the output stream capabilities of the camera and configure them in the **profile** field in **CameraOutputCapability**. The procedure for creating a **CameraManager** instance is as follows:
// Create a preview stream. For details about the surfaceId parameter, see the XComponent section. The preview stream is the surface provided by the XComponent.
console.error("Failed to create the PreviewOutput instance.")
}
// Create an ImageReceiver object and set image parameters.
// Create an ImageReceiver object and set photo parameters. The resolution is set based on the photographing resolutions supported by the current device, which are obtained by photoProfilesArray.
console.error('Failed to create the videoOutput instance.');
return;
}
```
Surfaces must be created in advance for the preview, shooting, and video recording stream. The preview stream is the surface provided by the **XComponent**, the shooting stream is the surface provided by **ImageReceiver**, and the video recording stream is the surface provided by **VideoRecorder**.
**XComponent**
```typescript
mXComponentController:XComponentController=newXComponentController// Create an XComponentController.
build(){
Flex(){
XComponent({// Create an XComponent.
id:'',
type:'surface',
libraryname:'',
controller:this.mXComponentController
})
.onload(()=>{// Set the onload callback.
// Set the surface width and height (1920 x 1080). For details about how to set the preview size, see the preview resolutions supported by the current device, which are obtained by previewProfilesArray.