如何用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 突然想起来我这个博客冷落了好多年了,也该更新一下,呵 ...
随机推荐
- Oracle中Error while performing database login with the XXXdriver; Listener refused the connection with the following error; ORA-12505,TNS:listener does not currently know of SID given inconnect descrip
一次连接数据库怎么也连接不上,查了多方面资料,终于找到答案,总结 首先应该保证数据库的服务启动 在myeclipse的数据库视图中点 右键->new 弹出database driver的窗口, ...
- 20155334 曹翔 Exp2 后门原理与实践
20155334 曹翔 Exp2 后门原理与实践 不多废话直接上实验过程,本实验的所有端口都是5334. 一.实验过程 查询主机Windows和虚拟机kali的ip地址: Windows获得Linux ...
- 【MEF】构建一个WPF版的ERP系统
原文:[MEF]构建一个WPF版的ERP系统 引言 MEF是微软的一个扩展性框架,遵循某种约定将各个部件组合起来.而ERP系统的一大特点是模块化,它们两者的相性很好,用MEF构建一个ERP系统是相当合 ...
- Selenium 爬取全国水质周报Word
很久没写爬虫了 ,昨天有个学姐说需要爬取水质的一些数据,给了个网站( http://xxfb.hydroinfo.gov.cn/ssIndex.html?type=2&tdsourcetag= ...
- PostgreSQL杂记页
磨砺技术珠矶,践行数据之道,追求卓越价值 luckyjackgao@gmail.com 返回顶级页:PostgreSQL索引页 此页,记录其他数据库,linux以及各种点滴事项 1--数据库设计 1 ...
- CF434D Nanami's Power Plant
就是切糕那道题,首先对每个函数连一串,然后\(x_u\leq x_v+d\)这个条件就是\(u\)函数\(i\)取值连向\(v\)函数\(i-d\)取值边权为inf,然后答案就是最小割了. #incl ...
- 【Android UI设计与开发】第02期:引导界面(二)使用ViewPager实现欢迎引导页面
本系列文章都会以一个程序的实例开发为主线来进行讲解,以求达到一个循序渐进的学习效果,这样更能加深大家对于程序为什么要这样写的用意,理论加上实际的应用才能达到事半功倍的效果,不是吗? 最下方有源码的下载 ...
- C#_Winform_聊天机器人
最近研究微信公众平台,搭建了一个微信聊天机器人,调用小黄鸡的公众接口,实现在线和小黄鸡聊天的功能. 接口调用不是很麻烦,不过是php版本,所以研究了一下C#的功能模块, Winfrom版 using ...
- Asp.Net_获取IP地址
//方法一 HttpContext.Current.Request.UserHostAddress; //方法二 HttpContext.Current.Request.ServerVariables ...
- JMeter:生成漂亮的接口/压力测试的HTML报告
JMeter生成HTML网页报告(非gui模式操作) 我们做性能测试的时候会经常使用一些性能测试工具,比如loardrunner和jmeter,我个人比较喜欢Jmeter这个工具,jmeter之前版本 ...