Set Barcode Size and Resolution in C++

This article outlines the capabilities provided by Aspose.BarCode for 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 C++, to link the values of such coordinates to real-world measurement units, such as inches or millimeters, BarcodeGenerator class includes class Unit and its specific property called Resolution that is measured in dots per inch (dpi). Class Unit serves to convert barcode size values defined in pixels, millimeters, inches, or points into any of the supported measurement units automatically. In this way, the size of elements in real-world measurement units can be converted into pixels to perform barcode image generation.

// The path to the documents directory.
System::String dataDir = RunExamples::GetDataDir_ManageBarCodesImages();

// Instantiate barcode object and set CodeText & Barcode Symbology
System::SharedPtr<BarcodeGenerator> generator = [&] { auto tmp_0 = System::MakeObject<BarcodeGenerator>(EncodeTypes::Code39Standard, u"1234567890"); tmp_0->get_Parameters()->get_Barcode()->set_AutoSizeMode(AutoSizeMode::Nearest); tmp_0->get_Parameters()->get_Barcode()->get_BarCodeHeight()->set_Millimeters(50); tmp_0->get_Parameters()->get_Barcode()->get_BarCodeWidth()->set_Millimeters(120); return tmp_0; }();
generator->Save(dataDir + u"barcode-custom-size_out.jpg");

Define Barcode Size in Different Units

Class Unit is used to set measurement units for various parameters of class BarcodeGenerator, such as BarCodeHeight, BarCodeWidth, XDimension, FontUnit, and some others. Class Unit allows converting different measurement units into the desired ones: Inches, Millimeters, Pixels, Point (1/72 inch), or Document (1/300 inch). The default size measurement unit for barcode generation is Millimeters.

Barcode images generated using different unit settings (three pixels and two millimeters) are demonstrated below.

Size Units Pixels Millimeters
// The path to the documents directory.
System::String dataDir = RunExamples::GetDataDir_ManageBarCodesImages();

// Instantiate barcode object
System::SharedPtr<BarcodeGenerator> generator = System::MakeObject<BarcodeGenerator>(EncodeTypes::Code128, u"1234567"); 
generator->get_Parameters()->get_Barcode()->get_BarHeight()->set_Point(3.0f);
generator->Save(dataDir + u"barcode-size-unit_out.jpeg", BarCodeImageFormat::Jpeg);
System::Console::WriteLine(System::Environment::get_NewLine() + u"Barcode saved at " + dataDir);

Set Barcode Image Resolution

Aspose.BarCode for C++ enables custom settings for barcode image resolution. To implement this, BarcodeGenerator class provides the Resolution property that is used to get or set the resolution of a barcode image and is intended to convert all non-pixel barcode size values into digital coordinates measured in pixels. The Resolution property is defined uniformly for all dimensions of a barcode image and is measured in dpi. Its default value equals 96 dpi.

Barcode labels created with different resolution settings (96 dpi and 300 dpi) are shown below.

Resolution Is Set to 96 dpi Is Set to 300 dpi
// The path to the documents directory.
System::String dataDir = RunExamples::GetDataDir_ManageBarCodesImages();

// Instantiate barcode object and set CodeText & Barcode Symbology
System::SharedPtr<BarcodeGenerator> generator = [&] { auto tmp_0 = System::MakeObject<BarcodeGenerator>(EncodeTypes::Code128, u"1234567"); tmp_0->get_Parameters()->set_Resolution(400.f); return tmp_0; }();

// Save the image to your system and set its image format to Jpeg
generator->Save(dataDir + u"barcode-image-resolution_out.jpeg", BarCodeImageFormat::Jpeg);
System::Console::WriteLine(System::Environment::get_NewLine() + u"Barcode saved at " + dataDir);