pdf_barcode_decoder 0.0.1
pdf_barcode_decoder: ^0.0.1 copied to clipboard
A fast, lightweight Flutter plugin to decode barcodes (QR, PDF417, Aztec, DataMatrix, Code128, etc.) directly from PDF files and bytes.
PDF Barcode Decoder #
A fast, lightweight, and offline Flutter plugin to scan and decode 1D & 2D barcodes directly from PDF files and raw bytes.
Under the hood, pdf_barcode_decoder renders PDF pages natively using Android PdfRenderer and Apple iOS PDFKit, then extracts barcodes using native, high-performance engines (ZXing Core on Android and Apple Vision Framework on iOS).
π Key Features #
- β‘ Zero External Cloud/Network Dependencies β 100% offline, private, and on-device.
- πͺΆ Lightweight & Clean β Zero Google Play Services, ML Kit, or Firebase dependencies.
- π Multiple Input Sources β Decode directly from
File, in-memoryUint8List, or bundled asset paths. - π― Multi-Barcode Detection β Detects multiple barcodes on the same page with IoU-based deduplication.
- π Precise Bounding Boxes β Returns exact pixel coordinates (
Rect) for bounding overlays and region cropping. - βοΈ Configurable Pipeline β Control rendering resolution (DPI), page ranges (
maxPages,firstPageOnly), early stopping (stopAfterFirst), and barcode format filters. - π Android 15 (16KB Page Size) Ready β Pure Java/Kotlin implementation without problematic C/C++ native binaries.
π± Platform Support #
| Platform | Minimum OS Version | PDF Renderer | Barcode Engine | Notes |
|---|---|---|---|---|
| Android | Android 5.0 (API 21+) | android.graphics.pdf.PdfRenderer |
ZXing core:3.5.4 |
No Google Play Services required |
| iOS | iOS 13.0+ | PDFKit (PDFDocument) |
Vision.framework (VNDetectBarcodesRequest) |
System framework, zero CocoaPods dependencies |
π¦ Supported Barcode Formats #
| Format Enum | Symbology / Barcode Type | Android (ZXing) | iOS (Vision) |
|---|---|---|---|
BarcodeFormat.qr |
QR Code | β | β |
BarcodeFormat.pdf417 |
PDF417 | β | β |
BarcodeFormat.aztec |
Aztec Code | β | β |
BarcodeFormat.dataMatrix |
DataMatrix | β | β |
BarcodeFormat.code128 |
Code 128 | β | β |
BarcodeFormat.ean13 |
EAN-13 | β | β |
BarcodeFormat.ean8 |
EAN-8 | β | β |
BarcodeFormat.upc |
UPC-A & UPC-E | β | β |
BarcodeFormat.itf |
Interleaved 2 of 5 (ITF) | β | β |
BarcodeFormat.codabar |
Codabar | β | β |
BarcodeFormat.all |
All supported formats | β | β |
ποΈ Architecture & How It Works #
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Flutter Application β
β PdfBarcodeDecoder.decode() / decodeFile() / decodeAsset() β
ββββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββ
β
Method Channel (decodePdf)
β
ββββββββββββββββββββ΄βββββββββββββββββββ
βΌ βΌ
βββββββββββββββββββββββββ βββββββββββββββββββββββββ
β Android Host β β iOS Host β
β (PdfDecodeManager) β β (PdfDecodeManager) β
βββββββββββββββββββββββββ€ βββββββββββββββββββββββββ€
β 1. PdfRenderer β β 1. PDFKit β
β (Render at DPI) β β (Render at DPI) β
β 2. ZXing Core β β 2. Vision Framework β
β (MultiFormatReader)β β (DetectBarcodes) β
β 3. IoU Deduplication β β 3. Coordinate Scaling β
βββββββββββββ¬ββββββββββββ βββββββββββββ¬ββββββββββββ
β β
ββββββββββββββββββββ¬βββββββββββββββββββ
βΌ
List<PdfBarcode> (with Page & Rect)
π₯ Getting Started #
Add pdf_barcode_decoder to your pubspec.yaml dependencies:
dependencies:
pdf_barcode_decoder: ^0.0.1
Or run:
flutter pub add pdf_barcode_decoder
π‘ Usage Examples #
1. Decode from a File (e.g. from file_picker or camera capture) #
import 'dart:io';
import 'package:pdf_barcode_decoder/pdf_barcode_decoder.dart';
Future<void> scanPdfFile(String filePath) async {
final file = File(filePath);
final List<PdfBarcode> barcodes = await PdfBarcodeDecoder.decodeFile(file);
for (final barcode in barcodes) {
print('Found ${barcode.type.name} on Page ${barcode.page + 1}: ${barcode.value}');
print('Bounding box: ${barcode.boundingBox}');
}
}
2. Decode from In-Memory Bytes (e.g. downloaded over HTTP) #
import 'dart:typed_data';
import 'package:http/http.dart' as http;
import 'package:pdf_barcode_decoder/pdf_barcode_decoder.dart';
Future<void> scanPdfFromUrl(String url) async {
final response = await http.get(Uri.parse(url));
final Uint8List pdfBytes = response.bodyBytes;
final List<PdfBarcode> barcodes = await PdfBarcodeDecoder.decode(pdfBytes);
print('Found ${barcodes.length} barcode(s) in downloaded PDF.');
}
3. Decode from Bundled App Asset #
import 'package:pdf_barcode_decoder/pdf_barcode_decoder.dart';
Future<void> scanBundledPdf() async {
final List<PdfBarcode> barcodes = await PdfBarcodeDecoder.decodeAsset(
'assets/invoices/sample_invoice.pdf',
);
for (final b in barcodes) {
print('Barcode value: ${b.value}');
}
}
4. Advanced Configuration #
Fine-tune rendering quality, filter specific formats, scan only specific pages, or exit early:
import 'dart:io';
import 'package:pdf_barcode_decoder/pdf_barcode_decoder.dart';
final barcodes = await PdfBarcodeDecoder.decodeFile(
File('/path/to/shipping_label.pdf'),
config: const DecoderConfig(
// Rendering resolution (higher DPI = better recognition of small/dense barcodes)
dpi: 300,
// Scan only the first page
firstPageOnly: false,
// Exit immediately after finding the first barcode
stopAfterFirst: true,
// Scan up to the first 3 pages
maxPages: 3,
// Target specific barcode formats
formats: [
BarcodeFormat.qr,
BarcodeFormat.pdf417,
BarcodeFormat.code128,
],
),
);
5. Error Handling #
All platform and rendering errors are wrapped in a typed PdfBarcodeException:
import 'dart:io';
import 'package:pdf_barcode_decoder/pdf_barcode_decoder.dart';
try {
final barcodes = await PdfBarcodeDecoder.decodeFile(File('protected.pdf'));
} on PdfBarcodeException catch (e) {
switch (e.code) {
case 'ENCRYPTED_PDF':
print('Password-protected PDFs cannot be scanned.');
break;
case 'INVALID_PDF':
print('The provided file is corrupted or not a valid PDF.');
break;
case 'RENDER_FAILED':
print('Failed to render PDF pages: ${e.message}');
break;
default:
print('Decoding error [${e.code}]: ${e.message}');
}
}
π API Reference #
DecoderConfig #
| Property | Type | Default | Description |
|---|---|---|---|
dpi |
int |
300 |
Resolution (DPI) at which PDF pages are rendered. Higher values improve detection on high-density 2D barcodes at the expense of memory. |
firstPageOnly |
bool |
false |
If true, only the first page (index 0) of the PDF is scanned. |
stopAfterFirst |
bool |
false |
If true, scanning halts immediately as soon as at least one barcode is detected. |
maxPages |
int? |
null |
Maximum number of pages to scan from the start of the document. If null, all pages are scanned. |
formats |
List<BarcodeFormat> |
[BarcodeFormat.all] |
Restricts detection to specified symbologies. |
PdfBarcode #
| Property | Type | Description |
|---|---|---|
type |
BarcodeFormat |
The detected barcode symbology (e.g. BarcodeFormat.qr, BarcodeFormat.pdf417). |
value |
String |
The raw decoded string value of the barcode. |
page |
int |
The 0-indexed page number where the barcode was found. |
boundingBox |
Rect |
The bounding box coordinates (in pixel space at the configured render DPI) locating the barcode on the page. |
β‘ Performance & Best Practices #
- Choosing the Optimal DPI:
150 DPIβ Fast; suitable for large standard QR codes and shipping label barcodes.300 DPI(Default) β Recommended balance for crisp renders, PDF417 boarding passes, and multi-code documents.400+ DPIβ Use when barcodes are physically tiny or high-density DataMatrix codes.
- Page Limits: For long documents (e.g., 50+ page invoices), always supply
maxPagesorstopAfterFirst: trueif you only need the header barcode. - Memory Management: Both native Android and iOS engines recycle page bitmaps and autorelease pools after each page scan to ensure low memory footprints.
π± Example App #
Check the example/ folder for a complete sample Flutter app demonstrating PDF picking, asset scanning, and real-time result listing.
To run the example app:
cd example
flutter run
π€ Contributing #
Contributions, issues, and feature requests are welcome! Feel free to check the issues page.
π License #
This project is licensed under the BSD 3-Clause License - see the LICENSE file for details.