This tutorial shows how to write 1D and 2D barcode information on a loaded image using the LEADTOOLS SDK in a Windows C DLL application.
Overview | |
---|---|
Summary | This tutorial covers how to use the L_BarCodeWrite function 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 by reviewing the Add References and Set a License and Detect and Extract Barcodes tutorials before working on the Write 1D and 2D Barcodes to an Image - Windows C DLL tutorial.
Start with a copy of the 64-bit Windows API project created in the Detect and Extract Barcodes - Windows C DLL tutorial. If the project is not available, create it by following the steps in that tutorial.
Ensure that the following required header and DLL files are included in the pre-compiled header file (either pch.h
or stdafx.h
, depending on the version of Visual Studio used).
#include "C:\LEADTOOLS23\Include\L_Bitmap.h" // use actual path where you installed LEADTOOLS
#pragma comment (lib, "C:\\LEADTOOLS23\\Lib\\CDLL\\x64\\Ltkrn_x.lib")
#pragma comment (lib, "C:\\LEADTOOLS23\\Lib\\CDLL\\x64\\Ltfil_x.lib") // file loading and saving
#pragma comment (lib, "C:\\LEADTOOLS23\\Lib\\CDLL\\x64\\Ltdis_x.lib") // image display
#include "C:\LEADTOOLS23\Include\Ltbar.h"
#pragma comment (lib, "C:\\LEADTOOLS23\\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, display, barcode, and save code added, coding can begin.
The steps below are for Visual Studio 2019; they could be different for other versions of Visual Studio.
Go to the Solution Explorer and double-click the resources file (.rc).
Add a new &Write Barcode menu item to the Barcode drop down menu. The new item's ID should be ID_BARCODE_WRITEBARCODE
.
Go to the WndProc
function and under the switch (wmId)
statement that is below the WM_COMMAND
case, add a new case:
switch (wmId)
{
case ID_BARCODE_WRITEBARCODE:
if (LEADBmp.Flags.Allocated)
{
Write1DBarcode(hWnd);
Write2DBarcode(hWnd);
}
else
MessageBox(hWnd, TEXT("Cannot write barcodes. No image loaded"), TEXT("Barcode Demo"), MB_ICONERROR);
break;
// keep rest of the code as is
Add the below code to the 2 new barcode writing functions. These functions can be placed above the WndProc
function:
void Write1DBarcode(HWND hwnd)
{
if (L_BarCodeInit(BARCODES_1D) != SUCCESS)
return;
BARCODEDATA BarCodeData = { 0 };
BARCODECOLOR BarColor = { 0 };
BARCODE1D BarCode1D = { 0 };
L_INT nRet;
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 = 50;
BarCodeData.rcBarLocation.right = 650;
BarCodeData.rcBarLocation.bottom = 150;
BarCodeData.nUnits = BARCODE_SCANLINES_PER_PIXELS;
BarCodeData.ulType = BARCODE_1D_CODE_128;
char szDataString[] = "Code 128 by LEADTOOLS";
BarCodeData.pszBarCodeData = szDataString;
BarCodeData.nSizeofBarCodeData = (L_INT)strlen(BarCodeData.pszBarCodeData);
BarCode1D.uStructSize = sizeof(BARCODE1D);
BarCode1D.bErrorCheck = TRUE;
BarCode1D.bOutShowText = TRUE;
//Write the barcode
nRet = L_BarCodeWrite(&LEADBmp, &BarCodeData, &BarColor, BARCODE_USECOLORS, &BarCode1D, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
if (SUCCESS == nRet)
InvalidateRect(hwnd, NULL, TRUE);
else
MessageBox(hwnd, TEXT("Could Not Write 1D Barcode"), TEXT("Barcode Demo"), MB_ICONERROR);
L_BarCodeExit();
}
void Write2DBarcode(HWND hwnd)
{
if (L_BarCodeInit(BARCODES_QR_WRITE) != SUCCESS)
return;
BARCODEDATA BarCodeData = { 0 };
BARCODECOLOR BarColor = { 0 };
BARCODEWRITEQR BarCodeQR = { 0 };
L_INT nRet;
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
char szDataString[] = "https://www.leadtools.com/help/sdk/dh/to/qr-code-barcodes-in-leadtools.html";
BarCodeData.pszBarCodeData = szDataString;
BarCodeData.nSizeofBarCodeData = (L_INT)strlen(BarCodeData.pszBarCodeData);
BarCodeQR.uStructSize = sizeof(BarCodeQR);
BarCodeQR.ulFlags = BARCODE_JUSTIFY_H_CENTER | BARCODE_JUSTIFY_V_CENTER;
//Write the barcode
nRet = L_BarCodeWrite(&LEADBmp, &BarCodeData, &BarColor, BARCODE_USECOLORS, NULL, NULL, NULL, &BarCodeQR, NULL, NULL, NULL, NULL);
if (SUCCESS == nRet)
InvalidateRect(hwnd, NULL, TRUE);
else
MessageBox(hwnd, TEXT("Could Not Write 2D Barcode"), TEXT("Barcode Demo"), MB_ICONERROR);
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 File -> Open to load the image you would like to write the barcode to. Select Barcode -> Write Barcode to have the application write 2 new barcodes on the image.
The LeadRect
bounds can be adjusted to select where the barcode is written on the image. Also, the barcode symbology to be written can be changed by modifying the value of BarCodeData.ulType
.
This tutorial covered how to write 2 types of barcodes on the loaded image using the LEADTOOLS SDK in a Windows C API application.