mirror of
https://github.com/yugasun/qrcode-decoder.git
synced 2026-08-13 04:31:36 +08:00
fix: support customize camera size
This commit is contained in:
+7
-1
@@ -31,7 +31,13 @@
|
|||||||
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;
|
||||||
}
|
}
|
||||||
|
|||||||
+14
-3
@@ -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;
|
||||||
@@ -20,7 +25,7 @@ class QrcodeDecoder {
|
|||||||
this.getUserMediaHandler = null;
|
this.getUserMediaHandler = null;
|
||||||
this.videoConstraints = {
|
this.videoConstraints = {
|
||||||
// default use rear camera
|
// default use rear camera
|
||||||
video: { facingMode: { exact: 'environment' } },
|
video: { ...videoSize, facingMode: { exact: 'environment' } },
|
||||||
audio: false,
|
audio: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -150,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,
|
||||||
@@ -169,7 +174,13 @@ class QrcodeDecoder {
|
|||||||
console.log('[OverconstrainedError] Can not use rear camera.');
|
console.log('[OverconstrainedError] Can not use rear camera.');
|
||||||
|
|
||||||
stream = await navigator.mediaDevices.getUserMedia({
|
stream = await navigator.mediaDevices.getUserMedia({
|
||||||
video: true,
|
video: {
|
||||||
|
...videoSize,
|
||||||
|
...{
|
||||||
|
width: opts.width,
|
||||||
|
height: opts.height,
|
||||||
|
},
|
||||||
|
},
|
||||||
audio: false,
|
audio: false,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user