Instascan
Fork of Schmich's Real-time webcam-driven HTML5 QR code scanner (Instascan) with updated libraries (Browser, iOS and Android are working) Try the live demo.
Installing
NPM
npm install --save @vitorpamplona/instascan
const Instascan = require('instascan');
Browser
Copy instascan.min.js
from the releases page and load with:
<script type="text/javascript" src="instascan.min.js"></script>
Example
<!DOCTYPE html>
<html>
<head>
<title>Instascan</title>
<script type="text/javascript" src="instascan.min.js"></script>
</head>
<body>
<video id="preview"></video>
<script type="text/javascript">
let scanner = new Instascan.Scanner({ video: document.getElementById('preview') });
scanner.addListener('scan', function (content) {
console.log(content);
});
Instascan.Camera.getCameras().then(function (cameras) {
if (cameras.length > 0) {
scanner.start(cameras[0]);
} else {
console.error('No cameras found.');
}
}).catch(function (e) {
console.error(e);
});
</script>
</body>
</html>
API
let scanner = new Instascan.Scanner(opts)
Create a new scanner with options:
let opts = {
continuous: true,
video: document.getElementById('preview'),
mirror: true,
captureImage: false,
backgroundScan: true,
refractoryPeriod: 5000,
scanPeriod: 1
};
scanner.start(camera)
- Activate
camera
and start scanning using it as the source. Returns promise. - This must be called in order to use
scanner.scan
or receive scan
events. camera
: Instance of Instascan.Camera
from Instascan.Camera.getCameras
..then(function () { ... })
: called when camera is active and scanning has started..catch(function (err) { ... })
- Called when an error occurs trying to initialize the camera for scanning.
err
: An Instascan.MediaError
in the case of a known getUserMedia
failure (see error types).
scanner.stop()
- Stop scanning and deactivate the camera. Returns promise.
.then(function () { ... })
: called when camera and scanning have stopped.
let result = scanner.scan()
- Scan video immediately for a QR code.
- QR codes recognized with this method are not emitted via the
scan
event. - If no QR code is detected,
result
is null
. result.content
: Scanned content decoded from the QR code.result.image
: Undefined if scanner.captureImage
is false
, otherwise, see the scan
event for format.
scanner.addListener('scan', callback)
- Emitted when a QR code is scanned using the camera in continuous mode (see
scanner.continuous
). callback
: function (content, image)
content
: Scanned content decoded from the QR code.image
: null
if scanner.captureImage
is false
, otherwise, a base64-encoded WebP-compressed data URI of the camera frame used to decode the QR code.
scanner.addListener('active', callback)
- Emitted when the scanner becomes active as the result of
scanner.start
or the tab gaining focus. - If
opts.video
element was specified, it will have the active
CSS class. callback
: function ()
scanner.addListener('inactive', callback)
- Emitted when the scanner becomes inactive as the result of
scanner.stop
or the tab losing focus. - If
opts.video
element was specified, it will have the inactive
CSS class. callback
: function ()
Instascan.Camera.getCameras()
- Enumerate available video devices. Returns promise.
.then(function (cameras) { ... })
- Called when cameras are available.
cameras
: Array of Instascan.Camera
instances available for use.
.catch(function (err) { ... })
- Called when an error occurs while getting cameras.
err
: An Instascan.MediaError
in the case of a known getUserMedia
failure (see error types).
camera.id
- Unique camera ID provided by the browser.
- These IDs are stable and can be persisted across instances of your application (e.g. in localStorage).
camera.name
- Camera name, including manufacturer and model
- e.g. "Microsoft LifeCam HD-3000".
Credits
Writen by Chris Schmich in 2016 and updated by several members of the community. Powered by the Emscripten JavaScript build of the C++ port of the ZXing Java library.
License
Copyright © 2016 Chris Schmich
MIT License. See LICENSE for details.