Example
This example shows how to use the OneDBarcodeReadOptions options class to read UPC barcodes from an image.
using Leadtools;
using Leadtools.Codecs;
using Leadtools.Barcode;
using Leadtools.ImageProcessing;
public void OneDBarcode_Example()
{
string imageFileName = Path.Combine(LEAD_VARS.ImagesDir, "MyOneDBarcodes.tif");
BarcodeEngine engine = new BarcodeEngine();
// Create the image to write the barcodes to
int resolution = 300;
using (RasterImage image = RasterImage.Create((int)(8.5 * resolution), (int)(11.0 * resolution), 1, resolution, RasterColor.FromKnownColor(RasterKnownColor.White)))
{
// Write a POSTNET and a PLANET barcodes
WriteBarcode(engine.Writer, image, BarcodeSymbology.UPCA, "01234567890", new LeadRect(10, 100, 400, 200));
WriteBarcode(engine.Writer, image, BarcodeSymbology.Code128, "Code 128", new LeadRect(10, 400, 400, 200));
WriteBarcode(engine.Writer, image, BarcodeSymbology.Code11, "124578", new LeadRect(10, 700, 400, 200));
// Save the image
using (RasterCodecs codecs = new RasterCodecs())
{
codecs.Save(image, imageFileName, RasterImageFormat.CcittGroup4, 1);
}
}
// Now read the barcodes again
using (RasterCodecs codecs = new RasterCodecs())
{
using (RasterImage image = codecs.Load(imageFileName))
{
// Read the UPC-A barcode
ReadBarcodes(engine.Reader, image, BarcodeSymbology.UPCA);
// Read the Code 128 barcode
ReadBarcodes(engine.Reader, image, BarcodeSymbology.Code128);
// Read the Code 11 barcode
ReadBarcodes(engine.Reader, image, BarcodeSymbology.Code11);
}
}
}
private void WriteBarcode(BarcodeWriter writer, RasterImage image, BarcodeSymbology symbology, string value, LeadRect bounds)
{
// Create the barcode data
BarcodeData barcode = new BarcodeData(symbology, value);
barcode.Bounds = bounds;
// Set the write options
OneDBarcodeWriteOptions options = new OneDBarcodeWriteOptions();
options.TextPosition = BarcodeOutputTextPosition.Default;
options.UseXModule = false;
options.XModule = 30;
options.EnableErrorCheck = false;
options.SetGS1DatabarLinkageBit = false;
options.WriteTruncatedGS1Databar = false;
options.Code128TableEncoding = Code128BarcodeTableEncoding.Auto;
options.Code11CheckDigitType = Code11BarcodeCheckDigitType.CDigit;
options.MSIModuloType = MSIBarcodeModuloType.Modulo10;
// Write it
Console.WriteLine("Writing {0} barcode with data: {1}", symbology, value);
writer.WriteBarcode(image, barcode, options);
}
private void ReadBarcodes(BarcodeReader reader, RasterImage image, BarcodeSymbology symbology)
{
// Setup read options
OneDBarcodeReadOptions options = new OneDBarcodeReadOptions();
options.SearchDirection = BarcodeSearchDirection.Horizontal;
options.EnableFastMode = true;
options.Granularity = 9;
options.MinimumStringLength = 3;
options.MaximumStringLength = 0;
options.WhiteLinesNumber = 3;
options.ReturnCheckDigit = BarcodeReturnCheckDigit.Default;
options.EnableErrorCheck = false;
options.AvoidCorruptedBlocks = false;
options.AllowPartialRead = false;
options.Code11CheckDigitType = Code11BarcodeCheckDigitType.CDigit;
options.MSIModuloType = MSIBarcodeModuloType.Modulo10;
Console.WriteLine("Reading {0} barcodes", symbology);
BarcodeData barcode = reader.ReadBarcode(image, LeadRect.Empty, symbology, options);
if (barcode != null)
{
Console.WriteLine(" At {0}, data: {1}", barcode.Bounds, barcode.Value);
}
else
{
Console.WriteLine(" Not found");
}
}
static class LEAD_VARS
{
public const string ImagesDir = @"C:\LEADTOOLS21\Resources\Images";
}
Imports Leadtools
Imports Leadtools.Codecs
Imports Leadtools.Barcode
Imports Leadtools.ImageProcessing
Public Sub OneDBarcode_Example()
Dim imageFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "MyOneDBarcodes.tif")
Dim engine As New BarcodeEngine()
' Create the image to write the barcodes to
Dim resolution As Integer = 300
Using image As RasterImage = RasterImage.Create(CType(8.5 * resolution, Integer), CType(11.0 * resolution, Integer), 1, resolution, RasterColor.FromKnownColor(RasterKnownColor.White))
' Write a POSTNET and a PLANET barcodes
WriteBarcode(engine.Writer, image, BarcodeSymbology.UPCA, "01234567890", New LeadRect(10, 100, 400, 200))
WriteBarcode(engine.Writer, image, BarcodeSymbology.Code128, "Code 128", New LeadRect(10, 400, 400, 200))
WriteBarcode(engine.Writer, image, BarcodeSymbology.Code11, "124578", New LeadRect(10, 700, 400, 200))
' Save the image
Using codecs As New RasterCodecs()
codecs.Save(image, imageFileName, RasterImageFormat.CcittGroup4, 1)
End Using
End Using
' Now read the barcodes again
Using codecs As New RasterCodecs()
Using image As RasterImage = codecs.Load(imageFileName)
' Read the UPC-A barcode
ReadBarcodes(engine.Reader, image, BarcodeSymbology.UPCA)
' Read the Code 128 barcode
ReadBarcodes(engine.Reader, image, BarcodeSymbology.Code128)
' Read the Code 11 barcode
ReadBarcodes(engine.Reader, image, BarcodeSymbology.Code11)
End Using
End Using
End Sub
Private Sub WriteBarcode(ByVal writer As BarcodeWriter, ByVal image As RasterImage, ByVal symbology As BarcodeSymbology, ByVal value As String, ByVal bounds As LeadRect)
' Create the barcode data
Dim barcode As New BarcodeData(symbology, value)
barcode.Bounds = bounds
' Set the write options
Dim options As New OneDBarcodeWriteOptions()
options.TextPosition = BarcodeOutputTextPosition.Default
options.UseXModule = False
options.XModule = 30
options.EnableErrorCheck = False
options.SetGS1DatabarLinkageBit = False
options.WriteTruncatedGS1Databar = False
options.Code128TableEncoding = Code128BarcodeTableEncoding.Auto
options.Code11CheckDigitType = Code11BarcodeCheckDigitType.CDigit
options.MSIModuloType = MSIBarcodeModuloType.Modulo10
' Write it
Console.WriteLine("Writing {0} barcode with data: {1}", symbology, value)
writer.WriteBarcode(image, barcode, options)
End Sub
Private Sub ReadBarcodes(ByVal reader As BarcodeReader, ByVal image As RasterImage, ByVal symbology As BarcodeSymbology)
' Setup read options
Dim options As New OneDBarcodeReadOptions()
options.SearchDirection = BarcodeSearchDirection.Horizontal
options.EnableFastMode = True
options.Granularity = 9
options.MinimumStringLength = 3
options.MaximumStringLength = 0
options.WhiteLinesNumber = 3
options.ReturnCheckDigit = BarcodeReturnCheckDigit.Default
options.EnableErrorCheck = False
options.AvoidCorruptedBlocks = False
options.AllowPartialRead = False
options.Code11CheckDigitType = Code11BarcodeCheckDigitType.CDigit
options.MSIModuloType = MSIBarcodeModuloType.Modulo10
Console.WriteLine("Reading {0} barcodes", symbology)
Dim barcode As BarcodeData = reader.ReadBarcode(image, LeadRect.Empty, symbology, options)
If Not IsNothing(barcode) Then
Console.WriteLine(" At {0}, data: {1}", barcode.Bounds, barcode.Value)
Else
Console.WriteLine(" Not found")
End If
End Sub
Public NotInheritable Class LEAD_VARS
Public Const ImagesDir As String = "C:\LEADTOOLS21\Resources\Images"
End Class