From e5fb82ea0344bbd51e49acc03a309ba08e8178dc Mon Sep 17 00:00:00 2001 From: lon9 <815882449@qq.com> Date: Tue, 22 Jul 2025 19:56:00 +0800 Subject: [PATCH 1/4] fix bug --- build-profile.json5 | 16 ++++++++- .../main/ets/entryability/EntryAbility.ets | 34 +++++++++++-------- entry/src/main/ets/pages/Index.ets | 25 ++++++++++---- entry/src/main/ets/utils/CameraUtil.ets | 10 ++---- entry/src/main/ets/utils/WindowUtil.ets | 7 ---- entry/src/main/ets/views/CommonView.ets | 6 ++-- 6 files changed, 57 insertions(+), 41 deletions(-) diff --git a/build-profile.json5 b/build-profile.json5 index 2844c74..f5c7df4 100644 --- a/build-profile.json5 +++ b/build-profile.json5 @@ -1,6 +1,20 @@ { "app": { - "signingConfigs": [], + "signingConfigs": [ + { + "name": "default", + "type": "HarmonyOS", + "material": { + "certpath": "C:\\Users\\杨家瑞\\.ohos\\config\\default_MultiDeviceCamera_fSLTdFFESETtDzZ4Xgz2lzZBWO-yN8oDW2Xy3-XLwQU=.cer", + "storePassword": "0000001BAA97964C68A5D13B7F35D74C67EFDFD22F086ABFD145967F7FCF37EBE5BB7DB3FA6CAA7C8971C0", + "keyAlias": "debugKey", + "keyPassword": "0000001B96AE27BE3A58B41DF7882ACB93F8925E51B8D5040CCB735CF44A44A8B375A228FADEE3F5BD0112", + "profile": "C:\\Users\\杨家瑞\\.ohos\\config\\default_MultiDeviceCamera_fSLTdFFESETtDzZ4Xgz2lzZBWO-yN8oDW2Xy3-XLwQU=.p7b", + "signAlg": "SHA256withECDSA", + "storeFile": "C:\\Users\\杨家瑞\\.ohos\\config\\default_MultiDeviceCamera_fSLTdFFESETtDzZ4Xgz2lzZBWO-yN8oDW2Xy3-XLwQU=.p12" + } + } + ], "products": [ { "name": "default", diff --git a/entry/src/main/ets/entryability/EntryAbility.ets b/entry/src/main/ets/entryability/EntryAbility.ets index 0eb48a2..8edeccd 100644 --- a/entry/src/main/ets/entryability/EntryAbility.ets +++ b/entry/src/main/ets/entryability/EntryAbility.ets @@ -28,15 +28,17 @@ export default class EntryAbility extends UIAbility { // [StartExclude WindowSizeChange] windowData?: window.Window; uiContext?: UIContext; - cameraUtil?: CameraUtil = CameraUtil.getInstance(); - windowUtil?: WindowUtil = WindowUtil.getInstance(); + cameraUtil?: CameraUtil; + windowUtil?: WindowUtil; isFirstCreated: boolean = true; + isFirstTime: boolean = true; // [EndExclude WindowSizeChange] // [Start Bp2] // [Start SelectCamera2] // [Start SelectCamera4] // [Start Preview2] onWindowSizeChange: (windowSize: window.Size) => void = (windowSize: window.Size) => { + console.info(`testLog --- 触发`); // [StartExclude SelectCamera4] // [StartExclude WindowSizeChange] // [StartExclude SelectCamera2] @@ -50,21 +52,19 @@ export default class EntryAbility extends UIAbility { AppStorage.setOrCreate('heightBp', heightBp); let displayOrientation: display.Orientation = display.getDefaultDisplaySync().orientation; AppStorage.setOrCreate('displayOrientation', displayOrientation); - // [EndExclude WindowSizeChange] - let isFront: boolean | undefined = AppStorage.get('isFront'); - let surfaceId: string | undefined = AppStorage.get('surfaceId'); - // [StartExclude WindowSizeChange] - if (widthBp === WidthBreakpoint.WIDTH_SM && heightBp === HeightBreakpoint.HEIGHT_MD) { - // When switching to a wide folding external screen, onForeground() will be triggered. - return; - } + // if (widthBp === WidthBreakpoint.WIDTH_SM && heightBp === HeightBreakpoint.HEIGHT_MD) { + // // When switching to a wide folding external screen, onForeground() will be triggered. + // return; + // } // [EndExclude WindowSizeChange] // [EndExclude SelectCamera2] // [EndExclude Preview2] - if (isFront) { - this.cameraUtil?.cameraShooting(surfaceId!, this.context!, camera.CameraPosition.CAMERA_POSITION_FRONT); + if (!this.isFirstTime) { + console.info(`testLog --- isNotFisrtTime`); + this.cameraUtil!.setXComponentRect(this.windowUtil!.getWindowSize()); } else { - this.cameraUtil?.cameraShooting(surfaceId!, this.context!, camera.CameraPosition.CAMERA_POSITION_BACK); + console.info(`testLog --- isFisrtTime`); + this.isFirstTime = false; } // [EndExclude SelectCamera4] // [EndExclude Bp2] @@ -101,7 +101,13 @@ export default class EntryAbility extends UIAbility { // Main window is created, set main page for this ability hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + AppStorage.setOrCreate('cameraUtil', new CameraUtil()); + this.cameraUtil = AppStorage.get('cameraUtil'); + AppStorage.setOrCreate('windowUtil', new WindowUtil()); + this.windowUtil = AppStorage.get('windowUtil'); this.windowUtil!.setWindowStage(windowStage); + // Monitor window size changes and update breakpoints. + windowStage.getMainWindowSync().on('windowSizeChange', this.onWindowSizeChange); // [EndExclude WindowSizeChange] // [EndExclude Bp2] windowStage.loadContent('pages/Index', (err) => { @@ -136,8 +142,6 @@ export default class EntryAbility extends UIAbility { let displayOrientation: display.Orientation = display.getDefaultDisplaySync().orientation; AppStorage.setOrCreate('displayOrientation', displayOrientation); // [EndExclude WindowSizeChange] - // Monitor window size changes and update breakpoints. - data.on('windowSizeChange', this.onWindowSizeChange); // [StartExclude WindowSizeChange] AppStorage.setOrCreate('isBackground', false); // [EndExclude Bp2] diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index 4ce1ba6..a21fa89 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -34,10 +34,10 @@ struct Index { @StorageLink('isFront') isFront: boolean = false; @StorageLink('isHalfFolded') isHalfFolded: boolean = false; @StorageLink('creaseRegion') creaseRegion: number[] = []; + @StorageLink('windowUtil') windowUtil: WindowUtil = new WindowUtil(); + @StorageLink('cameraUtil') cameraUtil: CameraUtil = new CameraUtil(); context?: Context = this.getUIContext().getHostContext(); xComponentController: XComponentController = new XComponentController(); - cameraUtil?: CameraUtil = CameraUtil.getInstance(); - windowUtil?: WindowUtil = WindowUtil.getInstance(); permissions: Array = [ 'ohos.permission.CAMERA', 'ohos.permission.READ_IMAGEVIDEO', @@ -51,20 +51,20 @@ struct Index { if (this.widthBp === WidthBreakpoint.WIDTH_MD && (orientation === display.Orientation.LANDSCAPE || orientation === display.Orientation.LANDSCAPE_INVERTED)) { this.isHalfFolded = true; - this.cameraUtil!.setXComponentRect(this.windowUtil!.getWindowSize()); - this.windowUtil?.setMainWindowOrientation(window.Orientation.LANDSCAPE); + this.cameraUtil!.setXComponentRect(this.windowUtil.getWindowSize()); + this.windowUtil.setMainWindowOrientation(window.Orientation.LANDSCAPE); } } // Exit the half folded status page. else { if (this.isHalfFolded) { this.isHalfFolded = false; - this.cameraUtil!.setXComponentRect(this.windowUtil!.getWindowSize()); + this.cameraUtil!.setXComponentRect(this.windowUtil.getWindowSize()); } else { this.isHalfFolded = false; } if (this.widthBp !== WidthBreakpoint.WIDTH_SM) { - this.windowUtil?.setMainWindowOrientation(window.Orientation.AUTO_ROTATION_RESTRICTED); + this.windowUtil.setMainWindowOrientation(window.Orientation.AUTO_ROTATION_RESTRICTED); } } } @@ -76,6 +76,17 @@ struct Index { display.on('foldStatusChange', this.onFoldStatusChange); // [EndExclude SelectCamera1] // Apply to the user for permission to access the camera and gallery. + // abilityAccessCtrl.createAtManager().requestPermissionsFromUser(this.context, this.permissions).then(() => { + // setTimeout(() => { + // // After obtaining permission, load the camera preview stream and ensure it is consistent with the aspect ratio of the surface. + // // [StartExclude SelectCamera1] + // this.cameraUtil?.setSurfaceProfile(this.xComponentController, this.getUIContext()); + // // [EndExclude SelectCamera1] + // this.cameraUtil?.cameraShooting(this.surfaceId, this.context!, camera.CameraPosition.CAMERA_POSITION_BACK); + // }, 200); + // }) + } + onPageShow(): void { abilityAccessCtrl.createAtManager().requestPermissionsFromUser(this.context, this.permissions).then(() => { setTimeout(() => { // After obtaining permission, load the camera preview stream and ensure it is consistent with the aspect ratio of the surface. @@ -90,7 +101,7 @@ struct Index { aboutToDisappear(): void { display.off('foldStatusChange'); - this.windowUtil!.offWindowSizeChange(); + this.windowUtil.offWindowSizeChange(); } build() { diff --git a/entry/src/main/ets/utils/CameraUtil.ets b/entry/src/main/ets/utils/CameraUtil.ets index 424bf33..8a2418a 100644 --- a/entry/src/main/ets/utils/CameraUtil.ets +++ b/entry/src/main/ets/utils/CameraUtil.ets @@ -33,14 +33,7 @@ export class CameraUtil { surfaceId: string = ''; xComponentController?: XComponentController; uiContext?: UIContext; - windowUtil?: WindowUtil = WindowUtil.getInstance(); - - static getInstance(): CameraUtil | undefined { - if (!AppStorage.get('cameraUtil')) { - AppStorage.setOrCreate('cameraUtil', new CameraUtil()); - } - return AppStorage.get('cameraUtil'); - } + windowUtil?: WindowUtil; async cameraShooting(surfaceId: string, context: Context, cameraPosition: camera.CameraPosition): Promise { this.surfaceId = surfaceId; @@ -91,6 +84,7 @@ export class CameraUtil { // Save photo. this.setPhotoOutputCb(); // Configure the camera preview stream to match the aspect ratio of the surface, otherwise the preview page will be compressed or stretched. + this.windowUtil = AppStorage.get('windowUtil'); this.setXComponentRect(this.windowUtil!.getWindowSize()); // Create the camera session and config. this.photoSession = cameraManager.createSession(camera.SceneMode.NORMAL_PHOTO); diff --git a/entry/src/main/ets/utils/WindowUtil.ets b/entry/src/main/ets/utils/WindowUtil.ets index 635df82..1f47c3a 100644 --- a/entry/src/main/ets/utils/WindowUtil.ets +++ b/entry/src/main/ets/utils/WindowUtil.ets @@ -21,13 +21,6 @@ export class WindowUtil { windowStage?: window.WindowStage; mainWindow?: window.Window; - static getInstance(): WindowUtil | undefined { - if (!AppStorage.get('windowUtil')) { - AppStorage.setOrCreate('windowUtil', new WindowUtil()); - } - return AppStorage.get('windowUtil'); - } - setWindowStage(windowStage: window.WindowStage): void { this.windowStage = windowStage; this.windowStage.getMainWindow((err, windowClass: window.Window) => { diff --git a/entry/src/main/ets/views/CommonView.ets b/entry/src/main/ets/views/CommonView.ets index 3bbf212..d640d03 100644 --- a/entry/src/main/ets/views/CommonView.ets +++ b/entry/src/main/ets/views/CommonView.ets @@ -75,7 +75,7 @@ export struct ShotAreaSm { @StorageLink('heightBp') heightBp: HeightBreakpoint = HeightBreakpoint.HEIGHT_SM; @StorageLink('photoUri') photoUri: string | Resource | PixelMap = ''; @StorageLink('surfaceId') surfaceId: string = ''; - cameraUtil?: CameraUtil = CameraUtil.getInstance(); + @StorageLink('cameraUtil') cameraUtil: CameraUtil = new CameraUtil(); context?: Context = this.getUIContext().getHostContext(); build() { @@ -162,7 +162,7 @@ export struct ShotArea { @StorageLink('widthBp') widthBp: WidthBreakpoint = WidthBreakpoint.WIDTH_SM; @StorageLink('photoUri') photoUri: string | Resource | PixelMap = ''; @StorageLink('surfaceId') surfaceId: string = ''; - cameraUtil?: CameraUtil = CameraUtil.getInstance(); + @StorageLink('cameraUtil') cameraUtil: CameraUtil = new CameraUtil(); context?: Context = this.getUIContext().getHostContext(); build() { @@ -240,7 +240,7 @@ export struct ShotAreaHalfFolded { @StorageLink('widthBp') widthBp: WidthBreakpoint = WidthBreakpoint.WIDTH_SM; @StorageLink('photoUri') photoUri: string | Resource | PixelMap = ''; @StorageLink('surfaceId') surfaceId: string = ''; - cameraUtil?: CameraUtil = CameraUtil.getInstance(); + @StorageLink('cameraUtil') cameraUtil: CameraUtil = new CameraUtil(); context?: Context = this.getUIContext().getHostContext(); build() { -- Gitee From 14330ad137bc7f71156b3b761968ad0fda724c42 Mon Sep 17 00:00:00 2001 From: lon9 <815882449@qq.com> Date: Tue, 22 Jul 2025 19:57:04 +0800 Subject: [PATCH 2/4] fix bug --- build-profile.json5 | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/build-profile.json5 b/build-profile.json5 index f5c7df4..2844c74 100644 --- a/build-profile.json5 +++ b/build-profile.json5 @@ -1,20 +1,6 @@ { "app": { - "signingConfigs": [ - { - "name": "default", - "type": "HarmonyOS", - "material": { - "certpath": "C:\\Users\\杨家瑞\\.ohos\\config\\default_MultiDeviceCamera_fSLTdFFESETtDzZ4Xgz2lzZBWO-yN8oDW2Xy3-XLwQU=.cer", - "storePassword": "0000001BAA97964C68A5D13B7F35D74C67EFDFD22F086ABFD145967F7FCF37EBE5BB7DB3FA6CAA7C8971C0", - "keyAlias": "debugKey", - "keyPassword": "0000001B96AE27BE3A58B41DF7882ACB93F8925E51B8D5040CCB735CF44A44A8B375A228FADEE3F5BD0112", - "profile": "C:\\Users\\杨家瑞\\.ohos\\config\\default_MultiDeviceCamera_fSLTdFFESETtDzZ4Xgz2lzZBWO-yN8oDW2Xy3-XLwQU=.p7b", - "signAlg": "SHA256withECDSA", - "storeFile": "C:\\Users\\杨家瑞\\.ohos\\config\\default_MultiDeviceCamera_fSLTdFFESETtDzZ4Xgz2lzZBWO-yN8oDW2Xy3-XLwQU=.p12" - } - } - ], + "signingConfigs": [], "products": [ { "name": "default", -- Gitee From 0869edf0085f46369008bc098a5f981af5bd1a90 Mon Sep 17 00:00:00 2001 From: lon9 <815882449@qq.com> Date: Tue, 22 Jul 2025 20:01:11 +0800 Subject: [PATCH 3/4] fix bug --- entry/src/main/ets/entryability/EntryAbility.ets | 3 --- 1 file changed, 3 deletions(-) diff --git a/entry/src/main/ets/entryability/EntryAbility.ets b/entry/src/main/ets/entryability/EntryAbility.ets index 8edeccd..762ec9d 100644 --- a/entry/src/main/ets/entryability/EntryAbility.ets +++ b/entry/src/main/ets/entryability/EntryAbility.ets @@ -38,7 +38,6 @@ export default class EntryAbility extends UIAbility { // [Start SelectCamera4] // [Start Preview2] onWindowSizeChange: (windowSize: window.Size) => void = (windowSize: window.Size) => { - console.info(`testLog --- 触发`); // [StartExclude SelectCamera4] // [StartExclude WindowSizeChange] // [StartExclude SelectCamera2] @@ -60,10 +59,8 @@ export default class EntryAbility extends UIAbility { // [EndExclude SelectCamera2] // [EndExclude Preview2] if (!this.isFirstTime) { - console.info(`testLog --- isNotFisrtTime`); this.cameraUtil!.setXComponentRect(this.windowUtil!.getWindowSize()); } else { - console.info(`testLog --- isFisrtTime`); this.isFirstTime = false; } // [EndExclude SelectCamera4] -- Gitee From 825c9203cd0aa75f968bfe07ef9c1ccf8b80cd25 Mon Sep 17 00:00:00 2001 From: lon9 <815882449@qq.com> Date: Tue, 22 Jul 2025 20:02:47 +0800 Subject: [PATCH 4/4] fix bug --- entry/src/main/ets/entryability/EntryAbility.ets | 4 ---- 1 file changed, 4 deletions(-) diff --git a/entry/src/main/ets/entryability/EntryAbility.ets b/entry/src/main/ets/entryability/EntryAbility.ets index 762ec9d..d8032b8 100644 --- a/entry/src/main/ets/entryability/EntryAbility.ets +++ b/entry/src/main/ets/entryability/EntryAbility.ets @@ -51,10 +51,6 @@ export default class EntryAbility extends UIAbility { AppStorage.setOrCreate('heightBp', heightBp); let displayOrientation: display.Orientation = display.getDefaultDisplaySync().orientation; AppStorage.setOrCreate('displayOrientation', displayOrientation); - // if (widthBp === WidthBreakpoint.WIDTH_SM && heightBp === HeightBreakpoint.HEIGHT_MD) { - // // When switching to a wide folding external screen, onForeground() will be triggered. - // return; - // } // [EndExclude WindowSizeChange] // [EndExclude SelectCamera2] // [EndExclude Preview2] -- Gitee