Kimako
Kimako

Reputation: 625

Scan codes and error undefined is not an object

I would like to set up a scan for EAN13 codes, I am using the react-native-camera package but I ran into a **

TypeError: undefined is not an object (evaluating '_reactNativeCamera.Camera.constants')

** and I will like your help to find out where is the error in my code.

If you can guide me, help me, that would be great, thank you very much.

import React, { Component } from 'react';
import {
Text,
View,
Alert,
TouchableOpacity,
Image
} from 'react-native';
import {Camera} from 'react-native-camera';
import styles from '../../../assets/styles'

export default class Ean13 extends Component {
  constructor(props) {
    super(props);
      this.handleTourch = this.handleTourch.bind(this);
        this.state = {
          torchOn: false
        }
      }
      onBarCodeRead = (e) => {
        Alert.alert("Barcode value is" + e.data, "Barcode type is" + e.type);
      }
      render() {
        return (
          <View style={styles.container}>
            <Camera
            style={styles.preview}
            torchMode={this.state.torchOn ? Camera.constants.TorchMode.on : Camera.constants.TorchMode.off}
            onBarCodeRead={this.onBarCodeRead}
            ref={cam => this.camera = cam}
            aspect={Camera.constants.Aspect.fill}
            >
              <Text style={{
              backgroundColor: 'white'
              }}>BARCODE SCANNER</Text>
            </Camera>
            <View style={styles.bottomOverlay}>
              <TouchableOpacity onPress={() => this.handleTourch(this.state.torchOn)}>
                <Image style={styles.cameraIcon}
                     source={this.state.torchOn === true ? require('../../../assets/images/flash.png') : require('../../../assets/images/no-flash.png')} />
              </TouchableOpacity>
            </View>
          </View>
        )
      }
handleTourch(value) {
  if (value === true) {
      this.setState({ torchOn: false });
    } else {
      this.setState({ torchOn: true });
    }
  }
}

Upvotes: 0

Views: 884

Answers (1)

Guruparan Giritharan
Guruparan Giritharan

Reputation: 16334

You have used Camera

this should be RNCamera

import { RNCamera } from 'react-native-camera';

And read the constants from

RNCamera.constants.TorchMode.on

Check the docs https://github.com/react-native-camera/react-native-camera/blob/master/docs/RNCamera.md

Upvotes: 1

Related Questions