diff --git a/README.md b/README.md index 714f35d18d5cdedba216f7ecc98dd25a7337c553..c343c459f01e09aedcd1d2112f93b70853dcda04 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ -# 生态应用相机实现系统级相机体验 +# 基于相机开放能力实现系统级相机 -本文针对三方相机开发场景,基于HarmonyOS提供的相机开放能力,实现系统相机级别的效果和能力,比如分辨率、动图、视频防抖、连续变焦等。 +本示例展示了如何使用HarmonyOS提供的相机开放能力,主要包括使用camera kit拍摄以及photoAccessHelper进行保存图片和视频。本示例主要适用于第三方应用调用系统相机能力,实现系统相机级别的效果和能力,比如分辨率、动图、视频防抖、连续变焦等。 ## 效果展示 diff --git a/entry/hvigorfile.ts b/entry/hvigorfile.ts index c6edcd90486dd5a853cf7d34c8647f08414ca7a3..0c65754e76c97d6a41b4a15ff53a6ccab9949f76 100644 --- a/entry/hvigorfile.ts +++ b/entry/hvigorfile.ts @@ -1,6 +1,6 @@ import { hapTasks } from '@ohos/hvigor-ohos-plugin'; export default { - system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ - plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ -} + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +}; diff --git a/entry/src/main/ets/constants/CameraConstants.ets b/entry/src/main/ets/constants/CameraConstants.ets index a149e9303ea1fcab3d72d6b58d37444b8f4c93f5..b2b5d73e2e3499b1c30d0a9a27a7e5dd77b0b79b 100644 --- a/entry/src/main/ets/constants/CameraConstants.ets +++ b/entry/src/main/ets/constants/CameraConstants.ets @@ -17,93 +17,93 @@ export class CameraConstants { /** * full screen */ - static readonly FULL_SCREEN: string = '100%'; + public static readonly FULL_SCREEN: string = '100%'; /** * camera selection width */ - static readonly CENTER_WIDTH: string = '40%'; + public static readonly CENTER_WIDTH: string = '40%'; /** * time format */ - static readonly TIME_FORMAT: string = 'mm:ss.SS'; + public static readonly TIME_FORMAT: string = 'mm:ss.SS'; /** * zoom button margin top */ - static readonly PREVIEW_HEIGHT_BUTTON: number = 420; + public static readonly PREVIEW_HEIGHT_BUTTON: number = 420; /** * surface height */ - static readonly SURFACE_HEIGHT: number = 500; + public static readonly SURFACE_HEIGHT: number = 500; /** * Parameter Configuration button size */ - static readonly IMAGE_SIZE: number = 24; + public static readonly IMAGE_SIZE: number = 24; /** * Parameter Configuration button size */ - static readonly IMAGE_HEIGHT: number = 23; + public static readonly IMAGE_HEIGHT: number = 23; /** * Parameter Configuration button margin */ - static readonly MARGIN: number = 5; + public static readonly MARGIN: number = 5; /** * Parameter Configuration button margin top */ - static readonly MARGIN_TOP: number = 30; + public static readonly MARGIN_TOP: number = 30; /** * camera switch icon size */ - static readonly CAMERA_SWITCH_SIZE: number = 50; + public static readonly CAMERA_SWITCH_SIZE: number = 50; /** * capture icon size */ - static readonly CAPTURE_SIZE: number = 70; + public static readonly CAPTURE_SIZE: number = 70; /** * margin top to surface */ - static readonly MARGIN_HEIGHT: number = 10; + public static readonly MARGIN_HEIGHT: number = 10; /** * dot opacity */ - static readonly OPACITY: number = 0.2; + public static readonly OPACITY: number = 0.2; /** * dot margin left */ - static readonly MARGIN_LEFT: number = 6; + public static readonly MARGIN_LEFT: number = 6; /** * dot position */ - static readonly LEFT_DISTANCE: number = 15; + public static readonly LEFT_DISTANCE: number = 15; /** * zoom button size */ - static readonly ZOOM_SIZE: number = 35; + public static readonly ZOOM_SIZE: number = 35; /** * dot size */ - static readonly SMALL_DOT: number = 2; + public static readonly SMALL_DOT: number = 2; /** * camera preview size */ - static readonly PREVIEW_WIDTH: number = 2048; + public static readonly PREVIEW_WIDTH: number = 2048; /** * camera preview size */ - static readonly PREVIEW_HEIGHT: number = 1536; + public static readonly PREVIEW_HEIGHT: number = 1536; /** * photo size */ - static readonly PHOTO_WIDTH: number = 4160; + public static readonly PHOTO_WIDTH: number = 4160; /** * photo size */ - static readonly PHOTO_HEIGHT: number = 3120; + public static readonly PHOTO_HEIGHT: number = 3120; /** * camera preview size */ - static readonly FRONT_WIDTH: number = 4096; + public static readonly FRONT_WIDTH: number = 4096; /** * camera preview size */ - static readonly FRONT_HEIGHT: number = 3072; + public static readonly FRONT_HEIGHT: number = 3072; } \ No newline at end of file diff --git a/entry/src/main/ets/entryability/EntryAbility.ets b/entry/src/main/ets/entryability/EntryAbility.ets index e203b7fe250f79c94c0bde6027f397948e0f9dcb..3c54dc88f34b872c05512860d088a356b9785592 100644 --- a/entry/src/main/ets/entryability/EntryAbility.ets +++ b/entry/src/main/ets/entryability/EntryAbility.ets @@ -29,7 +29,7 @@ export default class EntryAbility extends UIAbility { // 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()) + AppStorage.setOrCreate('window', windowStage.getMainWindowSync()) 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 bfd13d039c50fbc555e87466b6e482da2b9ce725..f350f508ef803ca4808271c3ff8feca56dd1ef0e 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -343,7 +343,7 @@ struct XComponentPage { Column() { Row() { - Text($r("app.string.Photo")) + Text($r('app.string.Photo')) .fontColor(this.isPhoto ? Color.White : Color.Gray) .onClick(async () => { if (!this.isPhoto) { @@ -354,7 +354,7 @@ struct XComponentPage { cameraShooting(isVideo, cameraPosition, surfaceId, context, foldAbleStatus); } }) - Text($r("app.string.Video")) + Text($r('app.string.Video')) .fontColor(this.isPhoto ? Color.Gray : Color.White) .onClick(() => { if (this.isPhoto) { diff --git a/entry/src/main/ets/utils/CameraShooter.ets b/entry/src/main/ets/utils/CameraShooter.ets index cd518daaae6403bf6802d8b78e4c0ba5f6f15aef..4812c351c0498ed48d805aa8db59827655a7097d 100644 --- a/entry/src/main/ets/utils/CameraShooter.ets +++ b/entry/src/main/ets/utils/CameraShooter.ets @@ -69,15 +69,15 @@ export async function cameraShooting(isVideo: boolean, cameraPosition: number, s return profile.size.height === 1440 && profile.size.width === 1920; } return profile.size.height <= screen.width && profile.size.height >= 1080 && - (profile.size.width / profile.size.height) < (screen.height / screen.width) - && (profile.size.width / profile.size.height) > + (profile.size.width / profile.size.height) < (screen.height / screen.width) && + (profile.size.width / profile.size.height) > (foldAbleStatus === display.FoldStatus.FOLD_STATUS_EXPANDED ? 1 : 4 / 3); }); let photoProfile: undefined | camera.Profile = photoProfilesArray.find((profile: camera.Profile) => { if (previewProfile) { - return profile.size.width <= 4096 && profile.size.width >= 2448 - && profile.size.height === (foldAbleStatus === display.FoldStatus.FOLD_STATUS_EXPANDED ? 1 : - (previewProfile.size.height / previewProfile.size.width)) * profile.size.width; + return profile.size.width <= 4096 && profile.size.width >= 2448 && + profile.size.height === (foldAbleStatus === display.FoldStatus.FOLD_STATUS_EXPANDED ? 1 : + (previewProfile.size.height / previewProfile.size.width)) * profile.size.width; } return undefined; }); diff --git a/entry/src/main/ets/utils/VideoRecorder.ets b/entry/src/main/ets/utils/VideoRecorder.ets index 57e84243dd9aad75f015fc99ac3b85475ba5dc82..e907eef664adb89f09d5330bfe3f623611b0fef8 100644 --- a/entry/src/main/ets/utils/VideoRecorder.ets +++ b/entry/src/main/ets/utils/VideoRecorder.ets @@ -66,28 +66,28 @@ export async function videoRecording(isStabilization: boolean, cameraPosition: n return profile.size.height === 1080 && profile.size.width === 1920; } return profile.size.height <= screen.width && profile.size.height >= 1080 && - (profile.size.width / profile.size.height) < (screen.height / screen.width) - && (profile.size.width / profile.size.height) > + (profile.size.width / profile.size.height) < (screen.height / screen.width) && + (profile.size.width / profile.size.height) > (foldAbleStatus === display.FoldStatus.FOLD_STATUS_EXPANDED ? 1 : 16 / 9); }) let videoProfile: undefined | camera.VideoProfile = videoProfilesArray.find((profile: camera.VideoProfile) => { if (previewProfile && cameraPosition === 1) { - return profile.size.width >= 1080 && profile.size.height >= 1080 - && profile.size.height === (foldAbleStatus === display.FoldStatus.FOLD_STATUS_EXPANDED ? 1 : - (previewProfile.size.height / previewProfile.size.width)) * profile.size.width - && profile.frameRateRange.max === 30; + return profile.size.width >= 1080 && profile.size.height >= 1080 && + profile.size.height === (foldAbleStatus === display.FoldStatus.FOLD_STATUS_EXPANDED ? 1 : + (previewProfile.size.height / previewProfile.size.width)) * profile.size.width && + profile.frameRateRange.max === 30; } if (previewProfile && qualityLevel === 0) { - return profile.size.width <= 1920 && profile.size.width >= 1080 && profile.size.height >= 1080 - && profile.size.height === (foldAbleStatus === display.FoldStatus.FOLD_STATUS_EXPANDED ? 1 : - (previewProfile.size.height / previewProfile.size.width)) * profile.size.width - && profile.frameRateRange.max === 60; + return profile.size.width <= 1920 && profile.size.width >= 1080 && profile.size.height >= 1080 && + profile.size.height === (foldAbleStatus === display.FoldStatus.FOLD_STATUS_EXPANDED ? 1 : + (previewProfile.size.height / previewProfile.size.width)) * profile.size.width && + profile.frameRateRange.max === 60; } if (previewProfile && qualityLevel === 1 && cameraPosition === 0) { - return profile.size.width <= 4096 && profile.size.width >= 3000 - && profile.size.height === (foldAbleStatus === display.FoldStatus.FOLD_STATUS_EXPANDED ? 1 : - (previewProfile.size.height / previewProfile.size.width)) * profile.size.width - && profile.frameRateRange.max === 60; + return profile.size.width <= 4096 && profile.size.width >= 3000 && + profile.size.height === (foldAbleStatus === display.FoldStatus.FOLD_STATUS_EXPANDED ? 1 : + (previewProfile.size.height / previewProfile.size.width)) * profile.size.width && + profile.frameRateRange.max === 60; } return undefined; }) diff --git a/hvigorfile.ts b/hvigorfile.ts index f3cb9f1a87a81687554a76283af8df27d8bda775..4bbcfd109c9683ce9b656d1c728ecf67282260d4 100644 --- a/hvigorfile.ts +++ b/hvigorfile.ts @@ -1,6 +1,6 @@ import { appTasks } from '@ohos/hvigor-ohos-plugin'; export default { - system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ - plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ -} + system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +};