这篇文章分享下如何结合React Webcam和Dynamsoft JavaScript Barcode SDK来创建Web扫码App。
  
  Web实时扫码
  
  从GitHub上下载react-webcam.js放到React工程中。 打开这个JS文件。在render()函数中添加一个button和canvas:
  
  render() {
  
  return (
  
  <div id='videoview' width={this.props.width} height={this.props.height}>
  
  <button onClick={this.scanBarcode}>Scan Barcodes</button>
  
  <video
  
  autoPlay
  
  width={this.props.width}
  
  height={this.props.height}
  
  src={this.state.src}
  
  muted={this.props.audio}
  
  className={this.props.className}
  
  playsInline
  
  style={this.props.style}
  
  ref={(ref) => {
  
  this.video = ref;
  
  }}
  
  />
  
  <canvas id="overlay" width={this.props.width} height={this.props.height}></canvas>
  
  </div>
  
  button用于触发扫码,canva用来绘制显示结果。为了让结果显示在video上方,创建一个react-webcam.css调整下布局:
  
  #videoview {
  
  position: relative;
  
  width: 640px;
  
  height: 480px;
  
  #video {
  
  position: relative;
  
  width: 100%;
  
  height: 100%;
  
  z-index: 1
  
  #overlay {
  
  position: absolute;
  
  top: 100;
  
  left: 0;
  
  width: 100%;
  
  height: 100%;
  
  z-index: 2
  
  在react-webcam.js中导入这个CSS文件:
  
  import React, { Component } from 'react';
  
  import PropTypes from 'prop-types';
  
  import './react-webcam.css';
  
  创建button的响应事件scanBarcode()。函数触发时,先把video绘制到一个临时canvas上。然后得到图像数据传入barcode解码接口中:
  
  scanBarcode() {
  
  if (window.reader) {
  
  let canvas = document.createElement('canvas');
  
  canvas.width = this.props.width;
  
  canvas.height = this.props.height
  
  let ctx = canvas.getContext('2d');
  
  ctx.drawImage(this.video, 0, 0, this.props.width, this.props.height);
  
  window.reader.decodeBuffer(
  
  ctx.getImageData(0, 0, canvas.width, canvas.height).data,
  
  canvas.width,
  
  canvas.height,
  
  canvas.width * 4,
  
  window.dynamsoft.BarcodeReader.EnumImagePixelFormat.IPF_ARGB_8888
  
  .then((results) => {
  
  this.showResults(results);
  
  构造函数中需要绑定this。如果不绑定,this在button点击的时候无法使用:
  
  constructor() {
  
  super();
  
  this.state = {
  
  hasUserMedia: false,
  
  this.scanBarcode = this.scanBarcode.bind(this);
  
  扫描触发之后需要持续调用scanBarcode(),并把结果显示在video上:
  
  showResults(results) {
  
  let context = this.clearOverlay();
  
  let txts = [];
  
  try {
  
  let localization;
  
  for (var i = 0; i < results.length; ++i) {
  
  if (results[i].LocalizationResult.ExtendedResultArray[0].Confidence >= 30) {
  
  txts.push(results[i].BarcodeText);
  
  localization = results[i].LocalizationResult;
  
  this.drawResult(context, localization, results[i].BarcodeText);
  
  this.scanBarcode();
  
  } catch (e) {
  
  this.scanBarcode();
  
  clearOverlay(www.tiaotiaoylzc.com/) {
  
  let context = document.getElementById('overlay').getContext('2d');
  
  context.clearRect(0, 0, this.props.width, this.props.height);
  
  context.strokeStyle = '#ff0000';
  
  context.lineWidth = 5;
  
  return context;
  
  drawResult(context, localization, text) {
  
  context.beginPath();
  
  context.moveTo(localization.X1, localization.Y1);
  
  context.lineTo(localization.X2, localization.Y2);
  
  context.lineTo(localization.X3, localization.Y3);
  
  context.lineTo(www.yongshi123.cn localization.X4, localization.Y4);
  
  context.lineTo(www.tiaotiaoylzc.com localization.X1, localization.Y1);
  
  context.stroke(www.yongshiyule178.com/);
  
  context.font = '18px Verdana';
  
  context.fillStyle = '#ff0000';
  
  let x = [ localization.X1, localization.X2, localization.X3, localization.X4 ];
  
  let y = [ localization.Y1, localization.Y2, localization.Y3, localization.Y4 ];
  
  x.sort(function(a, www.zhenghongyule.cn b) {
  
  return a - b;
  
  });
  
  y.sort(function(a, b) {
  
  return b - a;
  
  });
  
  let left = x[0];
  
  let top = y[0];
  
  context.fillText(text, left, top + 50);
  
  在public/index.html中加载dbr-6.4.1.3.min.js文件,并创建可用于全局访问的window.reader:
  
  <body>
  
  <img src="loading.gif" style="margin-top:10px" id="anim-loading">
  
  <script src="https://demo.dynamsoft.com/dbr_www.yingka178.com wasm/js/dbr-6.4.1.3.min.js"></script>
  
  <script>
  
  dynamsoft.dbrEnv.resourcesPath = 'https://www.tianjuyuLe.cn demo.dynamsoft.com/dbr_wasm/js';
  
  dynamsoft.dbrEnv.onAutoLoadWasmSuccess =www.dashuju2.cn function () {
  
  window.reader = new dynamsoft.BarcodeReader(www.wujirongyaoy.com);
  
  window.dynamsoft = dynamsoft;
  
  document.getElementById('anim-loading'www.yibaoyule1.com).style.display = 'none';
  
  };
  
  dynamsoft.dbrEnv.onAutoLoadWasmError = function (ex) {
  
  document.getElementById('anim-loading').style.display = 'none';
  
  alert('Fail to load the wasm file.');
  
  };
  
  dynamsoft.dbrEnv.bUseWorker = true;
  
  // Get a free trial license from https://www.michenggw.com /CustomerPortal/Portal/TrialLicense.aspx
  
  dynamsoft.dbrEnv.licenseKey = "Your Barcode SDK License"
  
  </script>
  
  <div id="root"></div>
  
  这里必须把dynamsoft.dbrEnv.bUseWorker设置成true。只有使用了web worker,主线程才不会因为条形码识别耗时而卡住。
  
  在App.js中添加React Webcam:
  
  import React, { Component } from 'react';
  
  import logo from './logo.svg';
  
  import './App.css';
  
  import {Barcode} from './Barcode';
  
  import Webcam from './react-webcam';
  
  class App extends www.zhongyiyuL.cn Component {
  
  render() {
  
  return (
  
  <div className="App">
  
  <Barcode/>
  
  <Webcam />
  
  </div>
  
  export default App;
  
  运行程序:
  
  npm start
  
  在浏览器中访问localhost:3000:

如何用React, Webcam和JS Barcode SDK创建Web扫码App的更多相关文章

  1. 超简单集成HMS Scan Kit扫码SDK,轻松实现扫码购

    前言   在前面的文章中,我们向大家介绍了HMS Scan Kit 的快速集成方法以及HMS Scan Kit和其他开源扫码工具的竞争力对比分析,如果没有看到也没关系,文章下方的往期链接中有文章入口. ...

  2. 06-Node.js学习笔记-创建web服务器

    创建web服务器 //引用系统模块 const http = require('http'); //创建web服务器 //用于处理url地址 const url = require('url'); c ...

  3. react快速上手一(使用js语法,创建虚拟DOM元素)

    1.装包,引包 首先需要安装两个包 react ,react-dom cnpm i react react-dom 介绍下这两个包: react:专门用来创建React组件,组件生命周期等这些东西. ...

  4. iOS开发——iOS7(及以后版本) SDK自带二维码(含条形码)扫码、二维码生成

    本文转载至 http://www.cnblogs.com/leotangcn/p/4357907.html 现在很多APP都涉及了二维码扫码功能,这个功能简单实用,很多情况下用户乐于使用,现在本文带来 ...

  5. 如何用 React Native 创建一个iOS APP?(三)

    前两部分,<如何用 React Native 创建一个iOS APP?>,<如何用 React Native 创建一个iOS APP (二)?>中,我们分别讲了用 React ...

  6. 如何用 React Native 创建一个iOS APP?(二)

    我们书接上文<如何用 React Native 创建一个iOS APP?>,继续来讲如何用 React Native 创建一个iOS APP.接下来,我们会涉及到很多控件. 1 AppRe ...

  7. 如何用 React Native 创建一个iOS APP?

    诚然,React Native 结合了 Web 应用和 Native 应用的优势,可以使用 JavaScript 来开发 iOS 和 Android 原生应用.在 JavaScript 中用 Reac ...

  8. 简单的Windows Webcam应用:Barcode Reader

    原文:简单的Windows Webcam应用:Barcode Reader 在Windows上用WinForm创建一个Webcam应用需要用到DirectShow.DirectShow没有提供C#的接 ...

  9. 使用React、Node.js、MongoDB、Socket.IO开发一个角色投票应用的学习过程(三)

    这几篇都是我原来首发在 segmentfault 上的地址:https://segmentfault.com/a/1190000005040834 突然想起来我这个博客冷落了好多年了,也该更新一下,呵 ...

随机推荐

  1. 常用的php数组函数

    以下是自己比较常用的数组函数 数组元素增加减少array_pusharray_poparray_shiftarray_unshift array_splice  (对数组的增删改) array_sli ...

  2. 解决php的交互式命令行不能正常启动的问题兼介绍psysh

    今天在自己的mac电脑上试着启动php的交互式命令行,发现敲下命令后一直卡在提示进入的地方,但没有出现已经进入的提示符,百度了下应该是与readline有关. 于是安装php的readline扩展,在 ...

  3. final、finally、 finalize 有什么不同

    感觉这三者除了长得像也没啥可作为比较的,但是面试题中经常会出现这三者的比较,就简单总结一下. 1.final final 可以用来修饰类.方法和变量,修饰类的时候表示类是不可以被继承的,修饰方法的时候 ...

  4. POJ1159

    这竟然是IOI虽然是2000年的,但其实也改变不了它水题的本质 我写了两种方法,这里都讲一下吧 考虑记忆化搜索,用f[i][j]表示当区间的左端为i,右端为j时最少要添加多少字符,所以ans就为f[1 ...

  5. UCOSII笔记---信号量、邮箱、消息队列、信号量集、软件定时器

    一.接收邮箱函数的参数:timeout表示的是滴答定时器的节拍数,比如设定5ms为一个节拍,超时为100ms,则timeout=20. void *OSMboxPend (OS_EVENT *peve ...

  6. 同步、异步、回调执行顺序之经典闭包setTimeout分析

    聊聊同步.异步和回调 同步,异步,回调,我们傻傻分不清楚, 有一天,你找到公司刚来的程序员小T,跟他说:“我们要加个需求,你放下手里的事情优先支持,我会一直等你做完再离开”.小T微笑着答应了,眼角却滑 ...

  7. 算法(JAVA)----两道小小课后题

    LZ最近翻了翻JAVA版的数据结构与算法,无聊之下将书中的课后题一一给做了一遍,在此给出书中课后题的答案(非标准答案,是LZ的答案,猿友们可以贡献出自己更快的算法). 1.编写一个程序解决选择问题.令 ...

  8. 【阿里巴巴】CBU技术部招聘

    如果你偏爱技术挑战,希望成就不一样的自己,欢迎投递简历至 yangyang.xiayy@alibaba-inc.com [业务简介] B2B内贸www.1688.com:1688.com是最大的内贸B ...

  9. 微软职位内部推荐-Senior BSP Engineer

    微软近期Open的职位: The position of Sr. BSP engineer requires experience and good knowledge in mobile hardw ...

  10. Windows7下Java运行时环境搭建

    第一步:下载JDK 地址:http://www.oracle.com/technetwork/java/javase/downloads/index.html,(由于Sun于2009年被oracle收 ...