EANBarCode.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #include "StdAfx.h"
  2. #include ".\eanbarcode.h"
  3. // binary representation of numbers in EAN
  4. char* szEANs [10] = {"0001101", "0011001", "0010011", "0111101", "0100011",
  5. "0110001", "0101111", "0111011", "0110111", "0001011"};
  6. CEANBarCode::CEANBarCode(void)
  7. {
  8. memset(m_szCode, '\0', 14);
  9. // This is an initial value, could be anything, product i mean.
  10. SetCode("7501031311309");
  11. }
  12. CEANBarCode::~CEANBarCode(void)
  13. {
  14. }
  15. void CEANBarCode::SetCode(CString Code)
  16. {
  17. // this for making sure that the code is 13 length.
  18. while(Code.GetLength()<13)
  19. Code.Insert(0,'0');
  20. strcpy(m_szCode, Code.GetBuffer());
  21. Code.ReleaseBuffer();
  22. }
  23. CString CEANBarCode::GetCode()
  24. {
  25. CString Code;
  26. Code = m_szCode;
  27. return Code;
  28. }
  29. ////////////////////////////////////////
  30. // Check the validity of a the code //
  31. ////////////////////////////////////////
  32. bool CEANBarCode::CheckCode()
  33. {
  34. int Sum = 0;
  35. for(int i=2;i<=12;i+=2)
  36. Sum += GetAt(i) * 3;
  37. for(int i=1;i<=12;i+=2)
  38. Sum += GetAt(i);
  39. if(10-(Sum%10)==GetAt(13))
  40. return true;
  41. return false;
  42. }
  43. //////////////////////////////////////////////////////////////////////////
  44. // Get a Number in the code by its position, starting from the left //
  45. //////////////////////////////////////////////////////////////////////////
  46. int CEANBarCode::GetAt(int Position)
  47. {
  48. char c[2];
  49. c[0] = m_szCode[Position-1];
  50. c[1] = '\0';
  51. return atoi(c);
  52. }
  53. ////////////////////////////////////////////////////////////////////////////////////////
  54. // Gets the binary string representing the left part of the code when even parity //
  55. ////////////////////////////////////////////////////////////////////////////////////////
  56. CString CEANBarCode::GetLeftEven(int Digit)
  57. {
  58. CString sBuffer;
  59. sBuffer = GetRight(Digit);
  60. ReverseString(sBuffer);
  61. return sBuffer;
  62. }
  63. ///////////////////////////////////////////////////////////////////////
  64. // Gets the binary string representing the right part of the code //
  65. ///////////////////////////////////////////////////////////////////////
  66. CString CEANBarCode::GetRight(int Digit)
  67. {
  68. CString sBuffer;
  69. sBuffer = GetLeftOdd(Digit);
  70. for(int i=0;i<7;i++)
  71. {
  72. if(sBuffer[i]=='0')
  73. sBuffer.SetAt(i, '1');
  74. else
  75. sBuffer.SetAt(i, '0');
  76. }
  77. return sBuffer;
  78. }
  79. //////////////////////////////////////////////////////////////////////////////////////
  80. // Gets the binary string representing the left part of the code when odd parity //
  81. //////////////////////////////////////////////////////////////////////////////////////
  82. CString CEANBarCode::GetLeftOdd(int Digit)
  83. {
  84. CString sBuffer;
  85. // gets the digit binary representation from the static array szEANs
  86. sBuffer = szEANs[Digit];
  87. return sBuffer;
  88. }
  89. ///////////////////////////////////////////////////////////////
  90. // Draws the code using the specified DC and the rectangle //
  91. ///////////////////////////////////////////////////////////////
  92. void CEANBarCode::Draw(CPaintDC &dc)
  93. {
  94. int PenWidth = 2;
  95. CBrush BlackBrush(RGB(0,0,0));
  96. CBrush WhiteBrush(RGB(255,255,255));
  97. char N[2];
  98. CString sBuffer;
  99. int X, i, x, y, j;
  100. RECT Rect;
  101. if(!CheckCode())
  102. {
  103. CString str;
  104. str = "Invalid Code";
  105. dc.TextOut(50, 50, str);
  106. return;
  107. }
  108. X = 10;
  109. // draw odd
  110. Rect.top=20;
  111. Rect.bottom=120;
  112. Rect.left=X;
  113. Rect.right=X+PenWidth;
  114. memset(N,'\0',2);
  115. N[0] = GetAt(1)+'0';
  116. dc.TextOut(X-10,Rect.bottom-Rect.top,N,1);
  117. dc.FillRect(&Rect,&BlackBrush);
  118. Rect.left+=PenWidth;
  119. Rect.right+=PenWidth;
  120. dc.FillRect(&Rect,&WhiteBrush);
  121. Rect.left+=PenWidth;
  122. Rect.right+=PenWidth;
  123. dc.FillRect(&Rect,&BlackBrush);
  124. Rect.left+=PenWidth;
  125. Rect.right+=PenWidth;
  126. for(i=2;i<=7;i++)
  127. {
  128. Rect.bottom=100;
  129. memset(N,'\0',2);
  130. N[0] = GetAt(i)+'0';
  131. x = Rect.left+PenWidth*4;
  132. y = Rect.bottom+10;
  133. dc.TextOut(x,y,N,1);
  134. if(i%2==0)
  135. sBuffer = GetLeftOdd(GetAt(i));
  136. else
  137. sBuffer = GetLeftEven(GetAt(i));
  138. for(j=0;j<7;j++)
  139. {
  140. if(sBuffer[j]=='1')
  141. dc.FillRect(&Rect,&BlackBrush);
  142. else
  143. dc.FillRect(&Rect,&WhiteBrush);
  144. Rect.left+=PenWidth;
  145. Rect.right+=PenWidth;
  146. }
  147. }
  148. Rect.bottom=120;
  149. dc.FillRect(&Rect,&WhiteBrush);
  150. Rect.left+=PenWidth;
  151. Rect.right+=PenWidth;
  152. dc.FillRect(&Rect,&BlackBrush);
  153. Rect.left+=PenWidth;
  154. Rect.right+=PenWidth;
  155. dc.FillRect(&Rect,&WhiteBrush);
  156. Rect.left+=PenWidth;
  157. Rect.right+=PenWidth;
  158. dc.FillRect(&Rect,&BlackBrush);
  159. Rect.left+=PenWidth;
  160. Rect.right+=PenWidth;
  161. dc.FillRect(&Rect,&WhiteBrush);
  162. Rect.left+=PenWidth;
  163. Rect.right+=PenWidth;
  164. for(i=8;i<=13;i++)
  165. {
  166. Rect.bottom=100;
  167. memset(N,'\0',2);
  168. N[0] = GetAt(i)+'0';
  169. x = Rect.left;
  170. y = Rect.bottom+10;
  171. dc.TextOut(x,y,N,1);
  172. sBuffer = GetRight(GetAt(i));
  173. for(j=0;j<7;j++)
  174. {
  175. if(sBuffer[j]=='1')
  176. dc.FillRect(&Rect,&BlackBrush);
  177. else
  178. dc.FillRect(&Rect,&WhiteBrush);
  179. Rect.left+=PenWidth;
  180. Rect.right+=PenWidth;
  181. }
  182. }
  183. Rect.bottom=120;
  184. dc.FillRect(&Rect,&BlackBrush);
  185. Rect.left+=PenWidth;
  186. Rect.right+=PenWidth;
  187. dc.FillRect(&Rect,&WhiteBrush);
  188. Rect.left+=PenWidth;
  189. Rect.right+=PenWidth;
  190. dc.FillRect(&Rect,&BlackBrush);
  191. Rect.left+=PenWidth;
  192. Rect.right+=PenWidth;
  193. WhiteBrush.DeleteObject();
  194. BlackBrush.DeleteObject();
  195. }
  196. //////////////////////////
  197. // reverse a String //
  198. //////////////////////////
  199. void CEANBarCode::ReverseString(CString &str)
  200. {
  201. int iRight, iLeft, iLength_2;
  202. char ctemp;
  203. iLength_2 = (str.GetLength() / 2);
  204. for(iLeft=0, iRight=str.GetLength()-1;iLeft<iLength_2;iLeft++, iRight--)
  205. {
  206. ctemp = str[iLeft];
  207. str.SetAt(iLeft, str[iRight]);
  208. str.SetAt(iRight, ctemp);
  209. }
  210. }