-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscanner.html
133 lines (122 loc) · 3.57 KB
/
scanner.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Barcode Scanner</title>
<style>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
height: 100vh;
width: 100vw;
padding: 1rem;
font-family: Arial, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
gap: 2%;
background-color: #f4f4f9;
}
#scanner {
width: 100%;
max-width: 768px;
height: 38%;
display: flex;
flex-direction: column;
align-items: center;
}
#scanner > video {
max-height: 100%;
max-width: 100%;
}
#copy-button {
padding: 0.5rem 1rem;
font-size: 1rem;
background-color: #000000;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
textarea {
width: 100%;
height: 34%;
padding: 0.3rem;
}
</style>
</head>
<body>
<h1>Barcode Scanner 0.7</h1>
<div id="scanner"></div>
<textarea
id="barcode-results"
placeholder="Scanned barcodes will appear here..."
></textarea>
<button id="copy-button">Copy</button>
<script src="https://cdnjs.cloudflare.com/ajax/libs/quagga/0.12.1/quagga.min.js"></script>
<script>
const resultsTextArea = document.getElementById("barcode-results");
const scannedBarcodes = new Set(); // Set to track added barcodes
const copyButton = document.getElementById("copy-button");
function addBarcode(barcode) {
// Regex: Start with 4 letters followed by numbers only
const isValid = /^[A-Za-z]{4}\d+$/.test(barcode);
if (!isValid) {
console.log("Invalid barcode format:", barcode);
return; // Skip invalid barcodes
}
if (!scannedBarcodes.has(barcode)) {
scannedBarcodes.add(barcode); // Add the barcode to the Set
resultsTextArea.value += barcode + "\n"; // Append to the textarea
} else {
console.log("Barcode already added:", barcode);
}
}
Quagga.init(
{
inputStream: {
name: "Live",
type: "LiveStream",
target: document.querySelector("#scanner"),
constraints: {
facingMode: { ideal: "environment" },
},
},
decoder: {
readers: [
"code_128_reader", // Common for shipping
"code_39_reader", // Common in logistics
],
},
},
function (err) {
if (err) {
console.error("QuaggaJS Initialization Error:", err);
alert("Error initializing camera");
return;
}
Quagga.start();
},
);
Quagga.onDetected(function (data) {
const barcode = data.codeResult.code;
addBarcode(barcode); // Validate and add the barcode
});
// Copy button functionality
copyButton.addEventListener("click", () => {
navigator.clipboard
.writeText(resultsTextArea.value)
.then(() => {
alert("Copied to clipboard!");
})
.catch((err) => {
console.error("Failed to copy text: ", err);
});
});
</script>
</body>
</html>