This tutorial shows how to create a barcode by encoding raw binary data, then how to decode that data from the barcode in a Windows C DLL application using the LEADTOOLS SDK. This can be useful when using characters from extended character sets.
Overview | |
---|---|
Summary | This tutorial covers how to encode and decode raw binary data from a barcode in a Windows C DLL Application. |
Completion Time | 30 minutes |
Visual Studio Project | Download tutorial project (19 KB) |
Platform | Windows C DLL Application |
IDE | Visual Studio 2017, 2019 |
Development License | Download LEADTOOLS |
Try it in another language |
|
Get familiar with the basic steps of creating a project and loading/displaying images by reviewing the Add References and Set a License and Load, Display, and Save Images tutorials, before working on the Read and Write Bytes in a Barcode - Windows C DLL tutorial.
Start with a copy of the project created in the Load, Display, and Save Images tutorial. If the project is not available, create it by following the steps in that tutorial.
To read and write barcodes using LEADTOOLS, add the required header and library files. Open the pre-compiled header file (either pch.h
or stdafx.h
, depending on the version of Visual Studio used) and add the following lines:
#include "C:\LEADTOOLS21\Include\Ltbar.h"
#pragma comment (lib, "C:\\LEADTOOLS21\\Lib\\CDLL\\x64\\Ltbar_x.lib") // Barcode support
Note
For a complete list of DLLs that are required for your application, refer to Files to be Included with your Application - C API.
The License unlocks the features needed for the project. It must be set before any toolkit functionality is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.
There are two types of runtime licenses:
Note
Adding LEADTOOLS references and setting a license are covered in more detail in the Add References and Set a License tutorial.
With the project created, the references added, the license set, and the load image code added, coding can begin.
In the Solution Explorer, double-click the resources file (.rc).
Add a new &Barcode drop down menu next to the File menu. Add two menu items in the table below to the newly-added &Barcode menu.
Menu Item Text | Item ID |
---|---|
&Encode Data | ID_BARCODE_ENCODEDATA |
&Decode Data | ID_BARCODE_DECODEDATA |
Navigate to the WndProc
function, and under the switch (wmId)
statement below the WM_COMMAND
case, add two new cases below.
switch (wmId)
{
case ID_BARCODE_ENCODEDATA:
{
wchar_t strUnicodeTxt[] = L"This is a Unicode string. It can be in any Unicode-supported language, which means it can include non-ASCII characters.";
CreateBarcode(hWnd, (BYTE*)strUnicodeTxt, sizeof strUnicodeTxt);
}
break;
case ID_BARCODE_DECODEDATA:
{
BYTE* pData = NULL;
L_INT nDataSize = 0;
ReadBarcode(hWnd, &pData, &nDataSize);
if (pData)
{
if(IsTextUnicode(pData, nDataSize, NULL))
MessageBoxW(hWnd, (wchar_t*)pData, L"Decoded Text", 0);
else
MessageBox(hWnd, TEXT("Data doesn't appear to be Unicode string"), TEXT("Barcode Tutorial"), MB_ICONERROR);
delete pData; // Must free it here, since it was allocated inside the ReadBarcode() function.
}
}
break;
// Keep rest of the code as is
Add two new functions above the WndProc
function named CreateBarcode
and ReadBarcode
.
Add the below code to the CreateBarcode
function to create a new QR barcode.
void CreateBarcode(HWND hwnd, BYTE * pData, L_INT nDataLen)
{
if (LEADBmp.Flags.Allocated)
L_FreeBitmap(&LEADBmp);
L_CreateBitmap(&LEADBmp, sizeof BITMAPHANDLE, TYPE_CONV, 2000, 1600, 24, ORDER_BGR, NULL, BOTTOM_LEFT, NULL, 0);
L_FillBitmap(&LEADBmp, RGB(255, 255, 255));
if (L_BarCodeInit(BARCODES_QR_WRITE) != SUCCESS)
return;
BARCODEDATA BarCodeData = { 0 };
BARCODECOLOR BarColor = { 0 };
BARCODEWRITEQR BarCodeQR = { 0 };
BarColor.uStructSize = sizeof(BARCODECOLOR);
BarColor.dwColorBar = RGB(0, 0, 0);
BarColor.dwColorSpace = RGB(255, 255, 255);
BarCodeData.uStructSize = sizeof(BarCodeData);
BarCodeData.rcBarLocation.left = 50;
BarCodeData.rcBarLocation.top = 250;
BarCodeData.rcBarLocation.right = 650;
BarCodeData.rcBarLocation.bottom = 550;
BarCodeData.nUnits = BARCODE_SCANLINES_PER_PIXELS;
BarCodeData.ulType = BARCODE_QR_M1_DEF; // Default Model 1 QR Code size
BarCodeData.pszBarCodeData = (L_CHAR*)pData;
BarCodeData.nSizeofBarCodeData = nDataLen;
BarCodeQR.uStructSize = sizeof(BarCodeQR);
BarCodeQR.ulFlags = BARCODE_JUSTIFY_H_CENTER | BARCODE_JUSTIFY_V_CENTER;
//Write the barcode
if (SUCCESS == L_BarCodeWrite(&LEADBmp, &BarCodeData, &BarColor, BARCODE_USECOLORS, NULL, NULL, NULL, &BarCodeQR, NULL, NULL, NULL, NULL))
InvalidateRect(hwnd, NULL, TRUE);
else
MessageBox(hwnd, TEXT("Could Not Write QR Barcode"), TEXT("Barcode Tutorial"), MB_ICONERROR);
L_BarCodeExit();
}
Add the below code to the ReadBarcode
function to recognize and decode the created QR barcode.
void ReadBarcode(HWND hwnd, BYTE** ppData, L_INT * pDataSize)
{
*pDataSize = 0;
if (L_BarCodeInit(BARCODES_QR_READ) != SUCCESS)
return;
pBARCODEDATA pBarCodes = NULL;
BARCODECOLOR BarColor = { 0 };
BarColor.uStructSize = sizeof BARCODECOLOR;
BarColor.dwColorBar = RGB(0, 0, 0);
BarColor.dwColorSpace = RGB(255, 255, 255);
// Search for QR barcodes in the entire bitmap
if (SUCCESS == L_BarCodeRead(&LEADBmp, NULL, BARCODE_QR_CODE, BARCODE_SCANLINES_PER_PIXELS, BARCODE_BLOCK_SEARCH, 0, NULL, NULL, &BarColor, &pBarCodes, sizeof BARCODEDATA))
{
if (pBarCodes->ulType & BARCODE_QR_DEF)
{
BYTE* pData = new BYTE[pBarCodes->nSizeofBarCodeData];
CopyMemory(pData, pBarCodes->pszBarCodeData, pBarCodes->nSizeofBarCodeData);
*ppData = pData; // IMPORTANT: must be freed by caller
*pDataSize = pBarCodes->nSizeofBarCodeData;
}
else
MessageBox(hwnd, TEXT("Barcode is not QR type"), TEXT("Barcode Tutorial"), MB_ICONERROR);
}
else
MessageBox(hwnd, TEXT("Could not read QR Barcode"), TEXT("Barcode Tutorial"), MB_ICONERROR);
if (pBarCodes)
L_BarCodeFree(&pBarCodes);
L_BarCodeExit();
}
Run the project by pressing F5, or by selecting Debug -> Start Debugging.
If the steps were followed correctly, the application should run. Select Barcode -> Encode Data to create an image and write on it a QR barcode containing the data. Select Barcode -> Decode Data to have the application run barcode recognition, then decode the contents of the QR code and if the data represents a Unicode string, display the text.
This tutorial covered how to write and read binary data in QR barcodes using the LEADTOOLS SDK in a Windows API application. It also covered how to use the BARCODEDATA
structure with the L_BarCodeRead
and L_BarCodeWrite
functions.