Set Barcode Size and Resolution in JavaScript
This article outlines the capabilities provided by Aspose.BarCode for JavaScript via C++ to customize barcode measurement units (inches, millimeters, points, or pixels) and the resolution of generated barcode images.
Overview
In Cartesian coordinate systems, image data are represented as the coordinates of pixels or drawable objects for raster or vector images, respectively.
In Aspose.BarCode for JavaScript via C++, to link these coordinates to real-world measurement units, such as inches or millimeters, the BarcodeGenerator class uses common notation like “1px”, “2mm”, “3in”, “4pt”.
Define Barcode Size in Different Units
The BarcodeGenerator class provides several parameters to configure the size of a barcode. These include:
- BarCodeHeight
- BarCodeWidth
- XDimension
- FontUnit
You can adjust these parameters to customize the barcode dimensions according to your requirements.
Barcode images generated with different unit settings (three pixels and two millimeters) are shown below.
Size Units | Pixels | Millimeters |
---|---|---|
The following code snippet shows how to set up these configurations as in the provided barcode images.
// Create a DataMatrix barcode generator with unit size settings
var gen = new BarCodeInstance.BarcodeGenerator("DataMatrix", "ASPOSE");
// Set unit size in 3 pixels
gen.Parameters.Barcode.XDimension = "3px";
document.getElementById("img").src = gen.GenerateBarCodeImage(); // Display barcode image with unit in 3 pixels
// Set unit size in 2 millimeters
gen.Parameters.Barcode.XDimension = "2mm";
document.getElementById("img2").src = gen.GenerateBarCodeImage(); // Display barcode image with unit in 2 millimeters
// Cleanup
gen.delete();
Set Barcode Image Resolution
Aspose.BarCode for JavaScript via C++ allows you to customize the resolution of a barcode image. The BarcodeGenerator class has a Resolution property that can be used to get or set the barcode image resolution. This property converts all non-pixel size values into digital coordinates measured in pixels and is uniform for all barcode image dimensions. The resolution is measured in dpi, with the default value set to 96 dpi.
The barcode images below show examples created with different resolution settings (96 dpi and 300 dpi).
Resolution | 96 dpi | 300 dpi |
---|---|---|
The following code example illustrates how to set the resolution for a barcode image.
// Generate a DataMatrix barcode with unit size settings in millimeters and different resolutions
var gen = new BarCodeInstance.BarcodeGenerator("DataMatrix", "ASPOSE");
// Set unit size to 1 millimeter and resolution to 96 DPI
gen.Parameters.Barcode.XDimension = "1mm";
gen.Parameters.Resolution = 96;
document.getElementById("img").src = gen.GenerateBarCodeImage(); // Display barcode image
// Set unit size to 1 millimeter and resolution to 300 DPI
gen.Parameters.Barcode.XDimension = "1mm";
gen.Parameters.Resolution = 300;
document.getElementById("img2").src = gen.GenerateBarCodeImage(); // Display barcode image
gen.delete();