The camera fails to start normally, and related logs indicate issues such as failed checks or parameter errors.
The normal camera startup process includes creating a camera manager (cameraManager), creating input and output streams, creating a session, configuring the session, and starting the session. These steps are interdependent. If any part fails to be created, is released during the process (resulting in a nonexistent object), or uses a mismatched profile, the camera cannot start properly. For example, an output stream may be created without first creating an input stream, or the output stream may be created without using the capabilities obtained from the corresponding input stream, causing a mismatch between the input and output streams. Specific causes may include the following:
An incorrect profile is used, causing the created output stream to mismatch the input stream, which results in a black screen when the camera starts.
Camera configuration APIs are called prematurely, before the corresponding configuration is fully set, causing the camera to fail to start.
The listener is registered at the wrong time, so it does not take effect, causing the camera to fail to start.
Problem Analysis Steps
Step 1: Create a camera manager (cameraManager), select the camera device to use (getSupportedCameras), and then obtain the output stream profile (getSupportedOutputCapability) through the selected camera device. Ensure that the profile matches the output stream mode (photo capture or video recording).
Step 2: Create the corresponding input stream based on the profile, call the open method of the input stream to open the camera device, and then create the output streams (preview, photo capture/video recording). It is recommended to register the photoAvailable listener at this point to obtain photos. Keep the resolutions of the output streams the same, and then create a session of the corresponding mode. Ensure that the mode matches the output streams.
Step 3: Call the beginConfig method of the session to start configuration. First, call the addInput method to add the input stream. Then, call the addOutput method to add the output streams. After that, call the commitConfig method to commit the session configuration. At this point, the session configuration is complete. Most issues are reported during this process. If a problem occurs, the entire commitConfig process cannot be completed normally.
Step 4: After commitConfig, most session listener methods can be configured at this stage, and certain effect settings, such as white balance, can be applied.
Before creating the output stream and input stream, compare the resolutions in their profiles to ensure they are consistent.
Most set methods are recommended to be called after commitConfig, such as the set APIs related to white balance.
Most listener-related APIs must be called after addInput to ensure that data can be returned through the listener after the camera is opened and added to the session.
For specific code, refer to Photo Capture Practices (ArkTS).