barcode detection from an image

34
1. INTRODUCTION 1.1 Problem Definition Barcodes provide a simple and inexpensive method of encoding text information that is easily read by inexpensive electronic readers. There are various barcode detection devices which use different methods to recognise the barcode. Due to the problems associated with the currently available barcode readers, the need for a sophisticated barcode reader is evident. In this project, we make use of the image based (camera based) barcode detection method. An image of the area containing a valid barcode will be captured and transferred to the computer using any transmission. We propose to build an application which will take the input as the image stored in the computer and give barcode in the image as the output. 1.2 Scope Our main goal is to build an application capable of identifying any barcode 1

Upload: apoorva-joshi

Post on 07-Apr-2015

558 views

Category:

Documents


8 download

TRANSCRIPT

Page 1: Barcode Detection From an Image

1. INTRODUCTION

1.1 Problem Definition

Barcodes provide a simple and inexpensive method of encoding text information that is easily read by inexpensive electronic readers. There are various barcode detection devices which use different methods to recognise the barcode. Due to the problems associated with the currently available barcode readers, the need for a sophisticated barcode reader is evident.

In this project, we make use of the image based (camera based) barcode detection method. An image of the area containing a valid barcode will be captured and transferred to the computer using any transmission. We propose to build an application which will take the input as the image stored in the computer and give barcode in the image as the output.

1.2 Scope

Our main goal is to build an application capable of identifying any barcode irrespective of its orientation and background, making it an entirely autonomous system.

1. The image must be captured by a high resolution camera .The application accepts a static image as input. The image must be stored on the hard disk of the computer. Also, the image should be in ‘.jpg’ format.

1

Page 2: Barcode Detection From an Image

2. The application can decode barcodes which make use of the following symbologies :

a. Code 39 b. Code 128

i. Code 128 – Aii. Code 128 – B

iii. Code 128 – Cc. Universal Product Code – A

3. The output contains the alphanumeric string encoded in the barcode.

2

Page 3: Barcode Detection From an Image

2. PROJECT DESCRIPTION

2.1 Barcode

A barcode is an optical machine-readable representation of data which consists of a series of lines of varying thicknesses printed in a parallel sequence, with numeric code above or below the lines. Barcodes are printed onto paper or embedded into a product, and can only be read by a scanner or barcode reader.Originally, barcodes represented data in the widths (lines) and the spacing of parallel lines, and may be referred to as linear or 1D (1 dimensional) barcodes or symbologies. They also come in patterns of squares, dots, hexagons and other geometric patterns within images termed 2D (2 dimensional) matrix codes or symbologies. Although 2D systems use symbols other than bars, they are generally referred to as barcodes as well.

The basic structure of a bar code consists of a leading and trailing quiet zone, a start pattern, one or more data characters, optionally one or two check characters and a stop pattern.

There are a variety of different types of bar code encoding schemes or "symbologies", each of which were originally developed to fulfil a specific need in a specific industry.

3

Page 4: Barcode Detection From an Image

2.2 Barcode Symbologies

The mapping between messages and barcodes is called a symbology. The specification of a symbology includes the encoding of the single digits/characters of the message as well as the start and stop markers into bars and space, the size of the quiet zone required to be before and after the barcode as well as the computation of a checksum.

The following is a detailed description of the most commonly used bar code symbologies. All of the following types of bar codes are fully supported by our application.

2.2.1 Code 39

The Normal CODE 39 is a variable length symbology that can encode the following 44 characters:1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ-. *$/+%. Code 39 is the most popular symbology in the non-retail world and is used extensively in manufacturing, military, and health applications. Each Code 39 bar code is framed by a start/stop character represented by an asterisk (*). The Asterisk is reserved for this purpose and may not be used in the body of a message.

Code 39 optionally allows for a (modulo 43) check character in cases where data security is important. The health care industry has adopted the use of this check character for health care applications.

4

Page 5: Barcode Detection From an Image

2.2.2 Code 128

Code 128 is a variable length, high density, alphanumeric symbology. Code 128 has 106 different bar and space patterns and each pattern can have one of three different meanings, depending on which of three different character sets is employed. Special start characters tell the reader which of the character sets is initially being used and three special shift codes permit changing character sets inside a symbol. One character sets encodes all upper case and ASCII control characters, another encodes all upper and lower case characters and the third set encodes numeric digit pairs 00 through 99. This third character set effectively doubles the code density when printing numeric data. Code 128 also employs a check digit for data security.

Code 128 actually includes 107 symbols: 103 data symbols, 3 start codes, and 1 stop code. To represent all 128 ASCII values, there are actually three subcodes, which can be mixed within a single barcode:

1. 128A - ASCII characters 00 to 95 (0-9, A-Z and control codes) and special characters.

2. 128B - ASCII characters 32 to 127 (0-9, A-Z, a-z) and special characters.

3. 128C - 00-99 (double density encoding of numeric only data) and FNC1.

5

Page 6: Barcode Detection From an Image

2.2.3 Universal Product Code – A

The "UPC-A bar code" is by far the most common and well-known symbology, at least in the United States. An UPC-A bar code is the bar code you will find on virtually every consumer good on the shelves of your local supermarket, as well as books, magazines, and newspapers. It is commonly called simply a "UPC bar code" or "UPC Symbol." This isn't entirely accurate since there are a number of other UPC formats (UPC-E, UPC 2-Digit Supplement, UPC 5-Digit Supplement).UPC-A encodes 11 digits of numeric (0 through 9) message data along with a trailing check digit, for a total of 12 digits of bar code data.

The human-readable digits are printed for the benefit of us lowly humans only. Obviously, the scanner doesn't pay any attention to them whatsoever and a bar code that is printed without these numbers will work just as well as one that includes them.

2.3 Barcode Scanner

A Barcode Scanner is an input device used to capture information using CCD, laser o imager technology that read information on printed barcodes for products identification. Barcode Scanner are perfect choice to increase throughput at retail, warehouse management for assets tracking, manufacturing centre for distribution control, checkout counter for assets, inventory tracking, hospital control medications and patients at the pharmacy, Access control, commercial and industrial applications. Barcode is a technology for automatic identification that allows collecting data accurately.

6

Page 7: Barcode Detection From an Image

A Barcode Scanner can use different technologies for data acquisition, for lights and heavy data input. Barcode Scanner types include:

CCD Barcode Scanner Laser Barcode Scanner Camera based Barcode Scanner Omni-Directional Barcode Scanner

In our project we have made use of the camera based scanning technique.

2.3.1 Camera based Barcode Scanner

The latest in barcode scanner technology, camera type scanners, or 2D imaging scanners, utilize a small video camera to record an image of a barcode and then image is then converted into digital information. These are actually quite similar to LED or CCD barcode scanners in that 2D imaging scanners use hundreds of tiny little light sensors to capture ambient light and then measure it, and decode it. The difference is that instead of a single row of little tiny light sensors, imaging scanners have hundreds of rows of little lights arranged in such a way that an image can be captured.

Camera based barcode readers, or 2D imaging scanners, are rarely used in the supermarkets these days. Chances are that you haven't heard very much about them. They are capable of scanning very quickly, so fast in fact, that they can also often read barcode labels that have been wrinkled or dirtied. Imaging scanners are commonly used in the mail order business for parcel sorting.

A lot of camera-based barcode scanners are capable of scanning barcode labels which are moving on a conveyor belt at speeds of around 600 fps (feet per second). And depending on the software used in conjunction with an imaging scanner, important diagnostic information can be obtained such as carton spacing, a parcel's position on the belt, and even changes in lighting or power supply. Many handheld imaging scanners now come in wireless or cordless models, which makes scanning in a warehouse that much easier. The battery life on such scanners is surprisingly efficient.

7

Page 8: Barcode Detection From an Image

And again, like most of the technologies used in barcode scanning, camera based barcode readers (or 2d imaging readers) can be housed in both handheld casing and larger stationary cases. Although there are handheld camera based scanners, there aren't used nearly as much as the stationary versions which are used in wholesale or retail distribution.

So as you can see, there is a lot to think about in respect to camera based scanners. With more advanced technology comes more device capability. And as always, if you are in the market for a barcode scanner, it is important to think about the circumstances under which you'll be using your scanner; an industrial, office, or retail environment, hot or cold, etc. some readers now even come in special housings that allow disinfectants to be used on them without damaging the electronics.

8

Page 9: Barcode Detection From an Image

4. PROJECT DESIGN

3.1 Technologies used :

3.1.1 JavaJava is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems’ Java Platform. The language derives much of its syntax from C or C++ but as a simpler object model and fewer low-level facilities. Java applications are typically compiled to bytecode ( class file ) that can run on any Java Virtual Machine ( JVM ) regardless of computer architecture. Java is general-purpose, concurrent, class-based, and object-oriented, and specifically designed to have as few implementation dependencies as possible. It is intended to let application developers “write-once run anywhere”.

Java was built almost exclusively as an object oriented language. All code is written inside a class and everything is an object, with the exception of the intrinsic data types (ordinal and real numbers, boolean values, and characters), which are not classes for performance reasons.

Java suppresses several features (such as operator overloading and multiple inheritance) for classes in order to simplify the language and to prevent possible errors and anti-pattern design.

3.1.2 Java Swing

Swing is a widget toolkit for Java. It is part of Sun Microsystems' Java Foundation Classes (JFC) — an API for providing a graphical user interface (GUI) for Java programs.

Swing was developed to provide a more sophisticated set of GUI components than the earlier Abstract Window Toolkit. Swing provides a native look and feel that emulates the look and feel of several platforms, and also supports a pluggable

9

Page 10: Barcode Detection From an Image

look and feel that allows applications to have a look and feel unrelated to the underlying platform.

Swing is a platform-independent, Model-View-Controller GUI framework for Java. It follows a single-threaded programming model.

3.1.3 DTK Barcode Reader Developer Library

DTK Barcode Reader is a highly accurate and powerful developer library which recognises 1-D and 2-D barcodes from digital images and bitmaps. Using this library one can integrate barcode recognition functionality, Windows applications, embedded systems Windows CE, Pocket PC, Smartphone) and web services. The unique and fast barcode recognition algorithm searches for barcodes in any position and orientation from your images.

3.1.4 exe4j

exe4j is a Java .exe maker that helps you integrate your Java applications into the Windows operating evnvironment, whether they are service, GUI or command line aplications. If you want your own process name instead of Java.exe in the task manager and a user-friendly task-bar grouping in Windows XP exe4j does the job. Exe4j helps you with starting your java applications in a safe way displaying native splash screen,detecting or distributing suitable JREs and JDKs, startup error handling and much more.

10

Page 11: Barcode Detection From an Image

3.2 Flowchart for detection

11

Accept Image

Select the area which contains the barcode

Detect start and end characters of the barcode

Interpret the parallel lines based on the barcode

symbology used

Generate the key detected in the earlier step

Display Result

Start

Stop

Identify the orientation of the barcode

Page 12: Barcode Detection From an Image

The barcode detection begins with acceptance of a static image which has to be done by taking the input as the path to the static image which is stored on the computer. To make the input easy, instead of taking the path as string the user is provided with an ‘Open’ Dialog Box . When the user clicks the ‘Select File’ option the dialog box prompts user to select the file from file storage system. This can be implemented by using ‘JFileChooser’ class in java. JFileChooser provides the user a simple mechanism to choose a file. It is done as follows :

JFileChooser fileChooser = new JFileChooser();

This creates an instance of the JFIleChooser class which opens the dialog box.

int returnValue = fileChooser.showOpenDialog(null);

This is to check if the user has selected any option before closing the dialog box. It will return a value if the user has selected some file. If the user has selected some option then we use the following code to retrieve the path given by the user.

File selectedFile = fileChooser.getSelectedFile();path = selectedFile.getAbsolutePath();

The File object is an abstract representation of file and directory pathnames.The method returns the absolute pathname string of this abstract pathname.

Now the chosen image should be loaded into a BufferedImage object which can be done as follows :

The BufferedImage subclass describes an image with an accessible buffer of image data.

BufferedImage buffImage = ImageIO.read(selectedFile);

Thus, the ImageIO.read() method takes the input of the type ‘File’ and loads the image into ‘buffImage’ which is of the type ‘BufferedImage’.

Now, we need to set the environment for detection of the barcodes by setting the types of barcodes which we want to detect. To implement this, we create an object of the type BarcodeReader which is defined in the library used.

12

Page 13: Barcode Detection From an Image

BarcodeReader barReader = new BarcodeReader();

The BarcodeReader class has a method setBarcodeTypes() where the barcode types are set to Code39,Code128 and UPC-A.

We can also choose the number of barcodes we need to scan from a single image. Here, we only detect one barcode at a time from a given image.Now, the scanning is done by the function ReadFromImage() which returns a collection of barcodes which is stored in the object ‘barcodes’.

BarcodeCollection barcodes = barReader.ReadFromImage(buffImage);

We first check if there are any barcodes stored in the barcodes by checking the length of the barcodes object. Since we are detecting only one barcode it will always return either 1 or 0 as the length.

To retrieve each barcode from the collection of barcodes, we use the following code.

Barcode bar = barcodes.getItem(0);

Since we detect only one barcode at a time we retrieve only first barcode from the collection.

The result of the barcode detection is stored in the Barcode object in barcodeString attribute. So, we can extract the code in the image by executing:

String barcodeStr = bar.barcodeString;

Thus, the barcode is retrieved from the image and displayed on the screen.

13

Page 14: Barcode Detection From an Image

4. IMPLEMENTATION

4.1 Hardware requirements1. Digital Camera ( 5 megapixel or higher).2. A transfer medium such as USB connector.

4.2 Software Requirements1. Java Development Kit (JDK) 1.5 or higher.

4.3 User Manual

1. Note the type of your barcode.

2. Switch on your camera.

3. Capture an image of your barcode from your camera.

4. Go to the icon of the software & Double click on the icon.

5. Now, click on ‘Select File’ which opens a dialog file.

6. Select the file from memory.

7. The image along with result will be displayed as result.

14

Page 15: Barcode Detection From an Image

4.4 Screenshots

Screenshot 1:

The above is the initial user screen which appears when you double click on the ‘Barcode Reader’ icon. This screen contains the option ‘Select File’ which allows user to open the dialog box to select file.

15

Page 16: Barcode Detection From an Image

Screenshot 2 :

This interface allows us to browse through the file system and hence select the desired file for further detection.

16

Page 17: Barcode Detection From an Image

Screenshot 3 :

Here is the output displayed after the required clipping, orientation, and interpretation has been performed by the program.

17

Page 18: Barcode Detection From an Image

Screenshot 4 :

This is the screen which appears when user chooses an image file that either does not contain a barcode or the barcode present in the image is

not defined by the symbologies within our scope.

18

Page 19: Barcode Detection From an Image

5. APPENDIX

Source Code:import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.File;import java.awt.event.*;import javax.imageio.ImageIO;import java.awt.image.BufferedImage;import javax.swing.*;import java.awt.*;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import javax.imageio.ImageIO;

import dtkbarcode.*;

public class Barcodereaderproject implements ActionListener{

public String barcodestring;JFrame frame;JLabel label,barcode;JButton button;BufferedImage image;

public Barcodereaderproject(){

JFrame.setDefaultLookAndFeelDecorated(true);JDialog.setDefaultLookAndFeelDecorated(true);frame = new JFrame("THE Barcode Detector");frame.setLayout(new FlowLayout()) ;frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);button = new JButton("Select File");barcode = new JLabel();label = new JLabel();frame.add(button);frame.getContentPane().add(label);frame.add(barcode);

frame.pack();frame.setSize(500,500);button.addActionListener(this);

frame.setVisible(true);

}

public static void main(String[] args) {

19

Page 20: Barcode Detection From an Image

Barcodereaderproject t = new Barcodereaderproject(); }

public void actionPerformed(ActionEvent ae){

JFileChooser fileChooser = new JFileChooser();String path;

int returnValue = fileChooser.showOpenDialog(null);frame.getContentPane().remove(label);frame.getContentPane().remove(barcode);

if (returnValue == JFileChooser.APPROVE_OPTION) {

File selectedFile = fileChooser.getSelectedFile(); path = selectedFile.getAbsolutePath(); System.load("C:\\DTKBarReader.dll");

BarcodeReader barReader = new BarcodeReader();String barcodeStr = new String();

BufferedImage buffImage = null;try{

buffImage = ImageIO.read(selectedFile);}catch (IOException e){

e.printStackTrace();}barReader.setBarcodeTypes(BarcodeTypeEnum.BT_Code39.getValue()+BarcodeTypeEnum.BT_Code128.getValue()+BarcodeTypeEnum.BT_Inter2of5.getValue()+BarcodeTypeEnum.BT_UPCA.getValue());barReader.setBarcodesToRead(1);barReader.setScanInterval(1);

barReader.setPDFReadingType(PDFReadingTypeEnum.PDF_Images);barReader.setBarcodeOrientation(BarcodeOrientationEnum.BO_LeftToRight.getValue() +BarcodeOrientationEnum.BO_RightToLeft.getValue() );BarcodeCollection barcodes = barReader.ReadFromImage(buffImage);if (barcodes.getCount() > 0){

Barcode bar = barcodes.getItem(0);if (bar.barcodeString.length() > 0){

barcodeStr = bar.barcodeString;System.out.println("Barcode: " + barcodeStr);label=null;if(barcodeStr.length()>0){

20

Page 21: Barcode Detection From an Image

String p = "C:\\SEMINAR\\Images\\1.jpg"; File file = new File(path); image = null; try {

image = ImageIO.read(file); } catch(Exception e) {}

label = new JLabel(new ImageIcon(image)); barcode.setText("The barcode in given image

is :"+barcodeStr);}

frame.getContentPane().setLayout(new GridLayout(3,1,10,10));frame.getContentPane().add(label);frame.add(barcode);

frame.setVisible(true);

}

}else

{frame.getContentPane().setLayout(new GridLayout(3,1,10,10));File file = new File(path);image = null;try{

image = ImageIO.read(file);}catch(Exception e){}label = new JLabel(new ImageIcon(image));frame.getContentPane().add(label);barcode = new JLabel("No valid barcode detected");frame.add(barcode);frame.setVisible(true);

} } }}

21

Page 22: Barcode Detection From an Image

6. REFERENCES

Books:

Patrick Naughton and Herbert Schildt, Java Complete Reference.

Technical Papers:

1. Robert Adelmann, Marc Langheinrich, Christian Flörkemeier, “Toolkit for Bar Code Recognition and Resolving on Camera Phones – Jump Starting the Internet of Things”.

2. Ruwan Janapriya, Lasantha Kularatne, Kosala Pannipitiya, Anuruddha Gamakumara ,Chathura de Silva, “A Low Cost Optical Barcode Reader Using A Webcam”.

3. Steffen Wachenfeld, Sebastian Terlunen, Xiaoyi Jiang,”Robust Recognition of 1-D Barcodes Using Camera Phones”.

Websites:

1. www.dtksoft.com

2. www.wikipedia.com

3. www.barcodeisland.com

4. java.sun.com/docs/books/tutorial

22

Page 23: Barcode Detection From an Image

7. ACKNOWLEDGEMENTWe would like to thank our guide and seminar project in-charge, Mr.Aejazul Khan guiding us through our research and the actual project implementation. Her expertise and guidance have helped us immensely to understand the topic well as well as implement it in the best way possible.

We also thank the Computers Department for providing us with the facilities that helped us immensely to accomplish the project in time. Last but not the least; we also thank our classmates to give us constant support throughout the academic year.

23