【问题标题】:Browser opens after scanning QR code- ReactNative扫描二维码后浏览器打开-React Native
【发布时间】:2018-09-27 03:59:30
【问题描述】:

我正在使用 react-native-camera 模块来扫描二维码。但是在扫描后它总是打开浏览器并尝试访问 qr 值作为 url。如何停止这个浏览器打开的东西。我只需要获得 qr 值.. 任何帮助。这是我的代码。

_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>);
            }

    }

【问题讨论】:

    标签: react-native qr-code react-native-camera


    【解决方案1】:

    假设您希望在成功扫描条形码后重定向到其他屏幕。据观察,在条码扫描后可能无法获得二维码数据。因此,必须处理是否成功检索到 qr 值。为此,您需要按如下方式更新您的“_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】:

      您的问题可能与注释行有关。尝试提取该行。如果没有任何效果,您可以尝试另一个模块。你可以使用'react-native-qrcode-scanner',这会更容易。这是githublink

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-04-07
        • 2020-12-04
        • 2017-12-22
        • 1970-01-01
        • 2021-12-16
        • 2022-09-28
        • 1970-01-01
        相关资源
        最近更新 更多