react-native-smart-barcode(react-native二維碼掃描)

我看了好多人的react-native-smart-barcode多多少少有些問題,踩了好多坑搞定了。

npm install react-native-smart-barcode --save

android端集成:

  1. 在android/settings.gradle文件中:
...
include ':react-native-smart-barcode'
project(':react-native-smart-barcode').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-smart-barcode/android')

2.在android/app/build.gradle文件中:

...
dependencies {
    ...
    // 在這里添加
    compile project(':react-native-smart-barcode')
}

  1. 在MainApplication.java文件中(官方上面的問題錯(cuò)誤,修改如下):
...
import com.reactnativecomponent.barcode.RCTCapturePackage;    //這是要添加的

//將原來的代碼注釋掉,換成這句
private ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
    //  private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
   ...........
    @Override
    protected List<ReactPackage> getPackages() {
      return Arrays.<ReactPackage>asList(
              new MainReactPackage(),
              //添加以下代碼
              new RCTCapturePackage()
      );
    }
  };
  //添加以下代碼
  public void setReactNativeHost(ReactNativeHost reactNativeHost) {
    mReactNativeHost = reactNativeHost;
  }
 
  @Override
   public ReactNativeHost getReactNativeHost() {
     return mReactNativeHost;
   }
...

4.在AndroidManifest.xml文件中添加相機(jī)權(quán)限:

...
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-feature android:name="android.hardware.camera"/>
<uses-feature android:name="android.hardware.camera.autofocus"/>
...

IOS端集成:

1.將\node_modules\react-native-smart-barcode\ios\RCTBarcode\RCTBarCode.xcodeproj 添加到Xcode的Libraries中

2.在Build Phases->Link Binary With Libraries 加入RCTBarCode.xcodeproj\Products\libRCTBarCode.a

3.查看Build Settings->Seach Paths->Header Search Paths是否有$(SRCROOT)/../../../react-native/React并設(shè)為recursive

4.將\node_modules\react-native-smart-barcode\ios\raw 文件夾拖到到Xcode項(xiàng)目中(確保文件夾是藍(lán)色的)


image.png

5.在info.plist添加相機(jī)權(quán)限 Privacy - Camera Usage Description:

image

看了好多代碼有問題,這是我的代碼(親測(cè)有效):

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react';
import {
  Platform,
  StyleSheet,
  Text,
  View,
    Alert
} from 'react-native';
import Barcode from 'react-native-smart-barcode'

type Props = {};
export default class App extends Component<Props> {
    //構(gòu)造方法
    constructor(props) {
        super(props);
        this.state = {
            viewAppear: false,
        };
    }
    componentDidMount() {
        //啟動(dòng)定時(shí)器
        this.timer = setTimeout(
            () => this.setState({viewAppear: true}),
            250
        );
    }
    //組件銷毀生命周期
    componentWillUnmount() {
        //清楚定時(shí)器
        this.timer && clearTimeout(this.timer);
    }

    _onBarCodeRead = (e) => {
        // console.log(`e.nativeEvent.data.type = ${e.nativeEvent.data.type}, e.nativeEvent.data.code = ${e.nativeEvent.data.code}`)
        this._stopScan();
        Alert.alert("二維碼", e.nativeEvent.data.code, [
            {text: '確認(rèn)', onPress: () => this._startScan()},
        ])
    };

    _startScan = (e) => {
        this._barCode.startScan()
    };

    _stopScan = (e) => {
        this._barCode.stopScan()
    }
    render() {
        return (
            <View style={{flex: 1}}>
                {this.state.viewAppear ?
                    <Barcode style={{flex: 1,}} ref={component => this._barCode = component}
                             onBarCodeRead={this._onBarCodeRead}/>
                    : null
                }
            </View>
        )
    }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
});


屬性

Prop 類型 可選的 默認(rèn) 描述
barCodeTypes array Yes 支持的條碼類型
scannerRectWidth number Yes 255 掃描區(qū)域的寬度
scannerRectHeight number Yes 255 掃描區(qū)域的高度
scannerRectTop number Yes 0 掃描區(qū)域下移
scannerRectLeft number Yes 0 掃描區(qū)域左移
scannerLineInterval number Yes 3000 掃描線移動(dòng)的頻率
scannerRectCornerColor string Yes #09BB0D 邊線的顏色

效果圖


image.png

配置時(shí)出現(xiàn)的bug,給大家梳理下

1.
image.png

2.
image.png

這個(gè)很常見其實(shí),解決辦法

npm install prop-types --save

然后修改一下文件

image.png

3.No resource found that matches the given name: attr 'android:keyboardNavigationCluster'
修改成如下參數(shù):
image.png

親測(cè)沒有問題,覺得有用的小伙伴點(diǎn)個(gè)小紅心就行??,么么噠。

友情提示:在開發(fā)中有遇到RN相關(guān)的技術(shù)問題,歡迎小伙伴加入交流群(620792950),在群里提問、互相交流學(xué)習(xí)。交流群也定期更新最新的RN學(xué)習(xí)資料給大家,謝謝大家支持!
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,725評(píng)論 25 709
  • 用到的組件 1、通過CocoaPods安裝 2、第三方類庫安裝 3、第三方服務(wù) 友盟社會(huì)化分享組件 友盟用戶反饋 ...
    SunnyLeong閱讀 15,139評(píng)論 1 180
  • 我想我應(yīng)該 應(yīng)該不會(huì)愛你 為了要努力 努力的不愛你 所以我讓自己那么喜歡你 這樣你就不忍心和我分離 ...
    藍(lán)金魚閱讀 433評(píng)論 0 0

友情鏈接更多精彩內(nèi)容