add camera preview

This commit is contained in:
wryk 2019-03-10 02:28:07 +01:00
parent 8f1f54e574
commit beefe38b3b
6 changed files with 27 additions and 15 deletions

6
package-lock.json generated
View file

@ -2826,8 +2826,7 @@
"version": "4.6.0", "version": "4.6.0",
"resolved": "https://registry.npmjs.org/co/-/co-4.6.0.tgz", "resolved": "https://registry.npmjs.org/co/-/co-4.6.0.tgz",
"integrity": "sha1-bqa989hTrlTMuOR7+gvz+QMfsYQ=", "integrity": "sha1-bqa989hTrlTMuOR7+gvz+QMfsYQ=",
"dev": true, "dev": true
"optional": true
}, },
"coa": { "coa": {
"version": "2.0.2", "version": "2.0.2",
@ -9536,8 +9535,7 @@
"version": "4.0.8", "version": "4.0.8",
"resolved": "https://registry.npmjs.org/rx-lite/-/rx-lite-4.0.8.tgz", "resolved": "https://registry.npmjs.org/rx-lite/-/rx-lite-4.0.8.tgz",
"integrity": "sha1-Cx4Rr4vESDbwSmQH6S2kJGe3lEQ=", "integrity": "sha1-Cx4Rr4vESDbwSmQH6S2kJGe3lEQ=",
"dev": true, "dev": true
"optional": true
}, },
"rx-lite-aggregates": { "rx-lite-aggregates": {
"version": "4.0.8", "version": "4.0.8",

View file

@ -1,7 +1,7 @@
<template> <template>
<div class="layout"> <div class="layout">
<welcome-screen v-if="!welcomed && !downloading.status"/> <welcome-screen v-if="!mediaStream && !downloading.status"/>
<capture-screen v-if="welcomed && !downloading.status"/> <capture-screen v-if="mediaStream && !downloading.status"/>
<download-screen v-if="downloading.status"/> <download-screen v-if="downloading.status"/>
</div> </div>
</template> </template>
@ -23,7 +23,7 @@ export default {
}, },
computed: { computed: {
...mapState([ ...mapState([
'welcomed', 'mediaStream',
'downloading' 'downloading'
]) ])
} }

View file

@ -27,7 +27,7 @@ export default {
}, },
methods: { methods: {
back () { back () {
this.$store.commit('welcome', false) this.$store.commit('updateMediaStream', null)
}, },
timeLabel (time) { timeLabel (time) {
return time + 's' return time + 's'

View file

@ -5,7 +5,7 @@ Vue.use(Vuex)
export default new Vuex.Store({ export default new Vuex.Store({
state: { state: {
welcomed: false, mediaStream: null,
timer: { timer: {
selected: 2, selected: 2,
list: [2, 3, 5] list: [2, 3, 5]
@ -22,8 +22,12 @@ export default new Vuex.Store({
} }
}, },
mutations: { mutations: {
welcome (store, value) { updateMediaStream(store, mediaStream) {
store.welcomed = value if (store.mediaStream) {
store.mediaStream.getTracks().forEach(track => track.stop())
}
store.mediaStream = mediaStream
}, },
updateTimer (store, time) { updateTimer (store, time) {
store.timer.selected = time store.timer.selected = time
@ -51,6 +55,12 @@ export default new Vuex.Store({
} }
}, },
actions: { actions: {
requestCamera ({ commit }) {
navigator.mediaDevices.getUserMedia({ video: true, audio: false })
.then(mediaStream => {
commit('updateMediaStream', mediaStream)
})
.catch(error => console.error(error))
}
} }
}) })

View file

@ -6,7 +6,7 @@
<capture-options v-else/> <capture-options v-else/>
<div class="preview"> <div class="preview">
<video class="preview-visual" height="200px"></video> <video ref="preview" class="preview-visual" height="200px" autoplay></video>
</div> </div>
<button class="capture-btn" :class="{ 'capture-btn--capturing': capturing.status }" @click="startCapture">Capture</button> <button class="capture-btn" :class="{ 'capture-btn--capturing': capturing.status }" @click="startCapture">Capture</button>
@ -31,6 +31,7 @@ export default {
}, },
computed: { computed: {
...mapState([ ...mapState([
'mediaStream',
'capturing', 'capturing',
'timer', 'timer',
'encoding' 'encoding'
@ -54,6 +55,9 @@ export default {
} }
}, interval) }, interval)
} }
} },
mounted: function () {
this.$refs.preview.srcObject = this.mediaStream
},
} }
</script> </script>

View file

@ -17,7 +17,7 @@ export default {
name: 'welcome', name: 'welcome',
methods: { methods: {
welcome () { welcome () {
this.$store.commit('welcome', true) this.$store.dispatch('requestCamera')
} }
} }
} }