harmony 鸿蒙Moving Photos (ArkTS)
Moving Photos (ArkTS)
The camera framework provides the capability of taking moving photos. With this capability, users can take a moving photo in one-click mode, in a way similar to taking an ordinary photo.
To develop the moving photo feature, perform the following steps:
- Check whether the device supports taking moving photos.
- Enable the capability of taking moving photos (if supported).
- Listen for the photo callback function and save the photo to the media library. For details, see Accessing and Managing Moving Photos.
How to Develop
Read Camera for the API reference.
NOTE
- Before enabling the capability of taking moving photos, you must enable deferred photo delivery.
- The permission ohos.permission.MICROPHONE is required for taking moving photos. For details about how to apply for and verify the permission, see Requesting Camera Development Permissions. Otherwise, there is no sound when a photo is being taken.
- Import dependencies. Specifically, import the camera, image, and mediaLibrary modules.
import { camera } from '@kit.CameraKit';
import { photoAccessHelper } from '@kit.MediaLibraryKit';
import { BusinessError } from '@kit.BasicServicesKit';
- Determine the photo output stream.
You can use the photoProfiles attribute of the CameraOutputCapability class to obtain the photo output streams supported by the device and use createPhotoOutput to create a photo output stream.
function getPhotoOutput(cameraManager: camera.CameraManager,
cameraOutputCapability: camera.CameraOutputCapability): camera.PhotoOutput|undefined {
let photoProfilesArray: Array<camera.Profile> = cameraOutputCapability.photoProfiles;
if (!photoProfilesArray) {
console.error("createOutput photoProfilesArray == null||undefined");
}
let photoOutput: camera.PhotoOutput|undefined = undefined;
try {
photoOutput = cameraManager.createPhotoOutput(photoProfilesArray[0]);
} catch (error) {
let err = error as BusinessError;
console.error(`Failed to createPhotoOutput. error: ${err}`);
}
return photoOutput;
}
Check whether the device supports taking moving photos.
NOTE
Before the check, you must configure, commit, and start a session. For details, see Camera Session Management.
function isMovingPhotoSupported(photoOutput: camera.PhotoOutput): boolean { let isSupported: boolean = false; try { isSupported = photoOutput.isMovingPhotoSupported(); } catch (error) { // If the operation fails, error.code is returned and processed. let err = error as BusinessError; console.error(`The isMovingPhotoSupported call failed. error code: ${err.code}`); } return isSupported; }
Enable the capability of taking moving photos.
function enableMovingPhoto(photoOutput: camera.PhotoOutput): void {
try {
photoOutput.enableMovingPhoto(true);
} catch (error) {
// If the operation fails, error.code is returned and processed.
let err = error as BusinessError;
console.error(`The enableMovingPhoto call failed. error code: ${err.code}`);
}
}
- Trigger photo capture. This procedure is the same as that in the common photo capture mode. For details, see Photo Capture.
Status Listening
During camera application development, you can listen for the output stream status of moving photos by registering the ‘photoAsset’ event. This event can be registered when a PhotoOutput instance is created.
function getPhotoAccessHelper(context: Context): photoAccessHelper.PhotoAccessHelper {
let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context);
return phAccessHelper;
}
async function mediaLibSavePhoto(photoAsset: photoAccessHelper.PhotoAsset,
phAccessHelper: photoAccessHelper.PhotoAccessHelper): Promise<void> {
try {
let assetChangeRequest: photoAccessHelper.MediaAssetChangeRequest = new photoAccessHelper.MediaAssetChangeRequest(photoAsset);
assetChangeRequest.saveCameraPhoto();
await phAccessHelper.applyChanges(assetChangeRequest);
console.info('apply saveCameraPhoto successfully');
} catch (err) {
console.error(`apply saveCameraPhoto failed with error: ${err.code}, ${err.message}`);
}
}
function onPhotoOutputPhotoAssetAvailable(photoOutput: camera.PhotoOutput, context: Context): void {
photoOutput.on('photoAssetAvailable', (err: BusinessError, photoAsset: photoAccessHelper.PhotoAsset): void => {
if (err) {
console.info(`photoAssetAvailable error: ${err}.`);
return;
}
console.info('photoOutPutCallBack photoAssetAvailable');
// Call the mediaLibrary flush API to save the first-phase images and moving photos.
mediaLibSavePhoto(photoAsset, getPhotoAccessHelper(context));
});
}
你可能感兴趣的鸿蒙文章
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框自动聚焦