代码之家  ›  专栏  ›  技术社区  ›  Buwaneka Sirinath

扫描二维码后打开浏览器-ReactNative

  •  0
  • Buwaneka Sirinath  · 技术社区  · 6 年前

    _handleBarCodeRead(e) {
    
            try {
                Vibration.vibrate();
                this.setState({scanning: false});
                this.setState({qrcode:e.data});
                //Linking.openURL(e.data).catch(err => console.error(‘An error occured’, err));
                console.log(e.data);
    
                const {navigate} = this.props.navigation;
                navigate(‘TransactionVerified’);
    
                //return;
            } catch (error) {
                console.log(error);
            }
        }
        getInitialState() {
            return {
                scanning: true,
                cameraType: Camera.constants.Type.back
            }
        }
    
        render(){
            if(this.state.scanning) {
                return (
                <View style={styles.container}>
                    <Text style={styles.welcome}>
                    Scan Code
                    </Text>
                    <View style={styles.rectangleContainer}>
                    <Camera
                     style={styles.camera}
                     type={this.state.cameraType}
                     onBarCodeRead={this._handleBarCodeRead.bind(this)}
                     barCodeTypes={[Camera.constants.BarCodeType.qr]}
                     >
                        <View style={styles.rectangleContainer}>
                        <View style={styles.rectangle}/>
                        </View>
                    </Camera>
                    </View>
                    {/* <Text style={styles.instructions}>
                    Double tap R on your keyboard to reload,{‘\n’}
                    </Text> */}
                </View>
                );
                }
                else{
                return (<View  style={styles.container}>
                    <Text style={styles.welcome}>
                    Scan Code
                    </Text>
                    {/* <Text style={styles.instructions}>
                    Double tap R on your keyboard to reload,{‘\n’}
                    </Text>      */}
                </View>);
                }
    
        }
    
    2 回复  |  直到 6 年前
        1
  •  2
  •   Sandy.....    6 年前

    假设您希望在条码扫描成功后重定向到其他屏幕。据观察,条形码扫描后可能无法获得qr数据。因此,必须处理是否成功地检索到qr值。为此,您需要更新“\u handleBarCodeRead”代码,如下所示:

    try {
        if (e.data !== undefined) {
            Vibration.vibrate();
            this.setState({scanning: false});
            this.setState({qrcode:e.data});
            //Linking.openURL(e.data).catch(err => console.error(‘An error occured’, err));
            console.log(e.data);
    
            const {navigate} = this.props.navigation;
            navigate(‘TransactionVerified’);
        }
    } catch (error) {
        console.log(error);
    }
    
        2
  •  0
  •   Berkay Kaan    6 年前

    你的问题可能与评论行有关。试着提取那条线。如果什么都不起作用,你可以尝试另一个模块。您可以使用“react native qrcode scanner”,这会更简单。这是github link .