1. 程式人生 > >react-native 實現條碼掃描(ios&android)

react-native 實現條碼掃描(ios&android)

從網上看了很多關於react native實現條碼掃描的文章,裡面出現了很多第三方庫,有的文章說react-native-camera不能支援Android,我就自己試著寫寫,發現他可以相容ios和Android。下面就來看看怎麼實現吧~http://www.jianshu.com/p/2cef1baf9a6f

一、新建專案

首先開啟終端,在相應的目錄下輸入命令建立新專案

react-native init CameraDemo

專案建立完成,進入專案根目錄下輸入命令下載react-native-camera庫

1.npm install react-native[email protected]
://github.com/lwansbrough/react-native-camera.git --save 2.react-native link react-native-camera

二、Android配置

1.開啟android/app/src/main/java/[...]/MainApplication.java檔案,新增import com.lwansbrough.RCTCamera.RCTCameraPackage;,在getPackages()方法裡新增new RCTCameraPackage(),
2.開啟android/settings.gradle檔案,新增

include ':react-native-camera'
project(':react-native-camera').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-camera/android')

3.開啟android/app/build.gradle檔案,在dependencies{}中新增compile project(':react-native-camera'
4.在AndroidManifest.xml配置檔案中新增相關許可權:

<uses-permission android:name="android.permission.CAMERA"
/>//相機許可權 <uses-permission android:name="android.permission.VIBRATE" />//震動許可權 <uses-feature android:name="android.hardware.camera" android:required="false" /> <uses-feature android:name="android.hardware.camera.front" android:required="false" />

注:android配置的前三個步驟一般情況下會自動新增,若沒有新增,按上述步驟手動新增!

三、iOS配置

1.使用Xcode開啟CameraDemo/ios/CameraDemo.xcodeproj檔案,在Project navigator->Libraries資料夾上右擊選擇Add Files to 'CameraDemo';
2.選擇專案中的node_modules->react-native-camera並且新增RCTCamera.xcodeproj檔案;
3.在Build Phases中新增libRCTCamera.a;
4.在Build Settings中找到Search Paths下的Header Search Paths,新增$(SRCROOT)/../../react-native/React和$(SRCROOT)/../../../React,並且選擇recursive;
如下圖:


[email protected]

5.開啟ScanDemo/ios/ScanDemo/Info.plist檔案,新增下列許可權

<key>NSCameraUsageDescription</key>
<string>請允許使用您的相機</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string></string>
<key>NSPhotoLibraryUsageDescription</key>
<string>請允許開啟您的相簿</string>

注:ios中前三個步驟也會自動載入,若沒有新增,需按上述步驟手動新增!

四、編寫程式碼

1.scan.js程式碼

import React, { Component } from 'react';
import { connect } from 'react-redux';
import {
    View,
    Text,
    StyleSheet,
    Image,
    Platform,
    Vibration,
    TouchableOpacity,
    Animated,
    Easing,
    Dimensions
} from 'react-native';

const {width, height}  = Dimensions.get('window');

import {ToastMessage} from '../../utils/toast';
import Camera from 'react-native-camera';
import ViewFinder from '../../components/order/viewFinder';

import backIcon from '../../../assets/img/backIcon.png';//返回按鈕
import scanLine from '../../../assets/img/scan_line.png';//掃描線

export default class Scan extends Component {
    constructor(props) {
        super(props);
        this.camera = null;
        this.state = {
            transCode:'',//條碼
            openFlash: false,
            active: true,
            flag:true,
            fadeInOpacity: new Animated.Value(0), // 初始值
            isEndAnimation:false,//結束動畫標記
        }
        this._goBack = this._goBack.bind(this);
        this._startAnimation = this._startAnimation.bind(this);
        this.barcodeReceived = this.barcodeReceived.bind(this);
        this._search = this._search.bind(this);
        this._changeFlash = this._changeFlash.bind(this);
        this.changeState = this.changeState.bind(this);
    }
    componentDidMount() {
         this._startAnimation(false);
    }
    //開始動畫,迴圈播放
    _startAnimation(isEnd) {
        Animated.timing(this.state.fadeInOpacity, {
            toValue: 1,
            duration: 3000,
            easing: Easing.linear
        }).start(
             () => {
                 if (isEnd){
                     this.setState({
                         isEndAnimation:true
                     })
                     return;
                 }
                 if (!this.state.isEndAnimation){
                     this.state.fadeInOpacity.setValue(0);
                     this._startAnimation(false)
                 }
             }
        );
        console.log("開始動畫");
    }
     barcodeReceived(e) {
        if (e.data !== this.transCode) {
            Vibration.vibrate([0, 500, 200, 500]);
            this.transCode = e.data; // 放在this上,防止觸發多次,setstate有延時
            if(this.state.flag){
                this.changeState(false);
                //通過條碼編號獲取資料
            }
            console.log("transCode="+this.transCode);
        }
    }
     //返回按鈕點選事件
    _goBack() {
        this.setState({
            isEndAnimation:true,
        });
        this.props.navigator.pop();
    }
    //開燈關燈
    _changeFlash() {
        this.setState({
            openFlash: !this.state.openFlash,
        });
    }
     //改變請求狀態
    changeState(status){
        this.setState({
            flag:status
        });
        console.log('status='+status);
    }

    render(){
        const {
                openFlash,
                active,
            } = this.state;
        return(
            <View style={styles.allContainer}>
                {(() => {
                    if (active) {
                        return (
                            <Camera
                                ref={cam => this.camera = cam}
                                style={styles.cameraStyle}
                                barcodeScannerEnabled={true}
                                onBarCodeRead={
                                    this.barcodeReceived
                                }
                                torchMode={openFlash ? 'on' : 'off'}>
                                    <View style={styles.container}>
                                        <View style={styles.titleContainer}>
                                            <View style={styles.leftContainer}>
                                                <TouchableOpacity activeOpacity={1} onPress={ this._goBack}>
                                                    <View>
                                                        <Image style={ styles.backImg } source={ backIcon }/>
                                                    </View>
                                                </TouchableOpacity>
                                           </View>
                                        </View>
                                    </View>
                                <View style={styles.centerContainer}/>
                                <View style={{flexDirection:'row'}}>
                                    <View style={styles.fillView}/>
                                    <View style={styles.scan}>
                                        <ViewFinder/>
                                        <Animated.View style={[styles.scanLine, {
                                            opacity: 1,
                                            transform:[{
                                                translateY:this.state.fadeInOpacity.interpolate({
                                                    inputRange:[0,1],
                                                    outputRange:[0,260]
                                                })
                                            }]
                                        }]}>
                                            <Image source={scanLine}/>
                                        </Animated.View>
                                    </View>
                                    <View style={styles.fillView}/>
                                </View>
                                <View style={styles.bottomContainer}>
                                <Text
                                    style={[
                                        styles.text,
                                        {
                                            textAlign: 'center',
                                            width: 260,
                                            marginTop: active ? 25 : 285,
                                        },
                                    ]}
                                    numberOfLines={2}
                                >
                                    將運單上的條碼放入框內即可自動掃描。
                                </Text>
                                <TouchableOpacity onPress={this._changeFlash}>
                                    <View style={styles.flash}>
                                        <Text style={styles.icon}>&#xe61a;</Text>
                                        <Text style={styles.text}>
                                            開燈/關燈
                                        </Text>
                                    </View>
                                </TouchableOpacity>
                                </View>
                            </Camera>
                         );
                    }
                })()}
            </View>
        )
    }
}

const styles =StyleSheet.create({
    allContainer:{
        flex:1,
    },
    container: {
        ...Platform.select({
            ios: {
                height: 64,
            },
            android: {
                height: 50
            }
        }),
        backgroundColor:BLACK_COLOR,
        opacity:0.5
    },
    titleContainer: {
        flex: 1,
        ...Platform.select({
            ios: {
                paddingTop: 15,
            },
            android: {
                paddingTop: 0,
            }
        }),
        flexDirection: 'row',
    },
    leftContainer: {
        flex:0,
        justifyContent: 'center',
    },
    backImg: {
        marginLeft: 10,
    },
    cameraStyle: {
        alignSelf: 'center',
        width: width,
        height: height,
    },
    flash: {
        flexDirection: 'column',
        alignItems: 'center',
        justifyContent: 'flex-start',
        marginTop: 60,
    },
    flashIcon: {
        fontSize: 1,
        color: WHITE_COLOR,
    },
    text: {
        fontSize: 14,
        color: WHITE_COLOR,
        marginTop:5
    },
    icon:{
        color:WHITE_COLOR,
        fontSize:20,
        fontFamily:'iconfont'
    },
    scanLine:{
         alignSelf:'center',
    },
    centerContainer:{
        ...Platform.select({
            ios: {
                height: 80,
            },
            android: {
                height: 60,
            }
        }),
        width:width,
        backgroundColor:BLACK_COLOR,
        opacity:0.5
    },
    bottomContainer:{
        alignItems:'center',
        backgroundColor:BLACK_COLOR,
        alignSelf:'center',
        opacity:0.5,
        flex:1,
        width:width
    },
    fillView:{
        width:(width-220)/2,
        height:220,
        backgroundColor:BLACK_COLOR,
        opacity:0.5
    },
    scan:{
        width:220,
        height:220,
        alignSelf:'center'
    }

})

2.viewFinder.js程式碼

import React, {
  Component,
  PropTypes,
} from 'react';
import {
  ActivityIndicator,
  StyleSheet,
  View,
} from 'react-native';

const styles = StyleSheet.create({
  container: {
    alignItems: 'center',
    justifyContent: 'center',
    position: 'absolute',
    top: 0,
    right: 0,
    bottom: 0,
    left: 0,
  },
  viewfinder: {
    alignItems: 'center',
    justifyContent: 'center',
    backgroundColor: 'transparent',
  },
  topLeftEdge: {
    position: 'absolute',
    top: 0,
    left: 0,
  },
  topRightEdge: {
    position: 'absolute',
    top: 0,
    right: 0,
  },
  bottomLeftEdge: {
    position: 'absolute',
    bottom: 0,
    left: 0,
  },
  bottomRightEdge: {
    position: 'absolute',
    bottom: 0,
    right: 0,
  },
});

class Viewfinder extends Component {
  constructor(props) {
    super(props);

    this.getBackgroundColor = this.getBackgroundColor.bind(this);
    this.getSizeStyles = this.getSizeStyles.bind(this);
    this.getEdgeSizeStyles = this.getEdgeSizeStyles.bind(this);
    this.renderLoadingIndicator = this.renderLoadingIndicator.bind(this);
  }

  getBackgroundColor() {
    return ({
      backgroundColor: this.props.backgroundColor,
    });
  }

  getEdgeColor() {
    return ({
      borderColor: this.props.color,
    });
  }

  getSizeStyles() {
    return ({
      height: this.props.height,
      width: this.props.width,
    });
  }

  getEdgeSizeStyles() {
    return ({
      height: this.props.borderLength,
      width: this.props.borderLength,
    });
  }

  renderLoadingIndicator() {
    if (!this.props.isLoading) {
      return null;
    }

    return (
      <ActivityIndicator
        animating={this.props.isLoading}
        color={this.props.color}
        size="large"
      />
    );
  }

render() {
    return (
      <View style={[styles.container, this.getBackgroundColor()]}>
        <View style={[styles.viewfinder, this.getSizeStyles()]}>
          <View
            style={[
              this.getEdgeColor(),
              this.getEdgeSizeStyles(),
              styles.topLeftEdge,
              {
                borderLeftWidth: this.props.borderWidth,
                borderTopWidth: this.props.borderWidth,
              },
            ]}
          />
          <View
            style={[
              this.getEdgeColor(),
              this.getEdgeSizeStyles(),
              styles.topRightEdge,
              {
                borderRightWidth: this.props.borderWidth,
                borderTopWidth: this.props.borderWidth,
              },
            ]}
          />
          {this.renderLoadingIndicator()}
          <View
            style={[
             this.getEdgeColor(),
              this.getEdgeSizeStyles(),
              styles.bottomLeftEdge,
              {
                borderLeftWidth: this.props.borderWidth,
                borderBottomWidth: this.props.borderWidth,
              },
            ]}
          />
          <View
            style={[
              this.getEdgeColor(),
              this.getEdgeSizeStyles(),
              styles.bottomRightEdge,
              {
                borderRightWidth: this.props.borderWidth,
                borderBottomWidth: this.props.borderWidth,
              },
            ]}
          />
        </View>
      </View>
   );
  }
}

Viewfinder.propTypes = {
  backgroundColor: PropTypes.string,
  borderWidth: PropTypes.number,
  borderLength: PropTypes.number,
  color: PropTypes.string,
  height: PropTypes.number,
  isLoading: PropTypes.bool,
  width: PropTypes.number,
};

Viewfinder.defaultProps = {
  backgroundColor: 'transparent',
  borderWidth: 3,
  borderLength: 20,
  color: COLOR_MAIN,
  height: 220,
  isLoading: false,
  width: 220,
};
module.exports = Viewfinder;

大功告成~最後上張效果圖:


1866D1330B2C6E2F40A40A7E7246C6BD.jpg

相關推薦

react-native 實現條碼掃描(ios&android)

從網上看了很多關於react native實現條碼掃描的文章,裡面出現了很多第三方庫,有的文章說react-native-camera不能支援Android,我就自己試著寫寫,發現他可以相容ios和Android。下面就來看看怎麼實現吧~http://www.jiansh

React Native 橋接原生 iOS 以及 Android 獲取 APP 版本號

在使用React Native進行跨平臺開發過程中,或多或少需要涉及到原生開發,這裡主要說一下如何橋接原生 iOS 以及 Android,在此以獲取 APP 版本號為例。 iOS 橋接 iOS 橋接比較簡單,只需要建立一個 Module 類,實現 RCTBridgeModule 協議就好。 首先我們需要建

(八)React Native實現呼叫android原生java方法並實現廣播的傳送和接受

接觸到混合應用開發提議後,首先想到了之前學的ionic,其次便是這兩年火遍全球的RN,由於ionic框架用的angular.js,而且angular1和angular2寫法區別很大,angular2對於初學者還是比較好接受的,但是angular1寫的很亂,程式碼

React Native實現二維碼掃描

依賴包匯入步驟 詳細步驟可看github上的說明,簡要記錄如下: 1. npm install react-native-camera –save 2. react-native link react-native-camera android手動l

React native 自定義彈窗(android使用原生ios彈窗)

android端執行效果圖(ios同樣適用): 1.自定義彈窗原始碼:    import React, { Component } from 'react'; import { StyleSheet, Text, View, Modal

[React Native]初探基於RN的Android開發

spa pen ati 結束 clas 最新 java ebp text 之前有一點React的經驗,覺得React Native不過就是把React到Web DOM的映射換成了React到Android Native的映射,用HTML CSS JavaScript寫And

React Native之通知欄訊息提示(android)

React Native之通知欄訊息提示(android)   一,需求分析與概述 1.1,推送作為手機應用的基本功能,是手機應用的重要部分,如果自己實現一套推送系統費時費力,所以大部分的應用都會選擇使用第三方的推送服務,如極光推送。 1.2,jpush-react-native 

基於React Native實現的介面載入元件react-native-loadview

react-native-loadview 基於React Native實現的介面載入元件, Installation npm install react-native-loadview --save Import into your project import

react-native 啟動頁設定(android

使用第三方外掛:react-native-splash-screen 下載地址:https://www.npmjs.com/package/react-native-splash-screen 第一步:安裝下載外掛 1、npm i react-native-splash-screen

React Native實現再按一次退出應用程式功能

解決點選兩次手機back鍵退出程式 程式碼及註釋如下: //雙擊返回鍵退出程式

react-native實現多張圖片上傳

最近在搞這個圖片上傳功能,,剛開始的時候iOS用的是 react-native-image-crop-picker這個外掛,,iOS那邊完美執行沒有毛病,,,但是到android這邊之後就開始報各種資源

React Native學習筆記2:Android環境搭建

背景 各位童鞋有木有感覺官方文件很坑啊,根據官網的描述,首先在chocolate就直接卡死了,VPN沒什麼卵用,於是逐個去官網下載,直到昨天才發現不用VPN也可以下!公司是windows環境,這裡就先用windows搭建。 因為本身是移動開發者,所以

React-Native實現熱門標籤功能

廢話少說,先上效果圖 Android原生有很多,react-native寫的,貌似沒有。貼程式碼: import React, {Component} from 'react'; import { StyleSheet, Text, View, Toucha

react native 實現ListView的區域性更新

在ReactNative中UI的重新整理大多數情況依賴於state的變更,通過呼叫元件的setState方法來更新state以達到通知元件重新渲染UI的目的。當然這種做法是官方提供的標準解決方案,在進行簡單UI設計時足以滿足大多數需求。 但是當遇到結構複雜並存在資料互動的介面設計時,手動管理state這種

React-Native 熱更新嘗試(Android

前言:由於蘋果釋出的ios的一些rn的app存在安全問題,主要就是由於一些第三方的熱更新庫導致的,然而訊息一出就鬧得沸沸揚揚的,導致有些人直接認為“學了大半年的rn白學啦~~!!真是哭笑不得。廢話不多說了,馬上進入我們今天的主題吧。“ 因為一直在做androi

利用CodePush對react-native專案熱更新(以android為例)

CodePush是提供給React native 或 Cordova開發的一箇中央倉庫,開發者可以將js、image等程式碼資源上傳上去,客戶端啟動的時候根據版本拉去CodePush上的程式碼進行覆蓋來實現客戶端的熱更新。 1,安裝CodePush npm install

react-native 實現漸變色背景

1、需要藉助外掛react-native-linear-gradient外掛    安裝:        yarn add react-native-linear-gradient        react-native link react-native-linear-gr

React Native自定義外掛,呼叫android原生程式碼

Hi,寶寶們,我又來了,16有你,17前行,18怎麼辦?18讓我們一起學習RN吧,hybrid現在是一個前沿,雖然官網說Learn once, write anywhere~~~ 僅需學習一次,編寫任何平臺。但是你不能指望它幹任何事啊,android原生的很多功能還是無法直接使用的,這個時

react-native實現新建頁面+路由跳轉

步驟: 1.新建一個新頁面,即自己所需要的檔案(拜訪新訊息) 配置: static navigationOptions = ({ navigation, navigationOptions }) => { const { params } = navigation.stat

React-Native實現登入頁面,並顯示和清除使用者的輸入

/** * Sample React Native App * https://github.com/facebook/react-native * ES6實現程式碼 */ import Re