How to Build Online Barcode Scanning App with Python Django
Industries, such as healthcare, finance, and education, have to deal with amounts of paper per day. An excellent electronic document management system can bring high-efficiency to companies. In modern document management system, barcode and QR code are playing a key role. In this article, we combine document scanning and barcode recognition technologies to implement an online document barcode reader with Python.
Implementing Online Barcode Reader in Python
Step 1: Load and Scan Barcode Images with Dynamic Web TWAIN SDK
To quickly implement a web document management application that supports image loading, scanning, and uploading, you can read the article Online Document Scanning with Django and Dynamic Web TWAIN, which has provided detailed steps.
Let’s get started with the source code:
git clone https://github.com/dynamsoft-dwt/Scan-Documents-with-Python.git
Step 2: Install Python Barcode Library
Dynamsoft has published Python barcode SDK to https://pypi.org/project/dbr/. You can install the SDK via pip command:
pip install dbr
- Linear Barcodes (1D) :
- Code 39 (including Code 39 Extended)
- Code 93
- Code 128
- Interleaved 2 of 5
- Industrial 2 of 5
- MSI Code
- 2D Barcodes :
- QR Code (including Micro QR Code)
- Data Matrix
- PDF417 (including Micro PDF417)
- Aztec Code
- MaxiCode (mode 2-5)
- Patch Code
- GS1 Composite Code
- GS1 DataBar :
- Stacked Omnidirectional
- Expanded Stacked
- Postal Codes :
- USPS Intelligent Mail
- Australian Post
- UK Royal Mail
Step 3: Integrating Python Barcode SDK into the Existing Django Project
Import the module to
from dbr import * import json reader = BarcodeReader() # Apply for a trial license: https://www.dynamsoft.com/customer/license/trialLicense?product=dbr&utm_source=github reader.init_license("LICENSE-KEY")
You need a valid license key to unlock the capabilities of Dynamsoft Barcode SDK.
Once an image is uploaded, you can call
decode_file() function to recognize barcodes:
def upload(request): out = "No barcode found" if request.method == 'POST': filePath = handle_uploaded_file(request.FILES['RemoteFile'], str(request.FILES['RemoteFile'])) try: text_results = reader.decode_file(filePath) if text_results != None: out = '' for text_result in text_results: out += "Barcode Format : " + text_result.barcode_format_string + "\n" out += "Barcode Text : " + text_result.barcode_text + "\n" out += "------------------------------------------------------------" except BarcodeReaderError as bre: print(bre) return HttpResponse(out) return HttpResponse(out) return HttpResponse(out) def handle_uploaded_file(file, filename): if not os.path.exists('upload/'): os.mkdir('upload/') filePath = 'upload/' + filename with open(filePath, 'wb+') as destination: for chunk in file.chunks(): destination.write(chunk) return filePath
Now, let’s run the project as follows:
python manage.py makemigrations python manage.py migrate --run-syncdb python manage.py runserver
Testing the Online Barcode Reader Application
Load a document image which contains multiple barcodes.
Click the Read Barcode button.
Check the returned results.