スマートフォンバーコードスキャン:カメラAPIとライブラリ
Building barcode scanning into mobile apps — iOS Vision framework, Android ML Kit, web-based ZXing.js, and performance optimization.
Smartphone Barcode Scanning: Camera APIs & Libraries
Building barcode scanning into mobile apps requires choosing the right SDK or API for your platform. Modern smartphones have the processing power and camera quality to rival dedicated scanners when the software is properly implemented.
iOS: Vision Framework
Apple's native Vision framework provides barcode detection:
let request = VNDetectBarcodesRequest { request, error in
guard let results = request.results as? [VNBarcodeObservation] else { return }
for barcode in results {
print(barcode.payloadStringValue ?? "")
}
}
Supported symbologies include EAN-13, UPC-A, Code 128, Code 39, Data Matrix, QR Code, PDF417, and Aztec.
Android: ML Kit
Google's ML Kit provides barcode scanning for Android:
- Real-time scanning from camera preview
- Support for all major 1D and 2D symbologies
- On-device processing (no network required)
- Available through Google Play Services
Cross-Platform: ZXing
ZXing ("Zebra Crossing") is the most widely used open-source barcode library:
- Available for Java, JavaScript, C++, Python, and other languages
- ZXing.js enables browser-based scanning via WebRTC
- Supports all common symbologies
- Free and open-source (Apache License 2.0)
Performance Optimization
Tips for fast, reliable scanning:
- Focus: Ensure the camera auto-focus is active and set to macro/close-up mode
- Lighting: Use the device flashlight in low-light conditions
- Frame rate: Process every frame for real-time feedback, but decode on a background thread
- Region of interest: Limit the decode area to the center of the frame to reduce processing
- Resolution: Use 720p or 1080p preview; higher resolutions do not significantly improve decode rates
- Orientation: Support both portrait and landscape scanning
- Feedback: Provide immediate visual and haptic feedback on successful decode
Multi-Code Scanning
Some applications need to read multiple barcodes simultaneously:
- Inventory counting: Scan multiple shelf labels in one camera frame
- Document processing: Read multiple barcodes on a shipping label
- Batch scanning: Continuous scanning of items moving past the camera
ML Kit and Vision both support multi-code detection in a single frame.
Web-Based Scanning
The BarcodeDetector Web API enables browser-based scanning without native app installation:
const barcodeDetector = new BarcodeDetector({
formats: ['ean_13', 'qr_code', 'data_matrix']
});
Browser support is growing but still requires polyfills for some platforms.
Library Comparison
| Library | Platform | 1D | 2D | Speed | License |
|---|---|---|---|---|---|
| Vision | iOS | Yes | Yes | Fast | Apple |
| ML Kit | Android | Yes | Yes | Fast | |
| ZXing | Cross-platform | Yes | Yes | Good | Apache 2.0 |
| Scandit | Cross-platform | Yes | Yes | Very fast | Commercial |
| Dynamsoft | Cross-platform | Yes | Yes | Fast | Commercial |