![]() |
Multimedia »
General Graphics »
Barcodes
Intermediate
Drawing an EAN13 barcodeBy lam do thanhDrawing an EAN13 barcode based on Neil Van Eps' and Rainman_63's articles. |
VC6, Windows, MFC, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
I am very interested in Neil Van Eps' series of articles that shows how to draw barcodes. And I have been waiting for his next article since I had found his stuff on CodeProject. Actually, I need the articles for EAN8/13, UPCA/B. I searched in CodeProject, and found "Creating EAN-13 Barcodes with C#", by rainman_63. It's really helpful for me to understand clearly how to decode EAN barcodes.
I am not familiar with C#, and C/C++ is a unique language that I had known. So I was very confused to research rainman_63's C# project. However, his detailed guiding was enough for me to implement an EAN13 project with Visual C++. In this article, I shall not explain how the EAN13 encoding works, for further information, you can read rainman_63's article "Creating EAN-13 Barcodes with C#".
My CEAN13 is based on the CBarcode class, that has been introduced in Neil Van Eps' series of articles. See the class declaration below.
class CBarcode { public: CBarcode(); void LoadData(CString csMessage, double dNarrowBar, double dFinalHeight, HDC pDC, int nStartingXPixel, int nStartingYPixel, double dRatio = 1.0); virtual void DrawBitmap() = 0; virtual void BitmapToClipboard() = 0; virtual ~CBarcode(); long GetBarcodePixelWidth(); long GetBarcodePixelHeight(); protected: CString m_csMessage; HDC m_hDC; long m_nFinalBarcodePixelWidth; long m_nNarrowBarPixelWidth; long m_nPixelHeight; long m_nStartingXPixel; long m_nStartingYPixel; long m_nSymbology; long m_nWideBarPixelWidth; virtual void DrawPattern(CString csPattern) = 0; };
And here is the CEAN13 class declaration. It's implemented to draw an EAN13 barcode.
class CEAN13 : public CBarcode { public: CEAN13(); virtual ~CEAN13(); void LoadData(CString csMessage, double dNarrowBar, double dFinalHeight, long nGuardbarHeight, HDC hDC, int nStartingXPixel, int nStartingYPixel, double dRatio); void DrawBitmap(); void BitmapToClipboard(); long CalculateCheckSumDigit(); private: long m_nGuardbarHeight; CString RetrieveLeftOddParityPattern(int iNumber); CString RetrieveLeftEvenParityPattern(int iNumber); CString RetrieveRightPattern(int iNumber); CString RetrieveCountryCodePattern(int iNumber); void DrawPattern(CString csPattern); };
The UPC/EAN/JAN standards include specifications for some of the bars to extend below the main body of the bar code. These are called "guard" or "security" bars.
HDC hDC = GetDC()->m_hDC; double dHeight = 1.5; nGuardbarHeight = 20; double dRatio = 3.0; CEAN24 CodeEAN13; CodeEAN13.LoadData(strMessage,0.02,dHeight, nGuardbarHeight,hDC,0,0,dRatio); CodeEAN13.DrawBitmap();
The DrawBitmap() function draws all the characters of the message. It divides a message into five parts and draws each part in turn.
void CEAN13::DrawBitmap() { int i, tmpGuardBarHeight; DrawPattern("sssssssss"); // draw quite zone DrawPattern("bsb"); // draw lead CString strCountryCodePattern; strCountryCodePattern = RetrieveCountryCodePattern((int)m_csMessage.GetAt(0)-48); tmpGuardBarHeight = m_nGuardbarHeight; m_nGuardbarHeight = 0; DrawPattern(RetrieveLeftOddParityPattern((int)m_csMessage.GetAt(1)-48)); for (i = 2 ; i < 7 ; i ++){ if (strCountryCodePattern[i-2] == 'O') DrawPattern(RetrieveLeftOddParityPattern((int)m_csMessage.GetAt(i)-48)); if (strCountryCodePattern[i-2] == 'E') DrawPattern(RetrieveLeftEvenParityPattern((int)m_csMessage.GetAt(i)-48)); } m_nGuardbarHeight = tmpGuardBarHeight; DrawPattern("sbsbs"); // draw separator bars tmpGuardBarHeight = m_nGuardbarHeight; m_nGuardbarHeight = 0; for (i = 7 ; i < 12 ; i ++) DrawPattern(RetrieveRightPattern((int)m_csMessage.GetAt(i)-48)); DrawPattern(RetrieveRightPattern(CalculateCheckSumDigit())); m_nGuardbarHeight = tmpGuardBarHeight; DrawPattern("bsb"); // draw trailer bars DrawPattern("sssssssss"); // draw quite zone }
My coding could not be nice ... ;=) but I tried my best to inherit from Neil Van Eps' and rainman_63's articles. I joined CodeProject 2 years ago, but have not contributed anything. It's time to do something for my favorite website. I would like to thank Neil Van Eps and rainman_63, your contributions were very helpful for me, and for everybody who would be interested in barcode topics. Thanks for any feedback.
| You must Sign In to use this message board. | |||||||||||||||
|
|||||||||||||||
|
|||||||||||||||
|
|||||||||||||||
|
|||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 18 Oct 2005 Editor: Smitha Vijayan |
Copyright 2005 by lam do thanh Everything else Copyright © CodeProject, 1999-2009 Web11 | Advertise on the Code Project |