diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index 1c36ee8584744ad0c1142f33d6cc987a0cad3bc2..9837ec2b93dee956159d98dcbee7627f220e6c38 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,12 +257,12 @@ struct XComponentPage { PinchGesture({ fingers: 2 }) .onActionUpdate((event: GestureEvent) => { if (event && !this.isStabilization) { - this.zoom = this.zoom * ((event.scale - 1) * 0.07 + 1) - this.isShowZoom = true - if (this.zoom > zoomRatioRange[1]) { - this.zoom = zoomRatioRange[1] + this.zoom = currentFov * event.scale; + this.isShowZoom = true; + 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] + this.zoom = zoomRatioRange[0]; } if (this.isPhoto) { setPhotoZoom(this.zoom); @@ -270,7 +272,12 @@ struct XComponentPage { } }) .onActionEnd(() => { - this.isShowZoom = false + if (this.isPhoto) { + currentFov = getPhotoZoom(); + } else { + currentFov = getVideoZoom(); + } + this.isShowZoom = false; }) ) .onLoad(async () => { @@ -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 e91cea5ac24163e43863c5a8324c8abcbfb7accc..6333c40580609c88b7a2d9230d99a2e894f7c6ce 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 126baa1a7075067174d35a5adcae0b6bdc3b192a..3c31ff37aea9fdd76ea800d582957d9b100ca32d 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' + }) }