From 015f6fd220e19a8252099e21b709ff67c8f3cffb Mon Sep 17 00:00:00 2001 From: lengye Date: Tue, 24 Oct 2023 13:22:23 +0000 Subject: [PATCH 1/3] add photo for ndk demo Signed-off-by: lengye --- .../entry/src/main/cpp/camera_manager.cpp | 55 ++++++++++-- .../entry/src/main/cpp/camera_manager.h | 10 ++- .../entry/src/main/cpp/main.cpp | 17 ++-- .../src/main/ets/views/modeSwitchPage.ets | 86 ++++++++++++++++++- 4 files changed, 149 insertions(+), 19 deletions(-) diff --git a/frameworks/native/camera/test/ndktest/camera_ndk_demo/entry/src/main/cpp/camera_manager.cpp b/frameworks/native/camera/test/ndktest/camera_ndk_demo/entry/src/main/cpp/camera_manager.cpp index 0394b18d6..a61a66c4e 100644 --- a/frameworks/native/camera/test/ndktest/camera_ndk_demo/entry/src/main/cpp/camera_manager.cpp +++ b/frameworks/native/camera/test/ndktest/camera_ndk_demo/entry/src/main/cpp/camera_manager.cpp @@ -14,7 +14,10 @@ */ #include "camera_manager.h" +#include +#define LOG_TAG "NDK DEMO" +#define LOG_DOMAIN 0x3200 NDKCamera* NDKCamera::ndkCamera_ = nullptr; std::mutex NDKCamera::mtx_; @@ -41,7 +44,7 @@ NDKCamera::NDKCamera(char* str) GetSupportedCameras(); GetSupportedOutputCapability(); CreatePreviewOutput(); - CreatePhotoOutput(); + //CreatePhotoOutput(); CreateCameraInput(); CameraInputOpen(); SessionFlowFn(); @@ -291,24 +294,28 @@ Camera_ErrorCode NDKCamera::CreatePreviewOutput(void) return ret_; } -Camera_ErrorCode NDKCamera::CreatePhotoOutput(void) +Camera_ErrorCode NDKCamera::CreatePhotoOutput(char* photoSurfaceId) { profile_ = cameraOutputCapability_->photoProfiles[0]; if (profile_ == nullptr) { OH_LOG_ERROR(LOG_APP, "Get photoProfiles failed."); return CAMERA_INVALID_ARGUMENT; } - ret_ = OH_CameraManager_CreatePhotoOutput(cameraManager_, profile_, photoSurfaceId_, &photoOutput_); - if (photoSurfaceId_ == nullptr || photoOutput_ == nullptr || ret_ != CAMERA_OK) { + + if (photoSurfaceId == nullptr) { OH_LOG_ERROR(LOG_APP, "CreatePhotoOutput failed."); return CAMERA_INVALID_ARGUMENT; } + + ret_ = OH_CameraManager_CreatePhotoOutput(cameraManager_, profile_, photoSurfaceId, &photoOutput_); + return ret_; } Camera_ErrorCode NDKCamera::CreateVideoOutput(char* videoId) { videoProfile_ = cameraOutputCapability_->videoProfiles[0]; + if (videoProfile_ == nullptr) { OH_LOG_ERROR(LOG_APP, "Get videoProfiles failed."); return CAMERA_INVALID_ARGUMENT; @@ -387,11 +394,12 @@ Camera_ErrorCode NDKCamera::PhotoOutputRelease(void) } return ret_; } - Camera_ErrorCode NDKCamera::startVideo(char* videoId) { OH_LOG_ERROR(LOG_APP, "startVideo begin."); + Camera_ErrorCode ret = SessionStop(); + if (ret == CAMERA_OK) { OH_LOG_ERROR(LOG_APP, "SessionStop success."); } else { @@ -420,4 +428,41 @@ Camera_ErrorCode NDKCamera::VideoOutputStart() OH_LOG_ERROR(LOG_APP, "OH_VideoOutput_Start failed. %d ", ret); } return ret; +} + +Camera_ErrorCode NDKCamera::startPhoto(char *mSurfaceId) { + Camera_ErrorCode ret = CAMERA_OK; + if (takePictureTimes == 0) { + ret = SessionStop(); + if (ret == CAMERA_OK) { + OH_LOG_ERROR(LOG_APP, "SessionStop success."); + } else { + OH_LOG_ERROR(LOG_APP, "SessionStop failed. %d ", ret); + } + ret = SessionBegin(); + if (ret == CAMERA_OK) { + OH_LOG_ERROR(LOG_APP, "SessionBegin success."); + } else { + OH_LOG_ERROR(LOG_APP, "SessionBegin failed. %d ", ret); + } + OH_LOG_ERROR(LOG_APP, "startPhoto begin."); + ret = CreatePhotoOutput(mSurfaceId); + + OH_LOG_ERROR(LOG_APP, "startPhoto CreatePhotoOutput ret = %{public}d.", ret); + ret = OH_CaptureSession_AddPhotoOutput(captureSession_, photoOutput_); + OH_LOG_ERROR(LOG_APP, "startPhoto AddPhotoOutput ret = %{public}d.", ret); + ret = SessionCommitConfig(); + + OH_LOG_ERROR(LOG_APP, "startPhoto SessionCommitConfig ret = %{public}d.", ret); + ret = SessionStart(); + OH_LOG_ERROR(LOG_APP, "startPhoto SessionStart ret = %{public}d.", ret); + } + ret = OH_PhotoOutput_Capture(photoOutput_); + OH_LOG_ERROR(LOG_APP, "startPhoto OH_PhotoOutput_Capture ret = %{public}d.", ret); + if (ret_ != CAMERA_OK) { + OH_LOG_ERROR(LOG_APP, "startPhoto failed."); + return CAMERA_INVALID_ARGUMENT; + } + takePictureTimes ++; + return ret_; } \ No newline at end of file diff --git a/frameworks/native/camera/test/ndktest/camera_ndk_demo/entry/src/main/cpp/camera_manager.h b/frameworks/native/camera/test/ndktest/camera_ndk_demo/entry/src/main/cpp/camera_manager.h index eeb53d8fa..411c24fd7 100644 --- a/frameworks/native/camera/test/ndktest/camera_ndk_demo/entry/src/main/cpp/camera_manager.h +++ b/frameworks/native/camera/test/ndktest/camera_ndk_demo/entry/src/main/cpp/camera_manager.h @@ -68,8 +68,8 @@ public: Camera_ErrorCode GetSupportedCameras(void); Camera_ErrorCode GetSupportedOutputCapability(void); Camera_ErrorCode CreatePreviewOutput(void); - Camera_ErrorCode CreatePhotoOutput(void); - Camera_ErrorCode CreateVideoOutput(char *videoId); + Camera_ErrorCode CreatePhotoOutput(char* photoId); + Camera_ErrorCode CreateVideoOutput(char* videoId); Camera_ErrorCode CreateMetadataOutput(void); Camera_ErrorCode IsCameraMuted(void); Camera_ErrorCode PreviewOutputStop(void); @@ -78,13 +78,14 @@ public: Camera_ErrorCode HasFlashFn(uint32_t mode); Camera_ErrorCode setZoomRatioFn(uint32_t zoomRatio); Camera_ErrorCode SessionFlowFn(); - Camera_ErrorCode SessionBegin(); + Camera_ErrorCode SessionBegin(); Camera_ErrorCode SessionCommitConfig(); Camera_ErrorCode SessionStart(); Camera_ErrorCode SessionStop(); - Camera_ErrorCode startVideo(char *videoId); + Camera_ErrorCode startVideo(char* videoId); Camera_ErrorCode AddVideoOutput(); Camera_ErrorCode VideoOutputStart(); + Camera_ErrorCode startPhoto(char *mSurfaceId); private: explicit NDKCamera(char *str); @@ -110,6 +111,7 @@ private: char* previewSurfaceId_; char* photoSurfaceId_; Camera_ErrorCode ret_; + uint32_t takePictureTimes = 0; // callback CameraManager_Callbacks* callback_; diff --git a/frameworks/native/camera/test/ndktest/camera_ndk_demo/entry/src/main/cpp/main.cpp b/frameworks/native/camera/test/ndktest/camera_ndk_demo/entry/src/main/cpp/main.cpp index fc3825bf2..9c4aabe35 100644 --- a/frameworks/native/camera/test/ndktest/camera_ndk_demo/entry/src/main/cpp/main.cpp +++ b/frameworks/native/camera/test/ndktest/camera_ndk_demo/entry/src/main/cpp/main.cpp @@ -19,6 +19,8 @@ #include "camera_manager.h" #include "hilog/log.h" +#define LOG_TAG "NDK DEMO" +#define LOG_DOMAIN 0x3200 static NDKCamera* ndkCamera_ = nullptr; static napi_value SetZoomRatio(napi_env env, napi_callback_info info) @@ -75,10 +77,10 @@ static napi_value StartPhotoOrVideo(napi_env env, napi_callback_info info) napi_value result; size_t typeLen = 0; size_t videoIdLen = 0; - char *modeFlag = nullptr; - char *videoId = nullptr; + char * modeFlag = nullptr; + char * videoId = nullptr; - napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); + napi_get_cb_info(env, info, &argc, args , nullptr, nullptr); napi_get_value_string_utf8(env, args[0], nullptr, 0, &typeLen); modeFlag = new char[typeLen + 1]; @@ -88,14 +90,16 @@ static napi_value StartPhotoOrVideo(napi_env env, napi_callback_info info) videoId = new char[videoIdLen + 1]; napi_get_value_string_utf8(env, args[1], videoId, videoIdLen + 1, &videoIdLen); - if (!strcmp(modeFlag, "photo")) { - // take photo func + if (!strcmp(modeFlag, "photo")){ + OH_LOG_ERROR(LOG_APP, "StartPhoto surfaceId %{public}s", videoId); + ndkCamera_->startPhoto(videoId); } else if (!strcmp(modeFlag, "video")) { ndkCamera_->startVideo(videoId); - OH_LOG_ERROR(LOG_APP, "StartPhotoOrVideo000 %{public}s", videoId); + OH_LOG_ERROR(LOG_APP, "StartPhotoOrVideo000 %{public}s",videoId); } return result; + } static napi_value VideoOutputStart(napi_env env, napi_callback_info info) @@ -107,6 +111,7 @@ static napi_value VideoOutputStart(napi_env env, napi_callback_info info) ndkCamera_->VideoOutputStart(); return result; + } EXTERN_C_START static napi_value Init(napi_env env, napi_value exports) diff --git a/frameworks/native/camera/test/ndktest/camera_ndk_demo/entry/src/main/ets/views/modeSwitchPage.ets b/frameworks/native/camera/test/ndktest/camera_ndk_demo/entry/src/main/ets/views/modeSwitchPage.ets index d602cabe2..7e7c70c54 100644 --- a/frameworks/native/camera/test/ndktest/camera_ndk_demo/entry/src/main/ets/views/modeSwitchPage.ets +++ b/frameworks/native/camera/test/ndktest/camera_ndk_demo/entry/src/main/ets/views/modeSwitchPage.ets @@ -30,14 +30,22 @@ import image from '@ohos.multimedia.image'; import media from '@ohos.multimedia.media' import MediaUtils from '../model/MediaUtils' import AVRecorder from '@ohos.multimedia.media' +import fileio from '@ohos.fileio' + +const CameraSize = { + WIDTH: 1280, + HEIGHT: 720 +} @Component export struct modeSwitchPage { - private tag: string = 'UI modeSwitchPage:' + private tag: string = 'CAIHF modeSwitchPage:' private mediaUtil = MediaUtils.getInstance() private fileAsset: mediaLibrary.FileAsset = undefined private fd: number = -1 @State videoId: string = '' + @State mSurfaceId: string = '' + private mReceiver: image.ImageReceiver = undefined private videoRecorder: media.AVRecorder = undefined private videoConfig: AVRecorder.AVRecorderConfig = { audioSourceType: 1, @@ -188,22 +196,92 @@ export struct modeSwitchPage { Logger.info(this.tag, `getVideoSurfaceID videoId: ${this.videoId}`) } + createImageReceiver() { + try { + // 多少数据 不能直接把buff , 写进去 + // buffer大小固定死 直接写进去就已经确定了 + // 需要拿到dataSize 再进行写入 + this.mReceiver = image.createImageReceiver(CameraSize.WIDTH, CameraSize.HEIGHT, 2000, 8) + Logger.info(this.tag, `createImageReceiver value: ${this.mReceiver} `) + this.mReceiver.on('imageArrival', () => { + Logger.info(this.tag, 'imageArrival start') + this.mReceiver.readNextImage((err, image) => { + Logger.info(this.tag, 'readNextImage start') + if (err || image === undefined) { + Logger.error(this.tag, 'readNextImage failed ') + return + } + image.getComponent(4, (errMsg, img) => { + Logger.info(this.tag, 'getComponent start') + if (errMsg || img === undefined) { + Logger.info(this.tag, 'getComponent failed ') + return + } + // let buffer = new ArrayBuffer(2048) + let buffer + if (img.byteBuffer) { + buffer = img.byteBuffer + } else { + Logger.error(this.tag, 'img.byteBuffer is undefined') + } + this.savePicture(buffer, image) + }) + }) + }) + } catch { + Logger.info(this.tag, 'savePicture err') + } + } + + // 读取图像 + async savePicture(buffer: ArrayBuffer, img: image.Image) { + try { + Logger.info(this.tag, 'savePicture start') + let imgFileAsset = await this.mediaUtil.createAndGetUri(mediaLibrary.MediaType.IMAGE) + let imgPhotoUri = imgFileAsset.uri + Logger.info(this.tag, `photoUri = ${imgPhotoUri}`) + let imgFd = await this.mediaUtil.getFdPath(imgFileAsset) + Logger.info(this.tag, `fd = ${imgFd}`) + await fileio.write(imgFd, buffer) + await imgFileAsset.close(imgFd) + await img.release() + Logger.info(this.tag, 'save image End') + if (this.handleTakePicture) { + this.handleTakePicture(imgPhotoUri) + } + } catch (err) { + Logger.info(this.tag, 'savePicture err' + JSON.stringify(err.message)) + } + } + // 判断录像或者照片模式 async isVideoPhotoFn() { - await this.getVideoSurfaceID() - //this.mReceiver = image.createImageReceiver(640, 480, 2000, 8) // @ts-ignore //this.videoId = this.mReceiver.getReceivingSurfaceId(); if (this.modelBagCol == 'photo') { - cameraDemo.startPhotoOrVideo(this.modelBagCol, this.videoId) + if(this.mReceiver) {} else { + this.createImageReceiver() + } + + this.mSurfaceId = await this.mReceiver.getReceivingSurfaceId() + if(this.mSurfaceId) { + Logger.info(this.tag, `createImageReceiver mSurfaceId: ${this.mSurfaceId} `) + } else { + Logger.info(this.tag, `Get mSurfaceId failed `) + } + + cameraDemo.startPhotoOrVideo(this.modelBagCol, this.mSurfaceId) + + //cameraDemo.startPhotoOrVideo(this.modelBagCol, this.videoId) } else if (this.modelBagCol == 'video') { this.isModeBol = false if (this.timer) { clearInterval(this.timer) } // 开始录制 + await this.getVideoSurfaceID() cameraDemo.startPhotoOrVideo(this.modelBagCol, this.videoId) cameraDemo.videoOutputStart() this.videoRecorder.start() -- Gitee From ce08e8ae1dd828845b72f413ce415623b7bac411 Mon Sep 17 00:00:00 2001 From: lengye Date: Tue, 24 Oct 2023 14:02:19 +0000 Subject: [PATCH 2/3] fix codecheck Signed-off-by: lengye --- .../entry/src/main/cpp/camera_manager.cpp | 5 +---- .../camera_ndk_demo/entry/src/main/cpp/main.cpp | 17 +++++++---------- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/frameworks/native/camera/test/ndktest/camera_ndk_demo/entry/src/main/cpp/camera_manager.cpp b/frameworks/native/camera/test/ndktest/camera_ndk_demo/entry/src/main/cpp/camera_manager.cpp index a61a66c4e..dc92766a6 100644 --- a/frameworks/native/camera/test/ndktest/camera_ndk_demo/entry/src/main/cpp/camera_manager.cpp +++ b/frameworks/native/camera/test/ndktest/camera_ndk_demo/entry/src/main/cpp/camera_manager.cpp @@ -16,7 +16,7 @@ #include "camera_manager.h" #include -#define LOG_TAG "NDK DEMO" +#define LOG_TAG "DEMO:" #define LOG_DOMAIN 0x3200 NDKCamera* NDKCamera::ndkCamera_ = nullptr; std::mutex NDKCamera::mtx_; @@ -44,7 +44,6 @@ NDKCamera::NDKCamera(char* str) GetSupportedCameras(); GetSupportedOutputCapability(); CreatePreviewOutput(); - //CreatePhotoOutput(); CreateCameraInput(); CameraInputOpen(); SessionFlowFn(); @@ -397,9 +396,7 @@ Camera_ErrorCode NDKCamera::PhotoOutputRelease(void) Camera_ErrorCode NDKCamera::startVideo(char* videoId) { OH_LOG_ERROR(LOG_APP, "startVideo begin."); - Camera_ErrorCode ret = SessionStop(); - if (ret == CAMERA_OK) { OH_LOG_ERROR(LOG_APP, "SessionStop success."); } else { diff --git a/frameworks/native/camera/test/ndktest/camera_ndk_demo/entry/src/main/cpp/main.cpp b/frameworks/native/camera/test/ndktest/camera_ndk_demo/entry/src/main/cpp/main.cpp index 9c4aabe35..da9a9dbd7 100644 --- a/frameworks/native/camera/test/ndktest/camera_ndk_demo/entry/src/main/cpp/main.cpp +++ b/frameworks/native/camera/test/ndktest/camera_ndk_demo/entry/src/main/cpp/main.cpp @@ -19,7 +19,7 @@ #include "camera_manager.h" #include "hilog/log.h" -#define LOG_TAG "NDK DEMO" +#define LOG_TAG "DEMO:" #define LOG_DOMAIN 0x3200 static NDKCamera* ndkCamera_ = nullptr; @@ -77,10 +77,10 @@ static napi_value StartPhotoOrVideo(napi_env env, napi_callback_info info) napi_value result; size_t typeLen = 0; size_t videoIdLen = 0; - char * modeFlag = nullptr; - char * videoId = nullptr; + char* modeFlag = nullptr; + char* videoId = nullptr; - napi_get_cb_info(env, info, &argc, args , nullptr, nullptr); + napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); napi_get_value_string_utf8(env, args[0], nullptr, 0, &typeLen); modeFlag = new char[typeLen + 1]; @@ -90,16 +90,15 @@ static napi_value StartPhotoOrVideo(napi_env env, napi_callback_info info) videoId = new char[videoIdLen + 1]; napi_get_value_string_utf8(env, args[1], videoId, videoIdLen + 1, &videoIdLen); - if (!strcmp(modeFlag, "photo")){ + if (!strcmp(modeFlag, "photo")) + { OH_LOG_ERROR(LOG_APP, "StartPhoto surfaceId %{public}s", videoId); ndkCamera_->startPhoto(videoId); } else if (!strcmp(modeFlag, "video")) { ndkCamera_->startVideo(videoId); - OH_LOG_ERROR(LOG_APP, "StartPhotoOrVideo000 %{public}s",videoId); + OH_LOG_ERROR(LOG_APP, "StartPhotoOrVideo000 %{public}s", videoId); } - return result; - } static napi_value VideoOutputStart(napi_env env, napi_callback_info info) @@ -109,9 +108,7 @@ static napi_value VideoOutputStart(napi_env env, napi_callback_info info) size_t argc = 0; napi_value result; ndkCamera_->VideoOutputStart(); - return result; - } EXTERN_C_START static napi_value Init(napi_env env, napi_value exports) -- Gitee From 7d385007a3f50107ccdb965274f83d937194ee09 Mon Sep 17 00:00:00 2001 From: lengye Date: Tue, 24 Oct 2023 14:24:33 +0000 Subject: [PATCH 3/3] fix codecheck Signed-off-by: lengye --- .../camera_ndk_demo/entry/src/main/cpp/camera_manager.cpp | 3 ++- .../camera_ndk_demo/entry/src/main/cpp/camera_manager.h | 2 +- .../test/ndktest/camera_ndk_demo/entry/src/main/cpp/main.cpp | 3 +-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/frameworks/native/camera/test/ndktest/camera_ndk_demo/entry/src/main/cpp/camera_manager.cpp b/frameworks/native/camera/test/ndktest/camera_ndk_demo/entry/src/main/cpp/camera_manager.cpp index dc92766a6..6de1410ec 100644 --- a/frameworks/native/camera/test/ndktest/camera_ndk_demo/entry/src/main/cpp/camera_manager.cpp +++ b/frameworks/native/camera/test/ndktest/camera_ndk_demo/entry/src/main/cpp/camera_manager.cpp @@ -427,7 +427,8 @@ Camera_ErrorCode NDKCamera::VideoOutputStart() return ret; } -Camera_ErrorCode NDKCamera::startPhoto(char *mSurfaceId) { +Camera_ErrorCode NDKCamera::startPhoto(char *mSurfaceId) +{ Camera_ErrorCode ret = CAMERA_OK; if (takePictureTimes == 0) { ret = SessionStop(); diff --git a/frameworks/native/camera/test/ndktest/camera_ndk_demo/entry/src/main/cpp/camera_manager.h b/frameworks/native/camera/test/ndktest/camera_ndk_demo/entry/src/main/cpp/camera_manager.h index 411c24fd7..3c44ffefb 100644 --- a/frameworks/native/camera/test/ndktest/camera_ndk_demo/entry/src/main/cpp/camera_manager.h +++ b/frameworks/native/camera/test/ndktest/camera_ndk_demo/entry/src/main/cpp/camera_manager.h @@ -78,7 +78,7 @@ public: Camera_ErrorCode HasFlashFn(uint32_t mode); Camera_ErrorCode setZoomRatioFn(uint32_t zoomRatio); Camera_ErrorCode SessionFlowFn(); - Camera_ErrorCode SessionBegin(); + Camera_ErrorCode SessionBegin(); Camera_ErrorCode SessionCommitConfig(); Camera_ErrorCode SessionStart(); Camera_ErrorCode SessionStop(); diff --git a/frameworks/native/camera/test/ndktest/camera_ndk_demo/entry/src/main/cpp/main.cpp b/frameworks/native/camera/test/ndktest/camera_ndk_demo/entry/src/main/cpp/main.cpp index da9a9dbd7..b72992048 100644 --- a/frameworks/native/camera/test/ndktest/camera_ndk_demo/entry/src/main/cpp/main.cpp +++ b/frameworks/native/camera/test/ndktest/camera_ndk_demo/entry/src/main/cpp/main.cpp @@ -90,8 +90,7 @@ static napi_value StartPhotoOrVideo(napi_env env, napi_callback_info info) videoId = new char[videoIdLen + 1]; napi_get_value_string_utf8(env, args[1], videoId, videoIdLen + 1, &videoIdLen); - if (!strcmp(modeFlag, "photo")) - { + if (!strcmp(modeFlag, "photo")) { OH_LOG_ERROR(LOG_APP, "StartPhoto surfaceId %{public}s", videoId); ndkCamera_->startPhoto(videoId); } else if (!strcmp(modeFlag, "video")) { -- Gitee