ReactNative二维码扫描

xiaoxiao2021-02-28  97

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

操作流程如下:

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

 

转载请注明原文地址: https://www.6miu.com/read-26182.html

最新回复(0)