quagga-react条码扫描器

时间:2018-02-01 01:53:11

标签: reactjs quaggajs

基本上我让演示工作除了实际扫描。即相机已开启等。不确定我错过了什么......

这是我的代码。

App.js文件:

import React, { Component } from 'react';
import Scanner from './Scanner';
import Result from './Result';

export default class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      scanning: false,
      results: [],
    };
    this._scan = this._scan.bind(this);
    this._onDetected = this._onDetected.bind(this);
  }

  _scan() {
    this.setState({ scanning: !this.state.scanning });
  }

  _onDetected(result) {
    this.setState({ results: this.state.results.concat([result]) });
  }
  render() {
    return (
      <div>
        <button onClick={this._scan}>{this.state.scanning ? 'Stop' : 'Start'}</button>
        <ul className="results">
          {this.state.results.map(result => {
            <Result key={result.codeResult.code} result={result} />;
          })}
        </ul>
        {this.state.scanning ? <Scanner onDetected={this.state._onDetected} /> : null}
      </div>
    );
  }
}

Scanner.js文件:

import React, { Component } from 'react';
import Quagga from 'quagga';

export default class Scanner extends Component {
    constructor(props) {
        super(props);
        this._onDetected = this._onDetected.bind(this);
    }

    componentDidMount() {
        Quagga.init(
            {
                inputStream: {
                    type: 'LiveStream',
                    constraints: {
                        width: 640,
                        height: 480,
                        facingMode: 'environment', // or user
                    },
                },
                locator: {
                    patchSize: 'medium',
                    halfSample: true,
                },
                numOfWorkers: 2,
                decoder: {
                    readers: ['upc_reader'],
                },
                locate: true,
            },
            function(err) {
                if (err) {
                    return console.log(err);
                }
                Quagga.start();
            }
        );
        Quagga.onDetected(this._onDetected);
    }

    componentWillUnmount() {
        Quagga.offDetected(this._onDetected);
    }

    _onDetected(result) {
        this.props.onDetected(result);
    }

    render() {
        return <div id="interactive" className="viewport" />;
    }
}

Result.js文件:

import React, { Component } from 'react';

export default class Result extends Component {
    render() {
        const result = this.props.result;

        if (!result) {
            return null;
        }
        return (
            <li>
                {result.codeResult.code} [{result.codeResult.format}]
            </li>
        );
    }
}

感谢我的朋友们!

4 个答案:

答案 0 :(得分:2)

您可能希望更改阅读器类型,默认情况下为code_128_reader

超市中使用的大多数条形码例如遵循EAN规范(至少我住的地方),因此您可以将其放在Scanner.js中以更改为ean_reader

decoder: {
  readers: ["ean_reader"]
},

Quagga的发起地点。

可在此处找到读者列表:Quagga documentation

如果这不起作用,我建议尝试其他阅读器/条形码组合。

答案 1 :(得分:0)

文档说,在Node中,您应该使用numOfWorkers: 0

https://serratus.github.io/quaggaJS/#node-example

答案 2 :(得分:0)

添加/ *禁止eslint禁用* /返回。

          {
/* eslint-disable */
this.state.results.map(result => {
            <Result key={result.codeResult.code} result={result} />;
          })}
        </ul>

答案 3 :(得分:0)

可能来不及回答,留在这里以防有人感兴趣。

我能够让它发挥作用。我必须解决的问题很少:

  1. 按照rpld的建议应用不同的解码器:
decoder: {
  readers: ["ean_reader"]
},
  1. 修正 TypeError: this.props.onDetected is not a function。在 App.js 中调整了以下行:
    来自: {this.state.scanning ? <Scanner onDetected={this.state._onDetected} /> : null}
    致:
    {this.state.scanning ? <Scanner onDetected={(result) => this._onDetected(result)} /> : null}
  2. 我也使用了 Quagga2,但不知道在这种情况下是否有什么不同。

留下完整版的工作代码,以防万一。

App.js:

import React, { Component } from 'react';
import Scanner from './Scanner';
import Result from './Result';

export default class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      scanning: false,
      results: []
    };
    this._scan = this._scan.bind(this);
    this._onDetected = this._onDetected.bind(this);
  }

  _scan() {
    this.setState({ scanning: !this.state.scanning });
  }

  _onDetected(result) {
    this.setState({ results: this.state.results.concat([result]) });
  }
  render() {
    return (
      <div>
        <button onClick={this._scan}>{this.state.scanning ? 'Stop' : 'Start'}</button>
        <ul className="results">
          {this.state.results.map((result, idx) => {
            return (<Result key={result.codeResult.code} result={result} />)
          })}
        </ul>
        {this.state.scanning ? <Scanner onDetected={(result) => this._onDetected(result)} /> : null}
      </div>
    );
  }
}

Scanner.js:

import React, { Component } from 'react';
import Quagga from '@ericblade/quagga2';

export default class Scanner extends Component {
    constructor(props) {
        super(props);
        this._onDetected = this._onDetected.bind(this);
    }

    componentDidMount() {
        Quagga.init(
            {
                inputStream: {
                    type: 'LiveStream',
                    constraints: {
                        width: 640,
                        height: 480,
                        facingMode: 'environment', // or user
                    },
                },
                locator: {
                    patchSize: 'medium',
                    halfSample: true,
                },
                numOfWorkers: 0,
                decoder: {
                    readers: ['ean_reader'],
                },
                locate: true,
            },
            function(err) {
                if (err) {
                    return console.log(err);
                }
                Quagga.start();
            }
        );
        Quagga.onDetected(this._onDetected);
    }

    componentWillUnmount() {
        Quagga.offDetected(this._onDetected);
    }

    _onDetected(result) {
        this.props.onDetected(result);
    }

    render() {
        return <div id="interactive" className="viewport" />;
    }
}

Result.js(根本没有接触过):

import React, { Component } from 'react';

export default class Result extends Component {
    render() {
        const result = this.props.result;

        if (!result) {
            return null;
        }
        return (
            <li >
                {result.codeResult.code} [{result.codeResult.format}]
            </li>
        );
    }
}