qr-code-detector
v1.0.2
Published
QR code detection and decoding with base64 image extraction. Native Node.js addon using OpenCV.
Maintainers
Readme
QR Code Detector
Node.js native addon for detecting and decoding QR codes using OpenCV. This module runs QR code detection in separate worker threads to ensure non-blocking operation.
Features
- Single QR Code Detection: Detect and decode a single QR code in an image
- Multiple QR Code Detection: Detect and decode multiple QR codes in a single image
- Quick Detection: Check if an image contains a QR code without decoding
- Non-blocking: Uses worker threads for asynchronous processing
- Multiple Input Formats: Supports both file paths and image buffers
- Corner Detection: Returns corner coordinates of detected QR codes
- Base64 Image Export: Extracted QR code region as base64-encoded PNG
Installation
npm installPrerequisites
- Node.js >= 14.0.0
- OpenCV 4.x installed on your system
macOS
brew install opencvLinux
sudo apt-get install libopencv-devUsage
Detect Single QR Code
const { detectQRCode } = require('./qr-detector');
// Using file path
const result = await detectQRCode('/path/to/image.jpg');
console.log(result);
// {
// detected: true,
// data: 'https://example.com',
// corners: [
// { x: 100, y: 100 },
// { x: 200, y: 100 },
// { x: 200, y: 200 },
// { x: 100, y: 200 }
// ],
// qrCodeImage: 'data:image/png;base64,iVBORw0KGgo...'
// }
// Using buffer
const fs = require('fs');
const imageBuffer = fs.readFileSync('/path/to/image.jpg');
const result = await detectQRCode(imageBuffer);Detect Multiple QR Codes
const { detectMultipleQRCodes } = require('./qr-detector');
const result = await detectMultipleQRCodes('/path/to/image.jpg');
console.log(result);
// {
// detected: true,
// count: 2,
// qrCodes: [
// {
// data: 'https://example.com',
// corners: [...]
// },
// {
// data: 'Another QR code data',
// corners: [...]
// }
// ]
// }Quick Detection (Without Decoding)
const { hasQRCode } = require('./qr-detector');
const result = await hasQRCode('/path/to/image.jpg');
console.log(result);
// {
// hasQRCode: true,
// corners: [...]
// }API
detectQRCode(input)
Detects and decodes a single QR code in an image.
Parameters:
input(string|Buffer): Image file path or buffer
Returns: Promise
detected(boolean): Whether a QR code was detecteddata(string|null): Decoded QR code datacorners(Array): Corner points of the QR codeqrCodeImage(string): Base64-encoded PNG of extracted QR code (format:data:image/png;base64,...)
detectMultipleQRCodes(input)
Detects and decodes multiple QR codes in an image.
Parameters:
input(string|Buffer): Image file path or buffer
Returns: Promise
detected(boolean): Whether any QR codes were detectedcount(number): Number of QR codes detectedqrCodes(Array): Array of detected QR codes with data and corners
hasQRCode(input)
Checks if an image contains a QR code without decoding it (faster).
Parameters:
input(string|Buffer): Image file path or buffer
Returns: Promise
hasQRCode(boolean): Whether a QR code was detectedcorners(Array): Corner points of the QR code
Architecture
This module follows the same architecture as the camera-sabotage-detector:
- C++ Native Addon: Uses OpenCV's QRCodeDetector for high-performance detection
- N-API: Ensures compatibility across Node.js versions
- Worker Threads: Runs detection in separate threads for non-blocking operation
- Multiple Input Formats: Supports both file paths and image buffers
License
MIT
