17 Commits
Author SHA1 Message Date
yugasun b9d89e0545 0.3.1 2022-05-29 12:07:49 +08:00
yugasun aeee33ee6c chore: add unpkg browser field 2022-05-29 12:07:37 +08:00
yugasun a5cd17b951 0.3.0 2022-05-29 11:23:48 +08:00
yugasun 615081cc95 fix: support customize camera size 2022-05-29 11:23:04 +08:00
yugasun 7d77236fc0 fix: try to use rear camera by default 2022-05-29 11:04:57 +08:00
yugasun 8c80e312b3 0.2.2 2021-07-06 15:42:41 +08:00
yugasun 6ab5e80028 fix: remove unuse file 2021-07-06 15:42:28 +08:00
yugasun 05b1c0d310 0.2.1 2021-07-06 15:40:23 +08:00
Lőrik LeventeandGitHub 4161b77272 fix: re add module property (#16)
* Fix typo

* Fix require

* Remove deprecated nextjs

* Update package.json

* Update package.json
2021-07-06 15:36:25 +08:00
yugasun be1f6a980b chore: fix tsconfig 2021-06-30 14:45:00 +08:00
yugasun 44737ddfe7 0.2.0 2021-05-19 10:46:47 +08:00
yugasun 032f9ba16a chore: update deploy to pages 2021-05-19 10:41:53 +08:00
yugasun d1a5b9de6b chore: update demo 2021-05-19 10:40:00 +08:00
yugasun fd6daa86a1 chore: fix deps 2021-05-12 20:52:14 +08:00
yugasun b8d25e8969 docs: update readme 2021-05-12 20:29:45 +08:00
yugasun 455b0deb25 0.1.3 2021-05-12 20:26:35 +08:00
yugasun d77b666da2 fix: crossOrigin on IOS, close #14 2021-05-12 20:26:14 +08:00
13 changed files with 200 additions and 181 deletions
+2
View File
@@ -9,3 +9,5 @@ tsconfig.tsbuildinfo
coverage coverage
yarn.lock yarn.lock
.rpt2_cache .rpt2_cache
demo/lib/index.min.js
+7
View File
@@ -20,3 +20,10 @@ tsconfig.json
tsconfig.base.json tsconfig.base.json
.rpt2_cache .rpt2_cache
.vscode .vscode
/demo
/doc
/scripts
tslint.json
yarn-error.log
.babelrc
yarn.lock
+2 -1
View File
@@ -1,7 +1,8 @@
# [qrcode-decoder](https://github.com/yugasun/qrcode-decoder) # [qrcode-decoder](https://github.com/yugasun/qrcode-decoder)
[![license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/yugasun/qrcode-decoder/blob/master/LICENSE) [![npm](https://img.shields.io/npm/v/qrcode-decoder)](http://www.npmtrends.com/qrcode-decoder)
[![NPM downloads](http://img.shields.io/npm/dm/qrcode-decoder.svg?style=flat-square)](http://www.npmtrends.com/qrcode-decoder) [![NPM downloads](http://img.shields.io/npm/dm/qrcode-decoder.svg?style=flat-square)](http://www.npmtrends.com/qrcode-decoder)
[![license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/yugasun/qrcode-decoder/blob/master/LICENSE)
[简体中文](./README.zh-CN.md) | English [简体中文](./README.zh-CN.md) | English
+2 -1
View File
@@ -1,7 +1,8 @@
# [qrcode-decoder](https://github.com/yugasun/qrcode-decoder) # [qrcode-decoder](https://github.com/yugasun/qrcode-decoder)
[![license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/yugasun/qrcode-decoder/blob/master/LICENSE) [![npm](https://img.shields.io/npm/v/qrcode-decoder)](http://www.npmtrends.com/qrcode-decoder)
[![NPM downloads](http://img.shields.io/npm/dm/qrcode-decoder.svg?style=flat-square)](http://www.npmtrends.com/qrcode-decoder) [![NPM downloads](http://img.shields.io/npm/dm/qrcode-decoder.svg?style=flat-square)](http://www.npmtrends.com/qrcode-decoder)
[![license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/yugasun/qrcode-decoder/blob/master/LICENSE)
简体中文 | [English](./README.md) 简体中文 | [English](./README.md)
+16 -7
View File
@@ -1,19 +1,21 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head>
<head>
<meta charset="UTF-8" /> <meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>QrcodeDecoder - Camera</title> <title>QrcodeDecoder - Camera</title>
</head> </head>
<body>
<body>
<button id="start">Start</button> <button id="stop">Stop</button><br /> <button id="start">Start</button> <button id="stop">Stop</button><br />
<span id="result">Click start to scan qrcode.</span><br /> <span id="result">Click start to scan qrcode.</span><br />
<hr /> <hr />
<video id="video" autoplay></video> <video id="video" autoplay></video>
<script src="./lib/vconsole.min.js"></script> <script src="./lib/vconsole.min.js"></script>
<script src="./index.min.js"></script> <script src="./lib/index.min.js"></script>
<script type="text/javascript"> <script type="text/javascript">
var vConsole = new VConsole(); var vConsole = new VConsole();
console.log('Hello world'); console.log('Hello world');
@@ -29,17 +31,24 @@
throw new Error('Canvas and getUserMedia are required'); throw new Error('Canvas and getUserMedia are required');
} }
let code = await qr.decodeFromCamera(video); let code = await qr.decodeFromCamera(video,
// you can customize your camera size like below
// {
// width: 400,
// height: 400,
// }
);
console.log('code', code); console.log('code', code);
result.innerText = 'Result: ' + code.data; result.innerText = 'Result: ' + code.data;
} }
start.onclick = startScan; start.onclick = startScan;
stop.onclick = function() { stop.onclick = function () {
qr.stop(); qr.stop();
}; };
} }
main(); main();
</script> </script>
</body> </body>
</html> </html>
+20 -15
View File
@@ -1,30 +1,34 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head>
<head>
<meta charset="UTF-8" /> <meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>QrcodeDecoder - Image</title> <title>QrcodeDecoder - Image</title>
</head> </head>
<body>
<body>
<section> <section>
<h3>Same domain image</h3> <h3>Same domain image</h3>
<img src="./assets/qrcode.png" alt="qr code" /><br /> <img id="img1" src="./assets/qrcode.png" alt="qr code" /><br />
<button id="decode1">Decode!</button><br /> <button id="decode1">Decode!</button><br />
<span id="result1"></span><br /> <span id="result1"></span><br />
</section> </section>
<hr /> <hr />
<section> <section>
<h3>Different domain image</h3> <h3>Different domain image</h3>
<input
id="img2"
value="https://yugasun.com/static/wechat.jpg"
style="width: 400px" /><br />
<button id="decode2">Decode!</button><br /> <button id="decode2">Decode!</button><br />
<span id="result2"></span><br /> <span id="result2"></span><br />
</section> </section>
<script src="./lib/vconsole.min.js"></script> <script src="./lib/vconsole.min.js"></script>
<script src="./index.min.js"></script> <script src="./lib/index.min.js"></script>
<script src="./index.min.js"></script>
<script type="module"> <script type="module">
var vConsole = new VConsole(); var vConsole = new VConsole();
console.log('Hello world');
function main() { function main() {
var qr = new QrcodeDecoder(); var qr = new QrcodeDecoder();
@@ -32,12 +36,13 @@
var btn2 = document.querySelector('button#decode2'); var btn2 = document.querySelector('button#decode2');
var result1 = document.querySelector('#result1'); var result1 = document.querySelector('#result1');
var result2 = document.querySelector('#result2'); var result2 = document.querySelector('#result2');
var img = document.querySelector('img'); var img1 = document.querySelector('#img1');
var img2 = document.querySelector('#img2');
btn1.onclick = async () => { btn1.onclick = async () => {
// you can also decode from image path // you can also decode from image path
// const code = await qr.decodeFromImage('./assets/qrcode.png'); // const code = await qr.decodeFromImage('./assets/qrcode.png');
const code = await qr.decodeFromImage(img); const code = await qr.decodeFromImage(img1);
console.log(code); console.log(code);
result1.innerText = code.data; result1.innerText = code.data;
}; };
@@ -45,17 +50,17 @@
btn2.onclick = async () => { btn2.onclick = async () => {
// you can also decode from image path // you can also decode from image path
// const code = await qr.decodeFromImage('./assets/qrcode.png'); // const code = await qr.decodeFromImage('./assets/qrcode.png');
const code = await qr.decodeFromImage( const code = await qr.decodeFromImage(img2.value, {
'https://yugasun.com/static/wechat.jpg',
{
crossOrigin: 'anonymous', crossOrigin: 'anonymous',
}, });
);
console.log(code); console.log(code);
result2.innerText = code.data; result2.innerText = code.data;
}; };
} }
window.onload = () => {
main(); main();
};
</script> </script>
</body> </body>
</html> </html>
-1
View File
File diff suppressed because one or more lines are too long
+9 -7
View File
@@ -1,21 +1,22 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head>
<head>
<meta charset="UTF-8" /> <meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>QrcodeDecoder - Video</title> <title>QrcodeDecoder - Video</title>
</head> </head>
<body>
<body>
<button id="start">Start</button> <button id="stop">Stop</button><br /> <button id="start">Start</button> <button id="stop">Stop</button><br />
<span id="result">Click start to scan qrcode.</span><br /> <span id="result">Click start to scan qrcode.</span><br />
<video src="./assets/qrcode-video.mp4"></video> <video src="./assets/qrcode-video.mp4"></video>
<script src="./lib/vconsole.min.js"></script> <script src="./lib/vconsole.min.js"></script>
<script src="./index.min.js"></script> <script src="./lib/index.min.js"></script>
<script type="text/javascript"> <script type="text/javascript">
var vConsole = new VConsole(); var vConsole = new VConsole();
console.log('Hello world');
function main() { function main() {
var video = document.querySelector('video'); var video = document.querySelector('video');
var result = document.querySelector('#result'); var result = document.querySelector('#result');
@@ -25,7 +26,7 @@
start.onclick = startScan; start.onclick = startScan;
stop.onclick = function() { stop.onclick = function () {
qr.stop(); qr.stop();
video.pause(); video.pause();
}; };
@@ -39,5 +40,6 @@
} }
main(); main();
</script> </script>
</body> </body>
</html> </html>
+11 -5
View File
@@ -1,8 +1,11 @@
{ {
"name": "qrcode-decoder", "name": "qrcode-decoder",
"version": "0.1.3", "version": "0.3.1",
"description": "Tool for decoding qrcode", "description": "Tool for decoding qrcode",
"main": "dist/index.js", "main": "dist/index.js",
"browser": "dist/index.min.js",
"unpkg": "dist/index.min.js",
"module": "dist/index.esm.js",
"sideEffects": false, "sideEffects": false,
"scripts": { "scripts": {
"build": "ts-node -P scripts/tsconfig.json scripts/bundle.ts umd,esm,aio && npm run copy-file", "build": "ts-node -P scripts/tsconfig.json scripts/bundle.ts umd,esm,aio && npm run copy-file",
@@ -10,9 +13,10 @@
"prettier:fix": "prettier --write '**/*.{ts,tsx,md}' --config .prettierrc", "prettier:fix": "prettier --write '**/*.{ts,tsx,md}' --config .prettierrc",
"test": "jest", "test": "jest",
"codecov": "jest --coverage && codecov", "codecov": "jest --coverage && codecov",
"copy-file": "cp dist/index.min.js demo/", "copy-file": "cp dist/index.min.js demo/lib",
"release": "git commit -am $npm_package_version && git tag $npm_package_version && git push && git push --tags", "release": "git commit -am $npm_package_version && git tag $npm_package_version && git push && git push --tags",
"deploy": "gh-pages -d demo --remote origin" "pages": "gh-pages -d demo --remote origin",
"demo": "http-server demo -c-1"
}, },
"husky": { "husky": {
"hooks": { "hooks": {
@@ -54,11 +58,13 @@
"codecov": "^3.8.1", "codecov": "^3.8.1",
"cross-env": "^6.0.3", "cross-env": "^6.0.3",
"fancy-log": "^1.3.3", "fancy-log": "^1.3.3",
"gh-pages": "^3.1.0",
"http-server": "^14.1.0",
"husky": "^3.0.9", "husky": "^3.0.9",
"jest": "^24.9.0", "jest": "^24.9.0",
"lint-staged": "^9.5.0", "lint-staged": "^9.5.0",
"prettier": "^1.18.2", "prettier": "^1.18.2",
"rolllup": "^2.38.1", "rollup": "^2.38.1",
"rollup-plugin-commonjs": "^8.3.0", "rollup-plugin-commonjs": "^8.3.0",
"rollup-plugin-json": "^3.1.0", "rollup-plugin-json": "^3.1.0",
"rollup-plugin-node-builtins": "^2.1.2", "rollup-plugin-node-builtins": "^2.1.2",
@@ -86,6 +92,6 @@
}, },
"homepage": "https://github.com/yugasun/qrcode-decoder#readme", "homepage": "https://github.com/yugasun/qrcode-decoder#readme",
"dependencies": { "dependencies": {
"jsqr": "^1.3.1" "jsqr": "^1.4.0"
} }
} }
+2 -12
View File
@@ -20,16 +20,6 @@ const plugins: rollup.Plugin[] = [
commonjs(), commonjs(),
typescript2({ typescript2({
tsconfig: path.join(__dirname, '..', 'tsconfig.json'), tsconfig: path.join(__dirname, '..', 'tsconfig.json'),
typescript: ts, // ensure we're using the same typescript (3.x) for rollup as for regular builds etc
tsconfigOverride: {
module: 'esnext',
stripInternal: true,
emitDeclarationOnly: false,
composite: false,
declaration: false,
declarationMap: false,
sourceMap: false,
},
}), }),
]; ];
@@ -77,7 +67,7 @@ async function bundleAio() {
uglify({ uglify({
compress: { compress: {
drop_debugger: true, drop_debugger: true,
drop_console: true, drop_console: false,
}, },
}), }),
], ],
@@ -86,7 +76,7 @@ async function bundleAio() {
file: 'dist/index.min.js', file: 'dist/index.min.js',
name: 'QrcodeDecoder', name: 'QrcodeDecoder',
format: 'iife', format: 'iife',
sourcemap: false, sourcemap: true,
}); });
} }
+31 -8
View File
@@ -1,6 +1,11 @@
import { Options } from 'jsqr'; import { Options } from 'jsqr';
import jsQR from 'jsqr'; import jsQR from 'jsqr';
const videoSize = {
width: { min: 360, ideal: 720, max: 1080 },
height: { min: 360, ideal: 720, max: 1080 },
};
class QrcodeDecoder { class QrcodeDecoder {
timerCapture: null | NodeJS.Timeout; timerCapture: null | NodeJS.Timeout;
canvasElem: null | HTMLCanvasElement; canvasElem: null | HTMLCanvasElement;
@@ -19,7 +24,8 @@ class QrcodeDecoder {
this.videoElem = null; this.videoElem = null;
this.getUserMediaHandler = null; this.getUserMediaHandler = null;
this.videoConstraints = { this.videoConstraints = {
video: true, // default use rear camera
video: { ...videoSize, facingMode: { exact: 'environment' } },
audio: false, audio: false,
}; };
@@ -149,7 +155,7 @@ class QrcodeDecoder {
* inversionAttempts - (attemptBoth (default), dontInvert, onlyInvert, or invertFirst) * inversionAttempts - (attemptBoth (default), dontInvert, onlyInvert, or invertFirst)
* refer to jsqr options: https://github.com/cozmo/jsQR * refer to jsqr options: https://github.com/cozmo/jsQR
*/ */
async decodeFromCamera(videoElem: HTMLVideoElement, options = {}) { async decodeFromCamera(videoElem: HTMLVideoElement, options: any = {}) {
const opts = { const opts = {
...this.defaultOption, ...this.defaultOption,
...options, ...options,
@@ -160,10 +166,29 @@ class QrcodeDecoder {
throw new Error("Couldn't get video from camera"); throw new Error("Couldn't get video from camera");
} }
let stream: MediaStream;
try { try {
const stream = await navigator.mediaDevices.getUserMedia( stream = await navigator.mediaDevices.getUserMedia(this.videoConstraints);
this.videoConstraints, } catch (e) {
); if ((e as OverconstrainedError).name === 'OverconstrainedError') {
console.log('[OverconstrainedError] Can not use rear camera.');
stream = await navigator.mediaDevices.getUserMedia({
video: {
...videoSize,
...{
width: opts.width,
height: opts.height,
},
},
audio: false,
});
} else {
throw e;
}
}
if (stream) {
videoElem.srcObject = stream; videoElem.srcObject = stream;
// videoElem.src = window.URL.createObjectURL(stream); // videoElem.src = window.URL.createObjectURL(stream);
this.videoElem = videoElem; this.videoElem = videoElem;
@@ -171,8 +196,6 @@ class QrcodeDecoder {
const code = await this.decodeFromVideo(videoElem, opts); const code = await this.decodeFromVideo(videoElem, opts);
return code; return code;
} catch (e) {
throw e;
} }
} }
@@ -221,10 +244,10 @@ class QrcodeDecoder {
imgDom = img; imgDom = img;
} else if (typeof img === 'string') { } else if (typeof img === 'string') {
imgDom = document.createElement('img'); imgDom = document.createElement('img');
imgDom.src = img;
if (options.crossOrigin) { if (options.crossOrigin) {
imgDom.crossOrigin = options.crossOrigin; imgDom.crossOrigin = options.crossOrigin;
} }
imgDom.src = img;
const proms = () => const proms = () =>
new Promise((resolve) => { new Promise((resolve) => {
imgDom!.onload = () => resolve(true); imgDom!.onload = () => resolve(true);
-24
View File
@@ -1,24 +0,0 @@
{
"compilerOptions": {
"rootDir": "src",
"outDir": "dist",
"baseUrl": ".",
"downlevelIteration": true,
"emitDecoratorMetadata": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"preserveConstEnums": true,
"importHelpers": false,
"target": "es5",
"module": "commonjs",
"lib": ["es5", "es6", "dom"],
"moduleResolution": "node",
"sourceMap": false,
"paths": {
"*": ["typings/*", "includes/*"]
},
"resolveJsonModule": true,
"noUnusedLocals": true,
"strict": true
}
}
+1 -3
View File
@@ -1,5 +1,6 @@
{ {
"compilerOptions": { "compilerOptions": {
"outDir": "dist",
"baseUrl": ".", "baseUrl": ".",
"composite": true, "composite": true,
"declaration": true, "declaration": true,
@@ -14,9 +15,6 @@
"module": "ESNext", "module": "ESNext",
"moduleResolution": "node", "moduleResolution": "node",
"sourceMap": false, "sourceMap": false,
"paths": {
"*": ["typings/*", "includes/*"]
},
"resolveJsonModule": true, "resolveJsonModule": true,
"noUnusedLocals": true, "noUnusedLocals": true,
"strict": true, "strict": true,