RN常用组件之二维码识别


react-native-barcodescanner

在项目中遇到了扫描二维码的需求,在网上找了一圈,发现这个组件很好用,目前已经加入到项目中了,表现还是可以的。下面说的这个组件的配置和基本使用。

安装配置

install

1
npm i --save react-native-barcodescanner
//In android/settings.gradle
...
include ':react-native-barcodescanner', ':app'
project(':react-native-barcodescanner').projectDir = new File(rootProject.projectDir,'../node_modules/react-native-barcodescanner/android')

//In android/app/build.gradle
...
dependencies {
...
compile project(':react-native-barcodescanner')
}

register

1
2
3
4
5
6
7
8
9
10
11
12
13
import com.eguma.barcodescanner.BarcodeScannerPackage;
...
public class MainApplication extends Application implements ReactApplication {
...

@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new BarcodeScannerPackage()//在这里添加注册
);
}
}

然后rebuild项目就可以使用组件了。

具体使用方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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}
cameraType={this.state.cameraType}
/>
);
}
}

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

ok,将上面代码复制到index.js跑起来试试吧~

-------------本文结束,感谢您的阅读-------------

本文标题:RN常用组件之二维码识别

文章作者:饭饭君~

发布时间:2018年03月07日 - 08:41

最后更新:2019年04月23日 - 10:50

原始链接:https://yangcf.github.io/2018/03/07/RN常用组件之二维码识别/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

如果我的文章有帮助到你,欢迎打赏~~
0%