From 473a04bb4ba4c150f9d18db4116f1315c1fbe216 Mon Sep 17 00:00:00 2001 From: suqikuan Date: Wed, 24 Sep 2025 15:00:48 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E5=91=8A=E8=AD=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: suqikuan --- .../main/ets/entryability/EntryAbility.ets | 8 +- entry/src/main/ets/pages/Index.ets | 20 ++-- entry/src/main/ets/utils/CameraShooter.ets | 102 +++++++++++------- entry/src/main/ets/utils/PreviewUtil.ets | 10 +- entry/src/main/ets/utils/VideoRecorder.ets | 75 ++++++++----- 5 files changed, 141 insertions(+), 74 deletions(-) diff --git a/entry/src/main/ets/entryability/EntryAbility.ets b/entry/src/main/ets/entryability/EntryAbility.ets index 78b0b23..3452786 100644 --- a/entry/src/main/ets/entryability/EntryAbility.ets +++ b/entry/src/main/ets/entryability/EntryAbility.ets @@ -28,8 +28,12 @@ export default class EntryAbility extends UIAbility { onWindowStageCreate(windowStage: window.WindowStage): void { // Main window is created, set main page for this ability hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); - windowStage.getMainWindowSync().setWindowLayoutFullScreen(true); - AppStorage.setOrCreate('window', windowStage.getMainWindowSync()) + try { + windowStage.getMainWindowSync().setWindowLayoutFullScreen(true); + AppStorage.setOrCreate('window', windowStage.getMainWindowSync()); + } catch (error) { + hilog.error(0x0000, 'testTag', 'Failed to use windowStage. Cause: %{public}s', JSON.stringify(error) ?? ''); + } windowStage.loadContent('pages/Index', (err, data) => { if (err.code) { hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index b51a44d..901dcca 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -226,7 +226,11 @@ struct XComponentPage { aboutToDisappear(): void { clearInterval(this.countDownInterval); - this.windowClass?.off('windowRectChange'); + try { + this.windowClass?.off('windowRectChange'); + } catch (error) { + console.error(`The aboutToDisappear call failed. error: ${JSON.stringify(error)}`); + } sensor.off(sensor.SensorId.GRAVITY); } @@ -686,11 +690,15 @@ struct XComponentPage { predicates: predicates }; let photoHelper = photoAccessHelper.getPhotoAccessHelper(this.context); - let fetchResult: photoAccessHelper.FetchResult = - await photoHelper.getAssets(fetchOptions); - if (fetchResult !== undefined) { - let photoAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); - this.photoUri = await photoAsset.getThumbnail(); + try { + let fetchResult: photoAccessHelper.FetchResult = + await photoHelper.getAssets(fetchOptions); + if (fetchResult !== undefined) { + let photoAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); + this.photoUri = await photoAsset.getThumbnail(); + } + } catch (error) { + console.error(`The getThumbnail call failed. error: ${JSON.stringify(error)}`); } } diff --git a/entry/src/main/ets/utils/CameraShooter.ets b/entry/src/main/ets/utils/CameraShooter.ets index 40f1fb1..90fab25 100644 --- a/entry/src/main/ets/utils/CameraShooter.ets +++ b/entry/src/main/ets/utils/CameraShooter.ets @@ -139,24 +139,39 @@ 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); + try { + // [Start is_support1] + let isSupported: boolean = photoOutPut.isMovingPhotoSupported(); + // [End is_support1] + // [Start photo_out_put] + if (isSupported) { + photoOutPut.enableMovingPhoto(isMovingPhoto); + } + // [End photo_out_put] + } catch (error) { + console.error(`The enableLivePic call failed. error: ${JSON.stringify(error)}`); } - // [End photo_out_put] } export function setPhotoZoom(zoom: number): void { - // [Start setZoom_Ratio] - photoSession.setZoomRatio(zoom); - // [End setZoom_Ratio] + try { + // [Start setZoom_Ratio] + photoSession.setZoomRatio(zoom); + // [End setZoom_Ratio] + } catch (error) { + console.error(`The enableLivePic call failed. error: ${JSON.stringify(error)}`); + } } export function getPhotoZoom(): number { - return photoSession.getZoomRatio(); + let zoomRatio = 1; + try { + zoomRatio = photoSession.getZoomRatio(); + } catch (error) { + console.error(`The getPhotoZoom call failed. error: ${JSON.stringify(error)}`); + } finally { + return zoomRatio; + } } export function setPhotoSmoothZoom(zoom: number): void { @@ -165,14 +180,18 @@ 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] + try { + // [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] + } catch (error) { + console.error(`The capture call failed. error: ${error.code}`); + } } export async function burstCapture(isFront: boolean, captureCount: number) { @@ -191,28 +210,35 @@ export async function burstCapture(isFront: boolean, captureCount: number) { } export async function setPhotoFlashMode(flashMode: number): Promise { - photoSession.setFlashMode(flashMode); + try { + photoSession.setFlashMode(flashMode); + } catch (error) { + console.error(`The setPhotoFlashMode call failed. error: ${error.code}`); + } } export async function releaseCamera(): Promise { - - if (photoSession) { - photoSession.stop(); - } - if (cameraInput) { - cameraInput.close(); - } - if (previewOutput) { - previewOutput.release(); - } - if (photoSession) { - photoSession.release(); - } - if (photoOutPut) { - emitter.off('burstCaptureOff'); - photoOutPut.off('photoAssetAvailable'); - photoOutPut.off('captureReady'); - photoOutPut.release(); + try { + if (photoSession) { + photoSession.stop(); + } + if (cameraInput) { + cameraInput.close(); + } + if (previewOutput) { + previewOutput.release(); + } + if (photoSession) { + photoSession.release(); + } + if (photoOutPut) { + emitter.off('burstCaptureOff'); + photoOutPut.off('photoAssetAvailable'); + photoOutPut.off('captureReady'); + photoOutPut.release(); + } + } catch (error) { + console.error(`The releaseCamera call failed. error: ${error.code}`); } } diff --git a/entry/src/main/ets/utils/PreviewUtil.ets b/entry/src/main/ets/utils/PreviewUtil.ets index ea362dc..7813f71 100644 --- a/entry/src/main/ets/utils/PreviewUtil.ets +++ b/entry/src/main/ets/utils/PreviewUtil.ets @@ -93,8 +93,14 @@ export function getTargetPreviewProfile(targetRatio: number, previewProfilesArra */ export function getCurrentWindowSize() { const windowClass = AppStorage.get('window'); - const windowSize = windowClass?.getWindowProperties().windowRect; - return { width: windowSize?.width ?? 0, height: windowSize?.height ?? 0 } as Size; + let size: Size = {width: 0, height: 0}; + try { + const windowSize = windowClass?.getWindowProperties().windowRect; + size = { width: windowSize?.width ?? 0, height: windowSize?.height ?? 0 } + } catch (error) { + console.error(`The getCurrentWindowSize call failed. error: ${JSON.stringify(error)}`); + } + return size; } /** diff --git a/entry/src/main/ets/utils/VideoRecorder.ets b/entry/src/main/ets/utils/VideoRecorder.ets index f4906e7..62ed7b9 100644 --- a/entry/src/main/ets/utils/VideoRecorder.ets +++ b/entry/src/main/ets/utils/VideoRecorder.ets @@ -190,29 +190,44 @@ export async function videoRecording(isStabilization: boolean, cameraPosition: n } export async function stopRecordPreview(): Promise { - if (videoSession) { - videoSession.stop(); - } - if (cameraInput) { - cameraInput.close(); - } - if (previewOutput) { - previewOutput.release(); - } - if (videoSession) { - videoSession.release(); - } - if (videoOutput) { - videoOutput.release(); + try { + if (videoSession) { + videoSession.stop(); + } + if (cameraInput) { + cameraInput.close(); + } + if (previewOutput) { + previewOutput.release(); + } + if (videoSession) { + videoSession.release(); + } + if (videoOutput) { + videoOutput.release(); + } + } catch (error) { + console.error(`The stopRecordPreview call failed. error: ${JSON.stringify(error)}`); } } export function setVideoZoom(zoom: number): void { - videoSession.setZoomRatio(zoom); + try { + videoSession.setZoomRatio(zoom); + } catch (error) { + console.error(`The setVideoZoom call failed. error: ${JSON.stringify(error)}`); + } } export function getVideoZoom(): number { - return videoSession.getZoomRatio(); + let zoomRatio = 1; + try { + zoomRatio = videoSession.getZoomRatio(); + } catch (error) { + console.error(`The getVideoZoom call failed. error: ${JSON.stringify(error)}`); + } finally { + return zoomRatio; + } } export function setVideoSmoothZoom(zoom: number): void { @@ -222,23 +237,31 @@ export function setVideoSmoothZoom(zoom: number): void { export async function startRecord(): Promise { // Update the rotation angle before starting recording const deviceDegree = await getGravity(); - await avRecorder.updateRotation(getVideoRotation(videoOutput, deviceDegree)); - await videoOutput.start(); - // [Start recorder1] - await avRecorder.start(); - // [End recorder1] + try { + await avRecorder.updateRotation(getVideoRotation(videoOutput, deviceDegree)); + await videoOutput.start(); + // [Start recorder1] + await avRecorder.start(); + // [End recorder1] + } catch (error) { + console.error(`The startRecord call failed. error: ${JSON.stringify(error)}`); + } } export async function setVideoFlashMode(flashMode: number): Promise { - videoSession.setFlashMode(flashMode); + try { + videoSession.setFlashMode(flashMode); + } catch (error) { + console.error(`The setVideoFlashMode call failed. error: ${JSON.stringify(error)}`); + } } export async function stopRecord(): Promise { - // [Start recorder2] - await avRecorder.stop(); - // [End recorder2] - await avRecorder.release(); try { + // [Start recorder2] + await avRecorder.stop(); + // [End recorder2] + await avRecorder.release(); fileIo.closeSync(file); } catch (error) { console.error(`The stopRecord call failed. error: ${JSON.stringify(error)}`); -- Gitee