《ReactNative》扫描二维码以及识别相册二维码

用react-native-camera 库可以实现用相机扫描二维码的功能

用react-native-image-crop-picker库选取相册二维码图片

用react-native-local-barcode-recognizer解析二维码图片

如果只需要扫描二维码,不需要识别相册中的二维码图片,只安装react-native-camera库就好

install

npm install react-native-camera
npm install react-native-image-crop-picker
npm install react-native-local-barcode-recognizer

link

react-native link react-native-camera
react-native link react-native-image-crop-picker
react-native link react-native-local-barcode-recognizer

 效果

 

源码:

import React,{Component} from 'react'
import{
  View,Animated,Easing,
  Image,TouchableOpacity,
  StyleSheet,
  Text
} from 'react-native'
import { RNCamera } from 'react-native-camera'
import ImagePicker from 'react-native-image-crop-picker'
import LocalBarcodeRecognizer from 'react-native-local-barcode-recognizer'
export default class Scan extends Component{
  constructor(props) {
    super(props);
    this.state = {
      moveAnim: new Animated.Value(0),
      data: '',
    };
  }
  componentDidMount() {
      this.startAnimation();
  }
  startAnimation = () => {
    this.state.moveAnim.setValue(0);
    Animated.timing(
        this.state.moveAnim,
        {
            toValue: -200,
            duration: 1500,
            easing: Easing.linear
        }
    ).start(() => this.startAnimation());
  };
  //  识别二维码
  onBarCodeRead = (result) => {
    if(this.state.data) return
    const {data} = result;
    alert('扫描结果:'+data)
    if(data) this.setState({data}
  };
  _pickerImg(){
    ImagePicker.openPicker({
      width: 300,
      height: 300,
      cropping: false,
      includeBase64:true
    }).then(image => {
		this._handleImage(image);
    });
  }
  _handleImage(image){
    if(image.data){
      this.recoginze(image.data)
    }
  }
  recoginze = async (data)=>{
    let result = await LocalBarcodeRecognizer.decode(data.replace("data:image/jpeg;base64,",""),{codeTypes:['ean13','qr']});
    alert('识别结果:'+result);
  }

  render() {
    return (
      <View style={styles.container}>
        <RNCamera
            ref={ref => {
                this.camera = ref;
            }}
            style={styles.preview}
            type={RNCamera.Constants.Type.back}
            flashMode={RNCamera.Constants.FlashMode.on}
            onBarCodeRead={this.onBarCodeRead}
        >
          <View style={styles.rectangleContainer}>
            
            <View style={styles.rectangle}/>
            <Animated.View style={[
                styles.border,
                {transform: [{translateY: this.state.moveAnim}]}]}/>
            <Text style={styles.rectangleText}>将二维码放入框内,即可自动扫描</Text>
          </View>
          <TouchableOpacity style={styles.btnView} onPress={()=>this._pickerImg()}>
            <Text style={styles.btnText}>相册</Text>
          </TouchableOpacity>
        </RNCamera>
      </View>
    );
  }
}
const styles = StyleSheet.create({
  btnText: {color:'white', fontSize:16},
  btnView: {position:'absolute',right:30,top:50},
    container: {
        flex: 1,
        flexDirection: 'row'
    },
    preview: {
        flex: 1,
        justifyContent: 'flex-end',
        alignItems: 'center'
    },
    rectangleContainer: {
        flex: 1,
        alignItems: 'center',
        justifyContent: 'center',
        backgroundColor: 'transparent'
    },
    rectangle: {
        height: 200,
        width: 200,
        borderWidth: 1,
        borderColor: '#00FF00',
        backgroundColor: 'transparent'
    },
    rectangleText: {
        flex: 0,
        color: '#fff',
        marginTop: 10
    },
    border: {
        flex: 0,
        width: 200,
        height: 2,
        backgroundColor: '#00FF00',
    }
});

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值