#include "StdAfx.h" #include ".\eanbarcode.h" // binary representation of numbers in EAN char* szEANs [10] = {"0001101", "0011001", "0010011", "0111101", "0100011", "0110001", "0101111", "0111011", "0110111", "0001011"}; CEANBarCode::CEANBarCode(void) { memset(m_szCode, '\0', 14); // This is an initial value, could be anything, product i mean. SetCode("7501031311309"); } CEANBarCode::~CEANBarCode(void) { } void CEANBarCode::SetCode(CString Code) { // this for making sure that the code is 13 length. while(Code.GetLength()<13) Code.Insert(0,'0'); strcpy(m_szCode, Code.GetBuffer()); Code.ReleaseBuffer(); } CString CEANBarCode::GetCode() { CString Code; Code = m_szCode; return Code; } //////////////////////////////////////// // Check the validity of a the code // //////////////////////////////////////// bool CEANBarCode::CheckCode() { int Sum = 0; for(int i=2;i<=12;i+=2) Sum += GetAt(i) * 3; for(int i=1;i<=12;i+=2) Sum += GetAt(i); if(10-(Sum%10)==GetAt(13)) return true; return false; } ////////////////////////////////////////////////////////////////////////// // Get a Number in the code by its position, starting from the left // ////////////////////////////////////////////////////////////////////////// int CEANBarCode::GetAt(int Position) { char c[2]; c[0] = m_szCode[Position-1]; c[1] = '\0'; return atoi(c); } //////////////////////////////////////////////////////////////////////////////////////// // Gets the binary string representing the left part of the code when even parity // //////////////////////////////////////////////////////////////////////////////////////// CString CEANBarCode::GetLeftEven(int Digit) { CString sBuffer; sBuffer = GetRight(Digit); ReverseString(sBuffer); return sBuffer; } /////////////////////////////////////////////////////////////////////// // Gets the binary string representing the right part of the code // /////////////////////////////////////////////////////////////////////// CString CEANBarCode::GetRight(int Digit) { CString sBuffer; sBuffer = GetLeftOdd(Digit); for(int i=0;i<7;i++) { if(sBuffer[i]=='0') sBuffer.SetAt(i, '1'); else sBuffer.SetAt(i, '0'); } return sBuffer; } ////////////////////////////////////////////////////////////////////////////////////// // Gets the binary string representing the left part of the code when odd parity // ////////////////////////////////////////////////////////////////////////////////////// CString CEANBarCode::GetLeftOdd(int Digit) { CString sBuffer; // gets the digit binary representation from the static array szEANs sBuffer = szEANs[Digit]; return sBuffer; } /////////////////////////////////////////////////////////////// // Draws the code using the specified DC and the rectangle // /////////////////////////////////////////////////////////////// void CEANBarCode::Draw(CPaintDC &dc) { int PenWidth = 2; CBrush BlackBrush(RGB(0,0,0)); CBrush WhiteBrush(RGB(255,255,255)); char N[2]; CString sBuffer; int X, i, x, y, j; RECT Rect; if(!CheckCode()) { CString str; str = "Invalid Code"; dc.TextOut(50, 50, str); return; } X = 10; // draw odd Rect.top=20; Rect.bottom=120; Rect.left=X; Rect.right=X+PenWidth; memset(N,'\0',2); N[0] = GetAt(1)+'0'; dc.TextOut(X-10,Rect.bottom-Rect.top,N,1); dc.FillRect(&Rect,&BlackBrush); Rect.left+=PenWidth; Rect.right+=PenWidth; dc.FillRect(&Rect,&WhiteBrush); Rect.left+=PenWidth; Rect.right+=PenWidth; dc.FillRect(&Rect,&BlackBrush); Rect.left+=PenWidth; Rect.right+=PenWidth; for(i=2;i<=7;i++) { Rect.bottom=100; memset(N,'\0',2); N[0] = GetAt(i)+'0'; x = Rect.left+PenWidth*4; y = Rect.bottom+10; dc.TextOut(x,y,N,1); if(i%2==0) sBuffer = GetLeftOdd(GetAt(i)); else sBuffer = GetLeftEven(GetAt(i)); for(j=0;j<7;j++) { if(sBuffer[j]=='1') dc.FillRect(&Rect,&BlackBrush); else dc.FillRect(&Rect,&WhiteBrush); Rect.left+=PenWidth; Rect.right+=PenWidth; } } Rect.bottom=120; dc.FillRect(&Rect,&WhiteBrush); Rect.left+=PenWidth; Rect.right+=PenWidth; dc.FillRect(&Rect,&BlackBrush); Rect.left+=PenWidth; Rect.right+=PenWidth; dc.FillRect(&Rect,&WhiteBrush); Rect.left+=PenWidth; Rect.right+=PenWidth; dc.FillRect(&Rect,&BlackBrush); Rect.left+=PenWidth; Rect.right+=PenWidth; dc.FillRect(&Rect,&WhiteBrush); Rect.left+=PenWidth; Rect.right+=PenWidth; for(i=8;i<=13;i++) { Rect.bottom=100; memset(N,'\0',2); N[0] = GetAt(i)+'0'; x = Rect.left; y = Rect.bottom+10; dc.TextOut(x,y,N,1); sBuffer = GetRight(GetAt(i)); for(j=0;j<7;j++) { if(sBuffer[j]=='1') dc.FillRect(&Rect,&BlackBrush); else dc.FillRect(&Rect,&WhiteBrush); Rect.left+=PenWidth; Rect.right+=PenWidth; } } Rect.bottom=120; dc.FillRect(&Rect,&BlackBrush); Rect.left+=PenWidth; Rect.right+=PenWidth; dc.FillRect(&Rect,&WhiteBrush); Rect.left+=PenWidth; Rect.right+=PenWidth; dc.FillRect(&Rect,&BlackBrush); Rect.left+=PenWidth; Rect.right+=PenWidth; WhiteBrush.DeleteObject(); BlackBrush.DeleteObject(); } ////////////////////////// // reverse a String // ////////////////////////// void CEANBarCode::ReverseString(CString &str) { int iRight, iLeft, iLength_2; char ctemp; iLength_2 = (str.GetLength() / 2); for(iLeft=0, iRight=str.GetLength()-1;iLeft