Learn how to generate and read back any generated barcode using Barcode Engine 1.3 library.
Introduction
Developing Barcode API that decode and encode barcode images (or binary streams) from your application, integration with hardware or even your mobile is a demanding market requirement nowadays.I will demonstrate my experience in tutorial like steps, to explain how to develop a barcode application to generate and consume back generated barcode to extract the encoded data, also decode any other generated picture that contains barcode, using my developed barcode library BarcodeEngine 1.3L (L for library).
It is useful to read the first article about the barcode (Barcode: Professional java barcode API (reader & writer)), to get the basic idea that will make it easy to understand this article.
Also the required software to install to be able to compile and run my BarcodeEngine 1.3L as it depends on two libraries the core.jar and javase.jar jars.
Software Pre-requisites
The following pieces of software are required to develop your barcode application:- The core.jar, javase.jar jars.
- The BarcodeEngine 1.3L and could be downloaded from here.
Prepare the project
After getting the required software libraries as described in previous section. Now we are going to create your first project that will be used to run the barcode engine. I will use the Netbeans IDE (7.x) here, but indeed you are free to develop on your favorite IDE.The following steps we are going to walk through will guides you to complete the barcode engine application:
- Open Netbeans and press (Ctrl + Shift + N) to open create project window, under categories list choose Java then in the other right hand list (Projects) choose Java Application then press next.
- In next window Name and Location write the following:
- Press Finish button and you should see something like this.
- Now we are going to attach the libs which we got earlier from the pre-requisites section, right click on the project and choose properties. In the categories choose libraries and on right hand side under compiler tab choose Add JAR/Folder and navigate to your libs to attach them then click open as the following snapshot.
- If all libs are added successfully, you should see something like this.
- Now our project is ready for developing barcode application.
Developing the application
Now after everything is setup successfully and ready. Let's explore the BarcodeEngine v1.3 library and do the coding to generate and read the barcodes, using proper configurations.- Open already created class named "BarcodeApplication" which contains the main method.
- Inside the main method copy and paste the following code:
- Run the code and you should find the generated barcode images created in the specified folder as the following:
- In the output console you should see the following output:
How it works
Now after we have created our application, and see the created barcodes and reading back its contents, let's explore the application and see how it works.First we have declared two files for two different barcode types, qrCodeFile and PDF417File in a specific folder which doesn't exists.
The second step is to create the encoding configuration (instance of EncodeConfig) which used by the barcode engine, encode method as hints for encoding barcode contents to the type we need to generate. The createDirectories(Boolean.TRUE) is responsible for forcing the engine to create the directories if it doesn't exists.
Note: that EncodeConfig and DecodeConfig configuration classes using builder pattern for creating the instances.
Then we have defined the contents to be encoded into QRCode as the following.
Then after configuration we asked the BarcodeEngine to encode the contentto qrCodeFile using encodeConfig encoding configurations. We did the same for the other type PDF417File. I do this to show up how to create the different types of barcodes.
Now it is the time to read back the barcode type's contents which we have generated before, using our BarcodeEngine API to decode and get back the results.
As we did before we need to configure the decoding configuration using a DecodeConfig instance, to be able to read the contents of any image containing barcode.
We pass the created decoding configuration along with the barcode file need to be decoded, to BarcodeEngine decode method, which return the result in form of map of DecodeResult enum as key and object as value.
We get the values from map as the following:
Note: All the BarcodeEngine API methods are static methods, and contain an Utils inner class that has many general useful methods.
Barcode engine also has other methods that extract the barcode from within an image, crop certain portion of the image, and rotate image. Consult the java documentation associated with API for more information.
Nice post
ReplyDeleteThanks sir
ReplyDeleteNice Post Sir Thanks....
ReplyDeleteCan you give me sample code for UPC type
ReplyDeleteUse the following code it is not very different from provided example code.
DeleteBut NOTE: that UPC codes should use numeric data only rang from 11 - 12 number, more than this the API will throw and exception.
public static void main(String[] args) {
// File will be used for creating the UPC_A barcode type.
File upcBarcode = new File("C:\\barcode\\UPC.png");
// Building the encoding configurations - using builder battern
EncodeConfig encodeConfig =
new EncodeConfig.Builder().createDirectories(Boolean.TRUE).build();
// Generating the UPC_A barcode
String content = "12345678910";
BarcodeEngine.encode(upcBarcode, content, BarcodeFormat.UPC_A, 105, 50, encodeConfig);
System.out.println("------------------- Begins Writing barcodes -------------------\n");
System.out.println("Is UPC_A barcode Created? " + (upcBarcode.exists() ? "Yes " : "Not not ") + "Created");
System.out.println("\n------------------- Finished Writing barcodes -------------------");
// Now we are going to decode (read) back contents of created barcodes
// Building the decoding configurations - using builder battern
DecodeConfig decodeConfig =
new DecodeConfig.Builder()
.withProductsBarcodes(Boolean.TRUE)
.build();
Map results = BarcodeEngine.decode(upcBarcode, decodeConfig);
String decodeText = (String) results.get(BarcodeEngine.DecodeResults.RESULT);
String barcodeType = ((BarcodeFormat) results.get(BarcodeEngine.DecodeResults.BARCODE_FORMATE)).name();
System.out.println("\n------------------- Begins reading barcodes -------------------\n");
System.out.println("The decoded contents is: \"" + decodeText + "\", Barcode type is: " + barcodeType);
System.out.println("\n------------------- Finished reading barcodes -------------------");
}
Thanks Alot , It was Very useful
ReplyDeleteYou can do the following scenario:
Delete1- You should first get the image stream from webcam.
2- Send the stream to the library as file.
3- It should be display the results encoded inside the barcode.