←Select platform

SetData Method

Summary

Sets the data of this barcode as raw byte array.

Syntax

C#
VB
Java
Objective-C
WinRT C#
C++
public virtual void SetData(  
   byte[] data 
) 
Public Overridable Sub SetData( _ 
   ByVal data() As Byte _ 
)  
public virtual void SetData(  
   byte[] data 
) 
@property (nonatomic, strong, nullable) NSData *data 
public void setData(byte[] data) 
 function Leadtools.Barcode.BarcodeData.SetData(  
   data  
) 
public: 
virtual void SetData(  
   array<byte>^ data 
)  

Parameters

data
An array of Byte that specifies the data of this barcode. A value of null (Nothing in VB) can be used.

Remarks

Reading Barcodes

The BarcodeReader.ReadBarcode or BarcodeReader.ReadBarcodes methods are used to read a barcode or more from an image. Each of these methods return an object or an array of objects of type BarcodeData for each barcode found. Inside each object, the data of the barcode will be stored as a raw byte array that can be accessed with the GetData method. The format of the data is dependent on the barcode symbology (type).

The Value property contains a string representation (as an ASCII text) of the data. The value of this property is simply an ASCII string of the byte array returned from GetData.

Writing Barcodes

The BarcodeWriter.WriteBarcode method is used to write a barcodes to an image. Create an instance of BarcodeData and fill its members before passing it to this method. Use the SetData method to set the raw data of the barcode as a byte array. You can also use the Value property to set the data as an ASCII string.

Some barcodes like QR and PDF417 support data that is not necessary ASCII text. For example, an image, a URL or just raw stream of bytes. For these barcodes when reading them, the Value property may not return an accurate representation of the data. Instead, use the BarcodeData.GetData method and then parse them. Also, when writing barcodes, you must set the raw data using the SetData method

The Australian post barcode (BarcodeSymbology.AustralianPost4State) string has a special format to distinguish different fields (i.e. FCC, DPID, and CIF). The string format puts dashes between fields as follows: "FCC-DIPD-CIF", where FCC is a 2-digit field (valid values are 11, 87, 45, 92, 59, 62, and 44), DPID is an 8-digit field representing the address, and CIF (optional) represents the customer information field. For more information, please refer to the standard. This string format applies for both read and write.

GS1 Databar Expanded (BarcodeSymbology.GS1DatabarExpanded) and GS1 Databar Expanded Stacked (BarcodeSymbology.GS1DatabarExpandedStaceked) barcode strings need to be written conforming to a string format that indicates a particular encoding method, such as Method "1", Method "0100", Method "0101", … etc. For example, a string (01)00012345678905(10)ABC123" is encoded using Method "1" (i.e. General Identification Data). Note that 14th digit of the item identification number (in this case, "5"), if it exists, is considered as a check digit and is ignored. A string that is written without following any of these special encoding formats will be encoded by Method "00" (i.e. General Purpose Data).

When a BarcodeData is returned from a read operation, an extra digit in curly brackets (i.e. "{" and "}") is added to indicate the linkage bit at the beginning of the string. The curly brackets are not part of the encoded barcode data, and they are not defined in the standard. Rather, they distinguish the linkage digit from other data. For example, if a barcode is written with string "(01)00012345678905(10)ABC123" while the linkage bit is set to zero, the read function result will be "{0}(01)00012345678905(10)ABC123", where {0} is the linkage digit and the remaining characters will be the barcode data.

Derived Types

Some barcode symbologies contain extra information that are not available in BarcodeData, for these types, LEADTOOLS adds derived classes to contain the extra information. Refer to BarcodeData for more information.

Example

This example writes a barcode to an image using raw data.

C#
VB
Silverlight C#
Silverlight VB
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Forms; 
using Leadtools.Barcode; 
using Leadtools.ImageProcessing; 
 
public void BarcodeData_SetDataExample() 
{ 
   string outFileName = Path.Combine(LEAD_VARS.ImagesDir, @"MyBarcode.tif"); 
 
   // This is UPC data to save a a string 
   string dataString = "01234567890"; 
   // Get it as a byte array to use in this example 
   byte[] bytes = Encoding.ASCII.GetBytes(dataString); 
 
   // Create a BarcodeData object from this data 
   BarcodeData data = new BarcodeData(); 
   data.Symbology = BarcodeSymbology.UPCA; 
   data.Bounds = new LogicalRectangle(0, 0, 400, 200, LogicalUnit.Pixel); 
 
   // Set the data 
   data.SetData(bytes); 
 
   // Write it to an image 
   BarcodeEngine engine = new BarcodeEngine(); 
   using (RasterCodecs codecs = new RasterCodecs()) 
   { 
      int resolution = 300; 
      LeadRect pixels = data.Bounds.ToRectangle(resolution, resolution); 
      using (RasterImage image = RasterImage.Create(pixels.Width, pixels.Height, 1, resolution, RasterColor.FromKnownColor(RasterKnownColor.White))) 
      { 
         engine.Writer.WriteBarcode(image, data, null); 
 
         codecs.Save(image, outFileName, RasterImageFormat.Tif, 1); 
      } 
   } 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\Users\Public\Documents\LEADTOOLS Images"; 
} 
Imports Leadtools 
Imports Leadtools.Codecs 
Imports Leadtools.Forms 
Imports Leadtools.Barcode 
Imports Leadtools.ImageProcessing 
 
Public Sub BarcodeData_SetDataExample() 
   Dim outFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "MyBarcode.tif") 
 
   ' This is UPC data to save a a string 
   Dim dataString As String = "01234567890" 
   ' Get it as a byte array to use in this example 
   Dim bytes() As Byte = Encoding.ASCII.GetBytes(dataString) 
 
   ' Create a BarcodeData object from this data 
   Dim data As New BarcodeData() 
   data.Symbology = BarcodeSymbology.UPCA 
   data.Bounds = New LogicalRectangle(0, 0, 400, 200, LogicalUnit.Pixel) 
 
   ' Set the data 
   data.SetData(bytes) 
 
   ' Write it to an image 
   Dim engine As New BarcodeEngine() 
   Using codecs As New RasterCodecs() 
      Dim resolution As Integer = 300 
      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)) 
         engine.Writer.WriteBarcode(image, data, Nothing) 
 
         codecs.Save(image, outFileName, RasterImageFormat.Tif, 1) 
      End Using 
   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; 
using Leadtools.Examples; 
 
public void BarcodeData_SetDataExample() 
{ 
   // This is UPC data to save a a string 
   string dataString = "01234567890"; 
   // Get it as a byte array to use in this example 
   byte[] bytes = Encoding.UTF8.GetBytes(dataString); 
 
   // Create a BarcodeData object from this data 
   BarcodeData data = new BarcodeData(); 
   data.Symbology = BarcodeSymbology.UPCA; 
   data.Bounds = new LogicalRectangle(0, 0, 400, 200, LogicalUnit.Pixel); 
 
   // Set the data 
   data.SetData(bytes); 
 
   // Write it to an image 
   BarcodeEngine engine = new BarcodeEngine(); 
   RasterCodecs codecs = new RasterCodecs(); 
 
   int resolution = 300; 
   LeadRect pixels = data.Bounds.ToRectangle(resolution, resolution); 
   using (RasterImage image = RasterImage.Create(pixels.Width, pixels.Height, 1, resolution, RasterColor.FromKnownColor(RasterKnownColor.White))) 
   { 
      engine.Writer.WriteBarcode(image, data, null); 
 
      using (SampleImageStream outputStream = new SampleImageStream("MyBarcode.tif")) 
      { 
         codecs.Save(image, outputStream, RasterImageFormat.Tif, 1); 
      } 
   } 
} 
Imports Leadtools 
Imports Leadtools.Codecs 
Imports Leadtools.Forms 
Imports Leadtools.Barcode 
Imports Leadtools.ImageProcessing 
 
Public Sub BarcodeData_SetDataExample() 
   ' This is UPC data to save a a string 
   Dim dataString As String = "01234567890" 
   ' Get it as a byte array to use in this example 
   Dim bytes As Byte() = Encoding.UTF8.GetBytes(dataString) 
 
   ' Create a BarcodeData object from this data 
   Dim data As BarcodeData = New BarcodeData() 
   data.Symbology = BarcodeSymbology.UPCA 
   data.Bounds = New LogicalRectangle(0, 0, 400, 200, LogicalUnit.Pixel) 
 
   ' Set the data 
   data.SetData(bytes) 
 
   ' Write it to an image 
   Dim engine As BarcodeEngine = New BarcodeEngine() 
   Dim codecs As RasterCodecs = New RasterCodecs() 
 
   Dim resolution As Integer = 300 
   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)) 
      engine.Writer.WriteBarcode(image, data, Nothing) 
 
      Using outputStream As SampleImageStream = New SampleImageStream("MyBarcode.tif") 
         codecs.Save(image, outputStream, RasterImageFormat.Tif, 1) 
      End Using 
   End Using 
End Sub 

Requirements

Target Platforms

Products | Support | Contact Us | Copyright Notices
© 1991-2017 LEAD Technologies, Inc. All Rights Reserved.
Leadtools.Barcode Assembly
Click or drag to resize