In my other life, I’m an independent book publisher who lives or dies by the readability of my EAN Bookland barcodes. So I understand the importance of barcodes. And given that a barcode wand can be had for as little as $100, you have every reason to get on the barcode bandwagon too.
This article examines the TBarCode DLL component from Tec-It, a barcoding solution you can use to print reports or stickers that will verify inventory when scanned.
Tec-It and The Three Bars
TBarCode DLL is just one of a family of three barcoding solutions offered by Tec-It. The other two are TBarCode OCX and BarCode Studio. TBarCode OCX is a Microsoft ActiveX-compliant barcode control that combines all common linear and 2D barcodes (Code 39, Code 128, UCC/EAN Codes, Code 25 Interleaved, MaxiCode, PDF417, MicroPDF, Data Matrix (ECC200), QR-Code, Codablock-F, and many others) into one product. The OCX is .NET compatible and can be integrated into Microsoft Office products, ASP, Visual Basic, and other ActiveX-hosted environments.TBarCode DLL offers all the same functionality as TBarCode OCX plus EAN/UCC Composite Symbology, MicroPDF, and all RSS Composite variants.
BarCode Studio is a complete end-user application for printing or exporting barcode images and is based on the Tec-It components.
Getting Started—Hello Barcode!
Tec-It offers a downloadable evaluation version of TBarCode OCX, which includes help files and three sample programs in Visual C++ 6.0 project format. The Tec-It installer organizes icons and folders for all of these items. If that’s not enough for you, you can download another set of about two dozen samples that highlight integration in various environments such as Crystal Reports, Internet Explorer 5, ASP, and other OCX hosts.My first test was to load up the BarCode Viewer app (“BCVIEW”) using Visual Studio 7.1. BCVIEW enables you to enter some piece of data and then display it on the screen, copy it to the clipboard, and/or save it as a bitmap type file. I entered the EAN-13 barcode for Beyond Trauma, 2nd Ed. (ISBN-13 data value 9781932690040), one of my own titles from 2005. The app compiled and ran with only a few minor compile warnings, and produced the following result:
Your First Barcode App
The demo program is nice, but let’s see some source code and how it really works. Breaking it down to the simplest level, we will look at Tec-It’s simplest provided C++ application. I’m a command-line kind of guy, so I used the following to compile and link in one step:
cl TBCDLLSample.cpp /Z7 /link /DEBUG TBarCode6.lib gdi32.lib user32.lib
Then, when I ran the app, the first thing it complained about was not finding TBarCode6.OCX. I located a copy of the OCX in the redist directory and punched in the OCX as follows:
regsvr32 Tbarcode6.ocx
This program is the smallest useful sample you can write. It performs the following tasks:
- Creates a Code128 barcode
- Draws the barcode onto a bitmap
- Burns the bitmap into a .JPG file
The following section goes through several areas of the program and makes some notes on what is happening in each:
1 // TBCDLLSample.cpp: Defines the entry point for the console application.
2 //
3
4 #include “stdafx.h”
5 #include <Windows.h>
6 #include <WTypes.h>
7
8 // include the header files for TBARCODE DLL
9 #define TECIT_DLLIMPORT
10 #include “TBarCodeDLL.h”
11 #undef TECIT_DLLIMPORT
12
13
14 /// include the tbarcode5.lib file to the linker section
15
16 //////////////////////////////////////////////////////////////////
17 // TBarCode DLL Sample
18 //////////////////////////////////////////////////////////////////
19 int main(int argc, char* argv[]) {
20 ERRCODE eCode = S_OK;
21 LOGFONTA* lf = NULL; // used to change font of barcode
22 t_BarCode* pBC;
23 char* szText;
24 INT nModules;
25
26 printf(“TBarCode DLL Samplen”);
27 printf(“——————-nn”);
28 printf(“This program will create a sample barcode and save it as “c:bitmap.jpg”.n”);
29 printf(“Barcode type: Code128n”);
30 printf(“Encoded data: HELLO WORLDnn”);
31
32 // License at startup of your application; get your license key from http://www.tec-it.com/order/
33 // BCLicenseMe( “Licensee String”, eKindOfLicense, 1, “Your Key”, eProductID); // for enums refer to TECBCEnum.h
34
35 // allocate memory for internal barcode structure
36 BCAlloc (&pBC);
37
38 // set the barcode type to Code 128
39 // refer to TECBCEnum.h (or the documentation)
40 eCode = BCSetBCType (pBC, eBC_Code128);
41
42 if (eCode == S_OK) {
43 // set the barcode data to “HELLO WORLD”
44 szText = “HELLO WORLD”;
45 eCode = BCSetText(pBC, szText, strlen(szText));
46
47 // change the font
48 lf = BCGetLogFont(pBC);
49 lf->lfHeight = 10;
50 strcpy(lf->lfFaceName, “Arial”); // face name must not exceed 31 characters
51
52 // Use this commands if you want to change the module width:
53 // BCSetModWidth (pBC, “254”); // Set module width to 0.254 mms
54 }
55
56 // check if the input data contains valid characters (for the selected barcode type)
57 if (eCode == S_OK)
58 eCode = BCCheck(pBC);
59
60 // compute the check digits (depends on barcode and CD-method)
61 if (eCode == S_OK)
62 eCode = BCCalcCD(pBC);
63
64 // prepare the internal barcode info-structure to be drawn
65 if (eCode == S_OK)
66 eCode = BCCreate(pBC);
67
68 // get number of horizontal barcode elements
69 if (eCode == S_OK)
70 nModules = (INT) BCGetCountModules (pBC);
71
72 // draw barcode to device context
73 if (eCode == S_OK) {
74 RECT rect;
75 HDC dc;
76 HBITMAP BM;
77
78 dc = CreateCompatibleDC(NULL);
79 BM = CreateCompatibleBitmap (dc, nModules * 2, 150); // area of Pixels
80 SelectObject ( dc, BM );
81
82 rect.left = 0;
83 rect.bottom = 0;
84 rect.right = 5000;
85 rect.top = 3000;
86
87 SetMapMode(dc, MM_HIMETRIC);
88 OffsetRect(&rect,100, 100);
89
90 eCode = BCDraw(pBC, dc, &rect); // draw barcode into device context
91 DeleteObject (BM);
92 DeleteDC(dc);
93 }
94
95 // save barcode to image file
96 if (eCode == S_OK) {
97 // use number of horizontal elements as for the x dimension of the image
98 // this avoids aliasing effects and ensures readability of barcode!
99 eCode = BCSaveImage(pBC, “c:bitmap.jpg”, eIMJpg, nModules, 150, 96, 96);
100 }
101
102 // show error message on demand
103 if (eCode != S_OK) {
104 // if an error occurred, display more info about the error
105 char szErr[32] = {0};
106 //ZeroMemory(szText, sizeof(szText));
107 BCGetErrorText(eCode, szErr, sizeof(szErr));
108 MessageBox(NULL, szErr, “Error creating barcode:”, MB_OK);
109 }
110
111 // Important: frees the allocated memory of the barcode
112 BCFree(pBC);
113
114 printf(“done.n”);
115 return eCode;
116 }
Boost to Human Productivity
TBarCode DLL and OCX solve a complex problem with a simple interface. If you need to print reports or stickers that can be scanned in the field to verify the presence of a part or piece of inventory, the lowly barcode can be a big boost to human productivity and cut down on data-entry errors and operator fatigue.About the Author
Victor Volkman has been writing for C/C++ Users Journal and other programming journals since the late 1980s. He is a graduate of Michigan Tech and a faculty advisor board member for Washtenaw Community College CIS department. Volkman is the editor of numerous books, including C/C++ Treasure Chest and is the owner of Loving Healing Press. He can help you in your quest for open source tools and libraries, just drop an e-mail to sysop@HAL9K.com.