Take the following steps to create and run a program that demonstrates how to read barcodes. Remember that the purpose of the tutorials is to provide you with a quick and easy way to generate a Barcode program.
Start Microsoft Visual Studio.
Select the File->New menu option, click the "Project" menu.
From "Project Types" select "Other Languages" to expand it, then select "Visual C++" to expand it, then select "MFC". From the right window select "MFC Application".
In the Project Name dialog box, enter " BarcodeTutor".
In the Location dialog box, enter the <LEADTOOLS_INSTALLDIR>\Examples\ClassLibrary\MSVC directory of your LEAD installation. Then Click OK. Then click "Next".
Choose "Dialog based" and click "Finish".
Click the "Solution Explorer" tab, and then click the "BarcodeTutor" project to expand it. Click the Header files, then Open "BarcodeTutor.h".
Add the following line immediately before the class CBarcodeTutorApp declaration, (keep in mind that you may have to change the path to the header files):
#include <..\..\..\..\include\ClassLib\ltwrappr.h>
Click the "Class View" tab.
Click to open the "BarcodeTutor" classes branch.
Click "CBarcodeTutorApp", and then double click the CBarcodeTutorApp(void) constructor.
Add the following lines after //TODO: add construction code here:
LBase::LoadLibraries(LT_ALL_LEADLIB);
L_TCHAR * pszLicenseFile = L"Replace this with the path to the LEADTOOLS license file";
L_TCHAR * pszDeveloperKey = L"Replace this with your developer key";
LSettings::SetLicenseFile(pszLicenseFile, pszDeveloperKey);
Create a new file called Imports.cpp in place it beside your project files.
#include "StdAfx.h"
#if defined(WIN64)
#pragma comment(lib, "..\\..\\..\\..\\Lib\\CDLLVC10\\x64\\Ltwvc_x.lib")
#else
#pragma comment(lib, "..\\..\\..\\..\\Lib\\CDLLVC10\\Win32\\Ltwvc_u.lib")
#endif // #if defined(WIN64)
Click on the "Class View" tab.
Right click "CBarcodeTutorDlg" and select Add "Add Variable...".
For Variable Type enter LBarCode, and for Variable Declaration put m_Barcode. Leave Access as "Public" and click OK.
Right click "CBarcodeTutorDlg" and select Add "Add Variable...".
For Variable Type enter LBitmapBase, and for Variable Declaration put m_Bitmap. Leave Access as "Public" and click OK.
Click on the "Solution Explorer" tab.
Double-click the "BarcodeTutor" folder to open it.
Double-click the "Resource Files" folder to open it. Then double click "BarcodeTutor.rc" file to open it, then double click "Dialog", and then double click "IDD_BARCODETUTOR_DIALOG".
Now, drag and drop 2 buttons, and change their properties as follows:
Button | ID | Caption |
---|---|---|
Button1 | IDC_LOAD | Load Image |
Button2 | IDC_READ_BARCODE | Read Barcode |
From View menu, select "Other Windows" menu, then select "Resource View" menu, then select Dialog, and select "IDD_BARCODETUTOR_DIALOG".
Double click the "Load Image" button, and add the following code:
m_Bitmap.SetFileName(MAKE_IMAGE_PATH(TEXT("barcode1.tif")));
m_Bitmap.Load();
From View menu, select "Other Windows" menu, then select "Resource View" menu, then select Dialog, and select "IDD_BARCODETUTOR_DIALOG"
Double click the "Read Barcode" button, and add the following code:
BARCODE1D Bar1d;
memset(&Bar1d, 0, sizeof(BARCODE1D));
Bar1d.uStructSize = sizeof(BARCODE1D);
Bar1d.bErrorCheck = TRUE;
Bar1d.nGranularity = 9;
Bar1d.nDirection = BARCODE_DIR_HORIZONTAL;
BARCODEREADOPT BarReadOpts;
memset(&BarReadOpts, 0, sizeof(BARCODEREADOPT));
BarReadOpts.ulSearchType = BARCODE_1D_READ_ANYTYPE;
BarReadOpts.nUnits = BARCODE_SCANLINES_PER_PIXELS;
BarReadOpts.ulFlags = BARCODE_BLOCK_SEARCH;
BarReadOpts.nMultipleMax = 0;
SetRect(&BarReadOpts.rcSearch, 0, 0, 0, 0);
BarReadOpts.bUseRgn = FALSE;
BarReadOpts.BarColor.uStructSize = sizeof(BARCODECOLOR);
BarReadOpts.BarColor.dwColorBar = RGB(0, 0, 0);
BarReadOpts.BarColor.dwColorSpace = RGB(255, 255, 255);
m_Barcode.SetReadOptions(&BarReadOpts);
m_Barcode.SetBitmap(&m_Bitmap);
// The Read method will search for all linear barcodes in the whole bitmap
L_INT nRet = m_Barcode.Read(BARCODES_1D, &Bar1d, NULL);
if (nRet > 0)
{
pBARCODEDATA pBarData = NULL;
L_INT nCount = nRet;
L_CHAR szBuffer[MAX_PATH];
for (L_INT i=0; i<nCount; i++)
{
pBarData = m_Barcode.GetBarCodeDataItem(i);
memset(szBuffer, 0, MAX_PATH);
wsprintfA(szBuffer,
"Barcode # %d\nData %s\nLeft %d\nTop %d\nRight %d\nBottom %d\n",
i+1,
pBarData->pszBarCodeData,
pBarData->rcBarLocation.left,
pBarData->rcBarLocation.top,
pBarData->rcBarLocation.right,
pBarData->rcBarLocation.bottom);
MessageBoxA(m_hWnd, szBuffer, ("Barcode Data"), MB_OK);
}
}
else
AfxMessageBox(TEXT("Failure while reading linear barcodes"));
Compile and test the program.