diff --git a/cvimage.js b/cvimage.js index f0112a6..7811986 100644 --- a/cvimage.js +++ b/cvimage.js @@ -6,91 +6,81 @@ * @flow * @author Adam Freeman, adamgf@gmail.com */ -import { NativeModules } from 'react-native'; -import React, {Component} from 'react'; +import React, { Component } from 'react'; import { Platform, Image } from 'react-native'; -const { RNOpencv3 } = NativeModules; -import { ColorConv } from './constants'; +import RNOpencv3 from './spec/turbomodule/NativeRNOpencv3'; +import { downloadAssetSource } from './downloadAssetSource'; -var RNFS = require('react-native-fs') +var RNFS = require('react-native-fs'); export class CvImage extends Component { - constructor(props) { - super(props) - this.resolveAssetSource = require('react-native/Libraries/Image/resolveAssetSource') - this.state = { 'destFile' : '' } + super(props); + this.resolveAssetSource = require('react-native/Libraries/Image/resolveAssetSource'); + this.state = { destFile: '' }; } componentDidMount = () => { - const assetSource = this.props.source - const uri = this.resolveAssetSource(assetSource).uri - const downloadAssetSource = require('./downloadAssetSource'); - - downloadAssetSource(uri) - .then((sourceFile) => { - let srcMat - let dstMat - RNOpencv3.Mat().then((res) => { - dstMat = res - RNOpencv3.imageToMat(sourceFile).then((res) => { - srcMat = res - RNFS.unlink(sourceFile).then(() => { + const assetSource = this.props.source; + const uri = this.resolveAssetSource(assetSource).uri; - // replace srcMat and dstMat strings with actual srcMat and dstMat - const { cvinvoke } = this.props - if (cvinvoke) { - for (let i=0;i < cvinvoke.paramsArr.length;i++) { - let params = cvinvoke.paramsArr[i] - for (let j=0;j < Object.keys(params).length;j++) { - const pnum = 'p' + (j + 1).toString() - if (params[pnum] && params[pnum] === 'srcMat') { - params[pnum] = srcMat - } - if (params[pnum] && params[pnum] === 'dstMat') { - params[pnum] = dstMat + downloadAssetSource(uri) + .then(sourceFile => { + let srcMat; + let dstMat; + RNOpencv3.Mat().then(res => { + dstMat = res; + RNOpencv3.imageToMat(sourceFile).then(res => { + srcMat = res; + RNFS.unlink(sourceFile) + .then(() => { + // replace srcMat and dstMat strings with actual srcMat and dstMat + const { cvinvoke } = this.props; + if (cvinvoke) { + for (let i = 0; i < cvinvoke.paramsArr.length; i++) { + let params = cvinvoke.paramsArr[i]; + for (let j = 0; j < Object.keys(params).length; j++) { + const pnum = 'p' + (j + 1).toString(); + if (params[pnum] && params[pnum] === 'srcMat') { + params[pnum] = srcMat; + } + if (params[pnum] && params[pnum] === 'dstMat') { + params[pnum] = dstMat; + } + } } + //alert('cvinvoke is: ' + JSON.stringify(this.props.cvinvoke)) + RNOpencv3.invokeMethods(cvinvoke); } - } - //alert('cvinvoke is: ' + JSON.stringify(this.props.cvinvoke)) - RNOpencv3.invokeMethods(cvinvoke) - } - RNOpencv3.matToImage(dstMat, sourceFile) - .then((image) => { - RNOpencv3.deleteMat(srcMat) - RNOpencv3.deleteMat(dstMat) - const { width, height, uri } = image - if (uri && uri.length > 0) { - this.setState({ destFile : uri }) - } - else { - console.error('Error getting image information.') - } - }) - .catch((err) => { - console.error(err) - }) - }) - .catch((err) => { - console.error(err) - }) - }) + RNOpencv3.matToImage(dstMat, sourceFile) + .then(image => { + RNOpencv3.deleteMat(srcMat); + RNOpencv3.deleteMat(dstMat); + const { width, height, uri } = image; + if (uri && uri.length > 0) { + this.setState({ destFile: uri }); + } else { + console.warning('Error getting image information.'); + } + }) + .catch(err => { + console.warning(err); + }); + }) + .catch(err => { + console.warning(err); + }); + }); + }); }) - }) - .catch((err) => { - console.error(err) - }) - } + .catch(err => { + console.warning(err); + }); + }; render() { - let imageFilePath = this.resolveAssetSource(this.props.source).uri - if (this.state.destFile.length > 0) { - const prependFilename = Platform.OS === 'ios' ? '' : 'file://' - imageFilePath = prependFilename + this.state.destFile - } - return( - - ) + let imageFilePath = this.resolveAssetSource(this.props.source).uri; + return ; } } diff --git a/downloadAssetSource.js b/downloadAssetSource.js index 206b2bd..067095c 100644 --- a/downloadAssetSource.js +++ b/downloadAssetSource.js @@ -59,4 +59,4 @@ async function downloadAssetSource(uri) { }) } -module.exports = downloadAssetSource; \ No newline at end of file +export {downloadAssetSource} \ No newline at end of file diff --git a/harmony/opencv3/.gitignore b/harmony/opencv3/.gitignore new file mode 100644 index 0000000..e2713a2 --- /dev/null +++ b/harmony/opencv3/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/harmony/opencv3/Index.ets b/harmony/opencv3/Index.ets new file mode 100644 index 0000000..ad8d496 --- /dev/null +++ b/harmony/opencv3/Index.ets @@ -0,0 +1,2 @@ +export * from "./src/main/ets/RNOpencvPackage"; +export * from "./src/main/ets/CvCameraView" \ No newline at end of file diff --git a/harmony/opencv3/build-profile.json5 b/harmony/opencv3/build-profile.json5 new file mode 100644 index 0000000..d5f94ff --- /dev/null +++ b/harmony/opencv3/build-profile.json5 @@ -0,0 +1,13 @@ +{ + "apiType": "stageMode", + "buildOption": { + }, + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest" + } + ] +} diff --git a/harmony/opencv3/hvigorfile.ts b/harmony/opencv3/hvigorfile.ts new file mode 100644 index 0000000..4218707 --- /dev/null +++ b/harmony/opencv3/hvigorfile.ts @@ -0,0 +1,6 @@ +import { harTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: harTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/harmony/opencv3/oh-package.json5 b/harmony/opencv3/oh-package.json5 new file mode 100644 index 0000000..54d873d --- /dev/null +++ b/harmony/opencv3/oh-package.json5 @@ -0,0 +1,9 @@ +{ + "name": "@react-native-oh-tpl/react-native-opencv3", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "Index.ets", + "author": "", + "license": "Apache-2.0", + "dependencies": {} +} diff --git a/harmony/opencv3/src/main/cpp/CMakeLists.txt b/harmony/opencv3/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000..7fcbfe5 --- /dev/null +++ b/harmony/opencv3/src/main/cpp/CMakeLists.txt @@ -0,0 +1,16 @@ + +cmake_minimum_required(VERSION 3.13) +set(CMAKE_VERBOSE_MAKEFILE on) +set(rnoh_opencv_generated_dir "${CMAKE_CURRENT_SOURCE_DIR}/generated") + +file(GLOB rnoh_opencv_SRC CONFIGURE_DEPENDS *.cpp) +file(GLOB_RECURSE rnoh_opencv_generated_SRC "${rnoh_opencv_generated_dir}/**/*.cpp") + +add_library(rnoh_opencv SHARED ${rnoh_opencv_SRC} ${rnoh_opencv_generated_SRC}) +target_include_directories(rnoh_opencv PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR} + ${rnoh_opencv_generated_dir} + ${CMAKE_CURRENT_SOURCE_DIR}/generated/react/render/components/react_native_opencv3 +) + +target_link_libraries(rnoh_opencv PUBLIC rnoh) \ No newline at end of file diff --git a/harmony/opencv3/src/main/cpp/OpencvPackage.h b/harmony/opencv3/src/main/cpp/OpencvPackage.h new file mode 100644 index 0000000..7422cd1 --- /dev/null +++ b/harmony/opencv3/src/main/cpp/OpencvPackage.h @@ -0,0 +1,13 @@ +#pragma once + +#include "RNOH/generated/BaseReactNativeOpencv3Package.h" + +namespace rnoh { + +class OpencvPackage : public BaseReactNativeOpencv3Package { + using Super = BaseReactNativeOpencv3Package; + +public: + OpencvPackage(Package::Context ctx) : Super(ctx) {} +}; +} // namespace rnoh \ No newline at end of file diff --git a/harmony/opencv3/src/main/cpp/generated/RNOH/generated/BaseReactNativeOpencv3Package.h b/harmony/opencv3/src/main/cpp/generated/RNOH/generated/BaseReactNativeOpencv3Package.h new file mode 100644 index 0000000..ff22bb4 --- /dev/null +++ b/harmony/opencv3/src/main/cpp/generated/RNOH/generated/BaseReactNativeOpencv3Package.h @@ -0,0 +1,76 @@ +/** + * This code was generated by "react-native codegen-lib-harmony" + */ + +#pragma once + +#include +#include "RNOH/Package.h" +#include "RNOH/ArkTSTurboModule.h" +#include "RNOH/generated/turbo_modules/CvCameraModule.h" +#include "RNOH/generated/turbo_modules/RNOpencv3.h" +#include "RNOH/generated/components/CvCameraViewJSIBinder.h" + +namespace rnoh { + +class BaseReactNativeOpencv3PackageTurboModuleFactoryDelegate : public TurboModuleFactoryDelegate { + public: + SharedTurboModule createTurboModule(Context ctx, const std::string &name) const override { + if (name == "CvCameraModule") { + return std::make_shared(ctx, name); + } + if (name == "RNOpencv3") { + return std::make_shared(ctx, name); + } + return nullptr; + }; +}; + +class BaseReactNativeOpencv3PackageEventEmitRequestHandler : public EventEmitRequestHandler { + public: + void handleEvent(Context const &ctx) override { + auto eventEmitter = ctx.shadowViewRegistry->getEventEmitter(ctx.tag); + if (eventEmitter == nullptr) { + return; + } + + std::vector supportedEventNames = { + "facesDetectedCv", + "cameraFrame", + "frameSize", + }; + if (std::find(supportedEventNames.begin(), supportedEventNames.end(), ctx.eventName) != supportedEventNames.end()) { + eventEmitter->dispatchEvent(ctx.eventName, ArkJS(ctx.env).getDynamic(ctx.payload)); + } + } +}; + + +class BaseReactNativeOpencv3Package : public Package { + public: + BaseReactNativeOpencv3Package(Package::Context ctx) : Package(ctx){}; + + std::unique_ptr createTurboModuleFactoryDelegate() override { + return std::make_unique(); + } + + std::vector createComponentDescriptorProviders() override { + return { + facebook::react::concreteComponentDescriptorProvider(), + }; + } + + ComponentJSIBinderByString createComponentJSIBinderByName() override { + return { + {"CvCameraView", std::make_shared()}, + }; + }; + + EventEmitRequestHandlers createEventEmitRequestHandlers() override { + return { + std::make_shared(), + }; + } +}; + +} // namespace rnoh diff --git a/harmony/opencv3/src/main/cpp/generated/RNOH/generated/components/CvCameraViewJSIBinder.h b/harmony/opencv3/src/main/cpp/generated/RNOH/generated/components/CvCameraViewJSIBinder.h new file mode 100644 index 0000000..beb0bdd --- /dev/null +++ b/harmony/opencv3/src/main/cpp/generated/RNOH/generated/components/CvCameraViewJSIBinder.h @@ -0,0 +1,38 @@ +/** + * This code was generated by "react-native codegen-lib-harmony" + */ + +#pragma once +#include "RNOHCorePackage/ComponentBinders/ViewComponentJSIBinder.h" + +namespace rnoh { +class CvCameraViewJSIBinder : public ViewComponentJSIBinder { + protected: + facebook::jsi::Object createNativeProps(facebook::jsi::Runtime &rt) override { + auto object = ViewComponentJSIBinder::createNativeProps(rt); + object.setProperty(rt, "facing", true); + object.setProperty(rt, "cvinvoke", true); + object.setProperty(rt, "useStorage", true); + object.setProperty(rt, "faceClassifier", true); + object.setProperty(rt, "eyesClassifier", true); + object.setProperty(rt, "noseClassifier", true); + object.setProperty(rt, "mouthClassifier", true); + object.setProperty(rt, "landmarksModel", true); + object.setProperty(rt, "overlayInterval", true); + return object; + } + + facebook::jsi::Object createBubblingEventTypes(facebook::jsi::Runtime &rt) override { + facebook::jsi::Object events(rt); + return events; + } + + facebook::jsi::Object createDirectEventTypes(facebook::jsi::Runtime &rt) override { + facebook::jsi::Object events(rt); + events.setProperty(rt, "topFacesDetectedCv", createDirectEvent(rt, "onFacesDetectedCv")); + events.setProperty(rt, "topCameraFrame", createDirectEvent(rt, "onCameraFrame")); + events.setProperty(rt, "topFrameSize", createDirectEvent(rt, "onFrameSize")); + return events; + } +}; +} // namespace rnoh diff --git a/harmony/opencv3/src/main/cpp/generated/RNOH/generated/turbo_modules/CvCameraModule.cpp b/harmony/opencv3/src/main/cpp/generated/RNOH/generated/turbo_modules/CvCameraModule.cpp new file mode 100644 index 0000000..01c47df --- /dev/null +++ b/harmony/opencv3/src/main/cpp/generated/RNOH/generated/turbo_modules/CvCameraModule.cpp @@ -0,0 +1,19 @@ +/** + * This code was generated by "react-native codegen-lib-harmony" + */ + +#include "CvCameraModule.h" + +namespace rnoh { +using namespace facebook; + +CvCameraModule::CvCameraModule(const ArkTSTurboModule::Context ctx, const std::string name) : ArkTSTurboModule(ctx, name) { + methodMap_ = { + ARK_METHOD_METADATA(setOverlay, 2), + ARK_ASYNC_METHOD_METADATA(takePicture, 2), + ARK_METHOD_METADATA(startRecording, 2), + ARK_ASYNC_METHOD_METADATA(stopRecording, 1), + }; +} + +} // namespace rnoh diff --git a/harmony/opencv3/src/main/cpp/generated/RNOH/generated/turbo_modules/CvCameraModule.h b/harmony/opencv3/src/main/cpp/generated/RNOH/generated/turbo_modules/CvCameraModule.h new file mode 100644 index 0000000..b087f21 --- /dev/null +++ b/harmony/opencv3/src/main/cpp/generated/RNOH/generated/turbo_modules/CvCameraModule.h @@ -0,0 +1,16 @@ +/** + * This code was generated by "react-native codegen-lib-harmony" + */ + +#pragma once + +#include "RNOH/ArkTSTurboModule.h" + +namespace rnoh { + +class JSI_EXPORT CvCameraModule : public ArkTSTurboModule { + public: + CvCameraModule(const ArkTSTurboModule::Context ctx, const std::string name); +}; + +} // namespace rnoh diff --git a/harmony/opencv3/src/main/cpp/generated/RNOH/generated/turbo_modules/RNOpencv3.cpp b/harmony/opencv3/src/main/cpp/generated/RNOH/generated/turbo_modules/RNOpencv3.cpp new file mode 100644 index 0000000..8841474 --- /dev/null +++ b/harmony/opencv3/src/main/cpp/generated/RNOH/generated/turbo_modules/RNOpencv3.cpp @@ -0,0 +1,34 @@ +/** + * This code was generated by "react-native codegen-lib-harmony" + */ + +#include "RNOpencv3.h" + +namespace rnoh { +using namespace facebook; + +RNOpencv3::RNOpencv3(const ArkTSTurboModule::Context ctx, const std::string name) : ArkTSTurboModule(ctx, name) { + methodMap_ = { + ARK_METHOD_METADATA(drawLine, 5), + ARK_METHOD_METADATA(cvtColor, 3), + ARK_ASYNC_METHOD_METADATA(imageToMat, 1), + ARK_ASYNC_METHOD_METADATA(matToImage, 2), + ARK_METHOD_METADATA(invokeMethods, 1), + ARK_METHOD_METADATA(invokeMethodWithCallback, 5), + ARK_METHOD_METADATA(invokeMethod, 2), + ARK_METHOD_METADATA(invokeInOutMethod, 4), + ARK_ASYNC_METHOD_METADATA(MatWithScalar, 4), + ARK_ASYNC_METHOD_METADATA(MatWithParams, 3), + ARK_ASYNC_METHOD_METADATA(Mat, 0), + ARK_ASYNC_METHOD_METADATA(getMatData, 3), + ARK_METHOD_METADATA(setTo, 2), + ARK_METHOD_METADATA(put, 4), + ARK_METHOD_METADATA(transpose, 1), + ARK_METHOD_METADATA(deleteMat, 1), + ARK_METHOD_METADATA(deleteMats, 0), + ARK_ASYNC_METHOD_METADATA(MatOfInt, 2), + ARK_ASYNC_METHOD_METADATA(MatOfFloat, 2), + }; +} + +} // namespace rnoh diff --git a/harmony/opencv3/src/main/cpp/generated/RNOH/generated/turbo_modules/RNOpencv3.h b/harmony/opencv3/src/main/cpp/generated/RNOH/generated/turbo_modules/RNOpencv3.h new file mode 100644 index 0000000..051bbdd --- /dev/null +++ b/harmony/opencv3/src/main/cpp/generated/RNOH/generated/turbo_modules/RNOpencv3.h @@ -0,0 +1,16 @@ +/** + * This code was generated by "react-native codegen-lib-harmony" + */ + +#pragma once + +#include "RNOH/ArkTSTurboModule.h" + +namespace rnoh { + +class JSI_EXPORT RNOpencv3 : public ArkTSTurboModule { + public: + RNOpencv3(const ArkTSTurboModule::Context ctx, const std::string name); +}; + +} // namespace rnoh diff --git a/harmony/opencv3/src/main/cpp/generated/react/renderer/components/react_native_opencv3/ComponentDescriptors.h b/harmony/opencv3/src/main/cpp/generated/react/renderer/components/react_native_opencv3/ComponentDescriptors.h new file mode 100644 index 0000000..93d9a45 --- /dev/null +++ b/harmony/opencv3/src/main/cpp/generated/react/renderer/components/react_native_opencv3/ComponentDescriptors.h @@ -0,0 +1,22 @@ + +/** + * This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen). + * + * Do not edit this file as changes may cause incorrect behavior and will be lost + * once the code is regenerated. + * + * @generated by codegen project: GenerateComponentDescriptorH.js + */ + +#pragma once + +#include +#include + +namespace facebook { +namespace react { + +using CvCameraViewComponentDescriptor = ConcreteComponentDescriptor; + +} // namespace react +} // namespace facebook diff --git a/harmony/opencv3/src/main/cpp/generated/react/renderer/components/react_native_opencv3/EventEmitters.cpp b/harmony/opencv3/src/main/cpp/generated/react/renderer/components/react_native_opencv3/EventEmitters.cpp new file mode 100644 index 0000000..15457fa --- /dev/null +++ b/harmony/opencv3/src/main/cpp/generated/react/renderer/components/react_native_opencv3/EventEmitters.cpp @@ -0,0 +1,44 @@ + +/** + * This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen). + * + * Do not edit this file as changes may cause incorrect behavior and will be lost + * once the code is regenerated. + * + * @generated by codegen project: GenerateEventEmitterCpp.js + */ + +#include + + +namespace facebook { +namespace react { + +void CvCameraViewEventEmitter::onFacesDetectedCv(OnFacesDetectedCv $event) const { + dispatchEvent("facesDetectedCv", [](jsi::Runtime &runtime) { + auto $payload = jsi::Object(runtime); + + return $payload; + }); +} + + +void CvCameraViewEventEmitter::onCameraFrame(OnCameraFrame $event) const { + dispatchEvent("cameraFrame", [](jsi::Runtime &runtime) { + auto $payload = jsi::Object(runtime); + + return $payload; + }); +} + + +void CvCameraViewEventEmitter::onFrameSize(OnFrameSize $event) const { + dispatchEvent("frameSize", [](jsi::Runtime &runtime) { + auto $payload = jsi::Object(runtime); + + return $payload; + }); +} + +} // namespace react +} // namespace facebook diff --git a/harmony/opencv3/src/main/cpp/generated/react/renderer/components/react_native_opencv3/EventEmitters.h b/harmony/opencv3/src/main/cpp/generated/react/renderer/components/react_native_opencv3/EventEmitters.h new file mode 100644 index 0000000..f054a02 --- /dev/null +++ b/harmony/opencv3/src/main/cpp/generated/react/renderer/components/react_native_opencv3/EventEmitters.h @@ -0,0 +1,39 @@ + +/** + * This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen). + * + * Do not edit this file as changes may cause incorrect behavior and will be lost + * once the code is regenerated. + * + * @generated by codegen project: GenerateEventEmitterH.js + */ +#pragma once + +#include + + +namespace facebook { +namespace react { +class CvCameraViewEventEmitter : public ViewEventEmitter { + public: + using ViewEventEmitter::ViewEventEmitter; + + struct OnFacesDetectedCv { + + }; + + struct OnCameraFrame { + + }; + + struct OnFrameSize { + + }; + void onFacesDetectedCv(OnFacesDetectedCv value) const; + + void onCameraFrame(OnCameraFrame value) const; + + void onFrameSize(OnFrameSize value) const; +}; +} // namespace react +} // namespace facebook diff --git a/harmony/opencv3/src/main/cpp/generated/react/renderer/components/react_native_opencv3/Props.cpp b/harmony/opencv3/src/main/cpp/generated/react/renderer/components/react_native_opencv3/Props.cpp new file mode 100644 index 0000000..ebc9d44 --- /dev/null +++ b/harmony/opencv3/src/main/cpp/generated/react/renderer/components/react_native_opencv3/Props.cpp @@ -0,0 +1,35 @@ + +/** + * This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen). + * + * Do not edit this file as changes may cause incorrect behavior and will be lost + * once the code is regenerated. + * + * @generated by codegen project: GeneratePropsCpp.js + */ + +#include +#include +#include + +namespace facebook { +namespace react { + +CvCameraViewProps::CvCameraViewProps( + const PropsParserContext &context, + const CvCameraViewProps &sourceProps, + const RawProps &rawProps): ViewProps(context, sourceProps, rawProps), + + facing(convertRawProp(context, rawProps, "facing", sourceProps.facing, {CvCameraViewFacing::Front})), + cvinvoke(convertRawProp(context, rawProps, "cvinvoke", sourceProps.cvinvoke, {})), + useStorage(convertRawProp(context, rawProps, "useStorage", sourceProps.useStorage, {false})), + faceClassifier(convertRawProp(context, rawProps, "faceClassifier", sourceProps.faceClassifier, {})), + eyesClassifier(convertRawProp(context, rawProps, "eyesClassifier", sourceProps.eyesClassifier, {})), + noseClassifier(convertRawProp(context, rawProps, "noseClassifier", sourceProps.noseClassifier, {})), + mouthClassifier(convertRawProp(context, rawProps, "mouthClassifier", sourceProps.mouthClassifier, {})), + landmarksModel(convertRawProp(context, rawProps, "landmarksModel", sourceProps.landmarksModel, {})), + overlayInterval(convertRawProp(context, rawProps, "overlayInterval", sourceProps.overlayInterval, {0})) + {} + +} // namespace react +} // namespace facebook diff --git a/harmony/opencv3/src/main/cpp/generated/react/renderer/components/react_native_opencv3/Props.h b/harmony/opencv3/src/main/cpp/generated/react/renderer/components/react_native_opencv3/Props.h new file mode 100644 index 0000000..0524733 --- /dev/null +++ b/harmony/opencv3/src/main/cpp/generated/react/renderer/components/react_native_opencv3/Props.h @@ -0,0 +1,53 @@ + +/** + * This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen). + * + * Do not edit this file as changes may cause incorrect behavior and will be lost + * once the code is regenerated. + * + * @generated by codegen project: GeneratePropsH.js + */ +#pragma once + +#include +#include + +namespace facebook { +namespace react { + +enum class CvCameraViewFacing { Front, Back }; + +static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, CvCameraViewFacing &result) { + auto string = (std::string)value; + if (string == "front") { result = CvCameraViewFacing::Front; return; } + if (string == "back") { result = CvCameraViewFacing::Back; return; } + abort(); +} + +static inline std::string toString(const CvCameraViewFacing &value) { + switch (value) { + case CvCameraViewFacing::Front: return "front"; + case CvCameraViewFacing::Back: return "back"; + } +} + +class CvCameraViewProps final : public ViewProps { + public: + CvCameraViewProps() = default; + CvCameraViewProps(const PropsParserContext& context, const CvCameraViewProps &sourceProps, const RawProps &rawProps); + +#pragma mark - Props + + CvCameraViewFacing facing{CvCameraViewFacing::Front}; + std::string cvinvoke{}; + bool useStorage{false}; + std::string faceClassifier{}; + std::string eyesClassifier{}; + std::string noseClassifier{}; + std::string mouthClassifier{}; + std::string landmarksModel{}; + int overlayInterval{0}; +}; + +} // namespace react +} // namespace facebook diff --git a/harmony/opencv3/src/main/cpp/generated/react/renderer/components/react_native_opencv3/ShadowNodes.cpp b/harmony/opencv3/src/main/cpp/generated/react/renderer/components/react_native_opencv3/ShadowNodes.cpp new file mode 100644 index 0000000..5f5a6dc --- /dev/null +++ b/harmony/opencv3/src/main/cpp/generated/react/renderer/components/react_native_opencv3/ShadowNodes.cpp @@ -0,0 +1,19 @@ + +/** + * This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen). + * + * Do not edit this file as changes may cause incorrect behavior and will be lost + * once the code is regenerated. + * + * @generated by codegen project: GenerateShadowNodeCpp.js + */ + +#include + +namespace facebook { +namespace react { + +extern const char CvCameraViewComponentName[] = "CvCameraView"; + +} // namespace react +} // namespace facebook diff --git a/harmony/opencv3/src/main/cpp/generated/react/renderer/components/react_native_opencv3/ShadowNodes.h b/harmony/opencv3/src/main/cpp/generated/react/renderer/components/react_native_opencv3/ShadowNodes.h new file mode 100644 index 0000000..7b2f104 --- /dev/null +++ b/harmony/opencv3/src/main/cpp/generated/react/renderer/components/react_native_opencv3/ShadowNodes.h @@ -0,0 +1,34 @@ + +/** + * This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen). + * + * Do not edit this file as changes may cause incorrect behavior and will be lost + * once the code is regenerated. + * + * @generated by codegen project: GenerateShadowNodeH.js + */ + +#pragma once + +#include +#include +#include +#include +#include + +namespace facebook { +namespace react { + +JSI_EXPORT extern const char CvCameraViewComponentName[]; + +/* + * `ShadowNode` for component. + */ +using CvCameraViewShadowNode = ConcreteViewShadowNode< + CvCameraViewComponentName, + CvCameraViewProps, + CvCameraViewEventEmitter, + CvCameraViewState>; + +} // namespace react +} // namespace facebook diff --git a/harmony/opencv3/src/main/cpp/generated/react/renderer/components/react_native_opencv3/States.cpp b/harmony/opencv3/src/main/cpp/generated/react/renderer/components/react_native_opencv3/States.cpp new file mode 100644 index 0000000..cce7144 --- /dev/null +++ b/harmony/opencv3/src/main/cpp/generated/react/renderer/components/react_native_opencv3/States.cpp @@ -0,0 +1,18 @@ + +/** + * This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen). + * + * Do not edit this file as changes may cause incorrect behavior and will be lost + * once the code is regenerated. + * + * @generated by codegen project: GenerateStateCpp.js + */ +#include + +namespace facebook { +namespace react { + + + +} // namespace react +} // namespace facebook diff --git a/harmony/opencv3/src/main/cpp/generated/react/renderer/components/react_native_opencv3/States.h b/harmony/opencv3/src/main/cpp/generated/react/renderer/components/react_native_opencv3/States.h new file mode 100644 index 0000000..ca3552b --- /dev/null +++ b/harmony/opencv3/src/main/cpp/generated/react/renderer/components/react_native_opencv3/States.h @@ -0,0 +1,36 @@ +/** + * This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen). + * + * Do not edit this file as changes may cause incorrect behavior and will be lost + * once the code is regenerated. + * + * @generated by codegen project: GenerateStateH.js + */ +#pragma once + +#ifdef ANDROID +#include +#include +#include +#endif + +namespace facebook { +namespace react { + +class CvCameraViewState { +public: + CvCameraViewState() = default; + +#ifdef ANDROID + CvCameraViewState(CvCameraViewState const &previousState, folly::dynamic data){}; + folly::dynamic getDynamic() const { + return {}; + }; + MapBuffer getMapBuffer() const { + return MapBufferBuilder::EMPTY(); + }; +#endif +}; + +} // namespace react +} // namespace facebook \ No newline at end of file diff --git a/harmony/opencv3/src/main/ets/CvCameraTurboModule.ts b/harmony/opencv3/src/main/ets/CvCameraTurboModule.ts new file mode 100644 index 0000000..a440f44 --- /dev/null +++ b/harmony/opencv3/src/main/ets/CvCameraTurboModule.ts @@ -0,0 +1,26 @@ +import { TurboModule } from '@rnoh/react-native-openharmony/ts'; +import { TM } from "./generated/ts" + +export class CvCameraModule extends TurboModule implements TM.CvCameraModule.Spec { + private logger = this.ctx.logger.clone("CvCameraViewModule") + + setOverlay(options: { overlayMat: number; }, viewHandle: number): void { + this.logger.info('setOverlay is been invoked') + return + } + + takePicture(options: { filename: string; }, viewHandle: number): Promise { + this.logger.info('takePicture is been invoked') + return Promise.resolve('str') + } + + startRecording(options: { filename: string; }, viewHandle: number): void { + this.logger.info('startRecording is been invoked') + return + } + + stopRecording(viewHandle: number): Promise { + this.logger.info('stopRecording is been invoked') + return Promise.resolve() + } +} \ No newline at end of file diff --git a/harmony/opencv3/src/main/ets/CvCameraView.ets b/harmony/opencv3/src/main/ets/CvCameraView.ets new file mode 100644 index 0000000..2f055b5 --- /dev/null +++ b/harmony/opencv3/src/main/ets/CvCameraView.ets @@ -0,0 +1,81 @@ +import { RNComponentContext, RNViewBase } from '@rnoh/react-native-openharmony'; +import { RNC } from "./generated/ts" + +@Component +export struct CvCameraView { + public static readonly NAME = RNC.CvCameraView.NAME + public ctx!: RNComponentContext + public tag: number = 0 + // 组件的状态包装器 + @State private descriptorWrapper: RNC.CvCameraView.DescriptorWrapper = + {} as RNC.CvCameraView.DescriptorWrapper + private eventEmitter: RNC.CvCameraView.EventEmitter | undefined = undefined + // 存储需要在组件卸载时清理的回调函数 + private cleanUpCallbacks: (() => void)[] = [] + + aboutToAppear() { + // 创建EventEmitter实例,用于处理事件的订阅和触发 + this.eventEmitter = new RNC.CvCameraView.EventEmitter(this.ctx.rnInstance, this.tag) + // 处理组件初始状态 + this.executeCommand() + this.onDescriptorWrapperChange(this.ctx.descriptorRegistry.findDescriptorWrapperByTag(this.tag)!) + // 订阅组件状态的变化,然后放入cleanUpCallbacks等待清理 + this.cleanUpCallbacks.push(this.ctx.descriptorRegistry.subscribeToDescriptorChanges(this.tag, + (_descriptor, newDescriptorWrapper) => { + // 更新组件状态 + this.onDescriptorWrapperChange(newDescriptorWrapper! as RNC.CvCameraView.DescriptorWrapper) + } + )) + } + + executeCommand() { + this.cleanUpCallbacks.push(this.ctx.componentCommandReceiver.registerCommandCallback( + this.tag, + (command: string, args: string[]) => this.registerCommandCallback(command, args))) + } + + registerCommandCallback(commandName: string, args: string[]) { + switch (commandName) { + case 'setOverlay': + console.log('setOverlay') + break + case 'takePicture': + console.log('takePicture') + break + case 'startRecording': + console.log('startRecording') + break + case 'stopRecording': + console.log('stopRecording') + break + default: + break + } + } + + // 更新组件状态 + private onDescriptorWrapperChange(descriptorWrapper: RNC.CvCameraView.DescriptorWrapper) { + this.descriptorWrapper = descriptorWrapper + } + + aboutToDisappear() { + // 执行所有清理回调 + this.cleanUpCallbacks.forEach(cb => cb()) + } + + build() { + RNViewBase({ ctx: this.ctx, tag: this.tag }) { + Column() { + Text(`facing is ${this.descriptorWrapper.props.facing}`).fontSize(16) + Text(`cvinvoke is ${JSON.stringify(this.descriptorWrapper.props.cvinvoke)}`).fontSize(16) + Text(`useStorage is ${this.descriptorWrapper.props.useStorage}`).fontSize(16) + Text(`faceClassifier is ${this.descriptorWrapper.props.faceClassifier}`).fontSize(16) + Text(`eyesClassifier is ${this.descriptorWrapper.props.eyesClassifier}`).fontSize(16) + Text(`noseClassifier is ${this.descriptorWrapper.props.noseClassifier}`).fontSize(16) + Text(`mouthClassifier is ${this.descriptorWrapper.props.mouthClassifier}`).fontSize(16) + Text(`landmarksModel is ${this.descriptorWrapper.props.landmarksModel}`).fontSize(16) + Text(`overlayInterval is ${this.descriptorWrapper.props.overlayInterval}`).fontSize(16) + }.margin({ top: 30, left: 0 }).width('100%').alignItems(HorizontalAlign.Start) + } + } +} diff --git a/harmony/opencv3/src/main/ets/RNOpencvPackage.ts b/harmony/opencv3/src/main/ets/RNOpencvPackage.ts new file mode 100644 index 0000000..3fef51c --- /dev/null +++ b/harmony/opencv3/src/main/ets/RNOpencvPackage.ts @@ -0,0 +1,44 @@ +import { RNPackage, TurboModulesFactory } from '@rnoh/react-native-openharmony/ts'; +import type { + TurboModule, + TurboModuleContext, + DescriptorWrapperFactoryByDescriptorTypeCtx, + DescriptorWrapperFactoryByDescriptorType +} from '@rnoh/react-native-openharmony/ts'; +import { RNOpencvTurboModule } from './RNOpencvTurboModule'; +import { CvCameraModule } from './CvCameraTurboModule'; +import { TM, RNC } from "./generated/ts" + +class RNOpencvTurboModuleFactory extends TurboModulesFactory { + createTurboModule(name: string): TurboModule | null { + if (name === TM.RNOpencv3.NAME) { + globalThis.uiAbilityContext = this.ctx.uiAbilityContext; + return new RNOpencvTurboModule(this.ctx); + } + if (name === TM.CvCameraModule.NAME) { + globalThis.uiAbilityContext = this.ctx.uiAbilityContext; + return new CvCameraModule(this.ctx); + } + return null; + } + + hasTurboModule(name: string): boolean { + return name === TM.RNOpencv3.NAME || name === TM.CvCameraModule.NAME; + } +} + +export class RNOpencvPackage extends RNPackage { + createTurboModulesFactory(ctx: TurboModuleContext): TurboModulesFactory { + return new RNOpencvTurboModuleFactory(ctx); + } + + createDescriptorWrapperFactoryByDescriptorType( + ctx: DescriptorWrapperFactoryByDescriptorTypeCtx + ): DescriptorWrapperFactoryByDescriptorType { + + return { + [RNC.CvCameraView.NAME]: (ctx) => + new RNC.CvCameraView.DescriptorWrapper(ctx.descriptor), + }; + } +} \ No newline at end of file diff --git a/harmony/opencv3/src/main/ets/RNOpencvTurboModule.ts b/harmony/opencv3/src/main/ets/RNOpencvTurboModule.ts new file mode 100644 index 0000000..9bc09af --- /dev/null +++ b/harmony/opencv3/src/main/ets/RNOpencvTurboModule.ts @@ -0,0 +1,102 @@ +import { TurboModule } from '@rnoh/react-native-openharmony/ts'; +import { TM } from "./generated/ts" + +export class RNOpencvTurboModule extends TurboModule implements TM.RNOpencv3.Spec { + private logger = this.ctx.logger.clone("RNOpencvTurboModule") + drawLine(inMat: TM.RNOpencv3.Mat, pt1: TM.RNOpencv3.CvPoint, pt2: TM.RNOpencv3.CvPoint, + scalarVal: TM.RNOpencv3.CvScalar, thickness: number): void { + this.logger.info('drawLine is been invoked') + return + } + + cvtColor(sourceMat: TM.RNOpencv3.Mat, destMat: TM.RNOpencv3.Mat, convColorCode: number): void { + this.logger.info('cvtColor is been invoked') + } + + imageToMat(inPath: string): Promise { + this.logger.info('imageToMat is been invoked') + return Promise.resolve('str') + } + + matToImage(srcMat: TM.RNOpencv3.Mat, outPath: string): Promise { + this.logger.info('matToImage is been invoked') + return Promise.resolve('str') + } + + invokeMethods(cvInvokeMap: TM.RNOpencv3.CvInvokeMap): void { + this.logger.info('invokeMethods is been invoked') + return + } + + invokeMethodWithCallback(ins: string, func: string, params: string, out: string, callback: string): void { + this.logger.info('invokeMethodWithCallback is been invoked') + return + } + + invokeMethod(func: string, params: string): void { + this.logger.info('invokeMethod is been invoked') + return + } + + invokeInOutMethod(ins: string, func: string, params: string, out: string): void { + this.logger.info('invokeInOutMethod is been invoked') + return + } + + MatWithScalar(rows: number, cols: number, cvtype: number, + scalarMap: TM.RNOpencv3.CvScalar): Promise { + this.logger.info('MatWithScalar is been invoked') + return Promise.resolve({matIndex: 1, rows: 1, cols: 1, CvType: 1}) + } + + MatWithParams(rows: number, cols: number, cvtype: number): Promise { + this.logger.info('MatWithParams is been invoked') + return Promise.resolve({matIndex: 1, rows: 1, cols: 1, CvType: 1}) + } + + Mat(): Promise { + this.logger.info('Mat is been invoked') + return Promise.resolve({matIndex: 1, rows: 1, cols: 1, CvType: 1}) + } + + getMatData(mat: TM.RNOpencv3.Mat, rownum: number, colnum: number): Promise { + this.logger.info('getMatData is been invoked') + return Promise.resolve('str') + } + + setTo(mat: TM.RNOpencv3.Mat, cvscalar: TM.RNOpencv3.CvScalar): void { + this.logger.info('setTo is been invoked') + return + } + + put(mat: TM.RNOpencv3.Mat, rownum: number, colnum: number, data: string[]): void { + this.logger.info('put is been invoked') + return + } + + transpose(mat: TM.RNOpencv3.Mat): void { + this.logger.info('transpose is been invoked') + return + } + + deleteMat(mat: TM.RNOpencv3.Mat): void { + this.logger.info('deleteMat is been invoked') + return + } + + deleteMats(): void { + this.logger.info('deleteMats is been invoked') + return + } + + MatOfInt(lomatint: number, himatint: number): Promise { + this.logger.info('MatOfInt is been invoked') + return Promise.resolve({matIndex: 1, rows: 1, cols: 1, CvType: 1}) + } + + MatOfFloat(lomatfloat: number, himatfloat: number): Promise { + this.logger.info('MatOfFloat is been invoked') + return Promise.resolve({matIndex: 1, rows: 1, cols: 1, CvType: 1}) + } + +} \ No newline at end of file diff --git a/harmony/opencv3/src/main/ets/generated/components/CvCameraView.ts b/harmony/opencv3/src/main/ets/generated/components/CvCameraView.ts new file mode 100644 index 0000000..5d1bb63 --- /dev/null +++ b/harmony/opencv3/src/main/ets/generated/components/CvCameraView.ts @@ -0,0 +1,156 @@ +/** + * This code was generated by "react-native codegen-lib-harmony" + */ + +import { + Descriptor as ComponentDescriptor, + ViewBaseProps, + ViewRawProps, + ViewDescriptorWrapperBase, + ColorValue, + Color, + RNInstance, + Tag, + RNComponentCommandReceiver, + ViewPropsSelector, +} from '@rnoh/react-native-openharmony/ts'; + + +export namespace CvCameraView { + export const NAME = "CvCameraView" as const + + export interface DirectRawProps { + facing?: 'front' | 'back'; + cvinvoke: string; + useStorage?: boolean; + faceClassifier?: string; + eyesClassifier?: string; + noseClassifier?: string; + mouthClassifier?: string; + landmarksModel?: string; + overlayInterval?: number; + } + + export interface Props extends ViewBaseProps {} + + export interface State {} + + export interface RawProps extends ViewRawProps, DirectRawProps {} + + export class PropsSelector extends ViewPropsSelector { + get facing() { + return this.rawProps.facing ?? 'front'; + } + + get cvinvoke() { + return this.rawProps.cvinvoke; + } + + get useStorage() { + return this.rawProps.useStorage ?? false; + } + + get faceClassifier() { + return this.rawProps.faceClassifier; + } + + get eyesClassifier() { + return this.rawProps.eyesClassifier; + } + + get noseClassifier() { + return this.rawProps.noseClassifier; + } + + get mouthClassifier() { + return this.rawProps.mouthClassifier; + } + + get landmarksModel() { + return this.rawProps.landmarksModel; + } + + get overlayInterval() { + return this.rawProps.overlayInterval ?? 0; + } + + + } + + export type Descriptor = ComponentDescriptor< + typeof NAME, + Props, + State, + RawProps + >; + + export class DescriptorWrapper extends ViewDescriptorWrapperBase< + typeof NAME, + Props, + State, + RawProps, + PropsSelector + > { + protected createPropsSelector() { + return new PropsSelector(this.descriptor.props, this.descriptor.rawProps) + } + } + + export interface EventPayloadByName { + "facesDetectedCv": {} + "cameraFrame": {} + "frameSize": {} + } + + export class EventEmitter { + constructor(private rnInstance: RNInstance, private tag: Tag) {} + + emit(eventName: TEventName, payload: EventPayloadByName[TEventName]) { + this.rnInstance.emitComponentEvent(this.tag, eventName, payload) + } + } + + export interface CommandArgvByName { + "setOverlay": [] + "takePicture": [] + "startRecording": [] + "stopRecording": [] + } + + export class CommandReceiver { + private listenersByCommandName = new Map void>>() + private cleanUp: (() => void) | undefined = undefined + + constructor(private componentCommandReceiver: RNComponentCommandReceiver, private tag: Tag) { + } + + subscribe(commandName: TCommandName, listener: (argv: CommandArgvByName[TCommandName]) => void) { + if (!this.listenersByCommandName.has(commandName)) { + this.listenersByCommandName.set(commandName, new Set()) + } + this.listenersByCommandName.get(commandName)!.add(listener) + const hasRegisteredCommandReceiver = !!this.cleanUp + if (!hasRegisteredCommandReceiver) { + this.cleanUp = this.componentCommandReceiver.registerCommandCallback(this.tag, (commandName: string, argv: any[]) => { + if (this.listenersByCommandName.has(commandName)) { + const listeners = this.listenersByCommandName.get(commandName)! + listeners.forEach(listener => { + listener(argv) + }) + } + }) + } + + return () => { + this.listenersByCommandName.get(commandName)?.delete(listener) + if (this.listenersByCommandName.get(commandName)?.size ?? 0 === 0) { + this.listenersByCommandName.delete(commandName) + } + if (this.listenersByCommandName.size === 0) { + this.cleanUp?.() + } + } + } + } + +} diff --git a/harmony/opencv3/src/main/ets/generated/components/ts.ts b/harmony/opencv3/src/main/ets/generated/components/ts.ts new file mode 100644 index 0000000..2d7e0c4 --- /dev/null +++ b/harmony/opencv3/src/main/ets/generated/components/ts.ts @@ -0,0 +1,5 @@ +/** + * This code was generated by "react-native codegen-lib-harmony" + */ + +export * from "./CvCameraView" diff --git a/harmony/opencv3/src/main/ets/generated/index.ets b/harmony/opencv3/src/main/ets/generated/index.ets new file mode 100644 index 0000000..041b7ed --- /dev/null +++ b/harmony/opencv3/src/main/ets/generated/index.ets @@ -0,0 +1,5 @@ +/** + * This code was generated by "react-native codegen-lib-harmony" + */ + +export * from "./ts" diff --git a/harmony/opencv3/src/main/ets/generated/ts.ts b/harmony/opencv3/src/main/ets/generated/ts.ts new file mode 100644 index 0000000..4c568a8 --- /dev/null +++ b/harmony/opencv3/src/main/ets/generated/ts.ts @@ -0,0 +1,6 @@ +/** + * This code was generated by "react-native codegen-lib-harmony" + */ + +export * as RNC from "./components/ts" +export * as TM from "./turboModules/ts" diff --git a/harmony/opencv3/src/main/ets/generated/turboModules/CvCameraModule.ts b/harmony/opencv3/src/main/ets/generated/turboModules/CvCameraModule.ts new file mode 100644 index 0000000..dff8b91 --- /dev/null +++ b/harmony/opencv3/src/main/ets/generated/turboModules/CvCameraModule.ts @@ -0,0 +1,20 @@ +/** + * This code was generated by "react-native codegen-lib-harmony" + */ + +import { Tag } from "@rnoh/react-native-openharmony/ts" + +export namespace CvCameraModule { + export const NAME = 'CvCameraModule' as const + + export interface Spec { + setOverlay(options: {overlayMat: number}, viewHandle: number): void; + + takePicture(options: {filename: string}, viewHandle: number): Promise; + + startRecording(options: {filename: string}, viewHandle: number): void; + + stopRecording(viewHandle: number): Promise; + + } +} diff --git a/harmony/opencv3/src/main/ets/generated/turboModules/RNOpencv3.ts b/harmony/opencv3/src/main/ets/generated/turboModules/RNOpencv3.ts new file mode 100644 index 0000000..bbdcb25 --- /dev/null +++ b/harmony/opencv3/src/main/ets/generated/turboModules/RNOpencv3.ts @@ -0,0 +1,58 @@ +/** + * This code was generated by "react-native codegen-lib-harmony" + */ + +import { Tag } from "@rnoh/react-native-openharmony/ts" + +export namespace RNOpencv3 { + export const NAME = 'RNOpencv3' as const + + export type Mat = {matIndex: number, rows: number, cols: number, CvType?: number} + + export type CvPoint = {x: number, y: number} + + export type CvScalar = {value: number} + + export type CvInvokeMap = {ins: string[], functions: string[], paramsArr: string[], outs: string[], callbacks: string[], groupids: string[]} + + export interface Spec { + drawLine(inMat: Mat, pt1: CvPoint, pt2: CvPoint, scalarVal: CvScalar, thickness: number): void; + + cvtColor(sourceMat: Mat, destMat: Mat, convColorCode: number): void; + + imageToMat(inPath: string): Promise; + + matToImage(srcMat: Mat, outPath: string): Promise; + + invokeMethods(cvInvokeMap: CvInvokeMap): void; + + invokeMethodWithCallback(ins: string, func: string, params: string, out: string, callback: string): void; + + invokeMethod(func: string, params: string): void; + + invokeInOutMethod(ins: string, func: string, params: string, out: string): void; + + MatWithScalar(rows: number, cols: number, cvtype: number, scalarMap: CvScalar): Promise; + + MatWithParams(rows: number, cols: number, cvtype: number): Promise; + + Mat(): Promise; + + getMatData(mat: Mat, rownum: number, colnum: number): Promise; + + setTo(mat: Mat, cvscalar: CvScalar): void; + + put(mat: Mat, rownum: number, colnum: number, data: string[]): void; + + transpose(mat: Mat): void; + + deleteMat(mat: Mat): void; + + deleteMats(): void; + + MatOfInt(lomatint: number, himatint: number): Promise; + + MatOfFloat(lomatfloat: number, himatfloat: number): Promise; + + } +} diff --git a/harmony/opencv3/src/main/ets/generated/turboModules/ts.ts b/harmony/opencv3/src/main/ets/generated/turboModules/ts.ts new file mode 100644 index 0000000..86f88b0 --- /dev/null +++ b/harmony/opencv3/src/main/ets/generated/turboModules/ts.ts @@ -0,0 +1,6 @@ +/** + * This code was generated by "react-native codegen-lib-harmony" + */ + +export * from "./CvCameraModule" +export * from "./RNOpencv3" diff --git a/harmony/opencv3/src/main/module.json5 b/harmony/opencv3/src/main/module.json5 new file mode 100644 index 0000000..6c5d828 --- /dev/null +++ b/harmony/opencv3/src/main/module.json5 @@ -0,0 +1,9 @@ +{ + "module": { + "name": "opencv3", + "type": "har", + "deviceTypes": [ + "default" + ] + } +} diff --git a/harmony/opencv3/src/main/resources/base/element/string.json b/harmony/opencv3/src/main/resources/base/element/string.json new file mode 100644 index 0000000..f51a9c8 --- /dev/null +++ b/harmony/opencv3/src/main/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "page_show", + "value": "page from package" + } + ] +} diff --git a/harmony/opencv3/src/main/resources/en_US/element/string.json b/harmony/opencv3/src/main/resources/en_US/element/string.json new file mode 100644 index 0000000..f51a9c8 --- /dev/null +++ b/harmony/opencv3/src/main/resources/en_US/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "page_show", + "value": "page from package" + } + ] +} diff --git a/harmony/opencv3/src/main/resources/zh_CN/element/string.json b/harmony/opencv3/src/main/resources/zh_CN/element/string.json new file mode 100644 index 0000000..f51a9c8 --- /dev/null +++ b/harmony/opencv3/src/main/resources/zh_CN/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "page_show", + "value": "page from package" + } + ] +} diff --git a/harmony/opencv3/ts.ts b/harmony/opencv3/ts.ts new file mode 100644 index 0000000..f1f915a --- /dev/null +++ b/harmony/opencv3/ts.ts @@ -0,0 +1,2 @@ + +export * from "./src/main/ets/RNOpencvPackage"; \ No newline at end of file diff --git a/index.js b/index.js index fd19b7d..9349c51 100644 --- a/index.js +++ b/index.js @@ -1,177 +1,146 @@ - -// @author Adam G. Freeman, adamgf@gmail.com -import { NativeModules, requireNativeComponent, View, UIManager, Platform, PermissionsAndroid } from 'react-native'; - -import React, { Component } from 'react'; +import React, { + forwardRef, + useCallback, + useImperativeHandle, + useRef, + Component, +} from 'react'; import PropTypes from 'prop-types'; -const { RNOpencv3 } = NativeModules; - import { ColorConv, CvType, Imgproc, Core } from './constants'; import { CvScalar, CvPoint, CvSize, CvRect } from './coretypes'; -import { Mat, MatOfInt, MatOfFloat, setTo, get } from './mats'; +import { Mat, MatOfInt, MatOfFloat } from './mats'; import { CvImage } from './cvimage'; import { findNodeHandle } from 'react-native'; - -const CvCameraView = requireNativeComponent('CvCameraView', CvCamera); - -var RNFS = require('react-native-fs') - -class CvCamera extends Component { - constructor(props) { - super(props) - this.state = { - cameraPermissed: Platform.OS === 'ios' ? true : PermissionsAndroid.PERMISSIONS.CAMERA +import RNOpencv3 from './spec/turbomodule/NativeRNOpencv3'; +import CVCameraModule from './spec/turbomodule/NativeCVCameraModule'; +import CvCameraView from './spec/fabric/CvCameraViewNativeComponent'; + +var RNFS = require('react-native-fs'); + +const CvCamera = React.forwardRef((props, ref) => { + const cameraRef = useRef(null); + + const setOverlay = overlayMat => { + if (cameraRef.current) { + const options = { overlayMat: overlayMat }; + cameraRef.current.setOverlay?.( + options, + findNodeHandle(cameraRef.current), + ); } - } - componentDidMount = () => { - if (Platform.OS === 'android') this.requestCameraPermissions() - } - async requestCameraPermissions() { + }; + + const takePicture = async filename => { + const outputFilename = RNFS.DocumentDirectoryPath + '/' + filename; + const pictureOptions = { filename: outputFilename }; try { - let granted = false - if (this.props.useStorage) { - granted = await PermissionsAndroid.requestMultiple([ - PermissionsAndroid.PERMISSIONS.CAMERA, - PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE, - PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE - ]) - } - else { - granted = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.CAMERA) - } - if (granted === PermissionsAndroid.RESULTS.GRANTED) { - const retJson = await NativeModules.CvCameraModule.initCamera(findNodeHandle(this)) - if (retJson.cameraInitialized) { - this.setState({cameraPermissed:true}) - } - } else { - console.log('Camera permission denied') - } - } catch (err) { - console.warn(err) + return await cameraRef.current?.takePicture?.( + pictureOptions, + findNodeHandle(cameraRef.current), + ); + } catch (e) { + console.error(e); } - } - setOverlay(overlayMat) { - if (Platform.OS === 'android') { - UIManager.dispatchViewManagerCommand( - findNodeHandle(this), - UIManager.getViewManagerConfig('CvCameraView').Commands.setOverlay, - [overlayMat], - ) + }; + + const startRecording = filename => { + const outputFilename = RNFS.DocumentDirectoryPath + '/' + filename; + const pictureOptions = { filename: outputFilename }; + cameraRef.current?.startRecording?.( + pictureOptions, + findNodeHandle(cameraRef.current), + ); + }; + + const stopRecording = async () => { + try { + return await cameraRef.current?.stopRecording?.( + findNodeHandle(cameraRef.current), + ); + } catch (e) { + console.error(e); } - else { - const options = { 'overlayMat' : overlayMat } - NativeModules.CvCameraView.setOverlay(options, findNodeHandle(this)) - } - } - async takePicture(filename) { - const outputFilename = RNFS.DocumentDirectoryPath + '/' + filename - const pictureOptions = { 'filename' : outputFilename } - - if (Platform.OS === 'android') { - return await NativeModules.CvCameraModule.takePicture(pictureOptions, findNodeHandle(this)) - } - else { - return await NativeModules.CvCameraView.takePicture(pictureOptions, findNodeHandle(this)) - } - } - startRecording(filename) { - var outputFilename = RNFS.DocumentDirectoryPath + '/' + filename - if (Platform.OS === 'android') { - outputFilename = RNFS.ExternalStorageDirectoryPath + '/' + filename - } - const pictureOptions = { 'filename' : outputFilename } - - if (Platform.OS === 'android') { - NativeModules.CvCameraModule.startRecording(pictureOptions, findNodeHandle(this)) - } - else { - NativeModules.CvCameraView.startRecording(pictureOptions, findNodeHandle(this)) - } - } - async stopRecording() { - if (Platform.OS === 'android') { - return await NativeModules.CvCameraModule.stopRecording(findNodeHandle(this)) - } - else { - return await NativeModules.CvCameraView.stopRecording(findNodeHandle(this)) - } - } - render() { - if (!this.state.cameraPermissed) return () - return (); - } -} - -CvCamera.propTypes = { - ...View.propTypes, - facing: PropTypes.string, - useStorage: PropTypes.bool -}; + }; + + useImperativeHandle( + ref, + () => ({ + setOverlay, + takePicture, + startRecording, + stopRecording, + }), + [cameraRef], + ); + + return ; +}); class CvInvokeGroup extends Component { static propTypes = { children: PropTypes.any.isRequired, - groupid: PropTypes.string.isRequired - } + groupid: PropTypes.string.isRequired, + }; constructor(props) { - super(props) + super(props); } renderChildren() { - const { children, groupid, cvinvoke } = this.props + const { children, groupid, cvinvoke } = this.props; - let ins = [] - let functions = [] - let paramsArr = [] - let outs = [] - let callbacks = [] - let groupids = [] + let ins = []; + let functions = []; + let paramsArr = []; + let outs = []; + let callbacks = []; + let groupids = []; if (cvinvoke && cvinvoke.ins) { - ins = cvinvoke.ins + ins = cvinvoke.ins; } if (cvinvoke && cvinvoke.functions) { - functions = cvinvoke.functions + functions = cvinvoke.functions; } if (cvinvoke && cvinvoke.paramsArr) { - paramsArr = cvinvoke.paramsArr + paramsArr = cvinvoke.paramsArr; } if (cvinvoke && cvinvoke.outs) { - outs = cvinvoke.outs + outs = cvinvoke.outs; } if (cvinvoke && cvinvoke.callbacks) { - callbacks = cvinvoke.callbacks + callbacks = cvinvoke.callbacks; } if (cvinvoke && cvinvoke.groupids) { - groupids = cvinvoke.groupids + groupids = cvinvoke.groupids; } - const offspring = React.Children.map(children, - (child,i) => { - if (child.type.displayName === 'CvInvoke') { - const {inobj, func, params, outobj, callback} = child.props - ins.push(inobj) // can be nil - functions.push(func) - paramsArr.push(params) - outs.push(outobj) // can be nil - callbacks.push(callback) // can be nil - groupids.push(groupid) - } - else { - return React.cloneElement(child, { - // pass info down to the next CvInvokeGroup or to the CvCamera - ...child.props, "cvinvoke" : { "ins" : ins, "functions" : functions, "paramsArr": paramsArr, "outs" : outs, "callbacks": callbacks, "groupids": groupids } - }) - } - }) - return offspring + const offspring = React.Children.map(children, (child, i) => { + if (child.type.displayName === 'CvInvoke') { + const { inobj, func, params, outobj, callback } = child.props; + ins.push(inobj); // can be nil + functions.push(func); + paramsArr.push(params); + outs.push(outobj); // can be nil + callbacks.push(callback); // can be nil + groupids.push(groupid); + } else { + return React.cloneElement(child, { + // pass info down to the next CvInvokeGroup or to the CvCamera + ...child.props, + cvinvoke: { + ins: ins, + functions: functions, + paramsArr: paramsArr, + outs: outs, + callbacks: callbacks, + groupids: groupids, + }, + }); + } + }); + return offspring; } render() { - return ( - - {this.renderChildren()} - - ) + return {this.renderChildren()}; } } @@ -181,66 +150,75 @@ class CvInvoke extends Component { func: PropTypes.string.isRequired, params: PropTypes.any, outobj: PropTypes.string, - callback: PropTypes.string - } + callback: PropTypes.string, + }; constructor(props) { - super(props) + super(props); } renderChildren() { - const { cvinvoke, inobj, func, params, outobj, callback, children } = this.props + const { cvinvoke, inobj, func, params, outobj, callback, children } = + this.props; if (children) { - let newins = [] - let newfunctions = [] - let newparamsarr = [] - let newouts = [] - let newcallbacks = [] + let newins = []; + let newfunctions = []; + let newparamsarr = []; + let newouts = []; + let newcallbacks = []; if (cvinvoke && cvinvoke.ins) { - newins = cvinvoke.ins + newins = cvinvoke.ins; } if (cvinvoke && cvinvoke.functions) { - newfunctions = cvinvoke.functions + newfunctions = cvinvoke.functions; } if (cvinvoke && cvinvoke.paramsArr) { - newparamsarr = cvinvoke.paramsArr + newparamsarr = cvinvoke.paramsArr; } if (cvinvoke && cvinvoke.outs) { - newouts = cvinvoke.outs + newouts = cvinvoke.outs; } if (cvinvoke && cvinvoke.callbacks) { - newcallbacks = cvinvoke.callbacks + newcallbacks = cvinvoke.callbacks; } - newins.push(inobj) - newfunctions.push(func) - newparamsarr.push(params) - newouts.push(outobj) - newcallbacks.push(callback) - - const newKidsOnTheBlock = React.Children.map(children, - (child,i) => React.cloneElement(child, { + newins.push(inobj); + newfunctions.push(func); + newparamsarr.push(params); + newouts.push(outobj); + newcallbacks.push(callback); + + const newKidsOnTheBlock = React.Children.map(children, (child, i) => + React.cloneElement(child, { // pass info down to the CvCamera - ...child.props, "cvinvoke" : { "ins" : newins, "functions" : newfunctions, "paramsArr": newparamsarr, "outs" : newouts, "callbacks": newcallbacks } - }) - ) - return newKidsOnTheBlock - } - else { + ...child.props, + cvinvoke: { + ins: newins, + functions: newfunctions, + paramsArr: newparamsarr, + outs: newouts, + callbacks: newcallbacks, + }, + }), + ); + return newKidsOnTheBlock; + } else { return ( - - ) + + ); } } render() { - return( - - {this.renderChildren()} - - ) + return {this.renderChildren()}; } } -const RNCv = RNOpencv3 +const RNCv = RNOpencv3; export { RNCv, @@ -258,5 +236,5 @@ export { CvScalar, CvPoint, CvSize, - CvRect + CvRect, }; diff --git a/mats.js b/mats.js index 7a09727..4f4e7a5 100644 --- a/mats.js +++ b/mats.js @@ -1,9 +1,7 @@ // author Adam G. Freeman, adamgf@gmail.com 01/26/2019 // Of course this is just a tiny subset of all the mat stuff ... -import { NativeModules } from 'react-native'; -const { RNOpencv3 } = NativeModules; -import { CvType } from './constants'; +import RNOpencv3 from './spec/turbomodule/NativeRNOpencv3' export class Mat { constructor(numRows, numCols, cvtype, scalarval) { diff --git a/package-lock.json b/package-lock.json deleted file mode 100644 index d75dcf6..0000000 --- a/package-lock.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "name": "react-native-opencv3", - "version": "1.0.8", - "lockfileVersion": 1 -} diff --git a/package.json b/package.json index afd232d..822e40d 100644 --- a/package.json +++ b/package.json @@ -1,15 +1,10 @@ { "name": "@react-native-oh-tpl/react-native-opencv3", + "main": "index.js", "version": "1.0.8", - "_integrity": "", - "_location": "/react-native-opencv3", - "_phantomChildren": {}, - "_requiredBy": [ - "/" - ], - "bundleDependencies": false, "dependencies": { - "react-native-opencv3": "1.0.8" + "react-native-opencv3": "1.0.8", + "react-native-fs": "^2.13.3" }, "deprecated": false, "description": "OpenCV+contrib ported to React-Native", @@ -17,15 +12,31 @@ "react-native OpenCV" ], "license": "BSD", - "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "codegen-lib": "react-native codegen-lib-harmony --no-safety-check --npm-package-name react-native-opencv3 --cpp-output-path ../harmony/opencv3/src/main/cpp/generated --ets-output-path ../harmony/opencv3/src/main/ets/generated --turbo-modules-spec-paths ./spec/turbomodule --arkts-components-spec-paths ./spec/fabric" + }, "peerDependencies": { - "react-native": "*", - "react-native-fs": "*" + "react": "*", + "react-native": "*" + }, + "devDependencies": { + "@rnoh/react-native-harmony-cli": "npm:@react-native-oh/react-native-harmony-cli@^0.0.27", + "@react-native-community/cli": "latest" }, "harmony": { "alias": "react-native-opencv3" }, - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - } + "files": [ + "harmony", + "spec", + "License", + "package.json", + "constants.js", + "coretypes.js", + "cvimage.js", + "downloadAssetSource.js", + "index.js", + "mats.js" + ] } diff --git a/spec/fabric/CvCameraViewNativeComponent.ts b/spec/fabric/CvCameraViewNativeComponent.ts new file mode 100644 index 0000000..dec4c0d --- /dev/null +++ b/spec/fabric/CvCameraViewNativeComponent.ts @@ -0,0 +1,40 @@ +import type { HostComponent, ViewProps } from 'react-native'; +import codegenNativeComponent from 'react-native/Libraries/Utilities/codegenNativeComponent'; +import { + DirectEventHandler, + Double, + Int32, + WithDefault, +} from 'react-native/Libraries/Types/CodegenTypes'; +import codegenNativeCommands from 'react-native/Libraries/Utilities/codegenNativeCommands'; + +export interface CvCameraViewNativeProps extends ViewProps { + facing?: WithDefault<'front' | 'back', 'front'>; + cvinvoke: string; // todo + useStorage?: boolean; + faceClassifier?: string; + eyesClassifier?: string; + noseClassifier?: string; + mouthClassifier?: string; + landmarksModel?: string; + overlayInterval?: Int32; + + onFacesDetectedCv?: DirectEventHandler<{}>; + onCameraFrame?: DirectEventHandler<{}>; + onFrameSize?: DirectEventHandler<{}>; +} + +export interface NativeCommands { + setOverlay: (viewRef: React.ElementRef> ) => void; + takePicture: (viewRef: React.ElementRef>) => void; + startRecording: (viewRef: React.ElementRef>) => void; + stopRecording: (viewRef: React.ElementRef>) => void; +} + +export const Commands = codegenNativeCommands({ + supportedCommands: ['setOverlay', 'takePicture', 'startRecording', 'stopRecording'], +}); + +export default codegenNativeComponent( + 'CvCameraView', +) as HostComponent; diff --git a/spec/turbomodule/NativeCVCameraModule.ts b/spec/turbomodule/NativeCVCameraModule.ts new file mode 100644 index 0000000..119fa9e --- /dev/null +++ b/spec/turbomodule/NativeCVCameraModule.ts @@ -0,0 +1,21 @@ +import { TurboModule } from 'react-native'; +import { TurboModuleRegistry } from 'react-native'; + +// 定义 CvCameraView 原生模块接口 +export interface Spec extends TurboModule { + // 设置叠加图像 + setOverlay(options: { overlayMat: number }, viewHandle: number): void; + + // 拍照 + takePicture(options: { filename: string }, viewHandle: number): Promise; + + // 开始录像 + startRecording(options: { filename: string }, viewHandle: number): void; + + // 停止录像 + stopRecording(viewHandle: number): Promise; +} + +export default TurboModuleRegistry.getEnforcing('CvCameraModule'); + + diff --git a/spec/turbomodule/NativeRNOpencv3.ts b/spec/turbomodule/NativeRNOpencv3.ts new file mode 100644 index 0000000..e82e3b7 --- /dev/null +++ b/spec/turbomodule/NativeRNOpencv3.ts @@ -0,0 +1,112 @@ +import { TurboModule, TurboModuleRegistry } from 'react-native'; + +type Mat = { + matIndex: number; + rows: number; + cols: number; + CvType?: number; +}; + +type CvScalar = { + value: number; +}; + +type CvPoint = { + x: number; + y: number; +}; + +type CvInvokeMap = { + ins: string[]; + functions: string[]; + paramsArr: string[]; + outs: string[]; + callbacks: string[]; + groupids: string[]; +}; + +export interface Spec extends TurboModule { + // 图形绘制 + drawLine( + inMat: Mat, + pt1: CvPoint, + pt2: CvPoint, + scalarVal: CvScalar, + thickness: number + ): void; + + // 转换颜色 + cvtColor( + sourceMat: Mat, + destMat: Mat, + convColorCode: number + ): void; + + // 图片路径转 Mat + imageToMat(inPath: string): Promise; + + // Mat 转图片 + matToImage(srcMat: Mat, outPath: string): Promise; + + // 执行多个 OpenCV 方法 + invokeMethods(cvInvokeMap: CvInvokeMap): void; + + // 方法调用带回调 + invokeMethodWithCallback( + ins: string, + func: string, + params: string, + out: string, + callback: string + ): void; + + // 执行 OpenCV 方法 + invokeMethod(func: string, params: string): void; + + // 执行带输入输出的 OpenCV 方法 + invokeInOutMethod(ins: string, func: string, params: string, out: string): void; + + // 通过标量创建 Mat + MatWithScalar( + rows: number, + cols: number, + cvtype: number, + scalarMap: CvScalar + ): Promise; + + // 通过参数创建 Mat + MatWithParams( + rows: number, + cols: number, + cvtype: number + ): Promise; + + // 创建空 Mat + Mat(): Promise; + + // 获取 Mat 数据 + getMatData(mat: Mat, rownum: number, colnum: number): Promise; + + // 设置 Mat 数据 + setTo(mat: Mat, cvscalar: CvScalar): void; + + // 设置 Mat 某位置的数据 + put(mat: Mat, rownum: number, colnum: number, data: string[]): void; + + // 转置 Mat + transpose(mat: Mat): void; + + // 删除 Mat + deleteMat(mat: Mat): void; + + // 删除所有 Mat + deleteMats(): void; + + // 创建 Int 型 Mat + MatOfInt(lomatint: number, himatint: number): Promise; + + // 创建 Float 型 Mat + MatOfFloat(lomatfloat: number, himatfloat: number): Promise; +} + +export default TurboModuleRegistry.getEnforcing('RNOpencv3');