Scan and Read 1D (Linear) Barcodes in C#

Recognizing one-dimensional (1D or linear) barcodes using GdPicture.NET is just like recognizing any other type of barcode. Recognition methods are identical in their usage, regardless of the barcode format.

GdPicture.NET supports 1D barcodes and the following 2D barcode formats:

  • Aztec Code
  • Data Matrix
  • MaxiCode
  • MicroQR
  • PDF417
  • QR

To recognize 1D barcodes and then write their values to the console, follow these steps:

  1. Create a GdPictureImaging object.

  2. Select the image by passing its path to the CreateGdPictureImageFromFile method of the GdPictureImaging object.

  3. Specify the barcode types to recognize.

  4. Scan the barcodes by passing the image as the parameter of the Barcode1DReaderDoScan method.

  5. Determine the number of scanned barcodes and loop through them.

  6. Save the value of each barcode.

  7. Write the values to the console.

  8. Release unnecessary resources.

The example below scans one-dimensional barcodes and then writes their values to the console:

using GdPictureImaging gdpictureImaging = new GdPictureImaging();
// Select the image to process.
int imageID = gdpictureImaging.CreateGdPictureImageFromFile(@"C:\temp\source.png");
// Specify the barcode types to recognize.
Barcode1DReaderType barcodeType = Barcode1DReaderType.Barcode1DReaderCode39 | Barcode1DReaderType.Barcode1DReaderCode128;
// Scan the barcodes.
gdpictureImaging.Barcode1DReaderDoScan(imageID);
// Determine the number of scanned barcodes.
int barcodeCount = gdpictureImaging.Barcode1DReaderGetBarcodeCount();
string content = "";
if (barcodeCount > 0)
{
    content = "Number of barcodes scanned: " + barcodeCount.ToString();
    // Save the value of each barcode.
    for (int i = 1; i <= barcodeCount; i++)
    {
        content += $"\nBarcode Number: {i} Value: {gdpictureImaging.Barcode1DReaderGetBarcodeValue(i)}";
    }
}
// Write the values to the console.
Console.WriteLine(content);
// Release unnecessary resources.
gdpictureImaging.Barcode1DReaderClear();
gdpictureImaging.ReleaseGdPictureImage(imageID);
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging()
    ' Select the image to process.
    Dim imageID As Integer = gdpictureImaging.CreateGdPictureImageFromFile("C:\temp\source.png")
    ' Specify the barcode types to recognize.
    Dim barcodeType As Barcode1DReaderType = Barcode1DReaderType.Barcode1DReaderCode39 Or Barcode1DReaderType.Barcode1DReaderCode128
    ' Scan the barcodes.
    gdpictureImaging.Barcode1DReaderDoScan(imageID)
    ' Determine the number of scanned barcodes.
    Dim barcodeCount As Integer = gdpictureImaging.Barcode1DReaderGetBarcodeCount()
    Dim content = ""
    If barcodeCount > 0 Then
        content = "Number of barcodes scanned: " & barcodeCount.ToString()
        ' Save the value of each barcode.
        For i = 1 To barcodeCount
            content = content & vbLf & "Barcode Number: " & i.ToString() & "    Value: " & gdpictureImaging.Barcode1DReaderGetBarcodeValue(i).ToString()
        Next
    End If
    ' Write the values to the console.
    Console.WriteLine(content);
    ' Release unnecessary resources.
    gdpictureImaging.Barcode1DReaderClear()
    gdpictureImaging.ReleaseGdPictureImage(imageID)
End Using
Used Methods and Properties

Related Topics