Skip to content

Commit

Permalink
Merge pull request #43 from olefirenko/feature/emit-whole-result-object
Browse files Browse the repository at this point in the history
Emit whole result object
  • Loading branch information
olefirenko authored Oct 27, 2022
2 parents 211035c + ca3c4e6 commit 895dfb5
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 124 deletions.
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Vue Barcode and QR code scanner

> ⚠️ Hello everyone! My name is Dmytro and I am from 🇺🇦 Ukraine. People in Ukraine **ARE DYING RIGHT NOW** because of 🇷🇺 Russia Agression! Please help Ukraine. Donate if you can to: [UKRAINE’S DEFENDERS](https://war.ukraine.ua/donate/), [CHARITABLE FOUNDATION "COME BACK ALIVE"](https://savelife.in.ua/en/donate/). Help to stop Russian aggression 🙏! #StandWithUkraine
> [🇺🇦 IT Army of Ukraine](https://t.me/itarmyofukraine2022)
[![npm version](https://badgen.net/npm/v/vue-barcode-reader)](https://www.npmjs.com/package/vue-barcode-reader) [![sponsored_by](https://badgen.net/badge/sponsored%20by/%F0%9F%A4%96kopiAI.com/f2a)](https://kopiai.com/?utm_source=npm&utm_medium=badge&utm_campaign=vue_barcode_reader)

A Vue.js set of components to scan (or upload images) barcodes and QR codes.

Expand Down Expand Up @@ -83,3 +82,19 @@ In your template you can use this syntax:
```html
methods: { onDecode (result) { console.log(result) } }
```

## Events

The library emits the following events:

### loaded

When the libraty is loaded and the camera is ready to scan

### decode

When a barcode or QR code is scanned. The result is passed as a parameter to the event handler. The result is the text encoded in the barcode or QR code.

### result

When a barcode or QR code is scanned. The result is passed as a parameter to the event handler. The result is the object with the following properties:
55 changes: 26 additions & 29 deletions src/components/ImageBarcodeReader.vue
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>
158 changes: 65 additions & 93 deletions src/components/StreamBarcodeReader.vue
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>

0 comments on commit 895dfb5

Please sign in to comment.