Skip to content
This repository has been archived by the owner on Feb 19, 2024. It is now read-only.

Commit

Permalink
added ability to programatically start/stop webcam
Browse files Browse the repository at this point in the history
  • Loading branch information
VinceG committed Jul 10, 2018
1 parent 9c019d4 commit 0a658db
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 10 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ npm run dev
| name | param | notes |
| -------------- | -------- | ----------------------------------------------------------------------- |
| capture | void | Capture the current image through the webcam as base64 encoded string |
| changeCamera | deviceId | change the currently selected camera. Must pass in the device ID |
| changeCamera | deviceId | change the currently selected camera. Must pass in the device ID |
| start | void | Programatically Start the camera after stopping it (relies on deviceId prop passed to the component) |
| stop | void | Programatically stop the camera |

## License

Expand Down
8 changes: 8 additions & 0 deletions demo/src/main.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
</div>
<div class="col-md-12">
<button type="button" class="btn btn-primary" @click="onCapture">Capture Photo</button>
<button type="button" class="btn btn-danger" @click="onStop">Stop Camera</button>
<button type="button" class="btn btn-success" @click="onStart">Start Camera</button>
</div>
</div>
</div>
Expand Down Expand Up @@ -81,6 +83,12 @@ export default {
onStopped(stream) {
console.log('On Stopped Event', stream);
},
onStop() {
this.$refs.webcam.stop();
},
onStart() {
this.$refs.webcam.start();
},
onError(error, stream) {
console.log('On Error Event', error, stream);
},
Expand Down
15 changes: 11 additions & 4 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,7 @@ exports.default = {
});
},
changeCamera: function changeCamera(deviceId) {
if (this.$refs.video !== null && this.$refs.video.srcObject) {
this.stopStreamedVideo(this.$refs.video);
}

this.stop();
this.$emit('camera-change', deviceId);
this.loadCamera(deviceId);
},
Expand All @@ -220,6 +217,16 @@ exports.default = {
});
videoElem.srcObject = null;
},
stop: function stop() {
if (this.$refs.video !== null && this.$refs.video.srcObject) {
this.stopStreamedVideo(this.$refs.video);
}
},
start: function start() {
if (this.deviceId) {
this.loadCamera(this.deviceId);
}
},
testMediaAccess: function testMediaAccess() {
var _this3 = this;

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vue-web-cam",
"version": "1.2.2",
"version": "1.2.3",
"description": "Webcam component for Vuejs applications",
"main": "dist/index.js",
"author": {
Expand Down
17 changes: 13 additions & 4 deletions src/webcam.vue
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,7 @@ export default {
* change to a different camera stream, like front and back camera on phones
*/
changeCamera(deviceId) {
if(this.$refs.video !== null && this.$refs.video.srcObject) {
this.stopStreamedVideo(this.$refs.video);
}
this.stop();
this.$emit('camera-change', deviceId);
this.loadCamera(deviceId);
},
Expand Down Expand Up @@ -144,6 +141,18 @@ export default {
});
videoElem.srcObject = null;
},
// Stop the video
stop() {
if(this.$refs.video !== null && this.$refs.video.srcObject) {
this.stopStreamedVideo(this.$refs.video);
}
},
// Start the video
start() {
if(this.deviceId) {
this.loadCamera(this.deviceId);
}
},
/**
* test access
*/
Expand Down

0 comments on commit 0a658db

Please sign in to comment.