A Ruby application for reading barcodes from PDF files. The application converts PDF pages to images, enhances them for better barcode detection, and extracts barcode data.
- Ruby 2.6.6 or higher
Only zbar-tools
needs to be installed as a system package. ImageMagick is handled through the mini_magick
gem.
sudo apt-get update
sudo apt-get install zbar-tools
sudo dnf install zbar # Use 'yum' instead of 'dnf' for older versions
brew install zbar
# If you don't have Homebrew installed
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
- First, install bundler if you haven't:
gem install bundler
- Install required gems:
bundle install
Key gems used:
pdf-reader
- For reading and processing PDF filesmini_magick
- For image processing and enhancement- Standard library:
tempfile
,fileutils
To scan a PDF file for barcodes:
cd test_files
bundle exec ruby barcode_reader.rb path/to/your/file.pdf
-
PDF Processing:
- Reads PDF using
pdf-reader
- Processes first two pages only
- Converts pages to high-resolution PNG (600 DPI)
- Reads PDF using
-
Image Enhancement (using
mini_magick
):- Converts to grayscale
- Auto-levels and normalizes
- Enhances contrast
- Applies sharpening
-
Barcode Detection:
- Uses
zbarimg
command-line tool - Extracts barcode data
- Parses into CRN and Amount
- Uses
The script outputs:
- Raw barcode data (e.g.,
*29581200051216188000014453
) - Parsed data:
- Customer Reference Number (CRN)
- Payment Amount (formatted as currency)
If you encounter PDF processing issues:
- Ensure PDF is not password-protected
- Check PDF is not corrupted
- Try with a different PDF to isolate the issue
If barcode is not being detected:
- Ensure PDF quality is good
- Check if barcode is visible and not distorted
- Try adjusting image enhancement parameters in the code
If you get command not found errors:
- Verify
zbar-tools
is installed:which zbarimg
- Check system PATH includes the necessary directories
- Try reinstalling the package