当前位置:   article > 正文

React-Navitate基础_react-native-vision-camera

react-native-vision-camera
  1. 样式---------------
  2. RN中的继承只发生在Text组件中,其他组件样式不能继承
  3. 命名采用小驼峰式
  4. 所有的尺寸都是没有单位 width:100
  5. 特殊的样式命名marginHorizontal(水平外边距),marginVertical(垂直外边距)
  6. RN样式的声明
  7. 方式一:通过style属性直接声明
  8. 1.属性值为对象 <组件 style={{样式}}>
  9. 2.属性值为数据 <组件 style={[{样式1},{样式2}]}> 如果两个样式一样,则后面会覆盖前面
  10. 方式二:StyleSheet声明
  11. 引入 import {StyleSheet, View} from 'react-native'
  12. 声明 const styles = StyleSheet.create({foo:{样式1}, bar:{样式2})
  13. 使用 <View stype={[styles.foo, styles.bar]}>内容</View>
  1. 弹性布局---------
  2. 容器:采用Flex布局的元素称为Flex容器
  3. 项目(Item):容器内所有子元素称为Flex项目
  4. 主轴(main axis):水平方向
  5. 交叉轴(cross axis):垂直方向
  6. 获取平台是ios还是android
  7. import {Platform} from 'react-native'
  8. if (Platform.OS === 'android') {} else if (Platform.OS === 'ios') {}
  9. 属性
  10. flexDirection声明主轴方向,web默认是row | rn默认是column
  11. justifyContent声明项目在主轴上的对齐方式
  12. alignItems声明交叉轴上的对其方式
  13. flex声明项目在主轴上的尺寸比例
  14. flexWrap: 'wrap'允许换行
  15. 响应式布局FlexBox---------
  16. FlexBox
  17. Dimensions
  18. import { Dimensions } from 'react-native'
  19. const windowWidth = Dimensions.get('window').width 获取屏幕宽度
  20. const windowHeight = Dimensions.get('window').height 获取屏幕高度
  1. RN中的核心组件,是对原生组件的封装
  2. 原生组件:Android或ios组件
  3. 核心组件:RN中常用的,来自react-native的组件
  4. 核心组件
  5. View视图组件
  6. 安装 yarn add react-native-webview
  7. 配置 https://github.com/react-native-webview/react-native-webview
  8. Text文本组件
  9. Alert警告组件
  10. Button按钮组件
  11. Switch开关组件
  12. StatusBar状态栏组件
  13. ActivityIndicator加载指示器组件
  14. Image图片组件
  15. 1.加载本地图片
  16. 2.加载url地址
  17. 3.加载Base64字符串
  18. TextInput输入框组件
  19. Touchable触碰组件
  20. ScrollView滚动视图组件
  21. SectionList分组列表组件
  22. FlatList高性能列表组件
  23. Animated动画组件
  24. 创建初始值Animated.Value()单个值、Animated.ValueXY()向量值
  25. 将初始值绑定到某个样式属性下
  26. 通过动画类型API一帧一帧的更改初始值Animated.decay()加速效果、Animated.spring()弹跳效果、Animated.timing()时间渐变效果
  1. 三方组件
  2. WebView相当于内置浏览器
  3. yarn add react-native-webview
  4. Picker下拉框
  5. yarn add @react-native-picker/picker
  6. Swiper展示轮播效果
  7. 官网链接
  8. https://github.com/leecade/react-native-swiper
  9. yarn add react-native-swiper
  10. AsyncStorage持久化全局存储系统
  11. 配置
  12. https://github.com/react-native-async-storage/async-storage
  13. 官网链接
  14. https://react-native-async-storage.github.io/async-storage/docs/install/
  15. 安装
  16. yarn add @react-native-async-storage/async-storage
  17. 使用
  18. set(k,v) 删delete(k) | clear() 改update(k,v) 查get(k)
  19. Geolocation获取定位信息
  20. 官网地址
  21. https://github.com/react-native-geolocation/react-native-geolocation
  22. 添加获取定位信息的授权许可
  23. 安装
  24. yarn add @react-native-community/geolocation
  25. 配置权限
  26. 根据官网配置定位权限
  27. Image-picker点击头像去相册选择还是拍照
  28. 安装 yarn add react-native-image-picker
  29. 官网链接 https://github.com/react-native-image-picker/react-native-image-picker
  30. 使用场景 1.调用摄像头;2.访问相册
  31. react-native-linear-gradient线性渐变组件
  32. 安装
  33. yarn add react-native-linear-gradient
  34. 配置
  35. https://github.com/react-native-linear-gradient/react-native-linear-gradient
  36. 使用
  37. import LinearGradient from 'react-native-gradient'
  38. <LinearGradient start={{x:0, y:0}} end={{x:1,y:0}} colors={['#ddd', '#333']}></LinearGradient>
  39. Redux
  40. 安装 yarn add redux react-redux redux-thunk 支持异步操作
  41. 创建 store
  42. 将store挂载到App组件上
  43. 在组件内部使用redux数据
  44. 路由鉴权
  45. 用户登录
  46. 已登录跳到首页
  47. 未登录跳到登录页
  48. 项目优化
  49. 使用第三方组件库 链接:github.com/callstack/react-native-paper
  50. react-native-paper
  51. 添加项目logo
  52. 修改项目名称 android/app/src/main/res/values/strings.xml
  53. ios/项目名称/Info.plist文件,定位到CFBundleDisplayName
  1. Camera调用摄像头
  2. 一、react-native-vision-camera简介:是一个用于构建 React Native 应用程序中与相机相关的功能的库。它提供了一种以高性能和可定制化的方式访问设备相机并捕获图像或视频的方法。该库是基于 iOS 和 Android 的本地相机 API 构建的,并为两个平台提供了统一的 API。它提供了各种功能,如实时图像处理、手动相机控制和自定义取景器叠加层
  3. 官方链接 https://react-native-camera.github.io/react-native-camera/docs/installation
  4. 安装 yarn add react-native-vision-camera
  5. 要使用相机或麦克风,先指定应用需要相机和麦克风权限
  6. 苹果需要Info.plist文件<dict>标签中添加以下代码打开相机和麦克风权限,Info.plist在~/ios/ios项目名称/Info.plist目录
  7. <!--相机权限-->
  8. <key>NSCameraUsageDescription</key>
  9. <string>$(PRODUCT_NAME) needs access to your Camera.</string>
  10. <!--麦克风权限-->
  11. <key>NSMicrophoneUsageDescription</key>
  12. <string>$(PRODUCT_NAME) needs access to your Microphone.</string>
  13. 安卓需要在AndroidManifest.xml文件<manifest>标签中添加以下代码
  14. <uses-permission android:name="android.permission.CAMERA" />
  15. <uses-permission android:name="android.permission.RECORD_AUDIO" />
  16. 示例
  17. import { useCameraDevices, Camera } from 'react-native-vision-camera'
  18. import * as React from 'react'
  19. import { useRef } from 'react'
  20. export function CameraDemo(props) {
  21. const camara = useRef(null)
  22. // 获取可用镜头
  23. const devices = useCameraDevices()
  24. // 使用前置摄像头
  25. const device = devices.front
  26. return device != null && (
  27. <Camera
  28. ref={camara} //绑定ref
  29. device={device} //绑定设备
  30. isActive={true}
  31. photo={true} //打开相机功能
  32. frameProcessorFps={'auto'}
  33. />
  34. )
  35. }
  36. 二、Frame Processors帧处理器简介:是指对相机捕获的帧进行实时处理的功能模块。在 React Native Vision Camera中,可以使用 Frame Processors 来对相机捕获的帧进行自定义的处理。如:使用帧处理器将视频的每一帧中的人脸来进行检测。
  37. 安装
  38. 帧处理器模块 yarn add react-native-reanimated
  39. 人脸检测模块 yarn add vision-camera-face-detector
  40. 三、sion-camera-face-detector简介:是React Native Vision Camera 库提供的一个人脸检测功能模块。它使用了 iOS 和 Android 平台的本地人脸检测算法,并提供了一个简单的 API,使开发人员可以轻松地在他们的应用程序中添加人脸检测功能。
  41. 说明 vision-camera-face-detector 提供了一个名为 useFaceDetector 的钩子,可以用于启用人脸检测功能。当使用 useFaceDetector 钩子启用人脸检测时,每次相机捕获到一帧画面时,该功能模块会自动检测画面中的人脸,并返回一个包含人脸信息的数组
  42. 示例
  43. import * as React from 'react'
  44. import { runOnJS } from 'react-native-reanimated'
  45. import { Platform } from 'react-native'
  46. import { useCameraDevices, useFrameProcessor, Camera } from 'react-native-vision-camera'
  47. import { scanFaces, Face } from 'vision-camera-face-detector'
  48. import { useRef } from 'react'
  49. export function FaceDetectorDemo(props) {
  50. const [hasPermission, setHasPermission] = React.useState(false)
  51. //存储人脸信息
  52. const [faces, setFaces] = React.useState([])
  53. //提示人脸的消息
  54. const [msg, setMsg] = React.useState(null)
  55. //用于打开相机等
  56. const [isActiveVal, setIsActiveVal] = React.useState(true)
  57. const camara = useRef(null)
  58. //获取可用相机列表
  59. const devices = useCameraDevices()
  60. //使用前置摄像头
  61. const device = devices.front
  62. React.useEffect(() => {
  63. let facesArr = []
  64. facesArr = faces
  65. // faces为帧处理器获得到的人脸图片,从而进行人脸检测处理
  66. if (facesArr.length > 0) {
  67. let face = facesArr[0]
  68. if (JSON.stringify(face.contours) == '{}') {
  69. setMsg('人脸信息不全')
  70. } else {
  71. if (face.leftEyeOpenProbability == 1 && face.rightEyeOpenProbability == 1 && face.pitchAngle == 0 && face.rollAngle == 0) {
  72. setMsg('面部遮挡')
  73. } else {
  74. if (face.leftEyeOpenProbability < 0.8 && face.rightEyeOpenProbability < 0.8) {
  75. setMsg('请睁眼')
  76. } else {
  77. if (!(Math.abs(face.pitchAngle) < 6 && Math.abs(face.rollAngle) < 6 && Math.abs(face.yawAngle) <= 6)) {
  78. setMsg('请正面朝向屏幕')
  79. } else {
  80. //关闭相机
  81. setIsActiveVal(false)
  82. //拍摄照片
  83. takePicture().then(res => {
  84. console.log('成功', res)
  85. }).catch()
  86. }
  87. }
  88. }
  89. }
  90. }
  91. }, [faces])
  92. React.useEffect(() => {
  93. (async () => {
  94. const status = await Camera.requestCameraPermission()
  95. setHasPermission(status === 'authorized')
  96. })()
  97. }, [])
  98. const frameProcessor = useFrameProcessor((frame) => {
  99. 'worklet'
  100. const scannedFaces = scanFaces(frame)
  101. runOnJS(setFaces)(scannedFaces)
  102. }, [])
  103. // 拍摄人脸照片
  104. const takePicture = async () => {
  105. if (camara.current) {
  106. let photo = null
  107. // 检测是ios还是安卓平台
  108. if (Platform.OS === 'ios') {
  109. photo = await camara.current.takePhoto({
  110. qualityPrioritization: 'speed',
  111. flash: 'off',
  112. enableAutoRedEyeReduction: true,
  113. })
  114. } else {
  115. photo = await camara.current.takeSnapshot({
  116. quality: 80,
  117. skipMetadata: true,
  118. })
  119. }
  120. // photo.path 为拍摄的人脸照片地址,可以把照片传入后端进行人脸比对
  121. let path = photo.path
  122. }
  123. }
  124. return device != null && hasPermission ? (
  125. <Camera
  126. ref={camara}
  127. device={device}
  128. isActive={isActiveVal}
  129. photo={true}
  130. frameProcessor={frameProcessor}
  131. frameProcessorFps={'auto'}
  132. />
  133. ) : null
  134. }

  1. 路由与导航基础
  2. RN中路由是通过React-Navigation来完成的,React中路由是通过React-Router来完成的
  3. 官网链接:https://reactnavigation.org
  4. 安装
  5. yarn add @react-navigation/native
  6. yarn add react-native-screens react-native-safe-area-context
  7. 添加头部文件(放到index.jsApp.js文件的顶部)
  8. import 'react-native-gesture-handler'
  9. 添加导航容器
  10. 引用 import { NavigationContainer } from '@react-navigation/native'
  11. 在入口文件中把整个应用包裹在导航容器中<NavigationContainer><Index /></NavigationContainer>
  12. Stack导航
  13. 简介
  14. RN中默认没有类似浏览器的history对象
  15. RN中跳转之前,先将路由声明在Stack
  16. 安装
  17. yarn add @react-navigation/stack
  18. yarn add react-native-gesture-handler
  19. yarn add @react-native-masked-view/masked-view
  20. 使用
  21. import { createStackNavigator } from '@react-navigation/stack'
  22. const stack = createStackNavigator()
  23. <Stack.Navigator initialRouteName='Home'><Stack.Screen /></Stack.Navigator>
  24. Navigator属性作用于所有screen
  25. initialRouteName指定默认初始化加载的路由
  26. headerMode声明头部信息(float IOS头部效果,screen Android头部效果,none不显示头部)
  27. screenOptions声明screen属性
  28. Screen属性作用于当前screen,Screen只有一个大的options属性,options中可以声明多个属性
  29. title声明screen标题
  30. headerTitleStyle声明标题样式
  31. headerStyle声明头样式
  32. headerLeft声明左侧内容
  33. headerRight声明右侧内容
  34. headerTintColor标题颜色
  35. BottomTab导航(底部导航)
  36. 安装
  37. yarn add @react-navigation/bottom-tabs
  38. 使用
  39. import { createBottomTabNavigator } from '@react-navigation/bottom-tabs'
  40. const Tab = createBottomTabNavigator()
  41. 矢量库图标
  42. 安装
  43. npm install --svae react-native-vector-icons
  44. 官网链接
  45. https://github.com/oblador/react-native-vector-icons
  46. Drawer导航(抽屉导航)
  47. 安装
  48. npm install @react-navigation/drawer
  49. 使用
  50. import { createDrawerNavigator } from '@react-navigation/drawer'
  51. const Drawer = createDrawerNavigator()
  52. 声明
  53. Drawer.NavigatorDrawer.Screen
  54. Navigator属性
  55. drawerPostion:菜单显示位置,left(默认) | right
  56. drawerType:菜单动画效果,front | back | slide | permanent
  57. drawerStyle: 菜单样式 backgroundColor、width.....
  58. drawerContentOptions:选中菜单项样式 activeTintColor:选中菜单字体颜色 itemStyle:所有菜单样式
  59. ScreenOptions属性
  60. title:菜单标题
  61. drawerLabel:替代title,返回复杂的组件。{focused:boolean,color:string}
  62. drawerIcon:返回图标的函数。{focused:boolean,color:string,size:number}
  63. headerShown:是否显示header。布尔型,默认false不显示
  64. headerLeft:函数,声明header左侧的显示内容
  65. headerRight:函数,声明header右侧的显示内容
  66. MaterialTopTap导航,可以滑动的导航
  67. 安装
  68. yarn add @react-navigation/material-top-tabs react-native-tab-view
  69. 使用
  70. import {createMaterialTopTabNavigator} from '@react-navigation/material-top-tabs'
  71. const Tab = createMaterialTopTabNavigator()
  72. 声明 Tab.NavigatorTab.Screen
  73. Navigator属性
  74. tabBarPosition:Tab显示位置。默认top,可以设置bottom
  75. tabBarOptions:包含tabBar组件属性的对象
  76. activeTintColor-当前菜单的标题或图标颜色
  77. inactiveTintColor-非当前菜单的标题或图标颜色
  78. showIcon-是否显示图标,默认是false
  79. showLabel-是否显示文字,默认是true
  80. tabStyle-标签样式对象
  81. labelStyle-标签文字样式对象,这里指定的颜色,会覆盖activeTintColor和inactiveTintColor的值
  82. iconStyle-图标样式对象
  83. Screen属性
  84. options:设置Screen组件的对象
  85. title-设置显示标题
  86. tabBarIcon-设置标签图标(需要现在Navigator中指定showIcon:true)
  87. 其值为函数,包含两个参数{focused:boolean,color:string}
  88. focused用来判断标签是否获取焦点,color为当前标签的颜色
  89. tabBarLabel-设置标签文字内容(当未定义时,会使用title)
  90. 其值为函数,包含两个参数:{focused:boolean,color:string}
  91. focused用来判断标签是否获取焦点,color为当前标签的颜色
  92. 路由嵌套
  93. 在一个导航内部渲染另一个导航
  94. 路由传参
  95. 传递参数
  96. navigation.navigate('路由名称',{key:123})
  97. 接收参数
  98. 类组件
  99. this.props.route.key
  100. 函数组件
  101. route.params.key

组件库对比:简评4款第三方 React Native 组件库 - 知乎

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/297649
推荐阅读
相关标签
  

闽ICP备14008679号