React Native 二维码扫描 react-native-camera的使用
来源:互联网 发布:严歌苓 知乎 编辑:程序博客网 时间:2024/04/30 22:24
最近写项目用到了二维码扫描,就去找了下,发现有个第三方的react-native-camera,支持android 和ios,功能还挺强大的,有兴趣的朋友可以去看下https://github.com/lwansbrough/react-native-camera。以下是我的自己写的一个仿微信样式写的二维码扫描。
首先导入react-native-camera
npm install react-native-camera@https://github.com/lwansbrough/react-native-camera.git --save
react-native link react-native-camera
import React from 'react';import { StyleSheet, TouchableOpacity, View, Text, InteractionManager, Animated, Easing, Platform, Image} from 'react-native';import Camera from 'react-native-camera';var Dimensions = require('Dimensions');var {width, height} = Dimensions.get('window');export default class CodeReading extends React.Component { constructor(props) { super(props); this.camera = null; this.state = { show:true, anim: new Animated.Value(0), camera: { aspect: Camera.constants.Aspect.fill, }, }; } componentDidMount(){ InteractionManager.runAfterInteractions(()=>{ this.startAnimation() }); } startAnimation(){ if(this.state.show){ this.state.anim.setValue(0) Animated.timing(this.state.anim,{ toValue:1, duration:1500, easing:Easing.linear, }).start(()=>this.startAnimation()); } } componentWillUnmount(){ this.state.show = false; } //扫描二维码方法 barcodeReceived = (e) =>{ if(this.state.show){ this.state.show = false; if (e) { this.props.navigator.pop() this.props.ReceiveCode(e.data) } else { Alert.alert( '提示', '扫描失败' [{text:'确定'}] ) } } } render() { return ( <View style={styles.container}> {/*导航条*/} {this.renderNavBar()} <Camera ref={(cam) => { this.camera = cam; }} style={styles.preview} aspect={this.state.camera.aspect} onBarCodeRead={this.barcodeReceived.bind(this)} barCodeTypes = {['qr']} > <View style = {{height: Platform.OS == 'ios' ? (height-264)/3:(height-244)/3,width:width,backgroundColor:'rgba(0,0,0,0.5)',}}> </View> <View style={{flexDirection:'row'}}> <View style={styles.itemStyle}/> <Image style={styles.rectangle} source={require('../AssetsImages/code_bar.png')}> <Animated.View style={[styles.animatiedStyle, { transform: [{ translateY: this.state.anim.interpolate({ inputRange: [0,1], outputRange: [0,200] }) }] }]}> </Animated.View> </Image> <View style={styles.itemStyle}/> </View> <View style={{flex:1,backgroundColor:'rgba(0, 0, 0, 0.5)',width:width,alignItems:'center'}}> <Text style={styles.textStyle}>将二维码放入框内,即可自动扫描</Text> </View> </Camera> </View> ); } // 导航条 renderNavBar(){ return( <View style={styles.navBarStyle}> <TouchableOpacity onPress={()=>{this.props.navigator.pop()}} style={styles.leftViewStyle}> <Image source={{uri:'nav_return'}} style={{height:20,width:20}} /> </TouchableOpacity> <Text style={[styles.navTitleStyle,{marginTop:Platform.OS == 'ios'?12:0,fontSize:20}]}> 二维码 </Text> </View> ) }}const styles = StyleSheet.create({ itemStyle:{ backgroundColor:'rgba(0,0,0,0.5)', width:(width-200)/2, height:200 }, textStyle:{ color:'#fff', marginTop:10, fontWeight:'bold', fontSize:18 }, navTitleStyle: { color:'white', fontWeight:'bold', }, navBarStyle:{ // 导航条样式 height: Platform.OS == 'ios' ? 64 : 44, backgroundColor:'rgba(34,110,184,1.0)', // 设置主轴的方向 flexDirection:'row', // 垂直居中 ---> 设置侧轴的对齐方式 alignItems:'center', justifyContent:'center' }, leftViewStyle:{ // 绝对定位 // 设置主轴的方向 flexDirection:'row', position:'absolute', left:10, bottom:Platform.OS == 'ios' ? 15:12, alignItems:'center', width:30 }, animatiedStyle:{ height:2, backgroundColor:'#00FF00' }, container: { flex: 1, }, preview: { flex: 1, }, rectangle: { height: 200, width: 200, }});
运行效果如下图所示,扫描成功后得到返回值e.data,传回上一个页面展示。
0 0
- React Native 二维码扫描 react-native-camera的使用
- React-Native中二维码扫描使用
- React Native之二维码扫描
- React Native 实现二维码扫描
- React Native 实现二维码扫描
- React Native之二维码扫描
- 【React Native 实战】二维码扫描
- React Native实现二维码扫描
- react-native-barcodescanner调用实现二维码扫描
- React Native 二维码扫描组件(简单,易用!)
- 自定义 React Native 二维码扫描组件
- React-Native 生成二维码
- react-native打开摄像机、ios端二维码扫描
- react-native打开摄像机、ios端二维码扫描
- React-Native打开摄像机、ios端二维码扫描
- 自定义 React Native 二维码扫描组件(简单,易用!)
- 自定义 React Native 二维码扫描组件(简单,易用!)
- 自定义 React Native 二维码扫描组件(简单,易用!)
- POJ 2031Building a Space Station(叉积点积的应用 )
- 牙疼怎么办 立刻止疼
- 同步与异步的概念
- 23种设计模式(10):命令模式
- A卡和N卡
- React Native 二维码扫描 react-native-camera的使用
- oracle---角色和权限,补充
- 金牌智能人工------ 纠错 , S2 金牌
- AndroidStudio Jni 开发初识
- 编程适合小孩学吗?
- 杭州住房公积金提取说明
- List Set Map存入null
- 笔记本没有声音
- AlertDialog 按钮不显示 按钮颜色设置问题