harmony 鸿蒙Device Input Management (ArkTS)
Device Input Management (ArkTS)
Before developing a camera application, request permissions by following the instructions provided in Camera Development Preparations.
A camera application invokes and controls a camera device to perform basic operations such as preview, photo capture, and video recording.
How to Develop
Read Camera for the API reference.
- Import the camera module, which provides camera-related attributes and methods.
import { camera } from '@kit.CameraKit';
import { BusinessError } from '@kit.BasicServicesKit';
NOTE
Before any camera device input, you must complete camera management by following the instructions provided in Camera Device Management.
- Call createCameraInput in the cameraManager class to create a camera input stream.
async function createInput(cameraDevice: camera.CameraDevice, cameraManager: camera.CameraManager): Promise<camera.CameraInput|undefined> {
// Create a camera input stream.
let cameraInput: camera.CameraInput|undefined = undefined;
try {
cameraInput = cameraManager.createCameraInput(cameraDevice);
} catch (error) {
let err = error as BusinessError;
console.error('Failed to createCameraInput errorCode = ' + err.code);
}
if (cameraInput === undefined) {
return undefined;
}
// Listen for camera input errors.
cameraInput.on('error', cameraDevice, (error: BusinessError) => {
console.error(`Camera input error code: ${error.code}`);
});
// Open the camera.
await cameraInput.open();
return cameraInput;
}
Call getSupportedSceneModes to obtain the list of scene modes supported by the current camera device. The list stores all the SceneModes supported by the camera device.
function getSupportedSceneMode(cameraDevice: camera.CameraDevice, cameraManager: camera.CameraManager): Array<camera.SceneMode> { // Obtain the list of scene modes supported by the camera device. let sceneModeArray: Array<camera.SceneMode> = cameraManager.getSupportedSceneModes(cameraDevice); if (sceneModeArray != undefined && sceneModeArray.length > 0) { for (let index = 0; index < sceneModeArray.length; index++) { console.info('Camera SceneMode : ' + sceneModeArray[index]); } return sceneModeArray; } else { console.error("cameraManager.getSupportedSceneModes error"); return []; } }
Call getSupportedOutputCapability to obtain all output streams supported by the current camera device, such as preview streams, photo streams, and video streams. The supported output streams are listed in the profile field in CameraOutputCapability. Different types of output streams must be added based on the value of SceneMode specified by the camera device.
async function getSupportedOutputCapability(cameraDevice: camera.CameraDevice, cameraManager: camera.CameraManager, sceneMode: camera.SceneMode): Promise<camera.CameraOutputCapability|undefined> {
// Obtain the output stream capability supported by the camera.
let cameraOutputCapability: camera.CameraOutputCapability = cameraManager.getSupportedOutputCapability(cameraDevice, sceneMode);
if (!cameraOutputCapability) {
console.error("cameraManager.getSupportedOutputCapability error");
return undefined;
}
console.info("outputCapability: " + JSON.stringify(cameraOutputCapability));
// The following uses the NORMAL_PHOTO mode as an example. You need to add the preview stream and photo stream.
// previewProfiles is the preview output streams supported by the current camera device.
let previewProfilesArray: Array<camera.Profile> = cameraOutputCapability.previewProfiles;
if (!previewProfilesArray) {
console.error("createOutput previewProfilesArray == null||undefined");
}
// photoProfiles is the photo output streams supported by the current camera device.
let photoProfilesArray: Array<camera.Profile> = cameraOutputCapability.photoProfiles;
if (!photoProfilesArray) {
console.error("createOutput photoProfilesArray == null||undefined");
}
return cameraOutputCapability;
}
你可能感兴趣的鸿蒙文章
harmony 鸿蒙Basic Camera Animation (ArkTS)
harmony 鸿蒙Practices for Camera Recovery from the Background (ArkTS)
harmony 鸿蒙Deferred Photo Delivery Practices (ArkTS)
harmony 鸿蒙Deferred Photo Delivery (ArkTS)
harmony 鸿蒙Practices for High-Performance Photo Capture (for System Applications Only) (ArkTS)
harmony 鸿蒙High-Performance Photo Capture (for System Applications Only) (ArkTS)
harmony 鸿蒙Depth Data (for System Applications Only) (ArkTS)
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦