如何用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:
如何用React, Webcam和JS Barcode SDK创建Web扫码App的更多相关文章
- 超简单集成HMS Scan Kit扫码SDK,轻松实现扫码购
前言 在前面的文章中,我们向大家介绍了HMS Scan Kit 的快速集成方法以及HMS Scan Kit和其他开源扫码工具的竞争力对比分析,如果没有看到也没关系,文章下方的往期链接中有文章入口. ...
- 06-Node.js学习笔记-创建web服务器
创建web服务器 //引用系统模块 const http = require('http'); //创建web服务器 //用于处理url地址 const url = require('url'); c ...
- react快速上手一(使用js语法,创建虚拟DOM元素)
1.装包,引包 首先需要安装两个包 react ,react-dom cnpm i react react-dom 介绍下这两个包: react:专门用来创建React组件,组件生命周期等这些东西. ...
- iOS开发——iOS7(及以后版本) SDK自带二维码(含条形码)扫码、二维码生成
本文转载至 http://www.cnblogs.com/leotangcn/p/4357907.html 现在很多APP都涉及了二维码扫码功能,这个功能简单实用,很多情况下用户乐于使用,现在本文带来 ...
- 如何用 React Native 创建一个iOS APP?(三)
前两部分,<如何用 React Native 创建一个iOS APP?>,<如何用 React Native 创建一个iOS APP (二)?>中,我们分别讲了用 React ...
- 如何用 React Native 创建一个iOS APP?(二)
我们书接上文<如何用 React Native 创建一个iOS APP?>,继续来讲如何用 React Native 创建一个iOS APP.接下来,我们会涉及到很多控件. 1 AppRe ...
- 如何用 React Native 创建一个iOS APP?
诚然,React Native 结合了 Web 应用和 Native 应用的优势,可以使用 JavaScript 来开发 iOS 和 Android 原生应用.在 JavaScript 中用 Reac ...
- 简单的Windows Webcam应用:Barcode Reader
原文:简单的Windows Webcam应用:Barcode Reader 在Windows上用WinForm创建一个Webcam应用需要用到DirectShow.DirectShow没有提供C#的接 ...
- 使用React、Node.js、MongoDB、Socket.IO开发一个角色投票应用的学习过程(三)
这几篇都是我原来首发在 segmentfault 上的地址:https://segmentfault.com/a/1190000005040834 突然想起来我这个博客冷落了好多年了,也该更新一下,呵 ...
随机推荐
- Redis Replication
Replication 官网说明:http://www.redis.io/topics/replication Redis使用异步复制; 一个Master可以有多个Slaves; Slaves可以接收 ...
- Modelsim SE 和 Quartus II 编译器(综合器)的区别
当对目标模块进行RTL描述后,习惯先会用Modelsim做一下功能仿真.当我们写好Tensbench文件,直接在Modelsim SE中对源文件(design和Testbench)进行编译时,如果源文 ...
- WPF之Manipulation
原文:WPF之Manipulation 需求:现,在窗口下有一个StackPanel控件. 1.可以拖动. 2.可以展开及收缩(不仅仅可以拖动还可以点击) 3.窗口向坐标轴一样分四个象限,在不同的区域 ...
- 汇编 REPNE/REPNZ 指令,SCASB 指令
知识点: REPNE/REPNZ 指令 SCASB 指令 一.SCASB 指令 cmp byte ptr [edi],al //对标志位的影响相当于sub指令 //同时还会修改寄存器EDI的值:如 ...
- stm32f051 DMA需要注意的一点
STM32f051的DMA注意事项 问题说明:在使用ADC的DMA通道时,遇到了序列转换的乱序问题,我使用的是DMA循环模式,但是采集的数据却总是错的:第二个内存地址存放的是ADC序列转换中的第一个通 ...
- Java过滤器与SpringMVC拦截器的差异学习笔记
学习摘录地址:http://blog.csdn.net/chenleixing/article/details/44573495 今天学习和认识了一下,过滤器和SpringMVC的拦截器的区别,学到了 ...
- vuex实践之路——笔记本应用(三)
Actions Action 类似于 mutation,不同在于: Action 提交的是 mutation,而不是直接变更状态. Action 可以包含任意异步操作. 让我们来注册一个简单的 act ...
- 在Microsoft Dynamic 365/2016环境使用LinqPad查询数据(不使用linqpad Microsoft Dynamic 365 Driver)
在Microsoft Dynamic 365/2016环境使用LinqPad查询数据 老规矩,先上效果图: 实体集合: 实体属性: 属性值: 查询出的结果可以导出的格式: 操作步骤: 1.下载Lin ...
- Metasploit 暴力破解演示
本文简要演示使用Metasploit 中的mysql_login.postgresql_login.tomcat_mgr_login模块暴力破解Metasploitable 2 上部署的服务. Pre ...
- 《Macro-Micro Adversarial Network for Human Parsing》论文阅读笔记
<Macro-Micro Adversarial Network for Human Parsing> 摘要:在人体语义分割中,像素级别的分类损失在其低级局部不一致性和高级语义不一致性方面 ...