@ericblade/quagga2 1.10.2 → 1.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/DEPENDENCIES.md +4 -13
- package/HALFSAMPLE_PERFORMANCE_ANALYSIS.md +178 -0
- package/README.md +111 -8
- package/dist/quagga.js +702 -250
- package/dist/quagga.min.js +4 -4
- package/docs/_config.yml +3 -0
- package/docs/assets/bb-binary.png +0 -0
- package/docs/assets/bb-rotated.png +0 -0
- package/docs/assets/binary.png +0 -0
- package/docs/assets/component-labeling-line.png +0 -0
- package/docs/assets/component-labeling-text.png +0 -0
- package/docs/assets/component-labeling.png +0 -0
- package/docs/assets/connected-patch-labels.png +0 -0
- package/docs/assets/mobile-detected.png +0 -0
- package/docs/assets/mobile-located.png +0 -0
- package/docs/assets/patches_found.png +0 -0
- package/docs/assets/quaggaJS-code128.png +0 -0
- package/docs/assets/remaining-patch-labels.png +0 -0
- package/docs/assets/skeleton.png +0 -0
- package/docs/assets/teaser.png +0 -0
- package/docs/contributing.md +102 -0
- package/docs/explanation/algorithm-overview.md +69 -0
- package/docs/explanation/architecture.md +117 -0
- package/docs/explanation/how-barcode-localization-works.md +197 -0
- package/docs/explanation/index.md +80 -0
- package/docs/explanation/input-streams.md +270 -0
- package/docs/getting-started.md +213 -0
- package/docs/how-to-guides/configure-readers.md +79 -0
- package/docs/how-to-guides/external-readers.md +234 -0
- package/docs/how-to-guides/handle-difficult-barcodes.md +127 -0
- package/docs/how-to-guides/index.md +77 -0
- package/docs/how-to-guides/optimize-performance.md +114 -0
- package/docs/how-to-guides/tips-and-tricks.md +115 -0
- package/docs/how-to-guides/use-debug-flags.md +776 -0
- package/docs/how-to-guides/working-with-coordinates.md +254 -0
- package/docs/index.md +118 -0
- package/docs/reference/api.md +716 -0
- package/docs/reference/browser-support.md +109 -0
- package/docs/reference/camera-access.md +376 -0
- package/docs/reference/configuration.md +1008 -0
- package/docs/reference/dependencies.md +295 -0
- package/docs/reference/index.md +74 -0
- package/docs/reference/readers.md +261 -0
- package/docs/tutorials/first-scan.md +141 -0
- package/docs/tutorials/index.md +77 -0
- package/docs/tutorials/node-usage.md +182 -0
- package/docs/tutorials/react-integration.md +176 -0
- package/docs/tutorials/static-image.md +130 -0
- package/lib/quagga.js +645 -265
- package/package.json +14 -11
- package/type-definitions/quagga.d.ts +86 -9
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
# Your First Barcode Scan {#first-scan}
|
|
2
|
+
|
|
3
|
+
This tutorial walks you through creating a simple barcode scanner using your device's camera.
|
|
4
|
+
|
|
5
|
+
## Prerequisites {#prerequisites}
|
|
6
|
+
|
|
7
|
+
- A web browser that supports camera access (Chrome, Firefox, Safari, Edge)
|
|
8
|
+
- A device with a camera
|
|
9
|
+
- Basic knowledge of HTML and JavaScript
|
|
10
|
+
|
|
11
|
+
## Step 1: Set Up the HTML {#step-1-html}
|
|
12
|
+
|
|
13
|
+
Create an `index.html` file:
|
|
14
|
+
|
|
15
|
+
```html
|
|
16
|
+
<!DOCTYPE html>
|
|
17
|
+
<html>
|
|
18
|
+
<head>
|
|
19
|
+
<meta charset="utf-8">
|
|
20
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
21
|
+
<title>Barcode Scanner</title>
|
|
22
|
+
<style>
|
|
23
|
+
#scanner-container {
|
|
24
|
+
position: relative;
|
|
25
|
+
width: 100%;
|
|
26
|
+
max-width: 640px;
|
|
27
|
+
}
|
|
28
|
+
#scanner-container video {
|
|
29
|
+
width: 100%;
|
|
30
|
+
}
|
|
31
|
+
#result {
|
|
32
|
+
margin-top: 20px;
|
|
33
|
+
padding: 10px;
|
|
34
|
+
background: #f0f0f0;
|
|
35
|
+
}
|
|
36
|
+
</style>
|
|
37
|
+
</head>
|
|
38
|
+
<body>
|
|
39
|
+
<h1>Barcode Scanner</h1>
|
|
40
|
+
<div id="scanner-container"></div>
|
|
41
|
+
<div id="result">Scan a barcode...</div>
|
|
42
|
+
|
|
43
|
+
<script src="https://cdn.jsdelivr.net/npm/@ericblade/quagga2/dist/quagga.min.js"></script>
|
|
44
|
+
<script src="app.js"></script>
|
|
45
|
+
</body>
|
|
46
|
+
</html>
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Step 2: Initialize Quagga {#step-2-initialize}
|
|
50
|
+
|
|
51
|
+
Create an `app.js` file:
|
|
52
|
+
|
|
53
|
+
```javascript
|
|
54
|
+
Quagga.init({
|
|
55
|
+
inputStream: {
|
|
56
|
+
type: "LiveStream",
|
|
57
|
+
target: document.querySelector('#scanner-container'),
|
|
58
|
+
constraints: {
|
|
59
|
+
facingMode: "environment" // Use back camera
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
decoder: {
|
|
63
|
+
readers: ["code_128_reader", "ean_reader", "upc_reader"]
|
|
64
|
+
}
|
|
65
|
+
}, function(err) {
|
|
66
|
+
if (err) {
|
|
67
|
+
console.error("Failed to initialize:", err);
|
|
68
|
+
document.querySelector('#result').textContent = "Error: " + err.message;
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
console.log("Scanner ready");
|
|
72
|
+
Quagga.start();
|
|
73
|
+
});
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## Step 3: Handle Detections {#step-3-handle-detections}
|
|
77
|
+
|
|
78
|
+
Add detection handling to `app.js`:
|
|
79
|
+
|
|
80
|
+
```javascript
|
|
81
|
+
Quagga.onDetected(function(result) {
|
|
82
|
+
const code = result.codeResult.code;
|
|
83
|
+
const format = result.codeResult.format;
|
|
84
|
+
|
|
85
|
+
document.querySelector('#result').textContent =
|
|
86
|
+
`Found ${format}: ${code}`;
|
|
87
|
+
|
|
88
|
+
console.log("Barcode detected:", code);
|
|
89
|
+
});
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## Step 4: Run It {#step-4-run}
|
|
93
|
+
|
|
94
|
+
1. Serve the files using a local web server (required for camera access)
|
|
95
|
+
2. Open the page in your browser
|
|
96
|
+
3. Allow camera access when prompted
|
|
97
|
+
4. Point the camera at a barcode
|
|
98
|
+
|
|
99
|
+
**Note**: Camera access requires HTTPS on non-localhost domains.
|
|
100
|
+
|
|
101
|
+
## Complete Code {#complete-code}
|
|
102
|
+
|
|
103
|
+
Here's the complete `app.js`:
|
|
104
|
+
|
|
105
|
+
```javascript
|
|
106
|
+
Quagga.init({
|
|
107
|
+
inputStream: {
|
|
108
|
+
type: "LiveStream",
|
|
109
|
+
target: document.querySelector('#scanner-container'),
|
|
110
|
+
constraints: {
|
|
111
|
+
facingMode: "environment"
|
|
112
|
+
}
|
|
113
|
+
},
|
|
114
|
+
decoder: {
|
|
115
|
+
readers: ["code_128_reader", "ean_reader", "upc_reader"]
|
|
116
|
+
}
|
|
117
|
+
}, function(err) {
|
|
118
|
+
if (err) {
|
|
119
|
+
console.error("Failed to initialize:", err);
|
|
120
|
+
document.querySelector('#result').textContent = "Error: " + err.message;
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
Quagga.start();
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
Quagga.onDetected(function(result) {
|
|
127
|
+
const code = result.codeResult.code;
|
|
128
|
+
const format = result.codeResult.format;
|
|
129
|
+
document.querySelector('#result').textContent = `Found ${format}: ${code}`;
|
|
130
|
+
});
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
## Next Steps {#next-steps}
|
|
134
|
+
|
|
135
|
+
- [Static Image Scanning](static-image.md) - Decode images without camera
|
|
136
|
+
- [Configuration Reference](../reference/configuration.md) - Customize behavior
|
|
137
|
+
- [Tips and Tricks](../how-to-guides/tips-and-tricks.md) - Improve results
|
|
138
|
+
|
|
139
|
+
---
|
|
140
|
+
|
|
141
|
+
[← Back to Tutorials](index.md)
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# Tutorials
|
|
2
|
+
|
|
3
|
+
Step-by-step tutorials to help you learn Quagga2 by building real examples. These are learning-oriented guides designed to help beginners get hands-on experience.
|
|
4
|
+
|
|
5
|
+
## Available Tutorials
|
|
6
|
+
|
|
7
|
+
### [Your First Barcode Scan](first-scan.md) ⭐ Start Here
|
|
8
|
+
Build a complete barcode scanner web app from scratch. You'll learn how to:
|
|
9
|
+
- Set up HTML structure
|
|
10
|
+
- Initialize Quagga2
|
|
11
|
+
- Handle camera permissions
|
|
12
|
+
- Process detected barcodes
|
|
13
|
+
- Display results to users
|
|
14
|
+
|
|
15
|
+
**Time**: 15 minutes | **Difficulty**: Beginner
|
|
16
|
+
|
|
17
|
+
### [Decoding Static Images](static-image.md)
|
|
18
|
+
Learn to decode barcodes from image files instead of camera feeds. Perfect for:
|
|
19
|
+
- Photo upload features
|
|
20
|
+
- Batch processing
|
|
21
|
+
- Server-side Node.js applications
|
|
22
|
+
- Testing without a camera
|
|
23
|
+
|
|
24
|
+
**Time**: 10 minutes | **Difficulty**: Beginner
|
|
25
|
+
|
|
26
|
+
### [Using with React](react-integration.md)
|
|
27
|
+
Integrate Quagga2 into a React application. Covers:
|
|
28
|
+
- Component lifecycle management
|
|
29
|
+
- Handling cleanup
|
|
30
|
+
- State management
|
|
31
|
+
- Common pitfalls and solutions
|
|
32
|
+
|
|
33
|
+
**Time**: 20 minutes | **Difficulty**: Intermediate
|
|
34
|
+
|
|
35
|
+
### [Using with Node.js](node-usage.md)
|
|
36
|
+
Use Quagga2 in server-side Node.js applications. Learn to:
|
|
37
|
+
- Process images from filesystem
|
|
38
|
+
- Handle image buffers
|
|
39
|
+
- Batch decode multiple images
|
|
40
|
+
- Build CLI tools
|
|
41
|
+
|
|
42
|
+
**Time**: 15 minutes | **Difficulty**: Intermediate
|
|
43
|
+
|
|
44
|
+
## Prerequisites
|
|
45
|
+
|
|
46
|
+
Before starting these tutorials, you should have:
|
|
47
|
+
|
|
48
|
+
- Basic HTML/JavaScript knowledge
|
|
49
|
+
- A text editor
|
|
50
|
+
- A web server (or ability to run `npx serve`)
|
|
51
|
+
- A device with a camera (for live scanning tutorials)
|
|
52
|
+
|
|
53
|
+
## Tutorial Approach
|
|
54
|
+
|
|
55
|
+
These tutorials follow a **learning-by-doing** approach:
|
|
56
|
+
|
|
57
|
+
1. **Complete examples** - Each tutorial provides full working code
|
|
58
|
+
2. **Progressive complexity** - Start simple, add features incrementally
|
|
59
|
+
3. **Hands-on practice** - Type the code yourself to build muscle memory
|
|
60
|
+
4. **Real-world scenarios** - Build things you'll actually use
|
|
61
|
+
|
|
62
|
+
## Need Help?
|
|
63
|
+
|
|
64
|
+
If you get stuck:
|
|
65
|
+
|
|
66
|
+
- Check the [API Reference](../reference/api.md) for method details
|
|
67
|
+
- Review the [Configuration Guide](../reference/configuration.md)
|
|
68
|
+
- Ask in [Gitter Chat](https://gitter.im/quaggaJS/Lobby)
|
|
69
|
+
- Search [GitHub Issues](https://github.com/ericblade/quagga2/issues)
|
|
70
|
+
|
|
71
|
+
## Contributing
|
|
72
|
+
|
|
73
|
+
Found a bug in a tutorial or have an idea for a new one? [Open an issue](https://github.com/ericblade/quagga2/issues) or submit a pull request!
|
|
74
|
+
|
|
75
|
+
---
|
|
76
|
+
|
|
77
|
+
**Next**: Start with [Your First Barcode Scan](first-scan.md) →
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
# Using Quagga2 in Node.js {#node-usage}
|
|
2
|
+
|
|
3
|
+
This tutorial covers server-side barcode scanning with Quagga2 in Node.js.
|
|
4
|
+
|
|
5
|
+
## Installation {#installation}
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @ericblade/quagga2
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Basic Usage {#basic-usage}
|
|
12
|
+
|
|
13
|
+
```javascript
|
|
14
|
+
const Quagga = require('@ericblade/quagga2').default;
|
|
15
|
+
|
|
16
|
+
Quagga.decodeSingle({
|
|
17
|
+
src: './barcode.jpg',
|
|
18
|
+
decoder: {
|
|
19
|
+
readers: ['code_128_reader', 'ean_reader']
|
|
20
|
+
}
|
|
21
|
+
}, function(result) {
|
|
22
|
+
if (result && result.codeResult) {
|
|
23
|
+
console.log('Barcode:', result.codeResult.code);
|
|
24
|
+
console.log('Format:', result.codeResult.format);
|
|
25
|
+
} else {
|
|
26
|
+
console.log('No barcode found');
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## With Promises {#with-promises}
|
|
32
|
+
|
|
33
|
+
Wrap in a Promise for async/await:
|
|
34
|
+
|
|
35
|
+
```javascript
|
|
36
|
+
const Quagga = require('@ericblade/quagga2').default;
|
|
37
|
+
|
|
38
|
+
function decodeBarcode(imagePath, readers = ['code_128_reader']) {
|
|
39
|
+
return new Promise((resolve, reject) => {
|
|
40
|
+
Quagga.decodeSingle({
|
|
41
|
+
src: imagePath,
|
|
42
|
+
decoder: { readers }
|
|
43
|
+
}, (result) => {
|
|
44
|
+
if (result && result.codeResult) {
|
|
45
|
+
resolve(result.codeResult);
|
|
46
|
+
} else {
|
|
47
|
+
resolve(null);
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Usage
|
|
54
|
+
async function main() {
|
|
55
|
+
const result = await decodeBarcode('./barcode.jpg');
|
|
56
|
+
if (result) {
|
|
57
|
+
console.log(`Found ${result.format}: ${result.code}`);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
main();
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## Express API Example {#express-example}
|
|
65
|
+
|
|
66
|
+
```javascript
|
|
67
|
+
const express = require('express');
|
|
68
|
+
const multer = require('multer');
|
|
69
|
+
const Quagga = require('@ericblade/quagga2').default;
|
|
70
|
+
|
|
71
|
+
const app = express();
|
|
72
|
+
const upload = multer({ dest: 'uploads/' });
|
|
73
|
+
|
|
74
|
+
app.post('/scan', upload.single('image'), (req, res) => {
|
|
75
|
+
if (!req.file) {
|
|
76
|
+
return res.status(400).json({ error: 'No image provided' });
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
Quagga.decodeSingle({
|
|
80
|
+
src: req.file.path,
|
|
81
|
+
decoder: {
|
|
82
|
+
readers: ['code_128_reader', 'ean_reader', 'upc_reader']
|
|
83
|
+
}
|
|
84
|
+
}, (result) => {
|
|
85
|
+
if (result && result.codeResult) {
|
|
86
|
+
res.json({
|
|
87
|
+
code: result.codeResult.code,
|
|
88
|
+
format: result.codeResult.format
|
|
89
|
+
});
|
|
90
|
+
} else {
|
|
91
|
+
res.json({ code: null, error: 'No barcode found' });
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
app.listen(3000, () => {
|
|
97
|
+
console.log('Barcode API running on port 3000');
|
|
98
|
+
});
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## Batch Processing {#batch-processing}
|
|
102
|
+
|
|
103
|
+
Process multiple images:
|
|
104
|
+
|
|
105
|
+
```javascript
|
|
106
|
+
const Quagga = require('@ericblade/quagga2').default;
|
|
107
|
+
const fs = require('fs');
|
|
108
|
+
const path = require('path');
|
|
109
|
+
|
|
110
|
+
async function decodeImage(imagePath) {
|
|
111
|
+
return new Promise((resolve) => {
|
|
112
|
+
Quagga.decodeSingle({
|
|
113
|
+
src: imagePath,
|
|
114
|
+
decoder: { readers: ['code_128_reader', 'ean_reader'] }
|
|
115
|
+
}, (result) => {
|
|
116
|
+
resolve({
|
|
117
|
+
file: path.basename(imagePath),
|
|
118
|
+
code: result?.codeResult?.code || null,
|
|
119
|
+
format: result?.codeResult?.format || null
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
async function processDirectory(dir) {
|
|
126
|
+
const files = fs.readdirSync(dir)
|
|
127
|
+
.filter(f => /\.(jpg|jpeg|png)$/i.test(f));
|
|
128
|
+
|
|
129
|
+
const results = [];
|
|
130
|
+
for (const file of files) {
|
|
131
|
+
const result = await decodeImage(path.join(dir, file));
|
|
132
|
+
results.push(result);
|
|
133
|
+
console.log(`${result.file}: ${result.code || 'No barcode'}`);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return results;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
processDirectory('./images');
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## Configuration Tips {#configuration-tips}
|
|
143
|
+
|
|
144
|
+
### Image Size {#image-size}
|
|
145
|
+
|
|
146
|
+
Control processing resolution:
|
|
147
|
+
|
|
148
|
+
```javascript
|
|
149
|
+
Quagga.decodeSingle({
|
|
150
|
+
src: './large-image.jpg',
|
|
151
|
+
inputStream: {
|
|
152
|
+
size: 1280 // Scale to max 1280px
|
|
153
|
+
},
|
|
154
|
+
decoder: { readers: ['ean_reader'] }
|
|
155
|
+
}, callback);
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
### Locator Settings {#locator-settings}
|
|
159
|
+
|
|
160
|
+
For difficult images:
|
|
161
|
+
|
|
162
|
+
```javascript
|
|
163
|
+
Quagga.decodeSingle({
|
|
164
|
+
src: './image.jpg',
|
|
165
|
+
locate: true,
|
|
166
|
+
locator: {
|
|
167
|
+
patchSize: 'small',
|
|
168
|
+
halfSample: false
|
|
169
|
+
},
|
|
170
|
+
decoder: { readers: ['code_128_reader'] }
|
|
171
|
+
}, callback);
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
## Related {#related}
|
|
175
|
+
|
|
176
|
+
- [Static Image Scanning](static-image.md) - Browser-side image decoding
|
|
177
|
+
- [API Reference](../reference/api.md#quagga-decodesingle) - `decodeSingle()` details
|
|
178
|
+
- [Configuration Reference](../reference/configuration.md) - All options
|
|
179
|
+
|
|
180
|
+
---
|
|
181
|
+
|
|
182
|
+
[← Back to Tutorials](index.md)
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
# Using Quagga2 with React {#react-integration}
|
|
2
|
+
|
|
3
|
+
This tutorial shows how to integrate Quagga2 into a React application.
|
|
4
|
+
|
|
5
|
+
## Installation {#installation}
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @ericblade/quagga2
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Basic Component {#basic-component}
|
|
12
|
+
|
|
13
|
+
```jsx
|
|
14
|
+
import React, { useEffect, useRef, useCallback } from 'react';
|
|
15
|
+
import Quagga from '@ericblade/quagga2';
|
|
16
|
+
|
|
17
|
+
function BarcodeScanner({ onDetected }) {
|
|
18
|
+
const scannerRef = useRef(null);
|
|
19
|
+
|
|
20
|
+
const handleDetected = useCallback((result) => {
|
|
21
|
+
if (result.codeResult) {
|
|
22
|
+
onDetected(result.codeResult.code);
|
|
23
|
+
}
|
|
24
|
+
}, [onDetected]);
|
|
25
|
+
|
|
26
|
+
useEffect(() => {
|
|
27
|
+
Quagga.init({
|
|
28
|
+
inputStream: {
|
|
29
|
+
type: 'LiveStream',
|
|
30
|
+
target: scannerRef.current,
|
|
31
|
+
constraints: {
|
|
32
|
+
facingMode: 'environment'
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
decoder: {
|
|
36
|
+
readers: ['code_128_reader', 'ean_reader']
|
|
37
|
+
}
|
|
38
|
+
}, (err) => {
|
|
39
|
+
if (err) {
|
|
40
|
+
console.error('Failed to initialize:', err);
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
Quagga.start();
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
Quagga.onDetected(handleDetected);
|
|
47
|
+
|
|
48
|
+
return () => {
|
|
49
|
+
Quagga.offDetected(handleDetected);
|
|
50
|
+
Quagga.stop();
|
|
51
|
+
};
|
|
52
|
+
}, [handleDetected]);
|
|
53
|
+
|
|
54
|
+
return <div ref={scannerRef} style={{ width: '100%' }} />;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export default BarcodeScanner;
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Usage {#usage}
|
|
61
|
+
|
|
62
|
+
```jsx
|
|
63
|
+
import BarcodeScanner from './BarcodeScanner';
|
|
64
|
+
|
|
65
|
+
function App() {
|
|
66
|
+
const handleScan = (code) => {
|
|
67
|
+
console.log('Scanned:', code);
|
|
68
|
+
alert(`Barcode: ${code}`);
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
return (
|
|
72
|
+
<div>
|
|
73
|
+
<h1>Scan a Barcode</h1>
|
|
74
|
+
<BarcodeScanner onDetected={handleScan} />
|
|
75
|
+
</div>
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## With Hooks {#with-hooks}
|
|
81
|
+
|
|
82
|
+
Create a reusable hook:
|
|
83
|
+
|
|
84
|
+
```jsx
|
|
85
|
+
import { useEffect, useRef, useState } from 'react';
|
|
86
|
+
import Quagga from '@ericblade/quagga2';
|
|
87
|
+
|
|
88
|
+
export function useQuagga(config = {}) {
|
|
89
|
+
const scannerRef = useRef(null);
|
|
90
|
+
const [scanning, setScanning] = useState(false);
|
|
91
|
+
const [result, setResult] = useState(null);
|
|
92
|
+
|
|
93
|
+
useEffect(() => {
|
|
94
|
+
if (!scannerRef.current) return;
|
|
95
|
+
|
|
96
|
+
Quagga.init({
|
|
97
|
+
inputStream: {
|
|
98
|
+
type: 'LiveStream',
|
|
99
|
+
target: scannerRef.current,
|
|
100
|
+
...config.inputStream
|
|
101
|
+
},
|
|
102
|
+
decoder: {
|
|
103
|
+
readers: ['code_128_reader'],
|
|
104
|
+
...config.decoder
|
|
105
|
+
},
|
|
106
|
+
...config
|
|
107
|
+
}, (err) => {
|
|
108
|
+
if (!err) {
|
|
109
|
+
Quagga.start();
|
|
110
|
+
setScanning(true);
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
Quagga.onDetected((data) => {
|
|
115
|
+
setResult(data.codeResult);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
return () => {
|
|
119
|
+
Quagga.stop();
|
|
120
|
+
setScanning(false);
|
|
121
|
+
};
|
|
122
|
+
}, []);
|
|
123
|
+
|
|
124
|
+
return { scannerRef, scanning, result };
|
|
125
|
+
}
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
## Stop on Detection {#stop-on-detection}
|
|
129
|
+
|
|
130
|
+
```jsx
|
|
131
|
+
function SingleScanComponent({ onComplete }) {
|
|
132
|
+
const scannerRef = useRef(null);
|
|
133
|
+
|
|
134
|
+
useEffect(() => {
|
|
135
|
+
Quagga.init({
|
|
136
|
+
inputStream: {
|
|
137
|
+
type: 'LiveStream',
|
|
138
|
+
target: scannerRef.current
|
|
139
|
+
},
|
|
140
|
+
decoder: {
|
|
141
|
+
readers: ['ean_reader']
|
|
142
|
+
}
|
|
143
|
+
}, (err) => {
|
|
144
|
+
if (!err) Quagga.start();
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
const handleDetected = (result) => {
|
|
148
|
+
Quagga.stop();
|
|
149
|
+
onComplete(result.codeResult.code);
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
Quagga.onDetected(handleDetected);
|
|
153
|
+
|
|
154
|
+
return () => {
|
|
155
|
+
Quagga.offDetected(handleDetected);
|
|
156
|
+
Quagga.stop();
|
|
157
|
+
};
|
|
158
|
+
}, [onComplete]);
|
|
159
|
+
|
|
160
|
+
return <div ref={scannerRef} />;
|
|
161
|
+
}
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
## Resources {#resources}
|
|
165
|
+
|
|
166
|
+
- [quagga2-react-example](https://github.com/ericblade/quagga2-react-example/) - Complete example
|
|
167
|
+
- [quagga2-redux-middleware](https://github.com/ericblade/quagga2-redux-middleware/) - Redux integration
|
|
168
|
+
|
|
169
|
+
## Related {#related}
|
|
170
|
+
|
|
171
|
+
- [API Reference](../reference/api.md) - Full API documentation
|
|
172
|
+
- [Configuration Reference](../reference/configuration.md) - All options
|
|
173
|
+
|
|
174
|
+
---
|
|
175
|
+
|
|
176
|
+
[← Back to Tutorials](index.md)
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
# Decoding Static Images {#static-image}
|
|
2
|
+
|
|
3
|
+
This tutorial shows how to decode barcodes from image files instead of a live camera feed.
|
|
4
|
+
|
|
5
|
+
## Basic Usage {#basic-usage}
|
|
6
|
+
|
|
7
|
+
Use `Quagga.decodeSingle()` to decode a single image:
|
|
8
|
+
|
|
9
|
+
```javascript
|
|
10
|
+
Quagga.decodeSingle({
|
|
11
|
+
src: '/path/to/barcode.jpg',
|
|
12
|
+
decoder: {
|
|
13
|
+
readers: ["code_128_reader", "ean_reader"]
|
|
14
|
+
}
|
|
15
|
+
}, function(result) {
|
|
16
|
+
if (result && result.codeResult) {
|
|
17
|
+
console.log("Barcode:", result.codeResult.code);
|
|
18
|
+
} else {
|
|
19
|
+
console.log("No barcode found");
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## From File Input {#from-file-input}
|
|
25
|
+
|
|
26
|
+
Allow users to upload images:
|
|
27
|
+
|
|
28
|
+
### HTML
|
|
29
|
+
|
|
30
|
+
```html
|
|
31
|
+
<input type="file" id="file-input" accept="image/*">
|
|
32
|
+
<div id="result"></div>
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### JavaScript
|
|
36
|
+
|
|
37
|
+
```javascript
|
|
38
|
+
document.querySelector('#file-input').addEventListener('change', function(e) {
|
|
39
|
+
const file = e.target.files[0];
|
|
40
|
+
if (!file) return;
|
|
41
|
+
|
|
42
|
+
const reader = new FileReader();
|
|
43
|
+
reader.onload = function(event) {
|
|
44
|
+
Quagga.decodeSingle({
|
|
45
|
+
src: event.target.result,
|
|
46
|
+
decoder: {
|
|
47
|
+
readers: ["code_128_reader", "ean_reader", "upc_reader"]
|
|
48
|
+
}
|
|
49
|
+
}, function(result) {
|
|
50
|
+
if (result && result.codeResult) {
|
|
51
|
+
document.querySelector('#result').textContent =
|
|
52
|
+
"Found: " + result.codeResult.code;
|
|
53
|
+
} else {
|
|
54
|
+
document.querySelector('#result').textContent = "No barcode found";
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
};
|
|
58
|
+
reader.readAsDataURL(file);
|
|
59
|
+
});
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Controlling Image Size {#controlling-image-size}
|
|
63
|
+
|
|
64
|
+
By default, `decodeSingle()` scales images to 800px. Adjust with `inputStream.size`:
|
|
65
|
+
|
|
66
|
+
```javascript
|
|
67
|
+
Quagga.decodeSingle({
|
|
68
|
+
src: '/path/to/image.jpg',
|
|
69
|
+
inputStream: {
|
|
70
|
+
size: 1280 // Process at higher resolution
|
|
71
|
+
},
|
|
72
|
+
decoder: {
|
|
73
|
+
readers: ["ean_reader"]
|
|
74
|
+
}
|
|
75
|
+
}, callback);
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Set `size: 0` to use original image dimensions.
|
|
79
|
+
|
|
80
|
+
## With Localization {#with-localization}
|
|
81
|
+
|
|
82
|
+
Enable `locate: true` (default) to find barcodes anywhere in the image:
|
|
83
|
+
|
|
84
|
+
```javascript
|
|
85
|
+
Quagga.decodeSingle({
|
|
86
|
+
src: '/path/to/image.jpg',
|
|
87
|
+
locate: true,
|
|
88
|
+
locator: {
|
|
89
|
+
patchSize: "medium",
|
|
90
|
+
halfSample: true
|
|
91
|
+
},
|
|
92
|
+
decoder: {
|
|
93
|
+
readers: ["code_128_reader"]
|
|
94
|
+
}
|
|
95
|
+
}, function(result) {
|
|
96
|
+
if (result && result.codeResult) {
|
|
97
|
+
console.log("Found:", result.codeResult.code);
|
|
98
|
+
console.log("Location:", result.box); // Bounding box
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## Node.js Usage {#nodejs-usage}
|
|
104
|
+
|
|
105
|
+
Quagga2 works in Node.js:
|
|
106
|
+
|
|
107
|
+
```javascript
|
|
108
|
+
const Quagga = require('@ericblade/quagga2').default;
|
|
109
|
+
|
|
110
|
+
Quagga.decodeSingle({
|
|
111
|
+
src: './barcode.jpg',
|
|
112
|
+
decoder: {
|
|
113
|
+
readers: ["code_128_reader"]
|
|
114
|
+
}
|
|
115
|
+
}, function(result) {
|
|
116
|
+
if (result && result.codeResult) {
|
|
117
|
+
console.log("Barcode:", result.codeResult.code);
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## Related {#related}
|
|
123
|
+
|
|
124
|
+
- [Your First Scan](first-scan.md) - Live camera scanning
|
|
125
|
+
- [Node.js Usage](node-usage.md) - Server-side scanning
|
|
126
|
+
- [API Reference](../reference/api.md#quagga-decodesingle) - `decodeSingle()` details
|
|
127
|
+
|
|
128
|
+
---
|
|
129
|
+
|
|
130
|
+
[← Back to Tutorials](index.md)
|