Skip to content

Commit 109cc69

Browse files
committed
Added autostart attribute and startStream() and stopStream() functionality
1 parent 3020724 commit 109cc69

File tree

4 files changed

+72
-37
lines changed

4 files changed

+72
-37
lines changed

README.md

+25-10
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,39 @@ A QR Code Reader Web Component. A custom event makes it easy to get notified abo
55
This repository was forked from https://github.com/educastellano/qr-reader.
66
It is using the [jsqrcode](https://github.com/LazarSoft/jsqrcode) lib.
77

8+
Feel free to send me any issues or pull requests!
9+
10+
11+
## Demo
12+
Check the [component page](https://istvank.github.io/qr-reader/) for more information.
13+
14+
15+
## Compatibility
16+
This is a highly experimental piece of software. To the best of my knowledge the QR reader only works on recent Chrome
17+
browsers (Desktop and Mobile on Android) and was tested with Chrome >= 48.0.
18+
819

920
## Usage
1021

1122
As simple as that:
1223

13-
```html
14-
<link rel="import" href="qr-reader.html">
24+
```html
25+
<link rel="import" href="qr-reader.html">
26+
27+
<qr-reader autostart></qr-reader>
28+
29+
<script>
30+
var qrReader = document.querySelector('qr-reader');
1531
16-
<qr-reader output></qr-reader>
32+
qrReader.addEventListener("qr-code-read", function(e) {
33+
alert(e.detail);
34+
});
35+
</script>
36+
```
1737

18-
<script>
19-
var qrReader = document.querySelector('qr-reader');
38+
Instead of defining the `autostart` attribute feel free to call `qrReader.startScan()` whenever your application is
39+
ready to scan.
2040

21-
qrReader.addEventListener("qr-code-read", function(e) {
22-
alert(e.detail);
23-
});
24-
</script>
25-
```
2641

2742
## License
2843

bower.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "qr-reader",
3-
"version": "0.1.0",
3+
"version": "0.2.0",
44
"homepage": "https://github.com/istvank/qr-reader",
55
"authors": [
66
"István Koren <https://istvank.eu>"

demo/index.html

+16-3
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,32 @@
1010
<p>Capture a QR Code with your camera. As soon as it is read, an alert shows the QR code's content.</p>
1111

1212
<!-- Using the Web Component -->
13-
<qr-reader output></qr-reader>
13+
<qr-reader autostart></qr-reader>
14+
15+
<button type="button" id="startScan">Start Scanning</button>
16+
<button type="button" id="stopScan">Stop Scanning</button>
1417

1518
<!-- Polyfill -->
16-
<script src="../bower_components/webcomponentsjs/webcomponents.min.js"></script>
19+
<script src="bower_components/webcomponentsjs/webcomponents.min.js"></script>
1720
<!-- Webcomponent and dependencies -->
1821
<link rel="import" href="../qr-reader.html">
1922

2023
<script>
2124
var qrReader = document.querySelector('qr-reader');
25+
var startButton = document.querySelector('#startScan');
26+
var stopButton = document.querySelector('#stopScan');
2227

23-
qrReader.addEventListener("qr-code-read", function(e) {
28+
qrReader.addEventListener('qr-code-read', function(e) {
2429
alert(e.detail);
2530
});
31+
32+
startButton.addEventListener('click', function(e) {
33+
qrReader.startScan();
34+
});
35+
36+
stopButton.addEventListener('click', function(e) {
37+
qrReader.stopScan();
38+
});
2639
</script>
2740
</body>
2841
</html>

qr-reader.js

+30-23
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
outputAttr: 'textContent',
2929
onRead: '',
3030
interval: 1000,
31-
scale: 0.5
31+
scale: 0.5,
32+
autostart: false
3233
}
3334
},
3435
defineAttributes: {
@@ -78,6 +79,24 @@
7879

7980
this.defineAttributes();
8081

82+
if (this.autostart !== false) {
83+
this.startScan();
84+
}
85+
}
86+
},
87+
attributeChangedCallback: {
88+
value: function (attrName, oldVal, newVal) {
89+
var fn = this[attrName+'Changed'];
90+
if (fn && typeof fn === 'function') {
91+
fn.call(this, oldVal, newVal);
92+
}
93+
}
94+
},
95+
//
96+
// Methods
97+
//
98+
startScan: {
99+
value: function () {
81100
var me = this,
82101
media_options,
83102
success,
@@ -114,7 +133,8 @@
114133
success = function (stream) {
115134
me.shadowRoot.getElementById('video').src = (window.URL && window.URL.createObjectURL(stream)) || stream;
116135
stream_obj = stream;
117-
me.startScan();
136+
me.stream = stream;
137+
//me.startScan();
118138
};
119139

120140
error = function (error) {
@@ -132,40 +152,25 @@
132152
console.log(err.name + ": " + error.message);
133153
});
134154

135-
}
136-
else {
155+
} else {
137156
console.log('Sorry, native web camera access is not supported by this browser...');
138157
}
139158

140-
//this.generate();
141-
}
142-
},
143-
attributeChangedCallback: {
144-
value: function (attrName, oldVal, newVal) {
145-
var fn = this[attrName+'Changed'];
146-
if (fn && typeof fn === 'function') {
147-
fn.call(this, oldVal, newVal);
148-
}
149-
this.generate();
150-
}
151-
},
152-
//
153-
// Methods
154-
//
155-
startScan: {
156-
value: function () {
157-
var me = this;
158159

159160
if (interval_id) {
160161
me.stop();
161162
}
162-
interval_id = setInterval(function (video, scale) {
163+
interval_id = setInterval(function(video, scale) {
163164
me.capture()
164165
}, this.interval);
165166
}
166167
},
167168
stopScan: {
168169
value: function () {
170+
// close the stream from the webcam
171+
this.stream.getTracks()[0].stop();
172+
173+
// stop trying to detect QR codes in the stream
169174
clearInterval(interval_id);
170175
}
171176
},
@@ -186,6 +191,8 @@
186191
},
187192
stop: {
188193
value: function () {
194+
var me = this;
195+
189196
this.stopScan();
190197
if (stream_obj) {
191198
if ('stop' in stream_obj) {

0 commit comments

Comments
 (0)