-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from olefirenko/feature/emit-whole-result-object
Emit whole result object
- Loading branch information
Showing
3 changed files
with
108 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,40 @@ | ||
<template> | ||
<input | ||
@change="onChangeInput" | ||
type="file" | ||
name="image" | ||
accept="image/*" | ||
capture="environment" | ||
/> | ||
<input @change="onChangeInput" type="file" name="image" accept="image/*" capture="environment" /> | ||
</template> | ||
|
||
<script> | ||
import { BrowserMultiFormatReader } from "@zxing/library"; | ||
export default { | ||
name: "image-barcode-reader", | ||
name: "image-barcode-reader", | ||
data() { | ||
return { | ||
codeReader: new BrowserMultiFormatReader() | ||
}; | ||
}, | ||
data() { | ||
return { | ||
codeReader: new BrowserMultiFormatReader(), | ||
}; | ||
}, | ||
methods: { | ||
onChangeInput(e) { | ||
const files = e.target.files || e.dataTransfer.files; | ||
if (!files.length) return; | ||
methods: { | ||
onChangeInput(e) { | ||
const files = e.target.files || e.dataTransfer.files; | ||
if (!files.length) return; | ||
const reader = new FileReader(); | ||
reader.onload = this.processFile; | ||
reader.readAsDataURL(files[0]); | ||
}, | ||
const reader = new FileReader(); | ||
reader.onload = this.processFile; | ||
reader.readAsDataURL(files[0]); | ||
}, | ||
processFile(e) { | ||
this.$el.innerHTML += `<img id="image" src="${e.target.result}"/>`; | ||
processFile(e) { | ||
this.$el.innerHTML += `<img id="image" src="${e.target.result}"/>`; | ||
this.codeReader | ||
.decodeFromImage("image") | ||
.then(result => this.$emit("decode", result.text)) | ||
.catch(error => this.$emit("error", error)); | ||
} | ||
} | ||
this.codeReader | ||
.decodeFromImage("image") | ||
.then((result) => { | ||
this.$emit("decode", result.text); | ||
this.$emit("result", result); | ||
}) | ||
.catch((error) => this.$emit("error", error)); | ||
}, | ||
}, | ||
}; | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,127 +1,99 @@ | ||
<template> | ||
<div class="scanner-container"> | ||
<div v-show="!isLoading"> | ||
<video poster="data:image/gif,AAAA" ref="scanner"></video> | ||
<div class="overlay-element"></div> | ||
<div class="laser"></div> | ||
</div> | ||
<div class="scanner-container"> | ||
<div v-show="!isLoading"> | ||
<video poster="data:image/gif,AAAA" ref="scanner"></video> | ||
<div class="overlay-element"></div> | ||
<div class="laser"></div> | ||
</div> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
import { BrowserMultiFormatReader, Exception } from "@zxing/library"; | ||
export default { | ||
name: "stream-barcode-reader", | ||
name: "stream-barcode-reader", | ||
data() { | ||
return { | ||
isLoading: true, | ||
codeReader: new BrowserMultiFormatReader(), | ||
isMediaStreamAPISupported: | ||
navigator && | ||
navigator.mediaDevices && | ||
"enumerateDevices" in navigator.mediaDevices | ||
}; | ||
}, | ||
data() { | ||
return { | ||
isLoading: true, | ||
codeReader: new BrowserMultiFormatReader(), | ||
isMediaStreamAPISupported: navigator && navigator.mediaDevices && "enumerateDevices" in navigator.mediaDevices, | ||
}; | ||
}, | ||
mounted() { | ||
if (!this.isMediaStreamAPISupported) { | ||
throw new Exception("Media Stream API is not supported"); | ||
return; | ||
} | ||
mounted() { | ||
if (!this.isMediaStreamAPISupported) { | ||
throw new Exception("Media Stream API is not supported"); | ||
return; | ||
} | ||
this.start(); | ||
this.$refs.scanner.oncanplay = event => { | ||
this.isLoading = false; | ||
this.$emit("loaded"); | ||
}; | ||
}, | ||
this.start(); | ||
this.$refs.scanner.oncanplay = (event) => { | ||
this.isLoading = false; | ||
this.$emit("loaded"); | ||
}; | ||
}, | ||
beforeUnmount() { | ||
this.codeReader.reset(); | ||
}, | ||
beforeUnmount() { | ||
this.codeReader.reset(); | ||
}, | ||
methods: { | ||
start() { | ||
this.codeReader.decodeFromVideoDevice( | ||
undefined, | ||
this.$refs.scanner, | ||
(result, err) => { | ||
if (result) { | ||
this.$emit("decode", result.text); | ||
} | ||
} | ||
); | ||
methods: { | ||
start() { | ||
this.codeReader.decodeFromVideoDevice(undefined, this.$refs.scanner, (result, err) => { | ||
if (result) { | ||
this.$emit("decode", result.text); | ||
this.$emit("result", result); | ||
} | ||
} | ||
}); | ||
}, | ||
}, | ||
}; | ||
</script> | ||
|
||
<style scoped> | ||
video { | ||
max-width: 100%; | ||
max-height: 100%; | ||
max-width: 100%; | ||
max-height: 100%; | ||
} | ||
.scanner-container { | ||
position: relative; | ||
position: relative; | ||
} | ||
.overlay-element { | ||
position: absolute; | ||
top: 0; | ||
width: 100%; | ||
height: 99%; | ||
background: rgba(30, 30, 30, 0.5); | ||
position: absolute; | ||
top: 0; | ||
width: 100%; | ||
height: 99%; | ||
background: rgba(30, 30, 30, 0.5); | ||
-webkit-clip-path: polygon( | ||
0% 0%, | ||
0% 100%, | ||
20% 100%, | ||
20% 20%, | ||
80% 20%, | ||
80% 80%, | ||
20% 80%, | ||
20% 100%, | ||
100% 100%, | ||
100% 0% | ||
); | ||
clip-path: polygon( | ||
0% 0%, | ||
0% 100%, | ||
20% 100%, | ||
20% 20%, | ||
80% 20%, | ||
80% 80%, | ||
20% 80%, | ||
20% 100%, | ||
100% 100%, | ||
100% 0% | ||
); | ||
-webkit-clip-path: polygon(0% 0%, 0% 100%, 20% 100%, 20% 20%, 80% 20%, 80% 80%, 20% 80%, 20% 100%, 100% 100%, 100% 0%); | ||
clip-path: polygon(0% 0%, 0% 100%, 20% 100%, 20% 20%, 80% 20%, 80% 80%, 20% 80%, 20% 100%, 100% 100%, 100% 0%); | ||
} | ||
.laser { | ||
width: 60%; | ||
margin-left: 20%; | ||
background-color: tomato; | ||
height: 1px; | ||
position: absolute; | ||
top: 40%; | ||
z-index: 2; | ||
box-shadow: 0 0 4px red; | ||
-webkit-animation: scanning 2s infinite; | ||
animation: scanning 2s infinite; | ||
width: 60%; | ||
margin-left: 20%; | ||
background-color: tomato; | ||
height: 1px; | ||
position: absolute; | ||
top: 40%; | ||
z-index: 2; | ||
box-shadow: 0 0 4px red; | ||
-webkit-animation: scanning 2s infinite; | ||
animation: scanning 2s infinite; | ||
} | ||
@-webkit-keyframes scanning { | ||
50% { | ||
-webkit-transform: translateY(75px); | ||
transform: translateY(75px); | ||
} | ||
50% { | ||
-webkit-transform: translateY(75px); | ||
transform: translateY(75px); | ||
} | ||
} | ||
@keyframes scanning { | ||
50% { | ||
-webkit-transform: translateY(75px); | ||
transform: translateY(75px); | ||
} | ||
50% { | ||
-webkit-transform: translateY(75px); | ||
transform: translateY(75px); | ||
} | ||
} | ||
</style> |