Products | Support | Email a link to this topic. | Send comments on this topic. | Back to Introduction - All Topics | Help Version 19.0.6.22
|
Leadtools.Barcode Namespace : BarcodeWriter Class |
public sealed class BarcodeWriter
'Declaration
Public NotInheritable Class BarcodeWriter
'Usage
Dim instance As BarcodeWriter
public sealed sealed class BarcodeWriter
@interface LTBarcodeWriter : NSObject
public class BarcodeWriter
function Leadtools.Barcode.BarcodeWriter()
public ref class BarcodeWriter sealed
The BarcodeWriter class is used to write a barcode to an image. You cannot create an instance of BarcodeWriter directly, instead, you use the instance created for you inside BarcodeEngine and accessed through the BarcodeEngine.Writer property:
[Visual Basic]
Dim engine As New BarcodeEngine()
Dim writer As BarcodeWriter = engine.Writer
' Use can use the writer now, for example, write a UPCA barcode to an image:
Dim data As New BarcodeData(BarcodeSymbology.UPCA,"01234567890")
data.Bounds = New LogicalRectangle(0, 0, 400, 200, LogicalUnit.Pixel)
writer.WriteBarcode(myImage, data, Nothing)
[C#]
BarcodeEngine engine = new BarcodeEngine();
BarcodeWriter writer = engine.Writer;
// Use can use the writer now, for example, write a UPCA barcode to an image:
BarcodeData data = new BarcodeData(BarcodeSymbology.UPCA,"01234567890");
data.Bounds = new LogicalRectangle(0, 0, 400, 200, LogicalUnit.Pixel);
writer.WriteBarcode(myImage, data, null);
Or you can use the BarcodeWriter directly through the BarcodeEngine.Writer property:
[Visual Basic]
Dim engine As New BarcodeEngine()
// Use the instance in BarcodeEngine directly, for example, write a UPCA barcode to an image:
Dim data As New BarcodeData(BarcodeSymbology.UPCA,"01234567890")
data.Bounds = New LogicalRectangle(0, 0, 400, 200, LogicalUnit.Pixel)
engine.Writer.WriteBarcode(myImage, data, Nothing)
[C#]
BarcodeEngine engine = new BarcodeEngine();
// Use the instance in BarcodeEngine directly, for example, write a UPCA barcode to an image:
BarcodeData data = new BarcodeData(BarcodeSymbology.UPCA,"01234567890");
data.Bounds = new LogicalRectangle(0, 0, 400, 200, LogicalUnit.Pixel);
engine.Write.WriteBarcode(myImage, data, null);
The BarcodeWriter class contains the following method used to write barcodes:
Method | Description |
---|---|
WriteBarcode |
Writes a barcode to an image. This method accepts as a parameter a BarcodeData object containing the data for the barcode (or one of its derived classes). This object contains the barcode symbology, its data as a raw stream of bytes and a rectangle that specifies the location and size of the output barcode. Extra write options may be provided too. |
Note that depending on the symbology type, a derived class may be provided (casted back to BarcodeData). Refer to BarcodeData for more information.
The WriteBarcode method accepts as a parameter a RasterImage object that contains the destination image data. This must be a valid object (cannot be null or Nothing). You typically obtain a RasterImage object by creating a new one, loading it from a disk file or stream using the Leadtools.Codecs.RasterCodecs class, acquiring from a scanning device using the Leadtools.Twain.TwainSession class or any of the many other means supported by LEADTOOLS.
After you write the barcode or barcodes to the image, you can use the Leadtools.Codecs.RasterCodecs class to save this image to disk using any of the file formats supported by LEADTOOLS.
The BarcodeData contains the BarcodeData.Bounds property that should be populated with the location and size of the final barcode on the image. Not all sizes can be used when writing a barcode. And the value of the width and height of the bounds can have a special meaning. Use the CalculateBarcodeDataBounds method to return the exact location of the destination barcode on the image without committing it based on the data, symbology and options selected. For more information, Writing Barcodes - Bounds and XModule.
The source BarcodeData also contains the BarcodeData.Symbology property, this must be set to the BarcodeSymbology enumeration value that specifies the barcode type to use when writing.
The BarcodeEngine.GetSupportedSymbologies method will return an array containing all the symbologies supported by LEADTOOLS. Not all of these symbologies might be available (usable) by the current BarcodeWriter object depending on level of LEADTOOLS support unlocked and presence of back end support assemblies. The GetAvailableSymbologies can be used at any time to return the subset of the symbologies that are currently usable by this BarcodeWriter object.
LEADTOOLS provides extra options to use when writing barcodes. These options are used to fine tune the parameters or provide extra pre-known information that are specific to certain types of barcodes. The base abstract class for the options is BarcodeWriteOptions. LEADTOOLS provides derived classes for each symbology (or group of symbologies). Refer to BarcodeWriteOptions for more information.
The BarcodeWriter class contains default options for each barcode symbology (or group of common symbologies). These options can be retrieved using the GetDefaultOptions method passing the symbology of interest. You may then change the members of the returned BarcodeWriteOptions (or after casting it to the appropriate derived class).
You can also create an instance of one of the derived BarcodeWriteOptions classes and use it directly in WriteBarcode.
The default options provide a way to easily change the options used by the BarcodeWriter object in one place ;you do not have to keep the option objects in your application.
In certain situations however, using explicit options may be required. For example, to fine tune writing a certain image or to write barcodes using independent options in multiple threads and a single BarcodeWriter. In these cases, you can use explicit options to override all or some of these options.
The default options can also be loaded or saved as XML file or stream using the LoadOptions and SaveOptions methods.
Note on writing barcodes and image regions: If the RasterImage contains a region, then the BarcodeWriter will respect the region boundaries when writing the barcode. In other words, the resulting barcode might not be completely visible because of the region of interest restrictions. The C# and VB Barcode Demos will check if the image has a region, save a copy of it and delete the image region before writing a barcode, then re-set the region again after the write operation.
Also note that the region location (its top and left position) does not affect the location of the barcodes to be written. This behavior has changed in LEADTOOLS v18 from previous versions.
This example creates a barcode for each symbology supported by LEADTOOLS. It will then save the barcodes as TIFF files.
Imports Leadtools Imports Leadtools.Codecs Imports Leadtools.Forms Imports Leadtools.Barcode Imports Leadtools.ImageProcessing Public Sub BarcodeWriter_Example() ' Create a directory to store the images we will create Dim outDir As String = Path.Combine(LEAD_VARS.ImagesDir, "MyBarcodes") If Directory.Exists(outDir) Then Directory.Delete(outDir, True) End If Directory.CreateDirectory(outDir) Dim resolution As Integer = 300 ' Create a Barcode engine Dim engine As New BarcodeEngine() ' Get the Barcode writer Dim writer As BarcodeWriter = engine.Writer ' All 1D options have the UseXModule set to false by default, we need to set it to true ' so we can calculate the default size. We will change the default options so we can ' pass null to CalculateBarcodeDataBounds and WriteBarcode below ' For all Standard 1D Dim oneDWriteOptions As OneDBarcodeWriteOptions = DirectCast(writer.GetDefaultOptions(BarcodeSymbology.UPCA), OneDBarcodeWriteOptions) oneDWriteOptions.UseXModule = True ' All GS1 Databar Stacked Dim gs1DatabarStackedWriteOptions As GS1DatabarStackedBarcodeWriteOptions = DirectCast(writer.GetDefaultOptions(BarcodeSymbology.GS1DatabarStacked), GS1DatabarStackedBarcodeWriteOptions) gs1DatabarStackedWriteOptions.UseXModule = True ' Patch Code Dim patchCodeWriteOptions As PatchCodeBarcodeWriteOptions = DirectCast(writer.GetDefaultOptions(BarcodeSymbology.PatchCode), PatchCodeBarcodeWriteOptions) patchCodeWriteOptions.UseXModule = True ' All PostNet/Planet Dim postNetPlanetWriteOptions As PostNetPlanetBarcodeWriteOptions = DirectCast(writer.GetDefaultOptions(BarcodeSymbology.PostNet), PostNetPlanetBarcodeWriteOptions) postNetPlanetWriteOptions.UseXModule = True ' We will use this object to save files Using codecs As New RasterCodecs() ' Get all the available write symbologies Dim symbologies() As BarcodeSymbology = writer.GetAvailableSymbologies() For Each symbology As BarcodeSymbology In symbologies Console.WriteLine("Processing {0}", symbology) ' Create the default data for this symbology Dim data As BarcodeData = BarcodeData.CreateDefaultBarcodeData(symbology) ' Calculate its size with default options writer.CalculateBarcodeDataBounds(LogicalRectangle.Empty, resolution, resolution, data, Nothing) ' Create an image to write the data to Dim pixels As LeadRect = data.Bounds.ToRectangle(resolution, resolution) Using image As RasterImage = RasterImage.Create(pixels.Width, pixels.Height, 1, resolution, RasterColor.FromKnownColor(RasterKnownColor.White)) ' Write the barcode with default options writer.WriteBarcode(image, data, Nothing) ' Save it Dim outFileName As String = Path.Combine(outDir, symbology.ToString() + ".tif") codecs.Save(image, outFileName, RasterImageFormat.Tif, 1) End Using Next End Using End Sub Public NotInheritable Class LEAD_VARS Public Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images" End Class
using Leadtools; using Leadtools.Codecs; using Leadtools.Forms; using Leadtools.Barcode; using Leadtools.ImageProcessing; public void BarcodeWriter_Example() { // Create a directory to store the images we will create string outDir = Path.Combine(LEAD_VARS.ImagesDir, "MyBarcodes"); if(Directory.Exists(outDir)) { Directory.Delete(outDir, true); } Directory.CreateDirectory(outDir); int resolution = 300; // Create a Barcode engine BarcodeEngine engine = new BarcodeEngine(); // Get the Barcode writer BarcodeWriter writer = engine.Writer; // All 1D options have the UseXModule set to false by default, we need to set it to true // so we can calculate the default size. We will change the default options so we can // pass null to CalculateBarcodeDataBounds and WriteBarcode below // For all Standard 1D OneDBarcodeWriteOptions oneDWriteOptions = writer.GetDefaultOptions(BarcodeSymbology.UPCA) as OneDBarcodeWriteOptions; oneDWriteOptions.UseXModule = true; // All GS1 Databar Stacked GS1DatabarStackedBarcodeWriteOptions gs1DatabarStackedWriteOptions = writer.GetDefaultOptions(BarcodeSymbology.GS1DatabarStacked) as GS1DatabarStackedBarcodeWriteOptions; gs1DatabarStackedWriteOptions.UseXModule = true; // Patch Code PatchCodeBarcodeWriteOptions patchCodeWriteOptions = writer.GetDefaultOptions(BarcodeSymbology.PatchCode) as PatchCodeBarcodeWriteOptions; patchCodeWriteOptions.UseXModule = true; // All PostNet/Planet PostNetPlanetBarcodeWriteOptions postNetPlanetWriteOptions = writer.GetDefaultOptions(BarcodeSymbology.PostNet) as PostNetPlanetBarcodeWriteOptions; postNetPlanetWriteOptions.UseXModule = true; // We will use this object to save files using(RasterCodecs codecs = new RasterCodecs()) { // Get all the available write symbologies BarcodeSymbology[] symbologies = writer.GetAvailableSymbologies(); foreach(BarcodeSymbology symbology in symbologies) { Console.WriteLine("Processing {0}", symbology); // Create the default data for this symbology BarcodeData data = BarcodeData.CreateDefaultBarcodeData(symbology); // Calculate its size with default options writer.CalculateBarcodeDataBounds(LogicalRectangle.Empty, resolution, resolution, data, null); // Create an image to write the data to LeadRect pixels = data.Bounds.ToRectangle(resolution, resolution); using(RasterImage image = RasterImage.Create(pixels.Width, pixels.Height, 1, resolution, RasterColor.FromKnownColor(RasterKnownColor.White))) { // Write the barcode with default options writer.WriteBarcode(image, data, null); // Save it string outFileName = Path.Combine(outDir, symbology + ".tif"); codecs.Save(image, outFileName, RasterImageFormat.Tif, 1); } } } } static class LEAD_VARS { public const string ImagesDir = @"C:\Users\Public\Documents\LEADTOOLS Images"; }
using Leadtools; using Leadtools.Codecs; using Leadtools.Barcode; using Leadtools.ImageProcessing; public async Task BarcodeWriter_Example() { int resolution = 300; // Create a Barcode engine BarcodeEngine engine = new BarcodeEngine(); // Get the Barcode writer BarcodeWriter writer = engine.Writer; // All 1D options have the UseXModule set to false by default, we need to set it to true // so we can calculate the default size. We will change the default options so we can // pass null to CalculateBarcodeDataBounds and WriteBarcode below // For all Standard 1D OneDBarcodeWriteOptions oneDWriteOptions = writer.GetDefaultOptions(BarcodeSymbology.UPCA) as OneDBarcodeWriteOptions; oneDWriteOptions.UseXModule = true; // All GS1 Databar Stacked GS1DatabarStackedBarcodeWriteOptions gs1DatabarStackedWriteOptions = writer.GetDefaultOptions(BarcodeSymbology.GS1DatabarStacked) as GS1DatabarStackedBarcodeWriteOptions; gs1DatabarStackedWriteOptions.UseXModule = true; // Patch Code PatchCodeBarcodeWriteOptions patchCodeWriteOptions = writer.GetDefaultOptions(BarcodeSymbology.PatchCode) as PatchCodeBarcodeWriteOptions; patchCodeWriteOptions.UseXModule = true; // All PostNet/Planet PostNetPlanetBarcodeWriteOptions postNetPlanetWriteOptions = writer.GetDefaultOptions(BarcodeSymbology.PostNet) as PostNetPlanetBarcodeWriteOptions; postNetPlanetWriteOptions.UseXModule = true; // We will use this object to save files using(RasterCodecs codecs = new RasterCodecs()) { // Get all the available write symbologies BarcodeSymbology[] symbologies = writer.GetAvailableSymbologies(); foreach(BarcodeSymbology symbology in symbologies) { Debug.WriteLine("Processing {0}", symbology); // Create the default data for this symbology BarcodeData data = BarcodeData.CreateDefaultBarcodeData(symbology); // Calculate its size with default options writer.CalculateBarcodeDataBounds(LeadRectHelper.Empty, resolution, resolution, data, null); // Create an image to write the data to LeadRect pixels = data.Bounds; using(RasterImage image = RasterImage.Create(pixels.Width, pixels.Height, 1, resolution, RasterColorHelper.FromKnownColor(RasterKnownColor.White))) { // Write the barcode with default options writer.WriteBarcode(image, data, null); // Save it string outFileName = symbology.ToString() + ".tif"; StorageFile saveFile = await Tools.AppLocalFolder.CreateFileAsync(outFileName); await codecs.SaveAsync(image, LeadStreamFactory.Create(saveFile), RasterImageFormat.Tif, 1); } } } }
using Leadtools; using Leadtools.Codecs; using Leadtools.Forms; using Leadtools.Barcode; using Leadtools.ImageProcessing; using Leadtools.Examples; public void BarcodeWriter_Example() { int resolution = 300; // Create a Barcode engine BarcodeEngine engine = new BarcodeEngine(); // Get the Barcode writer BarcodeWriter writer = engine.Writer; // All 1D options have the UseXModule set to false by default, we need to set it to true // so we can calculate the default size. We will change the default options so we can // pass null to CalculateBarcodeDataBounds and WriteBarcode below // For all Standard 1D OneDBarcodeWriteOptions oneDWriteOptions = writer.GetDefaultOptions(BarcodeSymbology.UPCA) as OneDBarcodeWriteOptions; oneDWriteOptions.UseXModule = true; // All GS1 Databar Stacked GS1DatabarStackedBarcodeWriteOptions gs1DatabarStackedWriteOptions = writer.GetDefaultOptions(BarcodeSymbology.GS1DatabarStacked) as GS1DatabarStackedBarcodeWriteOptions; gs1DatabarStackedWriteOptions.UseXModule = true; // Patch Code PatchCodeBarcodeWriteOptions patchCodeWriteOptions = writer.GetDefaultOptions(BarcodeSymbology.PatchCode) as PatchCodeBarcodeWriteOptions; patchCodeWriteOptions.UseXModule = true; // All PostNet/Planet PostNetPlanetBarcodeWriteOptions postNetPlanetWriteOptions = writer.GetDefaultOptions(BarcodeSymbology.PostNet) as PostNetPlanetBarcodeWriteOptions; postNetPlanetWriteOptions.UseXModule = true; // We will use this object to save files RasterCodecs codecs = new RasterCodecs(); // Get all the available write symbologies BarcodeSymbology[] symbologies = writer.GetAvailableSymbologies(); foreach(BarcodeSymbology symbology in symbologies) { Console.WriteLine("Processing {0}", symbology); // Create the default data for this symbology BarcodeData data = BarcodeData.CreateDefaultBarcodeData(symbology); // Calculate its size with default options writer.CalculateBarcodeDataBounds(LogicalRectangle.Empty, resolution, resolution, data, null); // Create an image to write the data to LeadRect pixels = data.Bounds.ToRectangle(resolution, resolution); using(RasterImage image = RasterImage.Create(pixels.Width, pixels.Height, 1, resolution, RasterColor.FromKnownColor(RasterKnownColor.White))) { // Write the barcode with default options writer.WriteBarcode(image, data, null); // Save it using (SampleImageStream outputStream = new SampleImageStream(symbology + ".tif")) { codecs.Save(image, outputStream, RasterImageFormat.Tif, 1); } } } }
Imports Leadtools Imports Leadtools.Codecs Imports Leadtools.Forms Imports Leadtools.Barcode Imports Leadtools.ImageProcessing Public Sub BarcodeWriter_Example() Dim resolution As Integer = 300 ' Create a Barcode engine Dim engine As BarcodeEngine = New BarcodeEngine() ' Get the Barcode writer Dim writer As BarcodeWriter = engine.Writer ' All 1D options have the UseXModule set to false by default, we need to set it to true ' so we can calculate the default size. We will change the default options so we can ' pass null to CalculateBarcodeDataBounds and WriteBarcode below ' For all Standard 1D Dim oneDWriteOptions As OneDBarcodeWriteOptions = TryCast(writer.GetDefaultOptions(BarcodeSymbology.UPCA), OneDBarcodeWriteOptions) oneDWriteOptions.UseXModule = True ' All GS1 Databar Stacked Dim gs1DatabarStackedWriteOptions As GS1DatabarStackedBarcodeWriteOptions = TryCast(writer.GetDefaultOptions(BarcodeSymbology.GS1DatabarStacked), GS1DatabarStackedBarcodeWriteOptions) gs1DatabarStackedWriteOptions.UseXModule = True ' Patch Code Dim patchCodeWriteOptions As PatchCodeBarcodeWriteOptions = TryCast(writer.GetDefaultOptions(BarcodeSymbology.PatchCode), PatchCodeBarcodeWriteOptions) patchCodeWriteOptions.UseXModule = True ' All PostNet/Planet Dim postNetPlanetWriteOptions As PostNetPlanetBarcodeWriteOptions = TryCast(writer.GetDefaultOptions(BarcodeSymbology.PostNet), PostNetPlanetBarcodeWriteOptions) postNetPlanetWriteOptions.UseXModule = True ' We will use this object to save files Dim codecs As RasterCodecs = New RasterCodecs() ' Get all the available write symbologies Dim symbologies As BarcodeSymbology() = writer.GetAvailableSymbologies() For Each symbology As BarcodeSymbology In symbologies Console.WriteLine("Processing {0}", symbology) ' Create the default data for this symbology Dim data As BarcodeData = BarcodeData.CreateDefaultBarcodeData(symbology) ' Calculate its size with default options writer.CalculateBarcodeDataBounds(LogicalRectangle.Empty, resolution, resolution, data, Nothing) ' Create an image to write the data to Dim pixels As LeadRect = data.Bounds.ToRectangle(resolution, resolution) Using image As RasterImage = RasterImage.Create(pixels.Width, pixels.Height, 1, resolution, RasterColor.FromKnownColor(RasterKnownColor.White)) ' Write the barcode with default options writer.WriteBarcode(image, data, Nothing) ' Save it Using outputStream As SampleImageStream = New SampleImageStream(symbology & ".tif") codecs.Save(image, outputStream, RasterImageFormat.Tif, 1) End Using End Using Next symbology End Sub
BarcodeWriter Members
Leadtools.Barcode Namespace
BarcodeSymbology Enumeration
BarcodeData Class
BarcodeWriter.WriteBarcode
Programming with LEADTOOLS Barcode
Supported Barcode Symbologies
Unlocking Barcode Support
Writing Barcodes Tutorial
UPC / EAN Barcodes in LEADTOOLS
2 of 5 Barcodes in LEADTOOLS
GS1 DataBar / RSS-14 Barcodes in LEADTOOLS
Code 128 Barcodes in LEADTOOLS
USPS and 4-State Barcodes in LEADTOOLS
MSI Barcodes (Pulse Width Modulated) in LEADTOOLS
Codabar Barcodes in LEADTOOLS
Miscellaneous Barcodes in LEADTOOLS
Datamatrix Barcodes in LEADTOOLS
PDF417 and MicroPDF417 Barcodes in LEADTOOLS
MicroPDF417 Barcodes in LEADTOOLS
QR Barcodes in LEADTOOLS
Writing Barcodes - Bounds and XModule