How To Generate & Read Barcodes In VB.NET
GoalCreate a Visual Basic (VB.NET) program that can generate barcode image files from an existing table of data to represent shipping orders, then read single or multiple barcodes from image files to represent receiving orders.
- Microsoft Windows 7 or greater
- Microsoft Visual Studio 2015 or greater
- Accusoft Barcode Xpress .NET
Programming SkillVisual Basic Intermediate Level
Need to create a barcode generator and reader in VB.NET? Accusoft Barcode Xpress is a barcode generator and reader library that supports over 30 different types of barcodes, and can accurately read barcodes even when they are damaged or scanned at low resolution. In this tutorial we'll create a simple order shipping and receiving example to demonstrate creating and reading barcodes in VB.NET.
VB.NET Barcode Generator/ Reader Sample CodeYou can download the Visual Basic sample code here. Note that you’ll also need to install a free evaluation copy of Barcode Xpress .NET on your machine. [Download Code Sample]
Getting Your Project Set UpFor this example we'll use Microsoft Visual Studio to generate our project files. After installing the Barcode Xpress .Net SDK, start a Visual Basic project. In order for this program to be properly generated, make sure that the Framework System.Drawing, and the Extension Accusoft Barcode Xpress 12 .Net are added to the project. The easiest way is to right-click on References in the Solution Explorer in Visual Studio, then click on Framework to select System.Drawing.
Then Click on Extensions and select Accusoft Barcode Xpress 12 .Net.
In the code for your Visual Basic project, place the following commands to import the libraries into your code:
Getting Started with DataFor this example, we will assume an environment where orders are being tracked in a database. To represent this, we can use a simple table to represent orders that need to be tracked for shipping and verified that they were received. For our example, the following data is generated:
Generating Barcodes In VB.NETData
To track shipments, we can generate barcodes that that would be attached to the shipping label. With Barcode Xpress, that's a simple task. First, we'll have our program look through the shipping table and any orders that haven't been shipped (shipped is False), take the shippingOrder field and give it to our GenerateBarCodes function:
The GenerateBarCodes function leverages Barcode Xpress. Barcode Xpress supports a plethora of barcode types – you can check them all out at http://help.accusoft.com/BarcodeXpress/v12.0/dotnet/webframe.html#Supported_Barcode_Types.html . For this example, we'll use the Code 39 standard to generate our barcodes.
Here's how we actually create our BarcodeExpress object and update the options:
What we've done here is create our BarcodeXpress object, set it to the Code39Barcode standard, and then set the barcode value to the current shippingOrder value. All that's left is to generate the barcode image itself and save it to a file. After that, the files can be used however we want – print them out as labels, embed them in documents – whatever works best. We can leverage the Visual Basic object System.Drawing.Bitmap to do that for us.
That's it! A few lines of code, and when we call the GenerateBarCode function with a shippingOrder that equals A123456, here's the result:
In the case of our sample program, it takes a few milliseconds to generate the barcode files and be ready for shipping. Our function returns a boolean value if it's successful, and our code then sets "shipped" to true and sets the date and time in our table of data.
Reading/Scanning Barcodes In VB.NET
So we've generated our barcodes to send out our shipments. But what about receiving them? Using the same Barcode Xpress object, we can have the system scan in the barcodes, get the data from the barcode, look the order up in our table, then update the table to show the order was received and what time it occurred. Depending on how we receive the images will depend on your setup – but for this example we'll just manually set a couple of files to test it out.
Here's a simple example using one of the barcode files we generated:
Our program has a command line switch. If you run it without any command line options, like this:
Then you'll just have the barcode files generated off of our table. Run it like this, though:
Where "imagefile" is a bitmap image with our barcode inside (such as, for example, the "C266760.png). We'll pick this up from our Main program like so:
Now we can scan our file with this command:
What we can do is generate an array of Accusoft.BarcodeXpressSdk.Result objects, then extract the values from there. The function ReadBarCodes can be called via this method:
The function starts by taking the filename submitted to it, loading it into a Bitmap object, then using the same BarcodeXpress object. Only this time, instead of creating a barcode image, it can read from the bitmap image, and store the results into the Result object:
Scanning the barcode above will return an array with just one value in it – the barcode data for shipment C266760 from our example, and we can update our table of values to show this order was received and the date/time we scanned it:
But what if we have more than one barcode in our image, like this one:
Not only do we have multiple barcodes – but looks like someone slapped on one of them at an angle. This is included in the sample as "received_002.jpg".
Angled barcodes? Not a problem. In fact, we can test this by displaying the table before and after scanning the file:
Even the one that was at a different angle was detected accurately. Barcode Xpress can handle scanning barcodes in a variety of standards and formats, from multiple angles and even when the image has been degraded.
Ready to get started?
Check out the Accusoft Barcode Xpress free trial at https://www.accusoft.com/products/barcode-xpress/overview/ and give it a try. Not into Visual Basic or .Net? That's fine – there are versions for Java, Linux and even your favorite mobile devices.