| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- #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<iLength_2;iLeft++, iRight--)
- {
- ctemp = str[iLeft];
- str.SetAt(iLeft, str[iRight]);
- str.SetAt(iRight, ctemp);
- }
- }
|