3

基本上我让演示工作除了实际扫描。即相机打开等。不知道我错过了什么......

这是我的代码。

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" />;
    }
}

结果.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

4 回答 4

2

您可能想要更改code_128_reader默认情况下的阅读器类型。

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

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

Quagga 开始的地方。

可以在此处找到读者列表:Quagga 文档

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

于 2018-04-25T12:15:52.963 回答
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,但不知道在这种情况下是否有任何区别。

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

应用程序.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>
    );
  }
}

扫描仪.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>
        );
    }
}
于 2021-02-17T22:20:13.223 回答
0

添加 /* eslint-disable */ in 以返回 .

          {
/* eslint-disable */
this.state.results.map(result => {
            <Result key={result.codeResult.code} result={result} />;
          })}
        </ul>
于 2020-05-18T13:04:03.247 回答
0

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

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

于 2018-10-10T00:46:34.970 回答