From c2116bcbcfe4115a318447777b25ea393f8593a7 Mon Sep 17 00:00:00 2001 From: "1362134550@qq.com" <1362134550@qq.com> Date: Tue, 9 Sep 2025 11:12:17 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 18 +++++++----------- entry/src/main/cpp/camera_manager.h | 1 - .../main/cpp/types/libentry/oh-package.json5 | 2 +- entry/src/main/ets/views/ModeSwitchPage.ets | 8 -------- 4 files changed, 8 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index d7c622a..c4771a0 100644 --- a/README.md +++ b/README.md @@ -14,14 +14,13 @@ 使用说明 1. 弹出是否允许“相机数据采集保存”使用相机?点击“允许” 2. 弹出是否允许“相机数据采集保存”使用麦克风?点击“允许” -3. 弹出是否允许“相机数据采集保存”访问文件?点击“允许” -4. 弹出是否允许“相机数据采集保存”访问图片和视频?点击“允许” -5. 进入预览界面,预览正常,点击画面模糊处,画面会变得清晰,对焦效果明显 -6. 进入预览界面,预览正常,上下滑动屏幕,屏幕场景亮度发生变化,曝光效果明显 -7. 进入预览界面,预览正常,进入拍照模式,点击拍照按钮,拍照正常,左下角会生成照片缩略图,点击左下角缩略图,能够跳转到图库,图片保存正常,打开图片显示正常 -8. 进入预览界面,预览正常,切换到前置摄像头,点击拍照按钮,拍照正常,左下角生成照片缩略图,点击左下角缩略图,能够跳转到图库,图片保存正常,打开图片显示正常 -9. 进入预览界面,预览正常,切换到录像模式,点击录像,开始录像,再点击停止录像按钮,录像成功,左下角会生成视频缩略图,点击左下角缩略图,能够跳转到图库,录像文件保存正常,播放录像文件正常 -10. 进入预览界面,预览正常,切换到后置摄像头,点击录像,开始录像,再点击停止录像按钮,录像成功,左下角会生成视频缩略图,点击左下角缩略图,能够跳转到图库,录像文件保存正常,播放录像文件正常 +3. 弹出是否允许“相机数据采集保存”访问图片和视频?点击“允许” +4. 进入预览界面,预览正常,点击画面模糊处,画面会变得清晰,对焦效果明显 +5. 进入预览界面,预览正常,上下滑动屏幕,屏幕场景亮度发生变化,曝光效果明显 +6. 进入预览界面,预览正常,进入拍照模式,点击拍照按钮,拍照正常,左下角会生成照片缩略图,点击左下角缩略图,能够跳转到图库,图片保存正常,打开图片显示正常 +7. 进入预览界面,预览正常,切换到前置摄像头,点击拍照按钮,拍照正常,左下角生成照片缩略图,点击左下角缩略图,能够跳转到图库,图片保存正常,打开图片显示正常 +8. 进入预览界面,预览正常,切换到录像模式,点击录像,开始录像,再点击停止录像按钮,录像成功,左下角会生成视频缩略图,点击左下角缩略图,能够跳转到图库,录像文件保存正常,播放录像文件正常 +9. 进入预览界面,预览正常,切换到后置摄像头,点击录像,开始录像,再点击停止录像按钮,录像成功,左下角会生成视频缩略图,点击左下角缩略图,能够跳转到图库,录像文件保存正常,播放录像文件正常 ### 工程目录 @@ -87,9 +86,6 @@ * ohos.permission.CAMERA * ohos.permission.MICROPHONE -* ohos.permission.READ_MEDIA -* ohos.permission.WRITE_MEDIA -* ohos.permission.READ_IMAGEVIDEO * ohos.permission.WRITE_IMAGEVIDEO ### 依赖 diff --git a/entry/src/main/cpp/camera_manager.h b/entry/src/main/cpp/camera_manager.h index 902d133..f3be1a7 100644 --- a/entry/src/main/cpp/camera_manager.h +++ b/entry/src/main/cpp/camera_manager.h @@ -16,7 +16,6 @@ #ifndef CAMERA_NDK_CAMERA_H #define CAMERA_NDK_CAMERA_H -#include #include "mutex" #include "ohcamera/camera.h" #include "ohcamera/camera_input.h" diff --git a/entry/src/main/cpp/types/libentry/oh-package.json5 b/entry/src/main/cpp/types/libentry/oh-package.json5 index 46e8724..ea41072 100644 --- a/entry/src/main/cpp/types/libentry/oh-package.json5 +++ b/entry/src/main/cpp/types/libentry/oh-package.json5 @@ -1,6 +1,6 @@ { "name": "libentry.so", "types": "./Index.d.ts", - "version": "", + "version": "1.0.0", "description": "Please describe the basic information." } \ No newline at end of file diff --git a/entry/src/main/ets/views/ModeSwitchPage.ets b/entry/src/main/ets/views/ModeSwitchPage.ets index 643bf5f..5f2c560 100644 --- a/entry/src/main/ets/views/ModeSwitchPage.ets +++ b/entry/src/main/ets/views/ModeSwitchPage.ets @@ -78,14 +78,6 @@ export struct ModeSwitchPage { width: 1280, height: 720 }; - private photoSettings: PhotoSettings = { - quality: 0, - rotation: 0, - mirror: false, - latitude: Constants.LATITUDE, - longitude: Constants.LONGITUDE, - altitude: Constants.ALTITUDE - }; private mReceiver?: image.ImageReceiver; private videoRecorder?: media.AVRecorder; private videoConfig: media.AVRecorderConfig = { -- Gitee From d9f056878e098bd56b4c3b2629241d17bbfdaefc Mon Sep 17 00:00:00 2001 From: "1362134550@qq.com" <1362134550@qq.com> Date: Tue, 9 Sep 2025 11:32:24 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry/src/main/ets/views/ModeSwitchPage.ets | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/entry/src/main/ets/views/ModeSwitchPage.ets b/entry/src/main/ets/views/ModeSwitchPage.ets index 5f2c560..f4034ac 100644 --- a/entry/src/main/ets/views/ModeSwitchPage.ets +++ b/entry/src/main/ets/views/ModeSwitchPage.ets @@ -78,6 +78,14 @@ export struct ModeSwitchPage { width: 1280, height: 720 }; + private photoSettings: PhotoSettings = { + quality: 0, + rotation: 0, + mirror: false, + latitude: Constants.LATITUDE, + longitude: Constants.LONGITUDE, + altitude: Constants.ALTITUDE + }; private mReceiver?: image.ImageReceiver; private videoRecorder?: media.AVRecorder; private videoConfig: media.AVRecorderConfig = { @@ -100,12 +108,6 @@ export struct ModeSwitchPage { videoOrientation: '' } }; - private photoRotationMap: PhotoRotationMap = { - rotation0: 0, - rotation90: 90, - rotation180: 180, - rotation270: 270, - }; private settingDataObj: SettingDataObj = { mirrorBol: false, videoStabilizationMode: 0, -- Gitee From ada08042fdcc156a6f2e951368eb4d4539a1bd1b Mon Sep 17 00:00:00 2001 From: "1362134550@qq.com" <1362134550@qq.com> Date: Tue, 9 Sep 2025 11:51:01 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- .../src/main/ets/common/utils/MediaUtils.ets | 34 +++++--- entry/src/main/ets/pages/Index.ets | 54 ++++++------ entry/src/main/ets/views/ModeSwitchPage.ets | 82 ++++++++++--------- 4 files changed, 99 insertions(+), 73 deletions(-) diff --git a/README.md b/README.md index c4771a0..27ed2f4 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ 使用说明 1. 弹出是否允许“相机数据采集保存”使用相机?点击“允许” 2. 弹出是否允许“相机数据采集保存”使用麦克风?点击“允许” -3. 弹出是否允许“相机数据采集保存”访问图片和视频?点击“允许” +3. 弹出是否允许“相机数据采集保存”修改你的图片和视频?点击“允许” 4. 进入预览界面,预览正常,点击画面模糊处,画面会变得清晰,对焦效果明显 5. 进入预览界面,预览正常,上下滑动屏幕,屏幕场景亮度发生变化,曝光效果明显 6. 进入预览界面,预览正常,进入拍照模式,点击拍照按钮,拍照正常,左下角会生成照片缩略图,点击左下角缩略图,能够跳转到图库,图片保存正常,打开图片显示正常 diff --git a/entry/src/main/ets/common/utils/MediaUtils.ets b/entry/src/main/ets/common/utils/MediaUtils.ets index 174b316..fd31aa1 100644 --- a/entry/src/main/ets/common/utils/MediaUtils.ets +++ b/entry/src/main/ets/common/utils/MediaUtils.ets @@ -30,7 +30,7 @@ export default class MediaUtils { private tag: string = 'MediaUtils'; public static getInstance() { - let instance: MediaUtils = new MediaUtils() + let instance: MediaUtils = new MediaUtils(); if (instance === undefined) { instance = new MediaUtils(); } @@ -38,20 +38,30 @@ export default class MediaUtils { } async createAndGetUri(mediaType: number) { - let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context); - Logger.info(this.tag, `createAndGetUri`); - let photoType: photoAccessHelper.PhotoType = mediaType; - let info = this.getInfoFromType(mediaType) - let extension: string = info.suffix; - // Return the created media resource. - return await phAccessHelper.createAsset(photoType, extension) + try { + let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context); + Logger.info(this.tag, `createAndGetUri`); + let photoType: photoAccessHelper.PhotoType = mediaType; + let info = this.getInfoFromType(mediaType); + let extension: string = info.suffix; + // Return the created media resource. + return await phAccessHelper.createAsset(photoType, extension); + } catch (e) { + Logger.error(this.tag, `createAndGetUri error ${JSON.stringify(e)}`); + } + return ''; } async getFdPath(uri: string) { - let file = fileIo.openSync(uri, fileIo.OpenMode.READ_WRITE); - let fd = file.fd; - Logger.info(this.tag, `fd = ${fd}`); - return fd; + try { + let file = fileIo.openSync(uri, fileIo.OpenMode.READ_WRITE); + let fd = file.fd; + Logger.info(this.tag, `fd = ${fd}`); + return fd; + } catch (e) { + Logger.error(this.tag, `getFdPath error ${JSON.stringify(e)}`); + } + return -1; } getInfoFromType(mediaType: number) { diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index 040713a..4f29b5d 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -89,11 +89,15 @@ struct Index { // Entry initialization function. async aboutToAppear() { - await this.requestPermissionsFn(); - let mDisplay = display.getDefaultDisplaySync(); - this.screenWidth = this.getUIContext().px2vp(mDisplay.width); - this.screenHeight = this.getUIContext().px2vp(mDisplay.height); - this.initXComponentSize(); + try { + await this.requestPermissionsFn(); + let mDisplay = display.getDefaultDisplaySync(); + this.screenWidth = this.getUIContext().px2vp(mDisplay.width); + this.screenHeight = this.getUIContext().px2vp(mDisplay.height); + this.initXComponentSize(); + } catch (e) { + Logger.error(this.tag, `getDefaultDisplaySync error ${JSON.stringify(e)}`); + } } initXComponentSize(): void { @@ -131,24 +135,28 @@ struct Index { } async getThumbnail() { - let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context); - let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); - let fetchOptions: photoAccessHelper.FetchOptions = { - fetchColumns: [], - predicates: predicates - }; - let fetchResult: photoAccessHelper.FetchResult = - await phAccessHelper.getAssets(fetchOptions); - let asset: photoAccessHelper.PhotoAsset = await fetchResult.getLastObject(); - console.info('asset displayName = ', asset.displayName); - asset.getThumbnail((err, pixelMap) => { - if (err === undefined) { - this.thumbnail = pixelMap; - console.info('getThumbnail successful ' + pixelMap); - } else { - console.error(`getThumbnail fail with error: ${err.code}, ${err.message}`); - } - }); + try { + let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context); + let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); + let fetchOptions: photoAccessHelper.FetchOptions = { + fetchColumns: [], + predicates: predicates + }; + let fetchResult: photoAccessHelper.FetchResult = + await phAccessHelper.getAssets(fetchOptions); + let asset: photoAccessHelper.PhotoAsset = await fetchResult.getLastObject(); + console.info('asset displayName = ', asset.displayName); + asset.getThumbnail((err, pixelMap) => { + if (err === undefined) { + this.thumbnail = pixelMap; + console.info('getThumbnail successful ' + pixelMap); + } else { + console.error(`getThumbnail fail with error: ${err.code}, ${err.message}`); + } + }); + } catch (e) { + Logger.error(this.tag, `getThumbnail error ${JSON.stringify(e)}`); + } } async onPageShow() { diff --git a/entry/src/main/ets/views/ModeSwitchPage.ets b/entry/src/main/ets/views/ModeSwitchPage.ets index f4034ac..1872e5f 100644 --- a/entry/src/main/ets/views/ModeSwitchPage.ets +++ b/entry/src/main/ets/views/ModeSwitchPage.ets @@ -155,49 +155,53 @@ export struct ModeSwitchPage { } async getVideoSurfaceID() { - Logger.info(this.tag, `getVideoSurfaceID`); - this.videoRecorder = await media.createAVRecorder(); - Logger.info(this.tag, `getVideoSurfaceID videoRecorder: ${this.videoRecorder}`); + try { + Logger.info(this.tag, `getVideoSurfaceID`); + this.videoRecorder = await media.createAVRecorder(); + Logger.info(this.tag, `getVideoSurfaceID videoRecorder: ${this.videoRecorder}`); - this.photoAsset = await this.mediaUtil.createAndGetUri(photoAccessHelper.PhotoType.VIDEO); - Logger.info(this.tag, `getVideoSurfaceID photoAsset: ${this.photoAsset}`); + this.photoAsset = await this.mediaUtil.createAndGetUri(photoAccessHelper.PhotoType.VIDEO); + Logger.info(this.tag, `getVideoSurfaceID photoAsset: ${this.photoAsset}`); - this.fd = await this.mediaUtil.getFdPath(this.photoAsset); - Logger.info(this.tag, `getVideoSurfaceID fd: ${this.fd}`); + this.fd = await this.mediaUtil.getFdPath(this.photoAsset); + Logger.info(this.tag, `getVideoSurfaceID fd: ${this.fd}`); - this.videoConfig.url = `fd://${this.fd}`; - Logger.info(this.tag, `getVideoSurfaceID videoConfig.url : ${this.videoConfig.url}`); + this.videoConfig.url = `fd://${this.fd}`; + Logger.info(this.tag, `getVideoSurfaceID videoConfig.url : ${this.videoConfig.url}`); - if (deviceInfo.deviceType === Constants.DEFAULT) { - Logger.info(this.tag, `deviceType = default`); - this.videoConfig.videoSourceType = media.VideoSourceType.VIDEO_SOURCE_TYPE_SURFACE_ES; - } - if (deviceInfo.deviceType === Constants.PHONE) { - Logger.info(this.tag, `deviceType = phone`) - this.videoConfig.videoSourceType = media.VideoSourceType.VIDEO_SOURCE_TYPE_SURFACE_YUV; - this.videoConfig.profile.videoCodec = media.CodecMimeType.VIDEO_AVC; - if (this.cameraDeviceIndex === 1) { - this.videoConfig.metadata = { - videoOrientation: '270' - }; - } else { - this.videoConfig.metadata = { - videoOrientation: '90' - }; + if (deviceInfo.deviceType === Constants.DEFAULT) { + Logger.info(this.tag, `deviceType = default`); + this.videoConfig.videoSourceType = media.VideoSourceType.VIDEO_SOURCE_TYPE_SURFACE_ES; + } + if (deviceInfo.deviceType === Constants.PHONE) { + Logger.info(this.tag, `deviceType = phone`) + this.videoConfig.videoSourceType = media.VideoSourceType.VIDEO_SOURCE_TYPE_SURFACE_YUV; + this.videoConfig.profile.videoCodec = media.CodecMimeType.VIDEO_AVC; + if (this.cameraDeviceIndex === 1) { + this.videoConfig.metadata = { + videoOrientation: '270' + }; + } else { + this.videoConfig.metadata = { + videoOrientation: '90' + }; + } + } + if (deviceInfo.deviceType === 'tablet') { + Logger.info(this.tag, `deviceType = tablet`); + this.videoConfig.videoSourceType = media.VideoSourceType.VIDEO_SOURCE_TYPE_SURFACE_YUV; } - } - if (deviceInfo.deviceType === 'tablet') { - Logger.info(this.tag, `deviceType = tablet`); - this.videoConfig.videoSourceType = media.VideoSourceType.VIDEO_SOURCE_TYPE_SURFACE_YUV; - } - this.videoConfig.profile.videoFrameWidth = cameraDemo.getVideoFrameWidth(); - this.videoConfig.profile.videoFrameHeight = cameraDemo.getVideoFrameHeight(); - this.videoConfig.profile.videoFrameRate = cameraDemo.getVideoFrameRate(); + this.videoConfig.profile.videoFrameWidth = cameraDemo.getVideoFrameWidth(); + this.videoConfig.profile.videoFrameHeight = cameraDemo.getVideoFrameHeight(); + this.videoConfig.profile.videoFrameRate = cameraDemo.getVideoFrameRate(); - await this.videoRecorder.prepare(this.videoConfig); - this.videoId = await this.videoRecorder.getInputSurface(); - Logger.info(this.tag, `getVideoSurfaceID videoId: ${this.videoId}`); + await this.videoRecorder.prepare(this.videoConfig); + this.videoId = await this.videoRecorder.getInputSurface(); + Logger.info(this.tag, `getVideoSurfaceID videoId: ${this.videoId}`); + } catch (e) { + Logger.error(this.tag, `getVideoSurfaceID error ${JSON.stringify(e)}`); + } } createImageReceiver() { @@ -290,7 +294,11 @@ export struct ModeSwitchPage { cameraDemo.startPhotoOrVideo(this.modelBagCol, this.videoId, this.mSurfaceId); cameraDemo.videoOutputStart(); if (this.videoRecorder) { - this.videoRecorder.start(); + try { + this.videoRecorder.start(); + } catch (e) { + Logger.error(this.tag, `videoRecorder.start error ${JSON.stringify(e)}`); + } } } } -- Gitee From 06a89fe361bf307f65218678cf26e225ddb78dd1 Mon Sep 17 00:00:00 2001 From: "1362134550@qq.com" <1362134550@qq.com> Date: Tue, 9 Sep 2025 11:51:53 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry/src/main/cpp/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/entry/src/main/cpp/CMakeLists.txt b/entry/src/main/cpp/CMakeLists.txt index e1428e7..add9d07 100644 --- a/entry/src/main/cpp/CMakeLists.txt +++ b/entry/src/main/cpp/CMakeLists.txt @@ -1,5 +1,5 @@ # the minimum version of CMake. -cmake_minimum_required(VERSION 3.4.1) +cmake_minimum_required(VERSION 3.5.0) project(CameraSample) set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) -- Gitee From 900f334abe7a552dda553d523c88cf0edcd40d35 Mon Sep 17 00:00:00 2001 From: "1362134550@qq.com" <1362134550@qq.com> Date: Tue, 9 Sep 2025 11:53:38 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry/src/main/ets/pages/Index.ets | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index 4f29b5d..d72d933 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -96,7 +96,7 @@ struct Index { this.screenHeight = this.getUIContext().px2vp(mDisplay.height); this.initXComponentSize(); } catch (e) { - Logger.error(this.tag, `getDefaultDisplaySync error ${JSON.stringify(e)}`); + Logger.error(TAG, `getDefaultDisplaySync error ${JSON.stringify(e)}`); } } @@ -155,7 +155,7 @@ struct Index { } }); } catch (e) { - Logger.error(this.tag, `getThumbnail error ${JSON.stringify(e)}`); + Logger.error(TAG, `getThumbnail error ${JSON.stringify(e)}`); } } -- Gitee