C# Code 128 Reader SDK Integration

Online tutorial for reading & scanning Code 128 barcode images using C#.NET class

How to read, scan, decode Code 128 images in C#.NET class, ASP.NET Web & Windows applications

  • Scan Code 128 barcode in C# class, Console applications
  • Detailed tutorials on read barcode in asp.net c#, read barcode in c# windows application
  • Read Code 128 barcode in C# ASP.NET web projects
  • Read, decode Code 128 images in Visual Studio C#.NET Windows Forms applications
  • Easy and simple to integrate Code 128 reader component (single dll file) into your C# project
  • Complete developed in C#.net 2005, for .net framework 2.0 and later version
  • Scanning, decoding Code 128 from multiple image formats, like BMP, GIF, JPEG, PNG, TIFF formats
  • Scanning, reading Code 128 from multi-page TIFF documents

How to scan, read Code 128 barcodes from image using C#

  1. Download C# Barcode Reader Library
  2. Install C# library to read barcode Code 128 from images in .NET apps
  3. Step by Step Tutorial










C# Code 128 Reader Introduction

Top
C# Code 128 Reader SDK is a high performance C# linear and 2d barcode recognition SDK for Microsoft Visual Studio C#.NET platform.

Scan and read Code 128 barcodes from image files is one of the barcode decoding functions in .NET Barcode Scanner component. To help .net developers easiy integrate barcode reader control into your .net projects, we provide detailed online tutorial for
C#.NET Barcode Reader component supports scanning and reading 20+ linear, 2d barcodes, including


Install

  • Copy OnBarcode.Barcode.BarcodeScanner.dll to your C#.NET project folder (do not copy dll file to your project bin folder).
  • Add OnBarcode.BarcodeReader.dll to your C# project reference.




Reading Code 128 in C# Class

Top
Using C# Barcode Reader library, call method BarcodeScanner.Scan() in your C# class, and pass your barcode image file, and Code 128 barcode type (BarcodeType.Code 128) as method parameters.

string[] datas = BarcodeScanner.Scan("code-128-barcode-image.gif", BarcodeType.Code128);





Scan Code 128 barcode image in memory

The Code 128 barcode scanning method Scan() also accept a Stream object as input barcode data source.

byte[] dataBytes = File.ReadAllBytes("code128-barcode.png");
Stream barcodeInStream = new MemoryStream(dataBytes);
string[] datas = BarcodeScanner.Scan(barcodeInStream, BarcodeType.Code128);




Scan Code 128 from multi-barcode formats

If the barcode image contains more than one barcodes with different barcode formats, you can scan and read all barcodes at one time.

In the following C# sample code, we will try to scan and read all Code 128 and Code 39 barcodes from the target image file in C# application.

  • In method BarcodeScanner.ScanInDetails, you can specify more than one target barcode formats, such as BarcodeType.Code128 | BarcodeType.Code39
  • In the returned barcodes in BarcodeDetail objects, you can use property Type to identify the scanned barcode format.
BarcodeDetail[] datasInDetail = BarcodeScanner.ScanInDetails(
    "sample-barcodes.png", BarcodeType.Code128 | BarcodeType.Code39);

if (datasInDetail != null && datasInDetail.Length > 0)
{
    foreach (BarcodeDetail barcodeInfo in datasInDetail)
    {
        if (barcodeInfo.Type == BarcodeType.Code128)
        {
            string Code128Text = barcodeInfo.GetMessage();
        }
    }
}




How to retrieve scanned Code 128 barcode properties in C#?

Top
Using OnBarcode C# Barcode Reader library, you can retrieve scanned Code 128 barcode information, including: Code 128 text message, verfied barcode format, Code 128 position in the image and Code 128 start and encoding code set, Code 128 checksum codeword value.


Extract Code 128 core information from BarcodeDetail

You can read and explore the following Code 128 core information from returned BarcodeDetail object.
  • Type: Verify the returned barcode is Code 128. It is important that if you are scanning image with multiple barcode formats specified.
  • GetMessage(): Get the decoded Code 128 text string.
  • GetASCIITextMessage(): Get the decoded Code 128 ASCII text string. All non-printing ASCII chars have been replaced with defined label chars.

The C# example code below shows how to read and explore scanned Code 128 information from BarcodeDetail object.

BarcodeDetail[] result = BarcodeScanner.ScanInDetails(
    "Code128-sample-image.png", BarcodeType.Code128);
if (result != null && result.Length > 0)
{
    foreach (BarcodeDetail barcodeInfo in result)
    {
        Console.WriteLine("Barcode Format: {0}", barcodeInfo.Type);
        Console.WriteLine("Code 128 text: {0}", barcodeInfo.GetMessage());
        Console.WriteLine("Code 128 ASCII text: {0}", barcodeInfo.GetASCIITextMessage());
    }
}





Code 128 position, rotation, check sum, and encoding mode

You can get Code 128 barcode rotation and pixel position in the image from BarcodeDetail object.

  • Rotation: get scanned barcode rotation angle.
  • X1, Y1, X2, Y2, X3, Y3, X4, Y4: get Code 128 barcode pixel position in the image.

After get the scanned Code 128 in BarcodeDetail object, you can call method GetBarcodeProps() to extract detected Code 128 barcode properties in Code128Props object.

  • Code 128 start code set in property StartSet. Get the Code 128 start code set. It should be Code Set A, Code Set B, or Code Set C.
  • Whether more than one Code Set is used. If the Code 128 barcode using more than one encoding Code Set, the property IsMixSet will return true.
  • Code 128 checksum codeword in property Checksum. The value is in range from 0 to 102.

BarcodeDetail[] result = BarcodeScanner.ScanInDetails(
    "Code128-sample-image.png", BarcodeType.Code128);
if (result != null && result.Length > 0)
{
    foreach (BarcodeDetail barcodeInfo in result)
    {
        Console.WriteLine("Barcode Format: {0}", barcodeInfo.Type);
        Console.WriteLine("Code 128 text: {0}", barcodeInfo.GetMessage());
        Console.WriteLine("Code 128 ASCII text: {0}", barcodeInfo.GetASCIITextMessage());

        Console.WriteLine("Rotation angle: {0}", barcodeInfo.Rotation);
        Console.WriteLine("Barcode Position X1: {0}, Y1: {1}", barcodeInfo.X1, barcodeInfo.Y1);
        Console.WriteLine("Barcode Position X2: {0}, Y2: {1}", barcodeInfo.X2, barcodeInfo.Y2);
        Console.WriteLine("Barcode Position X3: {0}, Y3: {1}", barcodeInfo.X3, barcodeInfo.Y3);
        Console.WriteLine("Barcode Position X4: {0}, Y4: {1}", barcodeInfo.X4, barcodeInfo.Y4);

        if (barcodeInfo.Type == BarcodeType.Code128)
        {
            Code128Props props = (Code128Props)barcodeInfo.GetBarcodeProps();
            //  Get start Set type
            Console.WriteLine("Start Set:   {0}", props.StartSet.ToString());
            //  Indicate if more than one Sets are used in encoding.
            Console.WriteLine("Is Mix Set:  {0}", props.IsMixSet.ToString());
            //  Checksum codeword, value in range from 0 to 102
            Console.WriteLine("Checksum:    {0}", props.Checksum.ToString());
        }
    }
}




How to read and process scanned Code 128 ASCII text in C#?

Top
Code 128 barcode supports full 128 ASCII characters encoding. 95 of them are printing characters, including digits (0-9), uppercase letters (A-Z), lowercase letters (a-z), and other special characters. The remaining 33 characters are control characters, and they are not printable.

Here is a sample Code 128 barcode image with encoding text Code[CR]128. [CR] is the character 'carriage return' in ASCII table, and it's ASCII integer value is 13.



After running the following C# code, you will get the scanned code 128 text data with control character [CR].

String[] result = BarcodeScanner.Scan(
    "C://Input//code128-none-printable-chars.png", BarcodeType.Code128);
foreach (String msg in result)
{
    Debug.WriteLine("Barcode Raw Message: '" + msg + "'");
    String tmp = msg.Replace("\r", "[CR]");
    Debug.WriteLine("Barcode Message: '" + tmp + "'");
}


You will get the following message in the Visual Studio Output window.




Using C# Barcode Reader library, you can call method GetASCIITextMessage() to scan and return non-printing ASCII characters with visual text labels replaced.

            ScanOptions opts = new ScanOptions(BarcodeType.Code128);
            BarcodeDetail[] datas = BarcodeScanner.Scan(
                "C://Input//code128-none-printable-chars.png", opts);
            foreach (BarcodeDetail obj in datas)
            {
                Debug.WriteLine(" Barcode Text: " + obj.GetASCIITextMessage());
            }


The output text message displayed in the Visual Studio Output window.




Verify scanned Code 128 barcode data using check character in C#

Top
Code 128 barcode symbol contains one mandatory check character. During Code 128 generation, the check character will be calculated and appended to the end of the Code 128 barcode characters. The Code 128 check character will not be printed on the Code 128 text label, and will not be returned by the barcode scanner device or software.



Using OnBarcode C# Barcode Reader library, you will get the Code 128 data characters only and will not get the check character. The C# barcode library will apply the scanned check character to verify the scanned Code 128 data characters. If the verification is failed, the barcode software will not return the scanned data.




Tutorial: How to improve Code128 barcode recognition rate?

Top
We have received one TIFF file from our customer, who cannot use our C# barcode reader SDK to recognize the Code 128 barcode below from the TIFF document.



Analyze the imperfect barcode image

The reason why C# barcode library cannot detect the barcode, is that the low quality of the bar modules in barcode.

Code 128 has 4 types of bar modules (including black module and white module), and their bar widths should have 1:2:3:4 ratio.





In the sample tiff file, the Code 128 four black modules width ratio is 5: 10 : 14 : 19, and narrowest white module width vs black module width is 4 : 5.

That's why C# barcode reader cannot scan and recognize the Code 128 barcode.


Improve Code 128 recognition rate by zooming the image

The image features high black‑white contrast, but the module width has errors of varying proportions, resulting in reduced recognition rate.

By scaling the image (which effectively introduces transitional grayscale pixels at the boundaries between modules and spaces), the impact on the algorithm is complex in principle. Simply put, the processed image will produce "random" results.

We have used the following C# example code to test Code 128 recognition rate by different zoom ratio.

float[] ratios = new float[] {
    0.05F, 0.1F, 0.2F, 0.3F, 0.5F, 0.75F, 1F,
    1.2F, 1.5F, 1.8F, 2F, 2.5F, 2.75F, 3F, 3.2F, 3.5F, 3.75F, 4F
};

for (int i = 0; i < ratios.Length; i++)
{
    Console.WriteLine(">>> Try ratio: " + ratios[i]);

    ScanOptions ops = new ScanOptions(BarcodeType.Code128);
    ops.ZoomRatio = ratios[i];
    BarcodeDetail[] messages = BarcodeScanner.Scan("sample-code128-image.tiff", ops);
    if (messages != null && messages.Length > 0)
    {
        foreach (BarcodeDetail b in messages)
            Console.WriteLine("Message: {0}", b.GetMessage());
    }
    else
        Console.WriteLine("[No Barcode Found]");
}


Here is the program run result.



  • From the test results, it can be seen that scaling with different ratio values may result in either successful recognition or failure. There is no obvious linear relationship between recognition and the scaling ratio. Therefore, a unified recommended scaling ratio cannot be provided. Instead, different scaling factors must be selected and tested.

  • It is recommended that the scaling factor be controlled between 0.2F and 4.0F.

    If the value is too small, the module width will be less than 1 pixel after shrinking, making it unrecognizable.

    If the value is too large, the image will be too large, resulting in excessively long recognition processing time.


Increase Code 128 Quiet Zone to improve recognition rate

Besides errors in the module width, another factor affecting the recognition result is the Code 128 barcode Quiet Zone.

In this image, the quiet zone reserved on both sides of the barcode is 30 px, approximately 6X (X is the Code 128 narrowest bar width).

The border is 3 px wide, about 0.6X, which causes significant interference with barcode recognition.



After removing the borders on both sides in the image above, the Quiet Zone has been expanded to over 60 px (about 12X).

In practice, ensuring around 50 px (10X) is generally sufficient to avoid interference with the recognition algorithm.

If the Module Width error is reduced, the requirement for the Quiet Zone can be relaxed.

Here is the result with image borders removed.



Conclusion

Failure to barcode recognize is not caused by a single factor, but by a combination of multiple factors.

Each factor adds a certain amount of interference, and the accumulation leads to a lower barcode recognition rate.

For this particular image, apply image zooming and expanding the quiet zone are an effective method to improve recognition rate.













Common Asked Questions

How to setup a C# ASP.NET or Windows project to read Code 128 barcodes?

To setup a C# ASP.NET or Windows .NET project to scan, read Code 128 barcodes, you can create a new project using Visual Studio, add C# Code 128 library dll to the project reference. Now you can start to scan and extract Code 128 barcode data from image file using one line of C# code. BarcodeScanner.Scan("code-128-image-file.png", BarcodeType.Code128)

What are the procedures to scan a Code 128 barcode image using C#?

When you scan, and decode a Code 128 barcode image, the C# barcode library will load the image data into .NET project, and use method BarcodeScanner.Scan scan the image content, detect the Code 128 barcode location, decode the Code 128 data, verify the Code 128 code with check character, and return all the detailed information in C# application.

What is the barcode 128 text code?

Code 128 barcode is a high-density 1D barcode format, which encodes digits, text, control characters, and full ASCII 128 characters. Using C# Barcode Reader library, you will be able to scan, recognize barcode text from images file in C# ASP.NET, MVC, Windows Forms, WPF applications.

How do you read a Code 128 barcode?

Code 128 barcodes are including four parts:
  • A start character
  • Code 128 data characters
  • A checksum digit
  • A stop character
To read a Code 128 barcode, use a barcode scanner or barcode reader software to scan the image target area and detect the four parts, and decode the Code 128 barcode data. OnBarcode C# Barcode Reader library supports reading and decoding Code 128 barcode images in C# .NET projects.

What scanners read Code 128?

Most commercial barcode scanner devices and free or paid barcode scanner software can read Code 128 barcodes from images or printed papers. OnBarcode C# Barcode Scanner library supports scanning and reading Code 128 barcodes in C# .NET app on Windows, macOS and Linux.

How do I scan a Code 128 barcode?

You can easily use a barcode scanner device, a phone or pad with camera, or barcode software or library to scan, decode Code 128 text from barcode images or printed papers.

C# Barcode Reader library will enable your Visual Studio ASP.NET web and Windows apps to scan and recogize all Code 128 and other 2d, 1d barcodes from image files.

How to read Code 128 checksum digit using barcode scanner software?

According to Code 128 ISO standard (ISO/IEC 15417), barcode scanner device or software will not return the Code 128 check sum character. OnBarcode C# Barcode Reader library will recognize and decode the Code 128 data text without the check digit in C# class.

Can Barcode Reader process different Code 128 standards in C#?

Yes. OnBarcode C# Code 128 Barcode Library supports Code 128 on ISO and GS1 standards. When you scan and decode Code 128 for GS1 (also known as GS1-128, EAN-128), the barcode library will format the Code 128 barcode value in GS1 data format.

How to verify the scanned Code 128 data?

Each Code 128 barcode includes a compulsory check character. C# Code 128 Barcode Reader library will automatically verify the scanned Code 128 data value with the check character.

How to get a trial license for C# Code 128 Barcode Reader?

Yes. You can download and install the free trial package from website. Or you can install the trial NuGet package from NuGet Package Manager in Visual Studio.

What are the environments to run the C# Code 128 library?

To install and use C# Barcode Reader library successfully, you need Visual Studio and a compatible version of the .NET. OnBarcode C# Barcode Reader library supports the following .NET Framework versions.
  • .NET 5+ (10, 9, 8, 7, 6)
  • .NET Standard 2.0
  • .NET Core 2.x, 3.x
  • .NET Framework 4.x, 3.x, 2.x




































Terms of Use | Privacy Policy
Copyright © OnBarcode.com . All rights reserved.