如何用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的使用,相比memcached
redis支持数据持久化,不像memcached断电或者重启就丢失数据了. 支持持久化主要有两种方式,在redis.conf配置文件里配置. 1. 使用.rdb格式存储,配置save参数(save N ...
- mysql中唯一约束用法
以前比较naive,有次同事一定要在表里建唯一约束的时候,我就很纳闷为啥非要在db层面做限制,在自己的业务代码里做啊,就是说入库的时候先查一遍有没有,没有记录的情况再准许入库. 后来发现如果只是自己处 ...
- 随机取出数组中的某些值,并删除它们array_splice,array_slice
今天遇到这样一个情景.这样的,一个抽奖活动,预先获取一堆人参与信息,一个人最多只能中奖一次.活动有活动的配置信息,比如说一等奖有多少人,二等奖有多少人等等.. 说白了,就是从这个参与人数组里,取出来一 ...
- CoreFoundation对象的内存管理
近来没什么新项目做,想学习一些swift开源项目,看了几个文件感觉有点懵,可能水平还没达到,等用到具体内容的时候再去仔细看吧. 关于现在的项目,想想单元测试还可以完善一下,就在framwork工程中写 ...
- 20155316 Exp1 PC平台逆向破解(5)M
前绪 实验收获与感想 初步从三个途径了解了什么是缓冲区溢出以及如何简单实现它,对汇编与反汇编有更直观的了解. 什么是漏洞?漏洞有什么危害? 漏洞是指机器体制设计时所没有顾及到的.可以被利用的bug,放 ...
- # 20155319 Exp3 免杀原理与实践
20155319 Exp3 免杀原理与实践 基础问题 (1)杀软是如何检测出恶意代码的? 基于特征码的检测 启发式的恶意软件检测 基于行为的恶意软件检测 (2)免杀是做什么? 免杀,从字面进行理解,避 ...
- vue 使用 proxyTable 解决跨域问题
1.在 main.js 中,在引入 axios: import axios from 'axios' Vue.config.productionTip = false Vue.prototype.$a ...
- phpmyadmin连接MySQL服务器被拒绝
显示: phpMyAdmin 尝试连接到MySQL服务器,但服务器拒绝连接.您应该检查配置文件中的主机.用户名和密码,并确认这些信息与 MySQL 服务器管理员所给出的信息一致. 原因: 可能是修改了 ...
- Beyond Compare文本对比中提示编辑禁止的解决方法
Beyond Compare是一款拥有文本比较功能的智能化软件,它支持在文本比较的同时,直接对差异文本进行修改.删除.编辑等一系列操作,这样一来,节约了文本对比的时间.但是在使用Beyond Comp ...
- Docker部署Zookeeper容器
获取zookeeper镜像 docker pull zookeeper 创建zookeeper容器 docker run --name="zookeeper" -p 2181:21 ...