Django Barcode Scanning App

Many industries, which need to process amounts of paper per day, like healthcare, finance, education and so on, are getting started to adopt Barcode scanning as the solution for the electronic document management system. For example, when you have a health examination, you will pick a form with some Barcode stickers. Once your health report generated, it is easy to track and record all relevant information. We can implement Barcode detection either on the client side or on the server side. Many client-side applications, especially mobile apps, tend to integrate Barcode functionality for shopping, boarding, social networking, logistics and so on. In contrast, some enterprise solutions, such as ECM (enterprise content management) system, need to detect Barcode on the server side while collecting various e-documents. In this post, I will demonstrate how to quickly build a Django barcode scanning app to load and scan document images via a Web page and detect barcode information on the server-side.

Download for Django Barcode Scanning App

To create the online Barcode reader, we need to download Django, Dynamic Web TWAIN SDK, and Dynamsoft Barcode SDK.

django

DWT download button

DBR download button

Online Barcode Reader in Python

Using Dynamic Web TWAIN SDK to Load and Scan Barcode Images

Read the article Uploading Files with Django and follow the necessary steps to create a Web page for image loading, scanning, and uploading.

load & scan images

Python Barcode Library with Dynamsoft Barcode C/C++ APIs

Read the article Wrapping C/C++ Methods of Dynamsoft Barcode SDK for Python to quickly create a Barcode library in Python

Python barcode sdk

Deploying Python Barcode Library to Django Project

Copy Python Barcode Reader to {Django Project Root}\dwtupload.
use Python Barcode
Create a Python module dbr.py.

import os.path
import DynamsoftBarcodeReader

formats = {
    0x1FFL : "OneD",
    0x1L   : "CODE_39",
    0x2L : "CODE_128",
    0x4L   : "CODE_93",
    0x8L : "CODABAR",
    0x10L   : "ITF",
    0x20L : "EAN_13",
    0x40L   : "EAN_8",
    0x80L : "UPC_A",
    0x100L   : "UPC_E",
}

def decodeFile(fileName):
    if not os.path.isfile(fileName):
        print "It is not a valid file."
        return

    results = DynamsoftBarcodeReader.decodeFile(fileName)
    json = {}
    tmp = []
    i = 0

    # Convert results to JSON
    for result in results:
        key = formats[result[0]]
        value = result[1]
        tmp = [key, value]
        json[i] = tmp
        i += 1;

    return str(json)

Import the module to views.py.

from django.shortcuts import render
from django.http import HttpResponse
import os
import dbr

# Create your views here.
def home(request):
    return render(request, 'index.htm', {'what':'Online Barcode Reader with Django'})

def upload(request):
    if request.method == 'POST':
        uploadedFile = handle_uploaded_file(request.FILES['RemoteFile'], str(request.FILES['RemoteFile']))
        results = dbr.decodeFile(uploadedFile)
        return HttpResponse(results)

    return HttpResponse("Failed")

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

Run the project as follows:

python manage.py runserver

Testing the Online Barcode Reader

Load a document image with multiple Barcodes.

Select Barcode images

Click Read Barcode.

load multi Barcode images

Check the returned results.
Barcode results

Source Code

https://github.com/dynamsoftsamples/django-barcode-reader