---
title: C++
slug: cisdk-cpp
docTags: 
createdAt: 2023-03-12T03:13:21.000Z
---

## Step 1:   Install ClearImage SDK

Download and install the [ClearImage SDK](https://barcode-reader.inliteresearch.com/get-cisdk) &#x20;

## Step 2:   Configure your project

Use Visual Studio [C++ ClearImage example project](https://barcode-reader.inliteresearch.com/get-ci_example_cpp) or follow the instruction below.
When using the example project, copy <font color="#eb144c">`ClearImage.dll`</font> in the executable folder or in the PATH.

In your Visual Studio C++  project:

- Include a copy of  <font color="#eb144c">`C:\Program Files (x86)\Inlite\ClearImageSDK\ClearImage.wrapper.hpp`</font> in every source file that uses ClearImage API.
- Add a copy of  <font color="#eb144c">`C:\Program Files (x86)\Inlite\ClearImageSDK\ClearImage.lib`</font> to your project's <font color="#eb144c">`Properties->Linker->Input->Additional Dependencies`</font> entry.
- Place <font color="#eb144c">`ClearImage.dll`</font>  in a location where it can be found according to  [Microsoft's DLL search order](https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-search-order) rules. Typically it is a working folder where your application started from.  &#x20;
  The  <font color="#eb144c">`ClearImage.dll`</font>  is installed in:
  - 32-bit version from <font color="#eb144c">`C:\Program Files (x86)\Inlite\ClearImageSDK\win32\ClearImage.dll`</font>
  - 64-bit version from <font color="#eb144c">`C:\Program Files (x86)\Inlite\ClearImageSDK\x64\ClearImage.dll`</font> &#x20;

This code demonstrates the use of ClearImage DLL API.  Runtime errors are reported in <font color="#eb144c">`ExceptionCI`</font> object.

```cpp
#include "ClearImage.wrapper.hpp"
 
void main() {
 	try {
		CiServer Server = CreateCiServer();
		//  your ClearImage API-based  code
		}
	catch (ExceptionCI ex) {
		std::wcout << L"ERROR: " << ex.getMessage() << std::endl;
	}
	catch (std::runtime_error ex) {
		std::wcout << "ERROR: " << ex.what() << std::endl;
	}
	catch (...) {
		std::wcout << "ERROR: " << "unknown"<< std::endl;
	}	
}
```

## ClearImage project development

### Extend functionality

While developing your application, you can combine code examples on this page with methods described in  [ClearImage API documentation](http://www.inliteresearch.com/help/ci). **DLL API,** as exposed through  <font color="#eb144c">`ClearImage.wrapper.hpp`</font>, is substantially similar to **COM API** and **.NET API** <font color="#eb144c">`Inlite.ClearImage`</font> namespace.

### Exception handling

ClearImage DLL API reports errors as an <font color="#eb144c">`ExceptionCI`</font> object.&#x20;

```csharp
try {
    //  Call ClearImage .NET API
    }
	catch (ExceptionCI ex)	{
		std::wcout << L"ERROR: " << ex.getMessage() << std::endl;
	}
```

## Barcode Reading

### Read 1D  barcodes from a page

This example reads code 39 and code 128 barcodes.   Set barcode types used in your application. Setting <font color="#eb144c">`reader->AutoDetect1D=true;`</font> automatically finds the barcode type, but it is slower and is not recommended for production. &#x20;


```cpp
void ReadBarcode1D_page(const CiServer &Server, const std::wstring &fileName, const long page)
{
	try {
		CiBarcodePro reader = Server->CreateBarcodePro();
		reader->Type = (FBarcodeType)(cibfCode39 | cibfCode128);
		// reader->AutoDetect1D = true;
		reader->Image->Open(fileName, page);
		reader->Find(0);
		for (int i = 1; i <= reader->Barcodes->Count; i++)
			std::wcout << reader->Barcodes->Item[i-1]->Text << std::endl;
	}
	catch (ExceptionCI ex)	{
		std::wcout << L"ERROR: " << ex.getMessage() << std::endl;
	}
}
```

### Read 2D Barcode from a page

This example reads PDF417, DataMatrix, and QR code. 2D barcodes might contain binary data available in  <font color="#eb144c">`CiBarcode->Data`</font> property as <font color="#eb144c">`std::vector<char>`</font>

```cpp
void ReadBarcode2D_page(const CiServer& Server, const std::wstring& fileName, const long page)
{
	try {
		CiQR reader = Server->CreateQR();
		// CiPdf417 reader = Server->CreatePdf417();
		// CiDataMatrix reader = Server->CreateDataMatrix();
		reader->Image->Open(fileName, page);
		reader->Find(0);
		for (int i = 1; i <= reader->Barcodes->Count; i++)
			std::wcout << reader->Barcodes->Item[i-1]->Text << std::endl;
	}
	catch (ExceptionCI ex)	{
		std::wcout << L"ERROR: " << ex.getMessage() << std::endl;
	}
}
```

### Read Postal Barcodes from a page

This example reads Postal Barcodes from an image file page.  &#x20;

```cpp
void ReadBarcodePostal_page(const CiServer& Server, const std::wstring& fileName, const long page)
{
	try {
		CiBarcodePro reader = Server->CreateBarcodePro();
		reader->Type =  cibf4State;
		reader->Image->Open(fileName, page);
		reader->Find(0);
		for (int i = 1; i <= reader->Barcodes->Count; i++)
		{  
			std::wstring type = L"";
			switch (reader->Barcodes->Item[i-1]->Type)
			{
			case cibUspsIntelligentMail: type = L"US Intelligent Mail "; break;
			case cibBpoPostcode: type = L"UK Royal Mail "; break;
			case cibAustralianPost: type = L"Australian Mail "; break;
			case cibSingaporePost: type = L"Singapore Mail "; break;
			}
			std::wcout << L"Type: " <<  type << L"   " << reader->Barcodes->Item[i-1]->Text << std::endl;
		}
	}
	catch (ExceptionCI ex)	{
		std::wcout << L"ERROR: " << ex.getMessage() << std::endl;
	}
}
```

### Read Driver's License barcode

This example reads the Driver's License barcode from an image file page.  &#x20;

```cpp
void ReadDriverLicBarcode_page(const CiServer& Server, const std::wstring& fileName, const long page)
{
	try {
		CiPdf417 reader = Server->CreatePdf417();
		reader->Image->Open(fileName, page);
		reader->Find(0);
		for (int i = 1; i <= reader->Barcodes->Count; i++) {
			CiBarcode Bc = reader->Barcodes->Item[i-1];
			std::wcout << Bc->Text << std::endl;
			if (Bc->Type == EBarcodeType::cibPdf417 && Bc->GetInfo(L"last") != L"")	{
				std::wcout << L"last:    " << Bc->GetInfo(L"last") << std::endl;
				std::wcout << L"first:   " << Bc->GetInfo(L"first") << std::endl;
				std::wcout << L"middle:  " << Bc->GetInfo(L"middle") << std::endl;
				std::wcout << L"suffix:  " << Bc->GetInfo(L"suffix") << std::endl;
				std::wcout << L"dob:     " << Bc->GetInfo(L"dob") << std::endl;
				std::wcout << L"eyes:    " << Bc->GetInfo(L"eyes") << std::endl;
				std::wcout << L"hair:    " << Bc->GetInfo(L"hair") << std::endl;
				std::wcout << L"sex:     " << Bc->GetInfo(L"sex") << std::endl;
				std::wcout << L"height:  " << Bc->GetInfo(L"height") << std::endl;
				std::wcout << L"weight:  " << Bc->GetInfo(L"weight") << std::endl;
				std::wcout << L"street:  " << Bc->GetInfo(L"street") << std::endl;
				std::wcout << L"city:    " << Bc->GetInfo(L"city") << std::endl;
				std::wcout << L"state:   " << Bc->GetInfo(L"state") << std::endl;
				std::wcout << L"postal:  " << Bc->GetInfo(L"postal") << std::endl;
				std::wcout << L"country: " << Bc->GetInfo(L"country") << std::endl;
				std::wcout << L"id:      " << Bc->GetInfo(L"id") << std::endl;
				std::wcout << L"issued:  " << Bc->GetInfo(L"issued") << std::endl;
				std::wcout << L"expires: " << Bc->GetInfo(L"expires") << std::endl;
			}
		}
	}
	catch (ExceptionCI ex)	{
		std::wcout << L"ERROR: " << ex.getMessage() << std::endl;
	}
}
```

### Read from a multi-page image file

This example reads barcodes from a single-page file or all pages of a multi-page image file.  &#x20;

```cpp
void ReadBarcode1D_file(const CiServer& Server, const std::wstring& fileName)
{
	try {
		// configure reader
		CiBarcodePro reader = Server->CreateBarcodePro();
		reader->Type = (FBarcodeType)(cibfCode39 | cibfCode128);
		int page = 1;
		while (true) {
			reader->Image->Open(fileName, page);
			reader->Find(0);
			for (int i = 1; i <= reader->Barcodes->Count; i++)
				std::wcout << "Page: " << page << " Text:  "  << reader->Barcodes->Item[i-1]->Text << std::endl;
			page++;	
			if (page > reader->Image->PageCount) 
				break;
		}
	}
	catch (ExceptionCI ex)
	{
		std::wcout << L"ERROR: " << ex.getMessage() << std::endl;
	}
}
```

### Optimize barcode reading

This [page](docId\:SmK4-LGRtfEx0zacVU2LV) describes methods for improving the recognition rate and/or speed

## Image Processing

### Repair image page

This example uses some of the most popular image-processing functions. In a production application, the specific sequence of functions should be selected based on specific application needs. 

```cpp
void RepairPage(const CiRepair& repair)
{
	repair->AutoDeskew();
	repair->AutoRotate();
	repair->AutoCrop(10, 10, 10, 10);         
	repair->Image->ToBitonal();               
	repair->BorderExtract(EBorderExtractFlags::ciBexBorderDeskewCrop,
			EBorderExtractAlgorithm::ciBeaCleaner); 
	repair->RemovePunchHoles();
	repair->SmoothCharacters(ESmoothType::ciSmoothDarkenEdges);
	repair->CleanNoiseExt ((ECleanNoiseFlags) (ciCnxBlackNoise | ciCnxWhiteNoise), 3, 3, 10); 
}

void Repair_page(const CiServer& Server, const std::wstring& fileName, const long page,
	const std::wstring& fileOut)
{
	try {
		CiRepair repair = Server->CreateRepair();
		repair->Image->Open(fileName, page);
		RepairPage(repair);
		repair->Image->SaveAs(fileOut, EFileFormat::ciEXT);
	}
	catch (ExceptionCI ex) {
		std::wcout << L"ERROR: " << ex.getMessage() << std::endl;
	}
}
```

### Repair all images in a multi-page file

This example repairs all images in a multi-page file and saves the result in the <font color="#eb144c">`fileOut`</font> file.

```cpp
using Inlite.ClearImageNet;

void Repair_file(const CiServer& Server, const std::wstring& fileName, const std::wstring& fileOut)
{
	try {
		CiRepair repair = Server->CreateRepair();
		int page = 1;
		while (true) {
			repair->Image->Open(fileName, page);
			RepairPage(repair);  // see code above
			if (page == 1) repair->Image->SaveAs(fileOut, EFileFormat::ciEXT);
			else repair->Image->Append(fileOut, EFileFormat::ciEXT);
			page++;	
			if (page > repair->Image->PageCount)
				break;
		}
	}
	catch (ExceptionCI ex) {
		std::wcout << L"ERROR: " << ex.getMessage() << std::endl;
	}
}
```







