From b4cfe2b408d60b48889f67470b31e9b6fd982020 Mon Sep 17 00:00:00 2001 From: l30052832 Date: Sat, 27 Jul 2024 10:35:32 +0800 Subject: [PATCH 1/2] =?UTF-8?q?1=E3=80=81=E5=8F=8C=E6=8C=87=E7=BC=A9?= =?UTF-8?q?=E6=94=BE=E7=AE=97=E6=B3=95=E4=BC=98=E5=8C=96=202=E3=80=81?= =?UTF-8?q?=E5=9B=BE=E5=83=8F=E9=A2=84=E8=A7=88=E6=9B=BF=E6=8D=A2=E4=B8=BA?= =?UTF-8?q?=E8=B7=B3=E8=BD=AC=E7=B3=BB=E7=BB=9F=E5=9B=BE=E5=BA=93=E9=A2=84?= =?UTF-8?q?=E8=A7=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry/src/main/ets/pages/Index.ets | 41 ++++++++++++++-------- entry/src/main/ets/utils/CameraShooter.ets | 35 +++++++++--------- entry/src/main/ets/utils/VideoRecorder.ets | 32 +++++++---------- 3 files changed, 55 insertions(+), 53 deletions(-) diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index 1c36ee8..89250cf 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -20,6 +20,7 @@ import { photoAccessHelper } from '@kit.MediaLibraryKit'; import { CameraConstants } from '../constants/CameraConstants'; import { filePreview } from '@kit.PreviewKit'; import { + getVideoZoom, previewVideo, setVideoFlashMode, setVideoSmoothZoom, @@ -33,6 +34,7 @@ import { cameraShooting, capture, enableLivePic, + getPhotoZoom, previewPhoto, releaseCamera, setPhotoFlashMode, @@ -56,6 +58,7 @@ let storage = new LocalStorage(); let context = getContext(this); let videoUri: string; let foldAbleStatus: number = 0; +let currentFov: number = 1; @Component @Entry(storage) @@ -73,7 +76,7 @@ struct XComponentPage { // Is recording now @State recording: boolean = false; @State isFoldAble: boolean = display.isFoldable(); - @State photoUri: string | Resource | PixelMap = ''; + @StorageLink("photoUri") photoUri: string | Resource | PixelMap = ''; // Indicates whether the current preview type is an image @State currentPic: boolean = true; @LocalStorageLink('flashPic') flashPic: Resource = $r('app.media.ic_camera_public_flash_off'); @@ -81,7 +84,6 @@ struct XComponentPage { @LocalStorageLink('isStabilization') isStabilization: boolean = false; @LocalStorageLink('isMovingPhoto') isMovingPhoto: boolean = false; textTimerController: TextTimerController = new TextTimerController(); - uri: string = ''; @State rotation: number = 0; @State isShowZoom: boolean = false; @@ -255,10 +257,10 @@ struct XComponentPage { PinchGesture({ fingers: 2 }) .onActionUpdate((event: GestureEvent) => { if (event && !this.isStabilization) { - this.zoom = this.zoom * ((event.scale - 1) * 0.07 + 1) + this.zoom = currentFov * event.scale this.isShowZoom = true - if (this.zoom > zoomRatioRange[1]) { - this.zoom = zoomRatioRange[1] + if (this.zoom > (this.isPhoto ? zoomRatioRange[1] : 15)) { + this.zoom = this.isPhoto ? zoomRatioRange[1] : 15 } else if (this.zoom < zoomRatioRange[0]) { this.zoom = zoomRatioRange[0] } @@ -270,6 +272,11 @@ struct XComponentPage { } }) .onActionEnd(() => { + if (this.isPhoto) { + currentFov = getPhotoZoom(); + } else { + currentFov = getVideoZoom(); + } this.isShowZoom = false }) ) @@ -286,6 +293,7 @@ struct XComponentPage { .height(CameraConstants.ZOOM_SIZE) .onClick(() => { this.zoom = zoomRatioRange[0]; + currentFov = this.zoom; if (this.isPhoto) { setPhotoZoom(this.zoom); } else { @@ -389,10 +397,12 @@ struct XComponentPage { .rotate({ angle: this.rotation }) .animation({ curve: curves.springMotion() }) .onClick(() => { - if (this.currentPic) { - previewPhoto(this.isMovingPhoto, context, this.uri); - } else { - previewVideo(context, videoUri); + if (this.photoUri != '') { + if (this.currentPic) { + previewPhoto(context); + } else { + previewVideo(context, videoUri); + } } }) Stack() { @@ -401,10 +411,7 @@ struct XComponentPage { .visibility(this.isPhoto ? Visibility.Visible : Visibility.Hidden) .onClick(() => { capture(); - setTimeout(() => { - this.getThumbnail(); - this.currentPic = true; - }, 500); + this.currentPic = true; }) Image($r('app.media.record')) .height(CameraConstants.CAPTURE_SIZE) @@ -422,6 +429,7 @@ struct XComponentPage { this.recording = false; this.currentPic = false; this.zoom = 1; + currentFov = 1; this.flashPic = $r('app.media.ic_camera_public_flash_off'); videoUri = await stopRecord(); stopRecordPreview(); @@ -472,6 +480,7 @@ struct XComponentPage { Initialize(): void { this.zoom = 1; + currentFov = 1; this.isStabilization = false; this.flashPic = $r('app.media.ic_camera_public_flash_off'); this.isMovingPhoto = false; @@ -482,11 +491,13 @@ struct XComponentPage { } setZoom(): void { + currentFov = this.zoom; this.isPhoto ? setPhotoSmoothZoom(this.zoom) : setVideoSmoothZoom(this.zoom); } async getThumbnail(): Promise { let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); + predicates.orderByDesc(photoAccessHelper.PhotoKeys.DATE_ADDED) let fetchOptions: photoAccessHelper.FetchOptions = { fetchColumns: [], predicates: predicates @@ -495,9 +506,8 @@ struct XComponentPage { let fetchResult: photoAccessHelper.FetchResult = await photoHelper.getAssets(fetchOptions); if (fetchResult !== undefined) { - let photoAsset: photoAccessHelper.PhotoAsset = await fetchResult.getLastObject(); + let photoAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); this.photoUri = await photoAsset.getThumbnail(); - this.uri = photoAsset.uri; } } } @@ -505,6 +515,7 @@ struct XComponentPage { export async function fromBack(): Promise { storage.setOrCreate('flashPic', $r('app.media.ic_camera_public_flash_off')); storage.setOrCreate('zoom', 1); + currentFov = 1; storage.setOrCreate('isStabilization', false); storage.setOrCreate('isMovingPhoto', false); cameraShooting(isVideo, cameraPosition, surfaceId, context, foldAbleStatus); diff --git a/entry/src/main/ets/utils/CameraShooter.ets b/entry/src/main/ets/utils/CameraShooter.ets index e91cea5..11caa9a 100644 --- a/entry/src/main/ets/utils/CameraShooter.ets +++ b/entry/src/main/ets/utils/CameraShooter.ets @@ -17,9 +17,8 @@ import { camera } from '@kit.CameraKit'; import { videoRecording } from './VideoRecorder'; import { BusinessError } from '@kit.BasicServicesKit'; import { photoAccessHelper } from '@kit.MediaLibraryKit'; -import { router } from '@kit.ArkUI'; -import { filePreview } from '@kit.PreviewKit'; import { CameraConstants } from '../constants/CameraConstants'; +import { common } from '@kit.AbilityKit'; let previewOutput: camera.PreviewOutput; let cameraInput: camera.CameraInput; @@ -27,6 +26,7 @@ let photoSession: camera.PhotoSession; let photoOutPut: camera.PhotoOutput; let previewSize: camera.Size; let currentContext: Context; +let uri: string; export async function cameraShooting(isVideo: boolean, cameraPosition: number, surfaceId: string, context: Context, foldAbleStatus: number): Promise { @@ -143,6 +143,10 @@ export function setPhotoZoom(zoom: number): void { photoSession.setZoomRatio(zoom); } +export function getPhotoZoom(): number { + return photoSession.getZoomRatio(); +} + export function setPhotoSmoothZoom(zoom: number): void { photoSession.setSmoothZoom(zoom); } @@ -188,24 +192,17 @@ function setPhotoOutputCb(photoOutput: camera.PhotoOutput): void { new photoAccessHelper.MediaAssetChangeRequest(photoAsset); assetChangeRequest.saveCameraPhoto(); await accessHelper.applyChanges(assetChangeRequest); + uri = photoAsset.uri; + AppStorage.setOrCreate("photoUri", await photoAsset.getThumbnail()); }); } -export function previewPhoto(isMovingPhoto: boolean, context: Context, uri: string): void { - if (isMovingPhoto) { - router.pushUrl({ url: 'pages/MovingPhotoPage' }); - return; - } - let displayInfo: filePreview.DisplayInfo = { - x: 100, - y: 100, - width: previewSize.width, - height: previewSize.height - }; - let fileInfo: filePreview.PreviewInfo = { - title: 'photo.jpg', - uri: uri, - mimeType: 'image/jpeg' - }; - filePreview.openPreview(context, fileInfo, displayInfo); +export function previewPhoto(context: Context): void { + let photoContext = context as common.UIAbilityContext; + photoContext.startAbility({ + parameters: { uri: uri }, + action: 'ohos.want.action.viewData', + bundleName: 'com.huawei.hmos.photos', + abilityName: 'com.huawei.hmos.photos.MainAbility' + }) } \ No newline at end of file diff --git a/entry/src/main/ets/utils/VideoRecorder.ets b/entry/src/main/ets/utils/VideoRecorder.ets index 126baa1..3c31ff3 100644 --- a/entry/src/main/ets/utils/VideoRecorder.ets +++ b/entry/src/main/ets/utils/VideoRecorder.ets @@ -17,8 +17,7 @@ import { camera } from '@kit.CameraKit'; import { media } from '@kit.MediaKit'; import { photoAccessHelper } from '@kit.MediaLibraryKit'; import { fileIo } from '@kit.CoreFileKit'; -import { filePreview } from '@kit.PreviewKit'; -import { CameraConstants } from '../constants/CameraConstants'; +import { common } from '@kit.AbilityKit'; let file: fileIo.File; let previewOutput: camera.PreviewOutput; @@ -26,10 +25,6 @@ let cameraInput: camera.CameraInput; let avRecorder: media.AVRecorder; let videoOutput: camera.VideoOutput; let videoSession: camera.VideoSession; -let previewSize: camera.Size = { - width: CameraConstants.PREVIEW_WIDTH, - height: CameraConstants.PREVIEW_HEIGHT -}; let uri: string; export async function videoRecording(isStabilization: boolean, cameraPosition: number, qualityLevel: number, @@ -145,9 +140,9 @@ export async function videoRecording(isStabilization: boolean, cameraPosition: n videoSession.addOutput(previewOutput); videoSession.addOutput(videoOutput); await videoSession.commitConfig(); - await videoSession.start(); videoSession.setVideoStabilizationMode(isStabilization ? camera.VideoStabilizationMode.HIGH : camera.VideoStabilizationMode.OFF); + await videoSession.start(); // Obtains the variable focal length ratio range supported by the camera. let zoomRatioRange = videoSession.getZoomRatioRange(); return zoomRatioRange; @@ -175,6 +170,10 @@ export function setVideoZoom(zoom: number): void { videoSession.setZoomRatio(zoom); } +export function getVideoZoom(): number { + return videoSession.getZoomRatio(); +} + export function setVideoSmoothZoom(zoom: number): void { videoSession.setSmoothZoom(zoom, camera.SmoothZoomMode.NORMAL); } @@ -196,16 +195,11 @@ export async function stopRecord(): Promise { } export function previewVideo(context: Context, videoUri: string): void { - let displayInfo: filePreview.DisplayInfo = { - x: 100, - y: 100, - width: previewSize.width, - height: previewSize.height - }; - let fileInfo: filePreview.PreviewInfo = { - title: 'video.mp4', - uri: videoUri, - mimeType: 'video/mp4' - }; - filePreview.openPreview(context, fileInfo, displayInfo); + let videoContext = context as common.UIAbilityContext; + videoContext.startAbility({ + parameters: { uri: videoUri }, + action: 'ohos.want.action.viewData', + bundleName: 'com.huawei.hmos.photos', + abilityName: 'com.huawei.hmos.photos.MainAbility' + }) } -- Gitee From 33532571c01a2548156df7438e0a6b47dfb0383a Mon Sep 17 00:00:00 2001 From: l30052832 Date: Sat, 27 Jul 2024 11:19:39 +0800 Subject: [PATCH 2/2] =?UTF-8?q?1=E3=80=81=E4=BB=A3=E7=A0=81=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry/src/main/ets/pages/Index.ets | 14 +++++++------- entry/src/main/ets/utils/CameraShooter.ets | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index 89250cf..9837ec2 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -76,7 +76,7 @@ struct XComponentPage { // Is recording now @State recording: boolean = false; @State isFoldAble: boolean = display.isFoldable(); - @StorageLink("photoUri") photoUri: string | Resource | PixelMap = ''; + @StorageLink('photoUri') photoUri: string | Resource | PixelMap = ''; // Indicates whether the current preview type is an image @State currentPic: boolean = true; @LocalStorageLink('flashPic') flashPic: Resource = $r('app.media.ic_camera_public_flash_off'); @@ -257,12 +257,12 @@ struct XComponentPage { PinchGesture({ fingers: 2 }) .onActionUpdate((event: GestureEvent) => { if (event && !this.isStabilization) { - this.zoom = currentFov * event.scale - this.isShowZoom = true + this.zoom = currentFov * event.scale; + this.isShowZoom = true; if (this.zoom > (this.isPhoto ? zoomRatioRange[1] : 15)) { - this.zoom = this.isPhoto ? zoomRatioRange[1] : 15 + this.zoom = this.isPhoto ? zoomRatioRange[1] : 15; } else if (this.zoom < zoomRatioRange[0]) { - this.zoom = zoomRatioRange[0] + this.zoom = zoomRatioRange[0]; } if (this.isPhoto) { setPhotoZoom(this.zoom); @@ -277,7 +277,7 @@ struct XComponentPage { } else { currentFov = getVideoZoom(); } - this.isShowZoom = false + this.isShowZoom = false; }) ) .onLoad(async () => { @@ -497,7 +497,7 @@ struct XComponentPage { async getThumbnail(): Promise { let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); - predicates.orderByDesc(photoAccessHelper.PhotoKeys.DATE_ADDED) + predicates.orderByDesc(photoAccessHelper.PhotoKeys.DATE_ADDED); let fetchOptions: photoAccessHelper.FetchOptions = { fetchColumns: [], predicates: predicates diff --git a/entry/src/main/ets/utils/CameraShooter.ets b/entry/src/main/ets/utils/CameraShooter.ets index 11caa9a..6333c40 100644 --- a/entry/src/main/ets/utils/CameraShooter.ets +++ b/entry/src/main/ets/utils/CameraShooter.ets @@ -193,7 +193,7 @@ function setPhotoOutputCb(photoOutput: camera.PhotoOutput): void { assetChangeRequest.saveCameraPhoto(); await accessHelper.applyChanges(assetChangeRequest); uri = photoAsset.uri; - AppStorage.setOrCreate("photoUri", await photoAsset.getThumbnail()); + AppStorage.setOrCreate('photoUri', await photoAsset.getThumbnail()); }); } -- Gitee