Hello @vanilsonsp
I will give the skeleton, it will be up to you to adapt it to your project.
What I did is this.
In your index.html, include the JS QR code reading libraries. You have several, in my case I chose instascan (because it seemed much more reliable than any other I tested before). So I included, just before cordova.js:
<script type="text/javascript" src="assets/js/instascan3.js"></script>
I assume you created a ionic page for the scanner.
In your page html, add a video element with id=“preview” (use the css to make sure it is visible, with a height and width not equal to 0):
<video id="preview" autoplay playsinline></video>
In your .ts file of the given ionic page, first declare InstaScan (or whatever library you are using) as a prototype. Just after the imports, before the @Component or export class:
declare var Instascan: any;
(This is needed to avoid errors during transpiling, because the js library is loaded in index.html in real life, but when transpiling, it is not found. Doing this you are just declaring “this function exists” even though you do not say what it does right now).
Then just create a function in the export class that is launched when the view is loaded.
ionViewDidLoad() {
this.startScanner();
}
startScanner() {
let opts = {
// Whether to scan continuously for QR codes. If false, use scanner.scan() to manually scan.
// If true, the scanner emits the "scan" event when a QR code is scanned. Default true.
continuous: true,
// The HTML element to use for the camera's video preview. Must be a <video> element.
// When the camera is active, this element will have the "active" CSS class, otherwise,
// it will have the "inactive" class. By default, an invisible element will be created to
// host the video.
video: document.getElementById('preview'),
// Whether to horizontally mirror the video preview. This is helpful when trying to
// scan a QR code with a user-facing camera. Default true.
mirror: false,
// Whether to include the scanned image data as part of the scan result. See the "scan" event
// for image format details. Default false.
captureImage: false,
// Only applies to continuous mode. Whether to actively scan when the tab is not active.
// When false, this reduces CPU usage when the tab is not active. Default true.
backgroundScan: false,
// Only applies to continuous mode. The period, in milliseconds, before the same QR code
// will be recognized in succession. Default 5000 (5 seconds).
refractoryPeriod: 5000,
// Only applies to continuous mode. The period, in rendered frames, between scans. A lower scan period
// increases CPU usage but makes scan response faster. Default 1 (i.e. analyze every frame).
scanPeriod: 1
};
let _this = this;
_this.scannerObject = new Instascan.Scanner(opts);
_this.scannerObject.addListener('scan', function (content) {
//Show the content read from the QR code
alert(content);
console.log("QR READ: " + content);
//And you can create your conditions here, to check the value of the scanned content
});
I think this should be enough for you to make it work… I hope it helps anyway.
Good luck!