【问题标题】:react-native-camera onBarCodeRead not workingreact-native-camera onBarCodeRead 不起作用
【发布时间】:2017-11-25 04:07:41
【问题描述】:

我在使用 react-native 时遇到 onBarCodeRead 问题。

预期行为:应用程序控制台记录条形码类型和数据。

实际行为:应用程序只是打开相机,从不调用 _onBarCodeRead。

任何解释都会令人满意。

代码如下:

'use strict';
import React, { Component } from 'react';
import {
  AppRegistry,
  Dimensions,
  StyleSheet,
  Text,
  TouchableHighlight,
  View
} from 'react-native';
import Camera from 'react-native-camera';

class camera_app extends Component {

  constructor(props) {
    super(props);
    this.state = {
      showCamera: true,
    };
  }

  renderCamera = () => {
    if(this.state.showCamera) {
      return (
        <Camera
          ref={(cam) => {
            this.camera = cam;
          }}
          style={styles.container}
          aspect={Camera.constants.Aspect.fill}
          onBarCodeRead={this._onBarCodeRead}>
        </Camera>
      );
    } else {
      return (
        <View></View>
      );
    }
  }

  render() {
    return (
      this.renderCamera()
    );
  }

  _onBarCodeRead = (e) => {
    this.setState({showCamera: false});
    alert("Barcode Found!",
          "Type: " + e.type + "\nData: " + e.data);
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: "center",
    alignItems: "center",
    backgroundColor: "transparent",
  },
});

AppRegistry.registerComponent('rn_camera', () => camera_app);

【问题讨论】:

    标签: camera barcode react-native-camera


    【解决方案1】:

    我试过你的代码,它工作得很好。只需更改警报功能,它就会显示条形码的类型和数据。也尝试检查条形码类型是否支持https://github.com/lwansbrough/react-native-camera

    alert("Barcode Found! \nType: " + e.type + "\nData: " + e.data);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-19
      • 2021-03-08
      • 2018-06-20
      相关资源
      最近更新 更多