From 31fcaa88660657ba2080145d0b5c5fa48d889686 Mon Sep 17 00:00:00 2001 From: WX1377357 Date: Mon, 28 Jul 2025 19:32:32 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- camera/oh-package-lock.json5 | 19 --- .../main/ets/cameramanagers/CameraManager.ets | 6 +- .../cameramanagers/ImageReceiverManager.ets | 4 +- .../main/ets/cameramanagers/PhotoManager.ets | 44 +++--- .../ets/cameramanagers/PreviewManager.ets | 6 +- .../main/ets/cameramanagers/VideoManager.ets | 128 ++++++++++-------- entry/oh-package-lock.json5 | 29 ---- entry/src/main/ets/pages/Index.ets | 30 ++-- .../src/main/ets/utils/PermissionManager.ets | 2 +- entry/src/main/ets/utils/WindowUtil.ets | 2 +- .../main/ets/viewmodels/PreviewViewModel.ets | 19 ++- .../src/main/ets/views/OperateButtonsView.ets | 3 +- 12 files changed, 139 insertions(+), 153 deletions(-) delete mode 100644 camera/oh-package-lock.json5 delete mode 100644 entry/oh-package-lock.json5 diff --git a/camera/oh-package-lock.json5 b/camera/oh-package-lock.json5 deleted file mode 100644 index ebb8b31..0000000 --- a/camera/oh-package-lock.json5 +++ /dev/null @@ -1,19 +0,0 @@ -{ - "meta": { - "stableOrder": true, - "enableUnifiedLockfile": false - }, - "lockfileVersion": 3, - "ATTENTION": "THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.", - "specifiers": { - "commons@../commons": "commons@../commons" - }, - "packages": { - "commons@../commons": { - "name": "commons", - "version": "1.0.0", - "resolved": "../commons", - "registryType": "local" - } - } -} \ No newline at end of file diff --git a/camera/src/main/ets/cameramanagers/CameraManager.ets b/camera/src/main/ets/cameramanagers/CameraManager.ets index 942e6a5..afa66c2 100644 --- a/camera/src/main/ets/cameramanagers/CameraManager.ets +++ b/camera/src/main/ets/cameramanagers/CameraManager.ets @@ -14,8 +14,8 @@ */ import { camera } from '@kit.CameraKit'; -import { Logger } from 'commons/src/main/ets/utils/Logger'; import { BusinessError } from '@kit.BasicServicesKit'; +import { Logger } from 'commons'; import OutputManager, { CreateOutputConfig } from './OutputManager'; const TAG = 'CameraManager'; @@ -36,8 +36,8 @@ export class CameraManager { addCameraStatusListener() { this.cameraManager.on('cameraStatus', (err: BusinessError, statusInfo: camera.CameraStatusInfo) => { - if (err && err.code) { - Logger.error(TAG, 'cameraStatus with errorCode = ' + err.code); + if (err && err.message) { + Logger.error(TAG, 'cameraStatus with errorMessage = ' + err.message); return; } Logger.info(TAG, `cameraStatusInfo: camera is ${statusInfo.camera.cameraId}, status is ${statusInfo.status}`); diff --git a/camera/src/main/ets/cameramanagers/ImageReceiverManager.ets b/camera/src/main/ets/cameramanagers/ImageReceiverManager.ets index c694361..2fc79bc 100644 --- a/camera/src/main/ets/cameramanagers/ImageReceiverManager.ets +++ b/camera/src/main/ets/cameramanagers/ImageReceiverManager.ets @@ -15,10 +15,10 @@ import { image } from '@kit.ImageKit'; import { camera } from '@kit.CameraKit'; +import { display } from '@kit.ArkUI'; import { BusinessError } from '@kit.BasicServicesKit'; -import { Logger } from 'commons/src/main/ets/utils/Logger'; +import { Logger } from 'commons'; import OutputManager, { CreateOutputConfig } from './OutputManager'; -import { display } from '@kit.ArkUI'; import CameraConstant from '../constants/CameraConstants'; const TAG = 'ImageReceiverManager'; diff --git a/camera/src/main/ets/cameramanagers/PhotoManager.ets b/camera/src/main/ets/cameramanagers/PhotoManager.ets index 01956ed..21cbbe6 100644 --- a/camera/src/main/ets/cameramanagers/PhotoManager.ets +++ b/camera/src/main/ets/cameramanagers/PhotoManager.ets @@ -19,10 +19,11 @@ import { photoAccessHelper } from '@kit.MediaLibraryKit'; import { sensor } from '@kit.SensorServiceKit'; import { Decimal } from '@kit.ArkTS'; import { image } from '@kit.ImageKit'; -import OutputManager, { CreateOutputConfig } from './OutputManager'; import { colorSpaceManager } from '@kit.ArkGraphics2D'; import { geoLocationManager } from '@kit.LocationKit'; -import { Logger } from 'commons/src/main/ets/utils/Logger'; +import { Logger } from 'commons'; +import OutputManager, { CreateOutputConfig } from './OutputManager'; +import CameraConstant from '../constants/CameraConstants'; const TAG_LOG = 'PhotoManager'; @@ -62,11 +63,10 @@ export class PhotoManager implements OutputManager { // [Start create_photo_output] public createPhotoOutput(cameraManager: camera.CameraManager, cameraDevice: camera.CameraDevice, profile: camera.Profile) { - const PROFILE_DIFFERENCE = 1e-10; let cameraPhotoOutput: camera.PhotoOutput | undefined = undefined; const cameraOutputCapability = cameraManager.getSupportedOutputCapability(cameraDevice, camera.SceneMode.NORMAL_PHOTO); - let photoProfilesArray: Array | undefined = cameraOutputCapability?.photoProfiles; + let photoProfilesArray: camera.Profile[] | undefined = cameraOutputCapability?.photoProfiles; if (photoProfilesArray?.length) { try { const displayRatio = profile.size.width / profile.size.height; @@ -75,7 +75,7 @@ export class PhotoManager implements OutputManager { .sort((a, b) => Math.abs(a.size.width - profileWidth) - Math.abs(b.size.width - profileWidth)) .find(pf => { const pfDisplayRatio = pf.size.width / pf.size.height; - return Math.abs(pfDisplayRatio - displayRatio) <= PROFILE_DIFFERENCE && + return Math.abs(pfDisplayRatio - displayRatio) <= CameraConstant.PROFILE_DIFFERENCE && pf.format === camera.CameraFormat.CAMERA_FORMAT_JPEG; }); if (!PhotoProfile) { @@ -103,7 +103,8 @@ export class PhotoManager implements OutputManager { assetChangeRequest.saveCameraPhoto(); await phAccessHelper.applyChanges(assetChangeRequest); phAccessHelper.release(); - } catch (err) { + } catch (error) { + let err = error as BusinessError; Logger.error(TAG_LOG, `apply saveCameraPhoto failed with error: ${err.code}, ${err.message}`); } } @@ -117,12 +118,10 @@ export class PhotoManager implements OutputManager { return; } let imageSource = image.createImageSource(data); - imageSource.createPixelMap((err: BusinessError, data: image.PixelMap) => { - if (err) { - Logger.error(TAG_LOG, `createPixelMap err:${err.code}`); - return; - } - callback(data, photoAsset.uri); + imageSource.createPixelMap().then((pixelMap: image.PixelMap) => { + callback(pixelMap, photoAsset.uri); + }).catch((err: BusinessError) => { + Logger.error(TAG_LOG, `createPixelMap err:${err.code}`); }) } } @@ -166,12 +165,11 @@ export class PhotoManager implements OutputManager { return; } let buffer: ArrayBuffer; - if (component.byteBuffer) { - buffer = component.byteBuffer; - } else { + if (!component.byteBuffer) { Logger.error(TAG_LOG, 'byteBuffer is null'); return; } + buffer = component.byteBuffer; let photoType: photoAccessHelper.PhotoType = photoAccessHelper.PhotoType.IMAGE; let extension: string = 'jpg'; let options: photoAccessHelper.CreateOptions = { @@ -226,12 +224,18 @@ export class PhotoManager implements OutputManager { let err = error as BusinessError; Logger.error(TAG_LOG, `The getSupportedColorSpaces call failed. error code: ${err.code}`); } - let isSupportedColorSpaces = colorSpaces.indexOf(colorSpace) >= 0; - if (isSupportedColorSpaces) { - session.setColorSpace(colorSpace); + if (!colorSpaces.includes(colorSpace)) { + Logger.info(TAG_LOG, `colorSpace: ${colorSpace} is not support`); + return; + } + Logger.info(TAG_LOG, `setColorSpace: ${colorSpace}`); + session.setColorSpace(colorSpace); + try { let activeColorSpace: colorSpaceManager.ColorSpace = session.getActiveColorSpace(); - } else { - Logger.error(TAG_LOG, `colorSpace: ${colorSpace} is not support`); + Logger.info(TAG_LOG, `activeColorSpace: ${activeColorSpace}`); + } catch (error) { + let err = error as BusinessError; + Logger.error(TAG_LOG, `getActiveColorSpace Faild: ${err.message}`); } } // [End set_color_space] diff --git a/camera/src/main/ets/cameramanagers/PreviewManager.ets b/camera/src/main/ets/cameramanagers/PreviewManager.ets index 4b27025..bcdb1db 100644 --- a/camera/src/main/ets/cameramanagers/PreviewManager.ets +++ b/camera/src/main/ets/cameramanagers/PreviewManager.ets @@ -14,8 +14,8 @@ */ import { camera } from '@kit.CameraKit'; -import { Logger } from 'commons/src/main/ets/utils/Logger'; import { BusinessError } from '@kit.BasicServicesKit'; +import { Logger } from 'commons'; import OutputManager, { CreateOutputConfig } from './OutputManager'; import CameraConstant from '../constants/CameraConstants'; @@ -61,7 +61,7 @@ export class PreviewManager implements OutputManager { addFrameStartEventListener(output: camera.PreviewOutput) { output.on('frameStart', (err: BusinessError) => { if (err !== undefined && err.code !== 0) { - Logger.error(TAG_LOG, `FrameStart callback Error, errorCode: ${err.code}`); + Logger.error(TAG_LOG, `FrameStart callback Error, errorMessage: ${err.message}`); return; } Logger.info(TAG_LOG, 'Preview frame started'); @@ -72,7 +72,7 @@ export class PreviewManager implements OutputManager { addFrameEndEventListener(output: camera.PreviewOutput) { output.on('frameEnd', (err: BusinessError) => { if (err !== undefined && err.code !== 0) { - Logger.error(TAG_LOG, `FrameStart callback Error, errorCode: ${err.code}`); + Logger.error(TAG_LOG, `FrameStart callback Error, errorMessage: ${err.message}`); return; } Logger.info(TAG_LOG, 'Preview frame end'); diff --git a/camera/src/main/ets/cameramanagers/VideoManager.ets b/camera/src/main/ets/cameramanagers/VideoManager.ets index 7dff1af..bb93c2a 100644 --- a/camera/src/main/ets/cameramanagers/VideoManager.ets +++ b/camera/src/main/ets/cameramanagers/VideoManager.ets @@ -21,10 +21,11 @@ import { sensor } from '@kit.SensorServiceKit'; import { Decimal } from '@kit.ArkTS'; import { image } from '@kit.ImageKit'; import { BusinessError } from '@kit.BasicServicesKit'; +import { colorSpaceManager } from '@kit.ArkGraphics2D'; +import { Logger } from 'commons'; import OutputManager, { CreateOutputConfig } from './OutputManager'; import CameraConstant from '../constants/CameraConstants'; -import { colorSpaceManager } from '@kit.ArkGraphics2D'; -import { Logger } from 'commons/src/main/ets/utils/Logger'; + const TAG_LOG = 'video'; @@ -95,7 +96,8 @@ export class VideoManager implements OutputManager { Logger.info(TAG_LOG, 'Succeeded in preparing'); } } catch (error) { - Logger.info(TAG_LOG, `Failed to prepare and catch error is ${error.message}`); + let err = error as BusinessError; + Logger.info(TAG_LOG, `Failed to prepare and catch error is ${err.message}`); } } @@ -110,7 +112,8 @@ export class VideoManager implements OutputManager { await this.avRecorder?.start(); } } catch (error) { - Logger.info(TAG_LOG, `Failed to start and catch error is ${error.message}`); + let err = error as BusinessError; + Logger.info(TAG_LOG, `Failed to start and catch error is ${err.message}`); } } // [End start_video] @@ -128,7 +131,8 @@ export class VideoManager implements OutputManager { } } } catch (error) { - Logger.info(TAG_LOG, `Failed to stop and catch error is ${error.message}`); + let err = error as BusinessError; + Logger.info(TAG_LOG, `Failed to stop and catch error is ${err.message}`); } } // [End stop_video] @@ -141,7 +145,8 @@ export class VideoManager implements OutputManager { await this.output?.stop(); } } catch (error) { - Logger.info(TAG_LOG, `Failed to pause and catch error is ${error.message}`); + let err = error as BusinessError; + Logger.info(TAG_LOG, `Failed to pause and catch error is ${err.message}`); } } // [End pause_video] @@ -154,7 +159,8 @@ export class VideoManager implements OutputManager { await this.avRecorder.resume(); } } catch (error) { - Logger.info(TAG_LOG, `Failed to resume and catch error is ${error.message}`); + let err = error as BusinessError; + Logger.info(TAG_LOG, `Failed to resume and catch error is ${err.message}`); } } // [End resume_video] @@ -202,33 +208,36 @@ export class VideoManager implements OutputManager { cameraManager.getSupportedOutputCapability(device, camera.SceneMode.NORMAL_VIDEO); let videoProfilesArray: camera.VideoProfile[] | undefined = cameraOutputCap?.videoProfiles; - const displayRatio = targetProfile.size.height / targetProfile.size.width; - const videoProfile = videoProfilesArray?.find((profile: camera.VideoProfile) => { - const profileRatio = profile.size.height / profile.size.width - if (this.cameraPosition === 1) { - return profile.size.width >= 1080 && profile.size.height >= 1080 && - Math.abs(profileRatio - displayRatio) <= CameraConstant.PROFILE_DIFFERENCE && - profile.frameRateRange.max === 30 && profile.format === camera.CameraFormat.CAMERA_FORMAT_YUV_420_SP; - } - if (this.qualityLevel === QualityLevel.NORMAL) { - return profile.size.width <= 1920 && profile.size.width >= 1080 && profile.size.height >= 1080 && - Math.abs(profileRatio - displayRatio) <= CameraConstant.PROFILE_DIFFERENCE * profile.size.width && - profile.frameRateRange.max === 60 && profile.format === camera.CameraFormat.CAMERA_FORMAT_YUV_420_SP; - } - if (this.qualityLevel === QualityLevel.HIGHER && this.cameraPosition === 0) { - return profile.size.width <= 4096 && profile.size.width >= 3000 && - Math.abs(profileRatio - displayRatio) <= CameraConstant.PROFILE_DIFFERENCE * profile.size.width && - profile.frameRateRange.max === 60 && profile.format === camera.CameraFormat.CAMERA_FORMAT_YUV_420_SP; + if (videoProfilesArray?.length) { + try { + const displayRatio = targetProfile.size.width / targetProfile.size.height; + const profileWidth = targetProfile.size.width; + const videoProfile = videoProfilesArray + .sort((a, b) => Math.abs(a.size.width - profileWidth) - Math.abs(b.size.width - profileWidth)) + .find(pf => { + const pfDisplayRatio = pf.size.width / pf.size.height; + return Math.abs(pfDisplayRatio - displayRatio) <= CameraConstant.PROFILE_DIFFERENCE && + pf.format === camera.CameraFormat.CAMERA_FORMAT_YUV_420_SP; + }); + if (!videoProfile) { + Logger.error(TAG_LOG, 'Failed to get video profile'); + return; + } + this.videoProfile = videoProfile; + } catch (error) { + let err = error as BusinessError; + Logger.error(TAG_LOG, `Failed to createPhotoOutput. error: ${JSON.stringify(err)}`); } - return false; - }) - if (!videoProfile) { - Logger.info(TAG_LOG, 'Failed to get videoProfile') } - this.videoProfile = videoProfile; } // [End create_video_output] + getCameraPosition(): camera.ImageRotation { + return this.cameraPosition === camera.CameraPosition.CAMERA_POSITION_FRONT + ? camera.ImageRotation.ROTATION_270 + : camera.ImageRotation.ROTATION_90 + } + async setAVConfig() { // [Start create_file] let options: photoAccessHelper.CreateOptions = { @@ -262,8 +271,7 @@ export class VideoManager implements OutputManager { profile: this.avProfile, url: `fd://${this.file.fd}`, metadata: { - videoOrientation: this.cameraPosition === camera.CameraPosition.CAMERA_POSITION_FRONT ? - camera.ImageRotation.ROTATION_270.toString() : camera.ImageRotation.ROTATION_90.toString(), + videoOrientation: this.getCameraPosition().toString() } } // [End av_config] @@ -316,23 +324,23 @@ export class VideoManager implements OutputManager { return promise; } } catch (error) { - Logger.info(TAG_LOG, `Failed to getGravity and catch error is ${error.message}`); + let err = error as BusinessError; + Logger.info(TAG_LOG, `Failed to getGravity and catch error is ${err.message}`); return 0 } } // [Start get_video_rotation] getVideoRotation(deviceDegree: number): camera.ImageRotation { - let videoRotation: camera.ImageRotation = - this.cameraPosition === camera.CameraPosition.CAMERA_POSITION_FRONT ? camera.ImageRotation.ROTATION_270 : - camera.ImageRotation.ROTATION_90; + let videoRotation: camera.ImageRotation = this.getCameraPosition(); try { if (this.output) { - videoRotation = this.output.getVideoRotation(deviceDegree); + videoRotation = this.output?.getVideoRotation(deviceDegree); Logger.info(TAG_LOG, `Video rotation is: ${videoRotation}`); } } catch (error) { - Logger.info(TAG_LOG, `Failed to getVideoRotation and catch error is: ${error.message}`); + let err = error as BusinessError; + Logger.info(TAG_LOG, `Failed to getVideoRotation and catch error is: ${err.message}`); } return videoRotation; } @@ -355,7 +363,8 @@ export class VideoManager implements OutputManager { pixelMap = await avImageGenerator.fetchFrameByTime(timeUs, queryOption, param); avImageGenerator.release(); } catch (error) { - Logger.info(TAG_LOG, `Failed to getVideoThumbnail and catch error is ${error.message}`); + let err = error as BusinessError; + Logger.info(TAG_LOG, `Failed to getVideoThumbnail and catch error is ${err.message}`); } return pixelMap; } @@ -366,46 +375,51 @@ export class VideoManager implements OutputManager { // [Start set_video_stabilization] - setVideoStabilizationMode(session: camera.VideoSession): boolean { + setVideoStabilizationMode(session: camera.VideoSession): boolean { let mode: camera.VideoStabilizationMode = camera.VideoStabilizationMode.AUTO; // Check whether video stabilization is supported let isSupported: boolean = session.isVideoStabilizationModeSupported(mode); - if (isSupported) { - Logger.info(TAG_LOG, `setVideoStabilizationMode: ${mode}`); - // Set video stabilization - session.setVideoStabilizationMode(mode); - let activeVideoStabilizationMode = session.getActiveVideoStabilizationMode(); - Logger.info(TAG_LOG, `activeVideoStabilizationMode: ${activeVideoStabilizationMode}`); - } else { + if (!isSupported) { Logger.info(TAG_LOG, `videoStabilizationMode: ${mode} is not support`); + return false; } + Logger.info(TAG_LOG, `setVideoStabilizationMode: ${mode}`); + // Set video stabilization + session.setVideoStabilizationMode(mode); + let activeVideoStabilizationMode = session.getActiveVideoStabilizationMode(); + Logger.info(TAG_LOG, `activeVideoStabilizationMode: ${activeVideoStabilizationMode}`); return isSupported; } // [End set_video_stabilization] // [Start set_video_color_space] getSupportedColorSpaces(session: camera.VideoSession): Array { - let colorSpaces: Array = []; + let colorSpaces: colorSpaceManager.ColorSpace[] = []; try { colorSpaces = session.getSupportedColorSpaces(); } catch (error) { let err = error as BusinessError; - Logger.error(TAG_LOG, `The getSupportedColorSpaces call failed. error code: ${err.code}`); + Logger.error(TAG_LOG, `The getSupportedColorSpaces call failed. error code: ${err.message}`); } return colorSpaces; } setColorSpaceAfterCommitConfig(session: camera.VideoSession, isHdr: boolean): void { - let colorSpace: colorSpaceManager.ColorSpace = isHdr? colorSpaceManager.ColorSpace.BT2020_HLG_LIMIT : colorSpaceManager.ColorSpace.BT709_LIMIT; - let colorSpaces: Array = this.getSupportedColorSpaces(session); - let isSupportedColorSpaces = colorSpaces.indexOf(colorSpace) >= 0; - if (isSupportedColorSpaces) { - Logger.info(TAG_LOG, `setColorSpace: ${colorSpace}`); - session.setColorSpace(colorSpace); - let activeColorSpace:colorSpaceManager.ColorSpace = session.getActiveColorSpace(); - Logger.info(TAG_LOG, `activeColorSpace: ${activeColorSpace}`); - } else { + let colorSpace: colorSpaceManager.ColorSpace = + isHdr ? colorSpaceManager.ColorSpace.BT2020_HLG_LIMIT : colorSpaceManager.ColorSpace.BT709_LIMIT; + let colorSpaces: colorSpaceManager.ColorSpace[] = this.getSupportedColorSpaces(session); + if (!colorSpaces.includes(colorSpace)) { Logger.info(TAG_LOG, `colorSpace: ${colorSpace} is not support`); + return; + } + Logger.info(TAG_LOG, `setColorSpace: ${colorSpace}`); + session.setColorSpace(colorSpace); + try { + let activeColorSpace: colorSpaceManager.ColorSpace = session.getActiveColorSpace(); + Logger.info(TAG_LOG, `activeColorSpace: ${activeColorSpace}`); + } catch (error) { + let err = error as BusinessError; + Logger.error(TAG_LOG, `getActiveColorSpace Faild: ${err.message}`); } } // [Start set_video_color_space] diff --git a/entry/oh-package-lock.json5 b/entry/oh-package-lock.json5 deleted file mode 100644 index 01723a1..0000000 --- a/entry/oh-package-lock.json5 +++ /dev/null @@ -1,29 +0,0 @@ -{ - "meta": { - "stableOrder": true, - "enableUnifiedLockfile": false - }, - "lockfileVersion": 3, - "ATTENTION": "THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.", - "specifiers": { - "camera@../camera": "camera@../camera", - "commons@../commons": "commons@../commons" - }, - "packages": { - "camera@../camera": { - "name": "camera", - "version": "1.0.0", - "resolved": "../camera", - "registryType": "local", - "dependencies": { - "commons": "file:../commons" - } - }, - "commons@../commons": { - "name": "commons", - "version": "1.0.0", - "resolved": "../commons", - "registryType": "local" - } - } -} \ No newline at end of file diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index 0d23662..4aaabb6 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -13,6 +13,19 @@ * limitations under the License. */ +import { sensor } from '@kit.SensorServiceKit'; +import { common } from '@kit.AbilityKit'; +import { display } from '@kit.ArkUI'; +import { curves } from '@kit.ArkUI'; +import { + CameraManager, + GridLine, + ImageReceiverManager, + LevelIndicator, + PhotoManager, + PreviewManager, + VideoManager +} from 'camera'; import CameraConstant from '../constants/Constants'; import { calCameraPoint, getClampedChildPosition, limitNumberInRange, showToast } from '../utils/CommonUtil'; import RefreshableTimer from '../utils/RefreshableTimer'; @@ -21,16 +34,7 @@ import ZoomButtonsView from '../views/ZoomButtonsView'; import ModeButtonsView from '../views/ModeButtonsView'; import SettingButtonsView from '../views/SettingButtonsView'; import OperateButtonsView from '../views/OperateButtonsView'; -import { sensor } from '@kit.SensorServiceKit'; -import { common } from '@kit.AbilityKit'; -import { display } from '@kit.ArkUI'; import PreviewViewModel from '../viewmodels/PreviewViewModel'; -import { CameraManager, - GridLine, - ImageReceiverManager, - LevelIndicator, - PhotoManager, PreviewManager, VideoManager } from 'camera'; -import { curves } from '@kit.ArkUI'; @Extend(SymbolGlyph) function funcButtonStyle() { @@ -385,13 +389,12 @@ struct Index { @Builder previewImageButton() { - SymbolGlyph( - this.isPreviewImageVisible ? $r('sys.symbol.eye') : $r('sys.symbol.eye_slash') - ) + SymbolGlyph(this.isPreviewImageVisible ? $r('sys.symbol.eye') : $r('sys.symbol.eye_slash')) .funcButtonStyle() .onClick(() => { this.isPreviewImageVisible = !this.isPreviewImageVisible; - const message = this.isPreviewImageVisible ? $r('app.string.preview_image_open') : $r('app.string.preview_image_close'); + const message = + this.isPreviewImageVisible ? $r('app.string.preview_image_open') : $r('app.string.preview_image_close'); showToast(this.getUIContext(), message); }) } @@ -490,6 +493,5 @@ struct Index { .height('100%') .width('100%') .backgroundColor(Color.Black) - .onClick(() => {}) } } \ No newline at end of file diff --git a/entry/src/main/ets/utils/PermissionManager.ets b/entry/src/main/ets/utils/PermissionManager.ets index af0c98f..c65190e 100644 --- a/entry/src/main/ets/utils/PermissionManager.ets +++ b/entry/src/main/ets/utils/PermissionManager.ets @@ -14,7 +14,7 @@ */ import { abilityAccessCtrl, Context, Permissions } from '@kit.AbilityKit'; -import { Logger } from 'commons/src/main/ets/utils/Logger'; +import { Logger } from 'commons'; const TAG = 'PermissionManager'; diff --git a/entry/src/main/ets/utils/WindowUtil.ets b/entry/src/main/ets/utils/WindowUtil.ets index bad30c8..9d54a73 100644 --- a/entry/src/main/ets/utils/WindowUtil.ets +++ b/entry/src/main/ets/utils/WindowUtil.ets @@ -14,7 +14,7 @@ */ import { display, window } from '@kit.ArkUI'; -import { Logger } from 'commons/src/main/ets/utils/Logger'; +import { Logger } from 'commons'; const TAG = 'WindowUtil' diff --git a/entry/src/main/ets/viewmodels/PreviewViewModel.ets b/entry/src/main/ets/viewmodels/PreviewViewModel.ets index 60fa3e0..3f87a89 100644 --- a/entry/src/main/ets/viewmodels/PreviewViewModel.ets +++ b/entry/src/main/ets/viewmodels/PreviewViewModel.ets @@ -1,7 +1,22 @@ -import CameraConstant from '../constants/Constants'; +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 ("the License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { curves, display } from '@kit.ArkUI'; import { camera } from '@kit.CameraKit'; import WindowUtil from '../utils/WindowUtil'; -import { curves, display } from '@kit.ArkUI'; +import CameraConstant from '../constants/Constants'; export enum CameraMode { PHOTO, diff --git a/entry/src/main/ets/views/OperateButtonsView.ets b/entry/src/main/ets/views/OperateButtonsView.ets index 067d151..4fdfa52 100644 --- a/entry/src/main/ets/views/OperateButtonsView.ets +++ b/entry/src/main/ets/views/OperateButtonsView.ets @@ -15,9 +15,8 @@ import { image } from '@kit.ImageKit'; import { common } from '@kit.AbilityKit'; -import { camera } from '@kit.CameraKit'; -import PreviewViewModel from '../viewmodels/PreviewViewModel'; import { AVRecorderState, CameraManager, PhotoManager, VideoManager } from 'camera'; +import PreviewViewModel from '../viewmodels/PreviewViewModel'; @Component struct OperateButtonsView { -- Gitee From ccd1ffef08277742250c6190084ee340d559f100 Mon Sep 17 00:00:00 2001 From: WX1377357 Date: Tue, 5 Aug 2025 17:11:42 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=89=8D=E7=BD=AE=E6=91=84=E5=83=8F?= =?UTF-8?q?=E5=A4=B4=E5=BD=95=E5=83=8F=E6=97=B6=E5=BC=80=E5=90=AF=E9=95=9C?= =?UTF-8?q?=E5=83=8F=E5=8A=9F=E8=83=BD=EF=BC=9Breview=E6=84=8F=E8=A7=81?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/ets/cameramanagers/PhotoManager.ets | 42 +++++--------- .../main/ets/cameramanagers/VideoManager.ets | 57 +++++++++---------- entry/src/main/ets/pages/Index.ets | 4 +- .../src/main/ets/views/OperateButtonsView.ets | 2 +- 4 files changed, 43 insertions(+), 62 deletions(-) diff --git a/camera/src/main/ets/cameramanagers/PhotoManager.ets b/camera/src/main/ets/cameramanagers/PhotoManager.ets index 21cbbe6..88401e6 100644 --- a/camera/src/main/ets/cameramanagers/PhotoManager.ets +++ b/camera/src/main/ets/cameramanagers/PhotoManager.ets @@ -84,8 +84,7 @@ export class PhotoManager implements OutputManager { } cameraPhotoOutput = cameraManager.createPhotoOutput(PhotoProfile); } catch (error) { - let err = error as BusinessError; - Logger.error(TAG_LOG, `Failed to createPhotoOutput. error: ${JSON.stringify(err)}`); + Logger.error(TAG_LOG, `Failed to createPhotoOutput. error: ${JSON.stringify(error)}`); } } this.output = cameraPhotoOutput; @@ -104,8 +103,7 @@ export class PhotoManager implements OutputManager { await phAccessHelper.applyChanges(assetChangeRequest); phAccessHelper.release(); } catch (error) { - let err = error as BusinessError; - Logger.error(TAG_LOG, `apply saveCameraPhoto failed with error: ${err.code}, ${err.message}`); + Logger.error(TAG_LOG, `apply saveCameraPhoto failed with error: ${error.code}, ${error.message}`); } } @@ -164,12 +162,11 @@ export class PhotoManager implements OutputManager { Logger.error(TAG_LOG, 'getComponent failed'); return; } - let buffer: ArrayBuffer; - if (!component.byteBuffer) { + const buffer: ArrayBuffer = component.byteBuffer; + if (!buffer) { Logger.error(TAG_LOG, 'byteBuffer is null'); return; } - buffer = component.byteBuffer; let photoType: photoAccessHelper.PhotoType = photoAccessHelper.PhotoType.IMAGE; let extension: string = 'jpg'; let options: photoAccessHelper.CreateOptions = { @@ -189,7 +186,6 @@ export class PhotoManager implements OutputManager { imageObj.release(); }); } - // [End save_photo_single] setPhotoOutputCallback(isSingle: boolean) { @@ -221,8 +217,7 @@ export class PhotoManager implements OutputManager { try { colorSpaces = session.getSupportedColorSpaces(); } catch (error) { - let err = error as BusinessError; - Logger.error(TAG_LOG, `The getSupportedColorSpaces call failed. error code: ${err.code}`); + Logger.error(TAG_LOG, `The getSupportedColorSpaces call failed. error code: ${error.code}`); } if (!colorSpaces.includes(colorSpace)) { Logger.info(TAG_LOG, `colorSpace: ${colorSpace} is not support`); @@ -234,8 +229,7 @@ export class PhotoManager implements OutputManager { let activeColorSpace: colorSpaceManager.ColorSpace = session.getActiveColorSpace(); Logger.info(TAG_LOG, `activeColorSpace: ${activeColorSpace}`); } catch (error) { - let err = error as BusinessError; - Logger.error(TAG_LOG, `getActiveColorSpace Faild: ${err.message}`); + Logger.error(TAG_LOG, `getActiveColorSpace Faild: ${error.message}`); } } // [End set_color_space] @@ -254,8 +248,7 @@ export class PhotoManager implements OutputManager { photoSession.setFlashMode(flashMode || camera.FlashMode.FLASH_MODE_CLOSE); } } catch (error) { - let err = error as BusinessError; - Logger.error(TAG_LOG, `Failed to hasFlash. error: ${JSON.stringify(err)}`); + Logger.error(TAG_LOG, `Failed to hasFlash. error: ${JSON.stringify(error)}`); } } @@ -267,9 +260,8 @@ export class PhotoManager implements OutputManager { photoSession.setFocusMode(focusMode || defaultMode); } } catch (error) { - let err = error as BusinessError; Logger.error(TAG_LOG, - `Failed to check whether the focus mode is supported. error: ${JSON.stringify(err)}`); + `Failed to check whether the focus mode is supported. error: ${JSON.stringify(error)}`); } } @@ -277,8 +269,7 @@ export class PhotoManager implements OutputManager { try { photoSession.setFocusPoint(focusPoint); } catch (error) { - let err = error as BusinessError; - Logger.error(TAG_LOG, `The setFocusPoint call failed. error code: ${err.code}`); + Logger.error(TAG_LOG, `The setFocusPoint call failed. error code: ${error.code}`); } } @@ -291,8 +282,7 @@ export class PhotoManager implements OutputManager { photoZoomRatio = zoomRatioRange[0]; } } catch (error) { - let err = error as BusinessError; - Logger.error(TAG_LOG, `Failed to get the zoom ratio range. error: ${JSON.stringify(err)}`); + Logger.error(TAG_LOG, `Failed to get the zoom ratio range. error: ${JSON.stringify(error)}`); } } photoSession.setZoomRatio(zoomRatio || photoZoomRatio); @@ -303,8 +293,7 @@ export class PhotoManager implements OutputManager { try { colorSpaces = session.getSupportedColorSpaces(); } catch (error) { - let err = error as BusinessError; - Logger.error(TAG_LOG,`The getSupportedColorSpaces call failed. error code: ${err.code}`); + Logger.error(TAG_LOG,`The getSupportedColorSpaces call failed. error code: ${error.code}`); } return colorSpaces; } @@ -315,8 +304,7 @@ export class PhotoManager implements OutputManager { try { photoRotation = photoOutput.getPhotoRotation(deviceDegree); } catch (error) { - let err = error as BusinessError; - Logger.error(TAG_LOG, `The photoOutput.getPhotoRotation call failed. error code: ${err.code}`); + Logger.error(TAG_LOG, `The photoOutput.getPhotoRotation call failed. error code: ${error.code}`); } return photoRotation; } @@ -376,8 +364,7 @@ export class PhotoManager implements OutputManager { try { isSupported = photoOutput.isMovingPhotoSupported(); } catch (error) { - let err = error as BusinessError; - Logger.error(TAG_LOG, `The isMovingPhotoSupported call failed. error code: ${err.code}`); + Logger.error(TAG_LOG, `The isMovingPhotoSupported call failed. error code: ${error.code}`); } return isSupported; } @@ -389,8 +376,7 @@ export class PhotoManager implements OutputManager { try { this.output?.enableMovingPhoto(enabled); } catch (error) { - let err = error as BusinessError; - Logger.error(TAG_LOG, `The enableMovingPhoto call failed. error code: ${err.code}`); + Logger.error(TAG_LOG, `The enableMovingPhoto call failed. error code: ${error.code}`); } } diff --git a/camera/src/main/ets/cameramanagers/VideoManager.ets b/camera/src/main/ets/cameramanagers/VideoManager.ets index bb93c2a..e6a60f1 100644 --- a/camera/src/main/ets/cameramanagers/VideoManager.ets +++ b/camera/src/main/ets/cameramanagers/VideoManager.ets @@ -20,13 +20,11 @@ import { fileIo } from '@kit.CoreFileKit'; import { sensor } from '@kit.SensorServiceKit'; import { Decimal } from '@kit.ArkTS'; import { image } from '@kit.ImageKit'; -import { BusinessError } from '@kit.BasicServicesKit'; import { colorSpaceManager } from '@kit.ArkGraphics2D'; import { Logger } from 'commons'; import OutputManager, { CreateOutputConfig } from './OutputManager'; import CameraConstant from '../constants/CameraConstants'; - const TAG_LOG = 'video'; enum QualityLevel { @@ -76,7 +74,7 @@ export class VideoManager implements OutputManager { Logger.info(TAG_LOG, 'on avRecorder state change: ', state) }); } catch (error) { - Logger.info(TAG_LOG, 'createAVRecorder call failed. error code: %{public}s', (error as BusinessError).code); + Logger.info(TAG_LOG, 'createAVRecorder call failed. error code: %{public}s', error.code); } if (this.avRecorder === undefined || this.avRecorder === null) { return; @@ -96,15 +94,22 @@ export class VideoManager implements OutputManager { Logger.info(TAG_LOG, 'Succeeded in preparing'); } } catch (error) { - let err = error as BusinessError; - Logger.info(TAG_LOG, `Failed to prepare and catch error is ${err.message}`); + Logger.info(TAG_LOG, `Failed to prepare and catch error is ${error.message}`); } } + isSupportMirror() { + let isSupported: boolean | undefined = this.output?.isMirrorSupported(); + return isSupported; + } + // [Start start_video] - async start() { + async start(isFront: boolean) { try { if (this.avRecorder?.state === AVRecorderState.PREPARED) { + if (this.isSupportMirror() && isFront) { + this.output?.enableMirror(true) + } // [StartExclude start_video] await this.avRecorder.updateRotation(this.getVideoRotation(await this.getGravity())); // [EndExclude start_video] @@ -112,8 +117,7 @@ export class VideoManager implements OutputManager { await this.avRecorder?.start(); } } catch (error) { - let err = error as BusinessError; - Logger.info(TAG_LOG, `Failed to start and catch error is ${err.message}`); + Logger.info(TAG_LOG, `Failed to start and catch error is ${error.message}`); } } // [End start_video] @@ -131,8 +135,7 @@ export class VideoManager implements OutputManager { } } } catch (error) { - let err = error as BusinessError; - Logger.info(TAG_LOG, `Failed to stop and catch error is ${err.message}`); + Logger.info(TAG_LOG, `Failed to stop and catch error is ${error.message}`); } } // [End stop_video] @@ -145,8 +148,7 @@ export class VideoManager implements OutputManager { await this.output?.stop(); } } catch (error) { - let err = error as BusinessError; - Logger.info(TAG_LOG, `Failed to pause and catch error is ${err.message}`); + Logger.info(TAG_LOG, `Failed to pause and catch error is ${error.message}`); } } // [End pause_video] @@ -159,8 +161,7 @@ export class VideoManager implements OutputManager { await this.avRecorder.resume(); } } catch (error) { - let err = error as BusinessError; - Logger.info(TAG_LOG, `Failed to resume and catch error is ${err.message}`); + Logger.info(TAG_LOG, `Failed to resume and catch error is ${error.message}`); } } // [End resume_video] @@ -197,7 +198,7 @@ export class VideoManager implements OutputManager { this.output = cameraManager.createVideoOutput(this.videoProfile, videoSurfaceId); } catch (error) { Logger.error(TAG_LOG, - `Failed to create the output instance. error code: ${(error as BusinessError).code}`); + `Failed to create the output instance. error code: ${error.code}`); } } @@ -225,14 +226,13 @@ export class VideoManager implements OutputManager { } this.videoProfile = videoProfile; } catch (error) { - let err = error as BusinessError; - Logger.error(TAG_LOG, `Failed to createPhotoOutput. error: ${JSON.stringify(err)}`); + Logger.error(TAG_LOG, `Failed to createPhotoOutput. error: ${JSON.stringify(error)}`); } } } // [End create_video_output] - getCameraPosition(): camera.ImageRotation { + getCameraImageRotation(): camera.ImageRotation { return this.cameraPosition === camera.CameraPosition.CAMERA_POSITION_FRONT ? camera.ImageRotation.ROTATION_270 : camera.ImageRotation.ROTATION_90 @@ -271,7 +271,7 @@ export class VideoManager implements OutputManager { profile: this.avProfile, url: `fd://${this.file.fd}`, metadata: { - videoOrientation: this.getCameraPosition().toString() + videoOrientation: this.getCameraImageRotation().toString() } } // [End av_config] @@ -324,23 +324,21 @@ export class VideoManager implements OutputManager { return promise; } } catch (error) { - let err = error as BusinessError; - Logger.info(TAG_LOG, `Failed to getGravity and catch error is ${err.message}`); + Logger.info(TAG_LOG, `Failed to getGravity and catch error is ${error.message}`); return 0 } } // [Start get_video_rotation] getVideoRotation(deviceDegree: number): camera.ImageRotation { - let videoRotation: camera.ImageRotation = this.getCameraPosition(); + let videoRotation: camera.ImageRotation = this.getCameraImageRotation(); try { if (this.output) { - videoRotation = this.output?.getVideoRotation(deviceDegree); + videoRotation = this.output.getVideoRotation(deviceDegree); Logger.info(TAG_LOG, `Video rotation is: ${videoRotation}`); } } catch (error) { - let err = error as BusinessError; - Logger.info(TAG_LOG, `Failed to getVideoRotation and catch error is: ${err.message}`); + Logger.info(TAG_LOG, `Failed to getVideoRotation and catch error is: ${error.message}`); } return videoRotation; } @@ -363,8 +361,7 @@ export class VideoManager implements OutputManager { pixelMap = await avImageGenerator.fetchFrameByTime(timeUs, queryOption, param); avImageGenerator.release(); } catch (error) { - let err = error as BusinessError; - Logger.info(TAG_LOG, `Failed to getVideoThumbnail and catch error is ${err.message}`); + Logger.info(TAG_LOG, `Failed to getVideoThumbnail and catch error is ${error.message}`); } return pixelMap; } @@ -398,8 +395,7 @@ export class VideoManager implements OutputManager { try { colorSpaces = session.getSupportedColorSpaces(); } catch (error) { - let err = error as BusinessError; - Logger.error(TAG_LOG, `The getSupportedColorSpaces call failed. error code: ${err.message}`); + Logger.error(TAG_LOG, `The getSupportedColorSpaces call failed. error code: ${error.message}`); } return colorSpaces; } @@ -418,8 +414,7 @@ export class VideoManager implements OutputManager { let activeColorSpace: colorSpaceManager.ColorSpace = session.getActiveColorSpace(); Logger.info(TAG_LOG, `activeColorSpace: ${activeColorSpace}`); } catch (error) { - let err = error as BusinessError; - Logger.error(TAG_LOG, `getActiveColorSpace Faild: ${err.message}`); + Logger.error(TAG_LOG, `getActiveColorSpace Faild: ${error.message}`); } } // [Start set_video_color_space] diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index 4aaabb6..c03def1 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -393,8 +393,7 @@ struct Index { .funcButtonStyle() .onClick(() => { this.isPreviewImageVisible = !this.isPreviewImageVisible; - const message = - this.isPreviewImageVisible ? $r('app.string.preview_image_open') : $r('app.string.preview_image_close'); + const message = this.isPreviewImageVisible ? $r('app.string.preview_image_open') : $r('app.string.preview_image_close'); showToast(this.getUIContext(), message); }) } @@ -493,5 +492,6 @@ struct Index { .height('100%') .width('100%') .backgroundColor(Color.Black) + .onClick(() => {}) } } \ No newline at end of file diff --git a/entry/src/main/ets/views/OperateButtonsView.ets b/entry/src/main/ets/views/OperateButtonsView.ets index 4fdfa52..a9a7357 100644 --- a/entry/src/main/ets/views/OperateButtonsView.ets +++ b/entry/src/main/ets/views/OperateButtonsView.ets @@ -105,7 +105,7 @@ struct OperateButtonsView { }) .justifyContent(FlexAlign.Center) .onClick(() => { - this.videoManager.start(); + this.videoManager.start(this.previewVM.isFront); }) } -- Gitee From 8919c9ac5af2fef7667d153a2e356de397401c20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=8B=E8=83=BD=5F=E5=BC=A0=E5=BE=B7=E7=9B=9B?= Date: Wed, 6 Aug 2025 10:55:44 +0800 Subject: [PATCH 3/3] =?UTF-8?q?review=E6=84=8F=E8=A7=81=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- camera/src/main/ets/cameramanagers/VideoManager.ets | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/camera/src/main/ets/cameramanagers/VideoManager.ets b/camera/src/main/ets/cameramanagers/VideoManager.ets index e6a60f1..805c9c8 100644 --- a/camera/src/main/ets/cameramanagers/VideoManager.ets +++ b/camera/src/main/ets/cameramanagers/VideoManager.ets @@ -333,10 +333,8 @@ export class VideoManager implements OutputManager { getVideoRotation(deviceDegree: number): camera.ImageRotation { let videoRotation: camera.ImageRotation = this.getCameraImageRotation(); try { - if (this.output) { - videoRotation = this.output.getVideoRotation(deviceDegree); + videoRotation = this.output!.getVideoRotation(deviceDegree); Logger.info(TAG_LOG, `Video rotation is: ${videoRotation}`); - } } catch (error) { Logger.info(TAG_LOG, `Failed to getVideoRotation and catch error is: ${error.message}`); } -- Gitee