这篇文章分享下如何结合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. DOTNET Core 命令

    dotnet 命令目录: 1.dotnet-new 2.dotnet-restore 3.dotnet-build 4.dotnet-run 5.dotnet-test 6.dotnet-pack 7 ...

  2. 软件设计、DDD概念及落地时的一些零碎思考和记录2

    主要是项目中一些落地经验和记录 技术人员.开发人员 大部分程序员真的不善于沟通,经常会显得很保守: 他们技术上的困惑.误解乃至郁闷都很难直接的表达清楚: 他们对自己的错误"印象"很 ...

  3. Django Rest Framework源码剖析(二)-----权限

    一.简介 在上一篇博客中已经介绍了django rest framework 对于认证的源码流程,以及实现过程,当用户经过认证之后下一步就是涉及到权限的问题.比如订单的业务只能VIP才能查看,所以这时 ...

  4. 2017战略No.2:开始电子化记账

    一.懒散的4年 大学毕业后,就没有怎么记账了. 自己花的钱,心里有个大概,但是不能算得很具体. 比如说,2016年,又没有攒几个钱,心里多少有点压抑. 大脑去算账,只能算房租吃饭等金额较大的开销,更多 ...

  5. 【转载】C++引用详解

    原文:http://www.cnblogs.com/gw811/archive/2012/10/20/2732687.html 引用的概念 引用:就是某一变量(目标)的一个别名,对引用的操作与对变量直 ...

  6. 微信小程序云开发之云函数创建

    云函数 云函数是一段运行在云端的代码,无需管理服务器,在开发工具内编写.一键上传部署即可运行后端代码. 小程序内提供了专门用于云函数调用的 API.开发者可以在云函数内使用 wx-server-sdk ...

  7. 使用Redis做分布式

    一 为什么使用 Redis 在项目中使用 Redis,主要考虑两个角度:性能和并发.如果只是为了分布式锁这些其他功能,还有其他中间件 Zookpeer 等代替,并非一定要使用 Redis. 性能: 如 ...

  8. python面试题(四)

    一.数据类型 1.字典 1.1 现有字典 dict={‘a’:24,‘g’:52,‘i’:12,‘k’:33}请按字典中的 value 值进行排序? sorted(dict.items(),key=l ...

  9. [转]JVM系列三:JVM参数设置、分析

    不管是YGC还是Full GC,GC过程中都会对导致程序运行中中断,正确的选择不同的GC策略,调整JVM.GC的参数,可以极大的减少由于GC工作,而导致的程序运行中断方面的问题,进而适当的提高Java ...

  10. 云容器云引擎:容器化微服务,Istio占C位出道

    在精彩的软件容器世界中,当新项目涌现并解决你认为早已解决的问题时,这感觉就像地面在你的脚下不断地移动.在许多情况下,这些问题很久以前被解决,但现在的云原生架构正在推动着更大规模的应用程序部署,这就需要 ...