Aspose.BarCode for C++ beginners guide
Tags: cpp, windows, aspose, barcode
Published: 2020-11-13 03:16:18

[**Download example source**](https://github.com/AsposeAG/CodeNotes/tree/main/cpp/ABTest)

#Introduction

[Aspose.BarCode for C++](https://products.aspose.com/barcode/cpp) is a C++ library providing the barcode generation and recognition features to your project and supports more than 60 barcode types and 7 image formats. The library is a part of Aspose.Barcode is multi-platform family of libraries are written on C++, .Net and Java.

In this article we describe simple example how to write and read barcodes on C++ for Windows platform. However, the library supports Windows, Linux and MacOS platforms. On Windows it can be used only in x64 project types and last [19.9.0 version](https://www.nuget.org/packages/Aspose.BarCode.Cpp/) of the library requires VS2017(v141) platform toolset. The projects can be developed in VS2017 and VS2019.

#Background

[Barcodes](https://en.wikipedia.org/wiki/Barcode) is a technology which allows to add machine reading tags or machine reading additional data to any object of real world with less than one cent cost. Before, it requires specialized devices to print or recognize barcode but currently they can be recognized with any camera equipped device and specialized software library - barcode library.

[Barcode libraries](https://en.wikipedia.org/wiki/Barcode_library) in main cases have two functions: reading and writing barcodes. Some of the libraries support only one function, some support both functions.

Aspose.BarCode for C++ supports two functions with the following components: **BarcodeGenerator** (writing), **BarCodeReader** (reading).

**BarcodeGenerator** allows generating more than 60 barcode types, both 1D barcode types and 2D barcode types. The component has wide options to specifying appearance-related properties, customize barcode encoding modes and parameters. Appearance-related properties include customization of image borders, style, margin, width, background / foreground colors, fonts, location (hide, above, below) and alignment. Barcode image, depends on requirements, can be rotated on any angle and be produced in high quality with anti-aliasing. Depends on type of appearance of screen or printing mode, image resolution or auto size mode also can be customized.

**BarCodeReader** allows to recognize more than 60 barcode types, both 1D barcode types and 2D barcode types, on scanned image in the most popular file formats like: JPEG, TIFF, PNG, BMP, GIF and EXIF. The recognition process is highly configurable and allows selection of preferred barcode types and image regions for recognition, also customization of internal engine variables to choose better balance between recognition performance and recognition quality.

#Obtaining license

Aspose.BarCode for C++ is proprietary library and requires license to support all of the features. Without license recognition and generation function could be watermarked. 

To obtain temporary 30 day license with mostly unlimited prolongation you could go to [Temporary License] (https://purchase.aspose.com/temporary-license) page select [purchase wizard](https://purchase.aspose.com/buy) and obtain Aspose.Total or Aspose.Barcode license. 

The other way is obtaining free [Publicity License](https://purchase.aspose.com/publicity-license)

#Create project and install the library

In this section we describe how to create console project which can support Aspose.BarCode for C++ and install the library by the NuGet.
##### 1.Run VS2019 and create console project.
![Console Application](https://dev-to-uploads.s3.amazonaws.com/i/5yyf1ozexrgtff33jg2l.png)


##### 2. Set platform to x64 version
![set x64](https://dev-to-uploads.s3.amazonaws.com/i/15i4dwelqjxghq6i9jbg.png)

##### 3. Setup platform toolset to VS2017(v141) and SDK to 8.1+ to Debug and Release configirations.
![set VS Platform to v141](https://dev-to-uploads.s3.amazonaws.com/i/rnocvt5uakp5e4qsh8ep.png)

##### 4. Press right click on project References and select “Manage NuGet Packages”
![Manage NuGet Packages](https://dev-to-uploads.s3.amazonaws.com/i/qsy51rxibqxglzq4u3fs.png)

##### 5. Find Aspose.BarCode.Cpp on “Browse” folder and push “Install”.
![install Aspose.BarCode.Cpp library](https://dev-to-uploads.s3.amazonaws.com/i/bqv0pxpfjmlkroai84jl.png)

#Using the code

At first add these headers to the example file, they are required for implementation of BarcodeGenerator and BarCodeReader basic functions.
```cpp
#include <windows.h>
#include <Licensing\License.h>
#include <Generation/EncodeTypes/SymbologyEncodeType.h>
#include <Generation/EncodeTypes/EncodeTypes.h>
#include <BarCode.Generation/BarcodeGenerator.h>
#include <BarCode.Generation/GenerationParameters/BaseGenerationParameters.h>
#include <BarCode.Generation/GenerationParameters/BarcodeParameters.h>
#include <BarCode.Generation/GenerationParameters/CodetextParameters.h>
#include <BarCode.Generation/Helpers/Unit.h>
#include <Generation/BarCodeImageFormat.h>

//BarCodeReader
#include <BarCodeRecognition/Recognition/RecognitionSession/BarCodeReader.h>
#include <BarCodeRecognition/Recognition/RecognitionSession/DecodeTypes/DecodeType.h>
#include <BarCodeRecognition/Recognition/RecognitionSession/DecodeTypes/MultyDecodeType.h>
#include <BarCodeRecognition/Recognition/RecognitionSession/DecodeTypes/SingleDecodeType.h>
```

##### Adding license
Then we add to “main” function the code, which applies license to the project. License file could be obtained on previous steps and without license generated images and recognized barcode text could be watermarked.
```cpp
//set license
System::SharedPtr<Aspose::BarCode::License> license = System::MakeObject<Aspose::BarCode::License>();
license->SetLicense(System::String::FromWCS(lExampleFolder + L"LicenseName.lic"));
```

##### BarcodeGenerator
In the function **GenerateBarcode** we generate **Code128** barcode with codetext. Any other barcode type with any other codetext can be generated. Also, we can setup barcode special parameters.
```cpp
void GenerateBarcode(std::wstring &folder)
{
    //initialize out codetext
    const System::String codeText = u"ABCTZCS1234567890T4444T5678901234XYZ";

    //create barcode generator with Code128 symbology
    System::SharedPtr<Aspose::BarCode::Generation::BarcodeGenerator> barcodeGenerator = System::MakeObject<Aspose::BarCode::Generation::BarcodeGenerator>(Aspose::BarCode::Generation::EncodeTypes::Code128, codeText);
    //set barcode parameters as no codetext
    barcodeGenerator->get_Parameters()->get_Barcode()->get_CodeTextParameters()->set_Location(Aspose::BarCode::Generation::CodeLocation::None);
    //and bar unit size 5 pixels
    barcodeGenerator->get_Parameters()->get_Barcode()->get_XDimension()->set_Pixels(5);

    //save our barcode in png format
    barcodeGenerator->Save(System::String::FromWCS(folder + L"code128.png"), Aspose::BarCode::BarCodeImageFormat::Png);
    std::wcout << L"Barcode generated!\n";
}
```

The library uses **System::String** type which is similar to **std::wstring** and can be converted with **FromWCS** and **ToWCS** functions.

##### BarCodeReader

In the function **RecognizeBarcodeToConsole** we recognize previously generated barcode and write recognition result to the console. It is advised to run this function in Release mode because **recognition time in Release and Debug modes can be different in 20-60 times**. Or you can replace debug libraries to release libraries in **“Aspose.BarCode.Cpp.targets”** file.
```cpp
void RecognizeBarcodeToConsole(std::wstring& folder)
{
    //PLEASE USE RELEASE MODE IT IS FASTER IN 20-60 TIMES THEN DEBUG MODE
    //create barcode reader with Code128 symbology
    System::SharedPtr<Aspose::BarCode::BarCodeRecognition::BarCodeReader> reader =
        System::MakeObject<Aspose::BarCode::BarCodeRecognition::BarCodeReader>(System::String::FromWCS(folder + L"code128.png"),
            Aspose::BarCode::BarCodeRecognition::DecodeType::Code128);
    
    //read recognized values and write to the console
    while (reader->Read())
    {
        std::wstring result = L"Type:" + reader->GetCodeTypeName().ToWCS() + L" Codetext:" + reader->GetCodeText().ToWCS() + L"\n";
        std::wcout << result.data();
    }
    std::wcout << L"Barcode recognized!\n";
}
```

#Documentation

Extended documentation about the library classes and functions is provided on the [product page](https://apireference.aspose.com/barcode/cpp).

#Conclusion

Aspose.BarCode for C++ can easily add any barcode feature to your projects on C++ and Windows platform. The library contains features which allows to use library in heavy industrial applications or use for your own business needs. The main library advantage is simplicity with rich abilities.