如何用React, Webcam和JS Barcode SDK创建Web扫码App

  这篇文章分享下如何结合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:

原文地址:https://www.cnblogs.com/qwangxiao/p/10456605.html

时间: 2024-08-30 12:01:45

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

轻松使用OpenCV Python控制Webcam,读取Barcode

虽然手机上Barcode应用已经非常流行,但是工作的时候还是用Webcam比较方便.比如需要检测Barcode,我只需要拿Webcam对着电脑屏幕或者纸张扫一下就可以了.今天分享下如何轻松使用OpenCV控制Webcam,以及如何获取一帧图像做Barcode检测. 参考原文:Reading Barcode with Webcam in OpenCV and Python 作者:Xiao Ling 翻译:yushulx 软件安装 Dynamsoft Barcode SDK: http://www.

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

本文转载至 http://www.cnblogs.com/leotangcn/p/4357907.html 现在很多APP都涉及了二维码扫码功能,这个功能简单实用,很多情况下用户乐于使用,现在本文带来iOS7自带二维码扫码的教程,也包括扫条形码,足以满足简单的扫码要求,而且避免使用第三方的繁琐. 后期项目中需要生成二维码,我在git上找到一个很方便使用的轻量级开源库,推荐给有需要的朋友:https://github.com/moqod/ios-qr-code-encoder 下面介绍如何使用iO

简单的Windows Webcam应用:Barcode Reader

原文:简单的Windows Webcam应用:Barcode Reader 在Windows上用WinForm创建一个Webcam应用需要用到DirectShow.DirectShow没有提供C#的接口.如果要用C#开发,需要创建一个桥接DLL.Touchless SDK是一个免费开源的.NET库,对DirectShow进行了简单的封装.使用Touchless可以很方便的在WinForm应用中调用camera.这里分享下如何创建一个调用webcam的barcode reader. 参考原文:Wi

Redux+React Router+Node.js全栈开发

详情请交流  QQ  709639943 01.Java深入微服务原理改造房产销售平台 02.跨平台混编框架 MUI 仿豆瓣电影 APP 03.Node.js入门到企业Web开发中的应用 04.Redux+React Router+Node.js全栈开发 05.Java秒杀系统方案优化 高性能高并发实战 06.企业级刚需Nginx入门,全面掌握Nginx配置+快速搭建高可用架构 07.快速上手Linux 玩转典型应用 08.全面系统讲解CSS 工作应用+面试一步搞定 09.Java Spring

手把手教你webpack、react和node.js环境配置(上篇)

很多人刚学习react的时候,往往因为繁琐的配置而头疼,这里我将手把手教大家怎么用webpack配置react和redux的环境,这篇教程包括前端react和后台node整个网站的环境配置,对node没兴趣的可以只看这篇. 这里是下篇链接:手把手教你webpack.react和node.js环境配置(下篇) 我把所有代码都放到了github上面供参考:webpack-react-express环境配置 1. 什么是webpack? Webpack 是当下最热门的前端资源模块化管理和打包工具.它可

如何用QtPy创建Webcam扫码应用

之前分享了如何用QtPy和Dynamsoft Barcode Reader创建一个简单的桌面应用, 通过加载一张图片来识别条形码.这篇文章要分享如何加上摄像头的支持做实时扫码. 如何用Python和PyQt代码显示Camera视频流 要获取视频流,最简单的方法就是用OpenCV: pip install opencv-python用OpenCV来显示视频流的代码很简单,只需要一个无限循环: import cv2vc = cv2.VideoCapture(0)while True:rval, fr

大数据技术之_18_大数据离线平台_02_Nginx+Mysql+数据收集+Web 工程 JS/JAVA SDK 讲解+Flume 故障后-如何手动上传 Nginx 日志文件至 HDFS 上

十一.Nginx11.1.介绍11.2.常见其他 Web 服务器11.3.版本11.4.Nginx 安装11.5.目录结构11.6.操作命令十二.Mysql12.1.介绍12.2.关系型数据库(SQL)种类12.3.特征12.4.术语12.4.与非关系型数据库比较(Not Only SQL)12.4.1.种类12.4.2.特征12.4.3.总结十三.数据收集13.1.收集方式13.2.数据的事件类型13.2.1.Launch 事件13.2.2.PageView 事件13.3.Nginx 日志收集

vue react 与原生 js jquery 渲染时间简单对比

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" con

React 还是 Vue: 你应该选择哪一个Web前端框架?

学还是要学的,用的多了,也就有更多的认识了,开发中遇到选择的时候也就简单起来了. 本文作者也做了总结: 如果你喜欢用(或希望能够用)模板搭建应用,请使用Vue    如果你喜欢简单和“能用就行”的东西,请使用Vue    如果你的应用需要尽可能的小和快,请使用Vue    如果你计划构建一个大型应用程序,请使用React    如果你想要一个同时适用于Web端和原生App的框架,请选择React    如果你想要最大的生态圈,请使用React    如果你已经对其中一个用得满意了,就没有必要换了