1.0.9-1.0.2 • Published 6 months ago

@ohmi/react-native-opencv3 v1.0.9-1.0.2

Weekly downloads
-
License
BSD
Repository
-
Last release
6 months ago

模板版本:v0.2.2

!TIP 本项目基于 react-native-opencv3 v1.0.8 开发

!TIP(https://gitee.com/kunyuan-hongke/react-native-opencv3)

安装与使用

请到三方库的 Releases 发布地址查看配套的版本信息:react-native-opencv3 Releases 。对于未发布到npm的版本安装tgz包。

进入到工程目录并输入以下命令:

npm

npm install @ohmi/react-native-opencv3

yarn

yarn add @ohmi/react-native-opencv3

下面的代码展示了这个库的基本使用场景:

!WARNING 使用时 import 的库名不变。

import {RNCv} from 'react-native-opencv3';
import { useState } from 'react';
import { View,Button, Image,Text } from 'react-native';
const resolveAssetSource = require('react-native/Libraries/Image/resolveAssetSource')
const downloadAssetSource = require('@ohmi/react-native-opencv3/downloadAssetSource')
const  App = (()=>{

 const sourceuri = ('https://b0.bdstatic.com/ugc/cXQkOFFycfYbqshIFjnXqQd9e028f9b414fb03cadc17881f88cd92.jpg')

  const [mat, setMat] = useState();// 图片Mat对象

  const [matToImageuri, setmatToImageuri] = useState('');//图片

async function imageToMat() {
        // 图片转Mat对象
    const sourceFiles = await downloadAssetSource(sourceuri);
     RNCv.imageToMat(sourceFiles).then((e:any)=> setMat(e))
}
    
async function matToImage (){
        // Mat转图片
        const sourceFiles = await downloadAssetSource(sourceuri);
      RNCv.matToImage(sourceFiles, mat).then((e:any) => {
      setmatToImageuri(e)
    })
}
    
return(
    <View style={{flex:1}}>
            {mat && <Text style={{ color: 'red' }}>Mat对象:{JSON.stringify(mat)}</Text>}
            <Button title='imageToMat' onPress={()=>imageToMat()}/>
            <Button title='matToImage' onPress={()=>matToImage()}/>
            {matToImageuri&&<Image source={{uri:matToImageuri}} width={200} height={200} />}
    </View>
)


})

export default App

Manual Link

本库 HarmonyOS 侧实现依赖@react-native-oh-tpl/react-native-fs 的原生端代码,如已在 HarmonyOS 工程中引入过该库,则无需再次引入,可跳过本章节步骤,直接使用。

如未引入请参照@react-native-oh-tpl/react-native-fs 文档进行引入

2.引入原生端代码

目前有两种方法:

  1. 通过 har 包引入(在 IDE 完善相关功能后该方法会被遗弃,目前首选此方法);
  2. 直接链接源码。

方法一:通过 har 包引入(推荐)

!TIP har 包位于三方库安装路径的 harmony 文件夹下。

打开 entry/oh-package.json5,添加以下依赖

"dependencies": {
    "@rnoh/react-native-openharmony": "file:../react_native_openharmony",

    "@ohmi/react-native-opencv3": "file:../../node_modules/@ohmi/react-native-opencv3/harmony/opencv3.har"
  }

点击右上角的 sync 按钮

或者在终端执行:

cd entry
ohpm install

3.配置 CMakeLists 和引入 AsynStoragePackge

打开 entry/src/main/cpp/CMakeLists.txt,添加:

project(rnapp)
cmake_minimum_required(VERSION 3.4.1)
set(CMAKE_SKIP_BUILD_RPATH TRUE)
set(RNOH_APP_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
set(NODE_MODULES "${CMAKE_CURRENT_SOURCE_DIR}/../../../../../node_modules")
+ set(OH_MODULES "${CMAKE_CURRENT_SOURCE_DIR}/../../../oh_modules")
set(RNOH_CPP_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../react-native-harmony/harmony/cpp")
set(LOG_VERBOSITY_LEVEL 1)
set(CMAKE_ASM_FLAGS "-Wno-error=unused-command-line-argument -Qunused-arguments")
set(CMAKE_CXX_FLAGS "-fstack-protector-strong -Wl,-z,relro,-z,now,-z,noexecstack -s -fPIE -pie")
set(WITH_HITRACE_SYSTRACE 1) # for other CMakeLists.txt files to use
add_compile_definitions(WITH_HITRACE_SYSTRACE)
add_subdirectory("${RNOH_CPP_DIR}" ./rn)

# RNOH_BEGIN: manual_package_linking_1
add_subdirectory("../../../../sample_package/src/main/cpp" ./sample-package)
+ add_subdirectory("${OH_MODULE_DIR}/@ohmi/react-native-opencv3/src/main/cpp" ./opencv3)
# RNOH_END: manual_package_linking_1

file(GLOB GENERATED_CPP_FILES "./generated/*.cpp")

add_library(rnoh_app SHARED
    ${GENERATED_CPP_FILES}
    "./PackageProvider.cpp"
    "${RNOH_CPP_DIR}/RNOHAppNapiBridge.cpp"
)
target_link_libraries(rnoh_app PUBLIC rnoh)

# RNOH_BEGIN: manual_package_linking_2
target_link_libraries(rnoh_app PUBLIC rnoh_sample_package)
+ target_link_libraries(rnoh_app PUBLIC rnoh_opencv)
# RNOH_END: manual_package_linking_2

打开 entry/src/main/cpp/PackageProvider.cpp,添加:

#include "RNOH/PackageProvider.h"
#include "SamplePackage.h"
+ #include "OpencvPackage.h"

using namespace rnoh;

std::vector<std::shared_ptr<Package>> PackageProvider::getPackages(Package::Context ctx) {
    return {
      std::make_shared<SamplePackage>(ctx),
+     std::make_shared<OpencvPackage>(ctx)
    };
}

4.在 ArkTs 侧引入 AsynStorage Package

打开 entry/src/main/ets/RNPackagesFactory.ts,添加:

...
+ import {RNOpencvPackage} from '@ohmi/react-native-opencv3/ts';

export function createRNPackages(ctx: RNPackageContext): RNPackage[] {
  return [
    new SamplePackage(ctx),
+   new RNOpencvPackage(ctx),
  ];
}

在 ArkTs 侧引入 CvCameraView 组件

找到 function buildCustomRNComponent(),一般位于 entry/src/main/ets/pages/index.ets 或 entry/src/main/ets/rn/LoadBundle.ets,添加:

  ...
+ import { CvCameraView } from "@ohmi/react-native-opencv3";

@Builder
export function buildCustomRNComponent(ctx: ComponentBuilderContext) {
+ if (ctx.componentName === CvCameraView.NAME) {
+   CvCameraView({
+     ctx: ctx.rnComponentContext,
+     tag: ctx.tag,
+   })
+ }

}

5.运行

点击右上角的 sync 按钮

或者在终端执行:

cd entry
ohpm install

然后编译、运行即可。

兼容性

要使用此库,需要使用正确的 React-Native 和 RNOH 版本。另外,还需要使用配套的 DevEco Studio 和 手机 ROM。

请到三方库相应的 Releases 发布地址查看 Release 配套的版本信息:react-native-opencv3 Releases

Opencv3

!TIP "Platform"列表示该属性在原三方库上支持的平台。

!TIP "HarmonyOS Support"列为 yes 表示 HarmonyOS 平台支持该属性;no 则表示不支持;partially 表示部分支持。使用方法跨平台一致,效果对标 iOS 或 Android 的效果。

NameDescriptionTypeRequiredPlatformHarmonyOS Support
drawLine使用opencv3方法在Mat对象绘制一条直线function(inMat,pt1,pt2,scalarVal,thickness)yesAllyes
cvtColor使用opencv3方法将Mat对象转换颜色(sourceMat,destMat,convColorCode)yesAllyes
imageToMat使用opencv3方法将图片转Mat对象function(inPath): PromiseyesAllyes
matToImage使用opencv3方法将Mat对象转图片function(outPath,srcMat): PromiseyesAllyes
invokeMethods通过opencv3方法名称调用opencv3方法,opencv3多个方法function({"callbacks": [],"paramsArr": [],"outs": [],"functions": [],"ins": []})yesAllyes
invokeMethodWithCallback通过opencv3方法名称调用opencv3方法,返回callbackfunction(ins,func,params,out,callback)yesAllyes
invokeMethod通过opencv3方法名称调用opencv3方法yesAllyes
invokeInOutMethod通过opencv3方法名称调用opencv3方法function(ins,func,params,out)yesAllyes
MatWithScalar创建MatWithScalar类-矩阵(多维数组)Matfunction(rows,cols,cvtype,scalarMap)yesAllyes
MatWithParams创建MatWithParams类-矩阵Matfunction(rows,cols,cvtype)yesAllyes
Mat创建空MatfunctionyesAllyes
getMatData获取Mat-ArrayBuffer数据function(mat,rownum,colnum)yesAllyes
put使用opencv3方法改变Mat图片像素值function(mat,rownum,colnum,data)yesAllyes
setTo使用opencv3方法改变Mat图片颜色通道function(mat,cvscalar)yesAllyes
transpose使用opencv3方法改变Mat旋转图片function(mat)yesAllyes
deleteMat通过Mat对象,删除单个Mat对象functionyesAllyes
deleteMats删除所有Mat对象functionyesAllyes
MatOfInt创建Int类型Mat对象function(lomatint,himatint)yesAllyes
MatOfFloat创建Float类型Mat对象function(lomatfloat,himatfloat)yesAllyes

CvCamera

!TIP "Platform"列表示该属性在原三方库上支持的平台。 !TIP "HarmonyOS Support"列为 yes 表示 HarmonyOS 平台支持该属性;no 则表示不支持;partially 表示部分支持。使用方法跨平台一致,效果对标 iOS 或 Android 的效果。

NameDescriptionTypeRequiredPlatformHarmonyOS Support
takePicture拍照functionNoAllyes
startRecording开始录像functionNoAllyes
startRecording停止录像functionNoAllyes
facing相机面向:back/frontstringNoAllyes
onFrameSize获取帧大小信息的回调函数functionNoAllyes
onDetectFaces检测到人脸的回调functionNoAllyes
faceClassifier面部分类器functionNoAllyes
eyesClassifier眼睛分类器functionNoAllyes
noseClassifer鼻子分类器functionNoAllyes
mouthClassifier口型分类器functionNoAllyes
onFacesDetectedCv人脸识别回调functionNoAllyes
onPayload滤镜结果回调functionNoAllyes
overlayInterval滤镜调用间隔functionNoAllyes
useStorage使用本地存储functionNoAllyes

遗留问题

其他

开源协议

本项目基于 The MIT License (MIT) ,请自由地享受和参与开源。

1.0.9-1.0.2

6 months ago

1.0.9-rc.1.0.1

7 months ago

1.0.9-rc.1

7 months ago