←Select platform

CalculateBarcodeDataBounds Method

Summary
Calculates the exact pixel size required to write the specified barcode.
Syntax
C#
Objective-C
C++/CLI
Java
Python
public void CalculateBarcodeDataBounds( 
   LeadRect writeBounds, 
   int xResolution, 
   int yResolution, 
   BarcodeData data, 
   BarcodeWriteOptions options 
) 
- (BOOL)calculateBarcodeDataBounds:(LeadRect)writeBounds xResolution:(NSInteger)xResolution yResolution:(NSInteger)yResolution data:(LTBarcodeData *)data options:(nullable LTBarcodeWriteOptions *)options error:(NSError **)error NS_SWIFT_NAME(calculateBarcodeDataBounds(writeBounds:xResolution:yResolution:data:options:)); 
public void calculateBarcodeDataBounds( 
  LeadRect writeBounds,  
  int xResolution,  
  int yResolution,  
  BarcodeData data,  
  BarcodeWriteOptions options 
) 
public: 
void CalculateBarcodeDataBounds(  
   LeadRect writeBounds, 
   int xResolution, 
   int yResolution, 
   BarcodeData^ data, 
   BarcodeWriteOptions^ options 
)  
def CalculateBarcodeDataBounds(self,writeBounds,xResolution,yResolution,data,options): 

Parameters

writeBounds
The location where the barcode should be written.

xResolution
Horizontal resolution value of the destination image that will be used to write the barcode to.

yResolution
Vertical resolution value of the destination image that will be used to write the barcode to.

data
An BarcodeData object that contains the barcode data to write.

options
Options write options to use.

Remarks

After this method returns, the width and height values of BarcodeData.Bounds will be updated with the exact pixel size required to write the barcode with the specified options.

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. You can 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.

Example

This example will use CalculateBarcodeDataBounds to calculate the smallest size required to write the specified QR barcode, then it will create an image with the specified size, and finally write the barcode to it.

C#
Java
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Barcode; 
using Leadtools.ImageProcessing; 
 
 
public void BarcodeWriter_CalculateBarcodeDataBoundsExample() 
{ 
   string imageFileName = Path.Combine(LEAD_VARS.ImagesDir, "MyQRBarcode.tif"); 
 
   BarcodeEngine engine = new BarcodeEngine(); 
   BarcodeWriter writer = engine.Writer; 
 
   // Create the QR barcode data, we will use the default but you change it 
   // if needed 
   QRBarcodeData barcode = BarcodeData.CreateDefaultBarcodeData(BarcodeSymbology.QR) as QRBarcodeData; 
 
   // Change the X Module to be 0.05 inches 
   QRBarcodeWriteOptions options = writer.GetDefaultOptions(BarcodeSymbology.QR) as QRBarcodeWriteOptions; 
   options.XModule = 50; 
 
   // Calculate the size of the barcode with these options 
   int resolution = 300; 
   writer.CalculateBarcodeDataBounds(LeadRect.Empty, resolution, resolution, barcode, options); 
 
   // Now create an image with the barcode size 
   LeadRect pixels = barcode.Bounds; 
   using (RasterImage image = RasterImage.Create(pixels.Width, pixels.Height, 1, resolution, RasterColor.FromKnownColor(RasterKnownColor.White))) 
   { 
      // Write the barcode 
      writer.WriteBarcode(image, barcode, options); 
 
      // Save the image to disk 
      using (RasterCodecs codecs = new RasterCodecs()) 
      { 
         codecs.Save(image, imageFileName, RasterImageFormat.CcittGroup4, 1); 
      } 
   } 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images"; 
} 
 
import static org.junit.Assert.assertTrue; 
 
import java.io.File; 
import java.io.IOException; 
import java.nio.file.Path; 
 
import org.junit.*; 
import org.junit.runner.JUnitCore; 
import org.junit.runner.Result; 
import org.junit.runner.notification.Failure; 
 
import leadtools.*; 
import leadtools.barcode.*; 
import leadtools.codecs.*; 
 
 
public void barcodeWriterCalculateBarcodeDataBoundsExample() { 
   final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images"; 
   String imageFileName = combine(LEAD_VARS_IMAGES_DIR, "MyQRBarcode.tif"); 
   BarcodeEngine engine = new BarcodeEngine(); 
   BarcodeWriter writer = engine.getWriter(); 
 
   // Create the QR barcode data, we will use the default but you change it 
   // if needed 
   QRBarcodeData barcode = (QRBarcodeData) BarcodeData.createDefaultBarcodeData(BarcodeSymbology.QR); 
 
   // Change the X Module to be 0.05 inches 
   QRBarcodeWriteOptions options = (QRBarcodeWriteOptions) writer.getDefaultOptions(BarcodeSymbology.QR); 
   options.setXModule(50); 
 
   // Calculate the size of the barcode with these options 
   int resolution = 300; 
   writer.calculateBarcodeDataBounds(LeadRect.getEmpty(), resolution, resolution, barcode, options); 
 
   // Now create an image with the barcode size 
   LeadRect pixels = barcode.getBounds(); 
   RasterImage image = RasterImage.create(pixels.getWidth(), pixels.getHeight(), 1, resolution, 
         RasterColor.fromKnownColor(RasterKnownColor.WHITE)); 
 
   // Write the barcode 
   writer.writeBarcode(image, barcode, options); 
 
   // Save the image to disk 
   RasterCodecs codecs = new RasterCodecs(); 
 
   codecs.save(image, imageFileName, RasterImageFormat.CCITT_GROUP4, 1); 
   assertTrue("Image unsuccessfully saved to " + imageFileName, (new File(imageFileName)).exists()); 
} 
Requirements

Target Platforms

Help Version 23.0.2024.2.29
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Barcode Assembly

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.