diff --git a/entry/src/main/ets/utils/CameraShooter.ets b/entry/src/main/ets/utils/CameraShooter.ets index f6d3a4abf970dee59b11323f05b8bbec9a09df2b..60fd483f5ab24e6003d6fac3573096927b149ebb 100644 --- a/entry/src/main/ets/utils/CameraShooter.ets +++ b/entry/src/main/ets/utils/CameraShooter.ets @@ -37,21 +37,29 @@ export async function cameraShooting(isVideo: boolean, cameraPosition: number, s currentContext = context; isVideo = false; releaseCamera(); + // [Start camera_manager] let cameraManager: camera.CameraManager = camera.getCameraManager(context); + // [End camera_manager] if (!cameraManager) { return []; } // Obtaining the Camera List + // [Start camera_Array] let cameraArray: camera.CameraDevice[] = cameraManager.getSupportedCameras(); + // [End camera_Array] if (cameraArray.length <= 0) { return []; } + // [Start camera_Input] cameraInput = cameraManager.createCameraInput(cameraArray[cameraPosition]); await cameraInput.open(); + // [End camera_Input] let sceneModes: camera.SceneMode[] = cameraManager.getSupportedSceneModes(cameraArray[cameraPosition]); + // [Start camera_OutputCap] let cameraOutputCap: camera.CameraOutputCapability = cameraManager.getSupportedOutputCapability(cameraArray[cameraPosition], camera.SceneMode.NORMAL_PHOTO); + // [End camera_OutputCap] let isSupportPhotoMode: boolean = sceneModes.indexOf(camera.SceneMode.NORMAL_PHOTO) >= 0; if (!isSupportPhotoMode) { return []; @@ -59,9 +67,12 @@ export async function cameraShooting(isVideo: boolean, cameraPosition: number, s if (!cameraOutputCap) { return []; } - + // [Start preview_ProfilesArray] let previewProfilesArray: camera.Profile[] = cameraOutputCap.previewProfiles; + // [End preview_ProfilesArray] + // [Start photo_Profiles_Array] let photoProfilesArray: camera.Profile[] = cameraOutputCap.photoProfiles; + // [End photo_Profiles_Array] let previewProfile: undefined | camera.Profile = previewProfilesArray.find((profile: camera.Profile) => { let screen = display.getDefaultDisplaySync(); if (screen.width <= 1080) { @@ -82,21 +93,27 @@ export async function cameraShooting(isVideo: boolean, cameraPosition: number, s } return undefined; }); + // [Start preview_ProfilesArray] previewOutput = cameraManager.createPreviewOutput(previewProfile, surfaceId); + // [End preview_ProfilesArray] if (previewOutput === undefined) { return []; } + // [Start photo_Profiles_Array] photoOutPut = cameraManager.createPhotoOutput(photoProfile); + // [End photo_Profiles_Array] if (photoOutPut === undefined) { return []; } // Save Picture setPhotoOutputCb(photoOutPut); - + // [Start photo_Session] photoSession = cameraManager.createSession(camera.SceneMode.NORMAL_PHOTO) as camera.PhotoSession; + // [End photo_Session] if (photoSession === undefined) { return []; } + // [Start photo_Session1] photoSession.beginConfig(); photoSession.addInput(cameraInput); photoSession.addOutput(previewOutput); @@ -104,16 +121,21 @@ export async function cameraShooting(isVideo: boolean, cameraPosition: number, s photoSession.setColorSpace(colorSpaceManager.ColorSpace.DISPLAY_P3); await photoSession.commitConfig(); await photoSession.start(); + // [End photo_Session1] // Check whether the device supports the flash. let flashStatus: boolean = photoSession.hasFlash(); if (flashStatus) { + // [Start setFlash_Mode] photoSession.setFlashMode(camera.FlashMode.FLASH_MODE_CLOSE); + // [End setFlash_Mode] } // Determine whether the continuous automatic zoom mode is supported. let focusModeStatus: boolean = photoSession.isFocusModeSupported(camera.FocusMode.FOCUS_MODE_CONTINUOUS_AUTO); if (focusModeStatus) { // Set the continuous auto zoom mode + // [Start setFocus_Mode] photoSession.setFocusMode(camera.FocusMode.FOCUS_MODE_CONTINUOUS_AUTO); + // [End setFocus_Mode] } // Obtains the variable focal length ratio range supported by the camera. let zoomRatioRange = photoSession.getZoomRatioRange(); @@ -121,14 +143,20 @@ export async function cameraShooting(isVideo: boolean, cameraPosition: number, s } export function enableLivePic(isMovingPhoto: boolean): void { + // [Start is_support1] let isSupported: boolean = photoOutPut.isMovingPhotoSupported(); + // [End is_support1] + // [Start photo_out_put] if (isSupported) { photoOutPut.enableMovingPhoto(isMovingPhoto); } + // [End photo_out_put] } export function setPhotoZoom(zoom: number): void { + // [Start setZoom_Ratio] photoSession.setZoomRatio(zoom); + // [End setZoom_Ratio] } export function getPhotoZoom(): number { @@ -141,12 +169,14 @@ export function setPhotoSmoothZoom(zoom: number): void { export async function capture(isFront: boolean) { const curRotation = await getGravity() + // [Start PhotoCapture_Setting] let settings: camera.PhotoCaptureSetting = { quality: camera.QualityLevel.QUALITY_LEVEL_HIGH, rotation: curRotation ? getPhotoRotation(photoOutPut, curRotation) : camera.ImageRotation.ROTATION_0, mirror: isFront }; photoOutPut.capture(settings); + // [End PhotoCapture_Setting] } export async function setPhotoFlashMode(flashMode: number): Promise { @@ -173,6 +203,7 @@ export async function releaseCamera(): Promise { } // Sets the callback function after capture +// [Start setPhotoOutput_Cb] function setPhotoOutputCb(photoOutput: camera.PhotoOutput): void { photoOutput.on('photoAssetAvailable', async (_err: BusinessError, photoAsset: photoAccessHelper.PhotoAsset): Promise => { @@ -186,10 +217,12 @@ function setPhotoOutputCb(photoOutput: camera.PhotoOutput): void { AppStorage.setOrCreate('photoUri', await photoAsset.getThumbnail()); }); } +// [End setPhotoOutput_Cb] /** * Jump to the system application gallery * @param context */ +// [Start preview_Photo] export function previewPhoto(context: Context): void { let photoContext = context as common.UIAbilityContext; photoContext.startAbility({ @@ -199,6 +232,7 @@ export function previewPhoto(context: Context): void { abilityName: 'com.huawei.hmos.photos.MainAbility' }) } +// [End preview_Photo] /** * Get cur photo camera.ImageRotation * @param photoOutput diff --git a/entry/src/main/ets/utils/VideoRecorder.ets b/entry/src/main/ets/utils/VideoRecorder.ets index cb15123e2d1d3e7b49ef221fcf4a942da12306ce..5b174a1946097adb879b9c6e2deadee9ecfd2670 100644 --- a/entry/src/main/ets/utils/VideoRecorder.ets +++ b/entry/src/main/ets/utils/VideoRecorder.ets @@ -72,6 +72,7 @@ export async function videoRecording(isStabilization: boolean, cameraPosition: n (profile.size.width / profile.size.height) > (foldAbleStatus === display.FoldStatus.FOLD_STATUS_EXPANDED ? 1 : 16 / 9); }) + // [Start video_profile1] let videoProfile: undefined | camera.VideoProfile = videoProfilesArray.find((profile: camera.VideoProfile) => { if (previewProfile && cameraPosition === 1) { return profile.size.width >= 1080 && profile.size.height >= 1080 && @@ -93,6 +94,7 @@ export async function videoRecording(isStabilization: boolean, cameraPosition: n } return undefined; }) + // [End video_profile1] // Set the parameters based on the actual hardware range. let aVRecorderProfile: media.AVRecorderProfile = { audioBitrate: 48000, @@ -107,6 +109,7 @@ export async function videoRecording(isStabilization: boolean, cameraPosition: n videoFrameHeight: videoProfile?.size.height, videoFrameRate: cameraPosition === 0 ? 60 : 30, } + // [Start option1] let options: photoAccessHelper.CreateOptions = { title: Date.now().toString() }; @@ -121,6 +124,7 @@ export async function videoRecording(isStabilization: boolean, cameraPosition: n rotation: cameraPosition === 0 ? 90 : 270, location: { latitude: 30, longitude: 130 } }; + // [End option1] uri = videoUri; avRecorder = await media.createAVRecorder(); if (avRecorder === undefined) { @@ -131,33 +135,49 @@ export async function videoRecording(isStabilization: boolean, cameraPosition: n if (videoSurfaceId === undefined) { return []; } + // [Start video_profile1] videoOutput = cameraManager.createVideoOutput(videoProfile, videoSurfaceId); + // [End video_profile1] if (videoOutput === undefined) { return []; } + // [Start video_session1] videoSession = cameraManager.createSession(camera.SceneMode.NORMAL_VIDEO) as camera.VideoSession; + // [End video_session1] if (videoSession === undefined) { return []; } + // [Start begin_config] videoSession.beginConfig(); + // [End begin_config] + // [Start open1] cameraInput = cameraManager.createCameraInput(cameraArray[cameraPosition]); + // [StartExclude open1] if (cameraInput === undefined) { return []; } + // [EndExclude open1] await cameraInput.open(); + // [End open1] + // [Start begin_config] videoSession.addInput(cameraInput); + // [End begin_config] let previewOutput: camera.PreviewOutput | undefined = cameraManager.createPreviewOutput(previewProfile, surfaceId); if (previewOutput === undefined) { return []; } + // [Start begin_config] videoSession.addOutput(previewOutput); videoSession.addOutput(videoOutput); await videoSession.commitConfig(); + // [StartExclude begin_config] if (videoSession.isVideoStabilizationModeSupported(camera.VideoStabilizationMode.MIDDLE)) { videoSession.setVideoStabilizationMode(isStabilization ? camera.VideoStabilizationMode.AUTO : camera.VideoStabilizationMode.OFF); } + // [EndExclude begin_config] await videoSession.start(); + // [End begin_config] // Obtains the variable focal length ratio range supported by the camera. let zoomRatioRange = videoSession.getZoomRatioRange(); return zoomRatioRange; @@ -198,7 +218,9 @@ export async function startRecord(): Promise { const deviceDegree = await getGravity() await avRecorder.updateRotation(getVideoRotation(videoOutput,deviceDegree)) await videoOutput.start(); + // [Start recorder1] await avRecorder.start(); + // [End recorder1] } export async function setVideoFlashMode(flashMode: number): Promise { @@ -206,12 +228,14 @@ export async function setVideoFlashMode(flashMode: number): Promise { } export async function stopRecord(): Promise { + // [Start recorder2] await avRecorder.stop(); + // [End recorder2] await avRecorder.release(); fileIo.closeSync(file); return uri; } - +// [Start pre_video1] export function previewVideo(context: Context, videoUri: string): void { let videoContext = context as common.UIAbilityContext; videoContext.startAbility({ @@ -221,7 +245,7 @@ export function previewVideo(context: Context, videoUri: string): void { abilityName: 'com.huawei.hmos.photos.MainAbility' }) } - +// [End pre_video1] /** * Get cur video camera.ImageRotation * @param photoOutput