ekreutz / react-native-barcode-scanner-google

Barcode scanner for react native, which implements barcode detection from Google's Vision API.
MIT License
113 stars 68 forks source link

Seems like working but onBarcodeRead is not working #19

Closed 3lhagvaasuren closed 6 years ago

3lhagvaasuren commented 6 years ago

I've created an app named BarcodeApp exactly same as in the doc and copied every line of codes in the doc. But I've not installed 1.2.0 version (called the latest version in the doc, actually the latest version was 1.4.2). But I've installed 1.4.2 by npm install --save react-native-barcode-scanner-google. And the app launched OK. But seems like onBarcodeRead could not triggered just showing the camera only. Here is my package.json:

{
  "name": "BarcodeApp",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node node_modules/react-native/local-cli/cli.js start",
    "test": "jest"
  },
  "dependencies": {
    "react": "16.3.0-alpha.1",
    "react-native": "0.54.2",
    "react-native-barcode-scanner-google": "^1.4.2"
  },
  "devDependencies": {
    "babel-jest": "23.0.0-alpha.0",
    "babel-preset-react-native": "4.0.0",
    "jest": "22.4.2",
    "react-test-renderer": "16.3.0-alpha.1"
  },
  "jest": {
    "preset": "react-native"
  }
}

Here is my index.js:

import React, { Component } from 'react';
import { AppRegistry, StyleSheet, Text, View, Alert } from 'react-native';

import BarcodeScanner, { Exception, FocusMode, CameraFillMode, BarcodeType, pauseScanner, resumeScanner } from 'react-native-barcode-scanner-google';

export default class BarcodeApp extends Component {
    render() {
        return (
            <View style={{ flex: 1 }}>
                <BarcodeScanner
                    style={{ flex: 1 }}
                    onBarcodeRead={({ data, type }) => {
                        alert('HEY');
                        // handle your scanned barcodes here!
                        // as an example, we show an alert:
                        // Alert.alert(`Barcode '${data}' of type '${type}' was scanned.`);
                        // alert(`Barcode '${data}' of type '${type}' was scanned.`);
                    }}
                    onException={exceptionKey => {
                        // check instructions on Github for a more detailed overview of these exceptions.
                        switch (exceptionKey) {
                            case Exception.NO_PLAY_SERVICES:
                            // tell the user they need to update Google Play Services
                            case Exception.LOW_STORAGE:
                            // tell the user their device doesn't have enough storage to fit the barcode scanning magic
                            case Exception.NOT_OPERATIONAL:
                            // Google's barcode magic is being downloaded, but is not yet operational.
                            default: break;
                        }
                    }}
                    focusMode={FocusMode.TAP /* could also be TAP or FIXED */}
                    cameraFillMode={CameraFillMode.COVER /* could also be FIT */}
                    barcodeType={BarcodeType.ALL /* replace with ALL for all alternatives */}
                />
            </View>
        );
    }
}

AppRegistry.registerComponent('BarcodeApp', () => BarcodeApp);
ekreutz commented 6 years ago

The latest version is not 1.4.2. Somebody else forked this repo and is maintaining their own repo on npm.