Scan, Read, and Generate Data Matrix Barcodes in C#
PSPDFKit GdPicture.NET Library enables you to recognize one-dimensional (1D or linear) and two-dimensional (2D) barcodes.
GdPicture.NET supports all 1D barcode formats and the following 2D barcode formats:
- Aztec Code
- Data Matrix
- MaxiCode
- Micro QR
- PDF417
- QR
To recognize Data Matrix barcodes and then write their values to the console, follow these steps:
-
Create a
GdPictureImaging
object. -
Select the image by passing its path to the
CreateGdPictureImageFromFile
method of theGdPictureImaging
object. -
Scan the barcodes by passing the image as the parameter of the
BarcodeDataMatrixReaderDoScan
method. -
Determine the number of scanned barcodes and loop through them.
-
Save the value of each barcode.
-
Write the values to the console.
-
Release unnecessary resources.
The example below scans Data Matrix 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"); // Scan the barcodes. gdpictureImaging.BarcodeDataMatrixReaderDoScan(imageID); // Determine the number of scanned barcodes. int barcodeCount = gdpictureImaging.BarcodeDataMatrixReaderGetBarcodeCount(); 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.BarcodeDataMatrixReaderGetBarcodeValue(i)}"; } } // Write the values to the console. Console.WriteLine(content); // Release unnecessary resources. gdpictureImaging.BarcodeDataMatrixReaderClear(); gdpictureImaging.ReleaseGdPictureImage(imageID);
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() ' Select the image to process. Dim imageID As Integer = gdpictureImaging.CreateGdPictureImageFromFile("C:\temp\source.png") ' Scan the barcodes. gdpictureImaging.BarcodeDataMatrixReaderDoScan(imageID) ' Determine the number of scanned barcodes. Dim barcodeCount As Integer = gdpictureImaging.BarcodeDataMatrixReaderGetBarcodeCount() 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.BarcodeDataMatrixReaderGetBarcodeValue(i).ToString() Next End If ' Write the values to the console. Console.WriteLine(content); ' Release unnecessary resources. gdpictureImaging.BarcodeDataMatrixReaderClear() gdpictureImaging.ReleaseGdPictureImage(imageID) End Using
Used Methods and Properties
Related Topics
Additional Options
To determine the number of rows and columns in a barcode, pass the index of the barcode to the BarcodeDataMatrixReaderGetBarcodeRows
and BarcodeDataMatrixReaderGetBarcodeColumns
methods.