ReactNative二维码扫描

来源:互联网 发布:20端口是什么 编辑:程序博客网 时间:2024/06/05 11:02

二维码扫描,其实也并没有什么神奇的东西,解码规则别人也已经封装好了,我们只需要调用就好了,那么当然就需要第三方库。。。

操作流程如下:

1、下载第三方库:npm i --save react-native-barcodescanner

2、配置该库:react-native link

3、直接引入使用就得了。。。案例如下:

import React, {

  AppRegistry,

  Component,

} from 'react-native';import BarcodeScanner from 'react-native-barcodescanner';

class BarcodeScannerApp extends Component {

  constructor(props) {

    super(props);

    this.state = {

      torchMode: 'off',

      cameraType: 'back',

    };

  }

 

  barcodeReceived(e) {

    console.log('Barcode: ' + e.data);

    console.log('Type: ' + e.type);

  }

  render() {

    return (

      <BarcodeScanner

        onBarCodeRead={this.barcodeReceived}

        style={{ flex: 1 }}

        torchMode={this.state.torchMode}  //点击对焦,on /off

        cameraType={this.state.cameraType}  //前置摄像头还是后置摄像头,‘back/front’ ,默认‘back’后置摄像头

      />

    );

  }

}

AppRegistry.registerComponent('BarcodeScannerApp', () => BarcodeScannerApp);

 

 

想了解更多请打开下面网站:

https://github.com/ideacreation/react-native-barcodescanner

 

原创粉丝点击