Dev Direct Solution Center

For more information and to buy this product...

Extracting barcode with Aspose.BarCode from bmp, jpg, tiff, gif etc. using C# and VB.NET

 Note: This solution requires the component to be installed first.
To download the installer, click here

Introduction

This sample can read the barcodes from scanned images of various formats e.g. bmp, jpg, tiff, gif etc. Whether the scanned image contains only the barcode image or it’s a combination of text, images and barcodes, it can recognize the barcodes within the image, decodes it and gives you the original code text.

Detail

The sample is implemented as a C# and VB.NET Windows Forms application. You can open an image file that contains the barcodes. The image will be displayed on the form. You can choose from various barcode symbology types e.g. Code39Standard, Code39Extended, Pdf417 etc. After selecting the image file and choosing the desired symbology, you can recognize the barcodes within the image. The Code Text will appear in the listbox. If an image contains multiple barcodes, the listbox will be populated with multiple codetext.

Setup:

First, install the free trial version of Aspose.BarCode for .NET from http://www.aspose.com/community/files/53/visual-components/aspose.barcode/entry124629.aspx. Open the sample project in Microsoft Visual Studio 2005 and add a reference to the Aspose.BarCode dll. The default location of the dll is "C:\Program Files\Aspose\Aspose.BarCode\Bin\Net 2.0\Aspose.BarCode.dll". If you cannot find the dll on that location, please locate this dll from the location where you installed Aspose.BarCode component. After referencing the component, build the project. It should compile without any errors.

Running the Project:

After running the project, on the main window press the "Open Image" button and select an image file that contains the Barcodes. The image will appear on the form. Next, choose the Symbology type. Then press the "Recognize BarCodes" button to recognize the BarCodes from the image. Some sample images are also provided with the project source files for testing. If the barcode is found and recognized, the code text will be displayed in the listbox. It can also detect multiple barcodes and populate the listbox with multiple code text values.

How the Code Works:

The sample project makes reference to the following library:

  • Aspose.BarCode.dll

The BarCode generation and recognition classes can be used by adding the following namespace:

  • Aspose.BarCode

The windows form has the button "Open Image", which opens the "Open File Dialog box". You can select an image from any folder of your disk. The "FileName" property of the "OpenFileDialog" is used to set the image for the "PictureBox" control.

[C#.NET]
private void btnOpenImage_Click(object sender, EventArgs e)

{

    OpenFileDialog fd = new OpenFileDialog();

    if (fd.ShowDialog() == DialogResult.OK)

    {

        picImage.ImageLocation = fd.FileName;

    }

}
[VB.NET]
Private Sub btnOpenImage_Click(ByVal sender As Object, ByVal e As EventArgs)

      Dim fd As OpenFileDialog = New OpenFileDialog()

      If fd.ShowDialog() = System.Windows.Forms.DialogResult.OK Then

            picImage.ImageLocation = fd.FileName

      End If

End Sub

The following code is executed, when you press the "Recognize BarCode" button. If first clears all items in the listbox, where code text are displayed. Then an object type BarCodeReader is created. In the constructor, we pass the image location. This value comes from the "ImageLocation" property of "PictureBox" control. Then, we declare an array of type "BarCodeInfo". The recognized barcodes will be populated in this array later. After this, we set the value of "SymbologyType" based on the combobox selected item. If nothing is selected in the combobox, the default value "Code39Standard" is assigned. Then we call the "Read" method of "BarCodeReader" object. This method detects the barcodes within the image files and put the results in the array that was declared above.

If some barcodes are detected, then the foreach loop adds the code text to the listbox. "CodeText" property of the "BarCodeInfo" object is used to get the code text. Then, we check the length of the "result" array. If its 0, that means no barcodes were found and we show the message on screen in that case.

[C#.NET]
private void btnRecognizeBarCodes_Click(object sender, EventArgs e)

{

    try

    {

        lstBarCodes.Items.Clear();

        BarCodeReader reader = new BarCodeReader(picImage.ImageLocation);

        Aspose.BarCode.BarCodeInfo[] result;

        if (cmbSymbology.SelectedItem == null)

        {

            reader.SymbologyType = Symbology.Code39Standard;

        }

        else

        {

            reader.SymbologyType = (Symbology)Enum.Parse(typeof(Symbology), cmbSymbology.SelectedItem.ToString(), true);

        }

        result = reader.Read();

        foreach (BarCodeInfo info in result)

        {

            lstBarCodes.Items.Add(info.CodeText);

        }

        if (result.Length == 0)

        {

            MessageBox.Show("No BarCode(s) found.");

        }

    }

    catch (Exception ex)

    {

        MessageBox.Show(ex.Message);

    }

}
[VB.NET]
Private Sub btnRecognizeBarCodes_Click(ByVal sender As Object, ByVal e As EventArgs)

      Try

            lstBarCodes.Items.Clear()

            Dim reader As BarCodeReader = New BarCodeReader(picImage.ImageLocation)

            Dim result As Aspose.BarCode.BarCodeInfo()

            If cmbSymbology.SelectedItem Is Nothing Then

                  reader.SymbologyType = Symbology.Code39Standard

            Else

                  reader.SymbologyType = CType(System.Enum.Parse(GetType(Symbology), cmbSymbology.SelectedItem.ToString(), True), Symbology)

            End If

            result = reader.Read()

            For Each info As BarCodeInfo In result

                  lstBarCodes.Items.Add(info.CodeText)

            Next info

            If result.Length = 0 Then

                  MessageBox.Show("No BarCode(s) found.")

            End If

      Catch ex As Exception

            MessageBox.Show(ex.Message)

      End Try

End Sub

Conclusion:

This sample demonstrates how we can detect and recognize the barcodes from an image file using the Aspose.BarCode component.

For more information about Aspose.BarCode component and online demos, please visit http://www.aspose.com/categories/visual-components/aspose.barcode-for-.net-and-java/default.aspx.

Visit Aspose Pty Ltd for more information and more samples.