| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- #include "stdafx.h"
- #include "DrawBarCode.h"
- namespace DrawBarCodes
- {
- const int g_aryCode128[2][207] =
- {
- {
- 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
- 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
- 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
- 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
- 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
- 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106,
- },
- {
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
- 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
- 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
- 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
- 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
- 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106,
- },
- };
- //////////////////////////////////////////////////////////////////////////
- // CBarCodeBase;
- CBarCodeBase::CBarCodeBase()
- {
- Clear();
- m_nRatio = 3;
- }
- CBarCodeBase::~CBarCodeBase()
- {
- }
- void CBarCodeBase::operator =(const CBarCodeBase& bc)
- {
- m_nRatio = bc.m_nRatio;
- m_nBufferLen = bc.m_nBufferLen;
- memcpy(m_byBuffer, bc.m_byBuffer, bc.m_nBufferLen);
- }
- void CBarCodeBase::Clear()
- {
- memset(m_byBuffer, 0, m_nBufferLen);
- m_nBufferLen = 0;
- }
- int CBarCodeBase::GetEncodeLength() const
- {
- int nLen = 0;
- BYTE *pBuffer = (BYTE*)m_byBuffer;
- for ( int i = 0; i < m_nBufferLen; i++ )
- {
- if ( *pBuffer & 2 )
- nLen += (m_nRatio - 1);
- pBuffer++;
- }
- return nLen + m_nBufferLen;
- }
- int CBarCodeBase::GetBufferLength() const
- {
- return m_nBufferLen;
- }
- const BYTE& CBarCodeBase::GetAt(const int& nIndex) const
- {
- return m_byBuffer[nIndex];
- }
- int CBarCodeBase::GetRatio() const
- {
- return m_nRatio;
- }
- int CBarCodeBase::SetRatio(const int& nRatio)
- {
- m_nRatio = nRatio;
- if ( nRatio <= 0 )
- m_nRatio = 1;
- return m_nRatio;
- }
- void CBarCodeBase::DrawBarCode(IN HDC hDC, IN int nStartX, IN int nStartY, IN int nBarHeight, IN int nBarHeight2, IN const COLORREF clrBar, IN const COLORREF clrSpace, IN const int nPenWidth)
- {
- HPEN hPenBar =::CreatePen(PS_SOLID, nPenWidth, clrBar);
- HPEN hPenSpace =::CreatePen(PS_SOLID, nPenWidth, clrSpace);
- HPEN hPenOld = (HPEN)::SelectObject(hDC,hPenBar);
- BYTE *pb = m_byBuffer;
- int i0,iNum0 = m_nBufferLen;
- BYTE bBar;
- int i1,iNum1;
- int iY;
- for( i0 = 0; i0 < iNum0; i0++)
- {
- bBar = *pb&0x01;
- iNum1 = (*pb&0x02) ? m_nRatio:1;
- iY = (*pb&0x04) ? nBarHeight2 : nBarHeight;
- for( i1=0; i1 < iNum1; i1++)
- {
- if(bBar) ::SelectObject(hDC,hPenBar);
- else ::SelectObject(hDC,hPenSpace);
- ::MoveToEx(hDC, nStartX, nStartY, 0);
- ::LineTo(hDC,nStartX,iY);
- nStartX += nPenWidth;
- }
- pb++;
- }
- ::SelectObject(hDC,hPenOld);
- ::DeleteObject(hPenBar);
- ::DeleteObject(hPenSpace);
- }
- //////////////////////////////////////////////////////////////////////////
- CBarCode39::CBarCode39()
- {
- }
- CBarCode39::~CBarCode39()
- {
- }
- BOOL CBarCode39::EnCode39(IN LPCTSTR lpszCodeIn)
- {
- int iLen = strlen(lpszCodeIn);
- char*pszCode = new char[iLen+3];
- sprintf(pszCode,"*%s*",lpszCodeIn);
- strupr(pszCode);
- BYTE*pFst = m_byBuffer;
- BYTE*p0 = pFst,*p1;
- iLen+=2;
- int i;
- for(i=0;i<iLen;i++)
- {
- p1 = GetNarrowWideBarSpace39(pszCode[i],p0);
- if(p1==0) return 0;
- p0=p1;
- }
- m_nBufferLen = p1-pFst;
- delete []pszCode;
- return 1;
- }
- void CBarCode39::Draw39(IN HDC hDC, IN int nStartX, IN int nStartY, IN int nBarHeight, IN int nBarHeight2, IN const COLORREF clrBar, IN const COLORREF clrSpace, IN const int nPenWidth)
- {
- DrawBarCode(hDC, nStartX, nStartY, nBarHeight, nBarHeight2, clrBar, clrSpace, nPenWidth);
- }
- BYTE* CBarCode39::GetNarrowWideBarSpace39(CHAR ch, BYTE* pb)
- {
- IntString infs[]=
- {
- {'1', "wnnwnnnnwn"},
- {'2', "nnwwnnnnwn"},
- {'3', "wnwwnnnnnn"},
- {'4', "nnnwwnnnwn"},
- {'5', "wnnwwnnnnn"},
- {'6', "nnwwwnnnnn"},
- {'7', "nnnwnnwnwn"},
- {'8', "wnnwnnwnnn"},
- {'9', "nnwwnnwnnn"},
- {'0', "nnnwwnwnnn"},
- {'A', "wnnnnwnnwn"},
- {'B', "nnwnnwnnwn"},
- {'C', "wnwnnwnnnn"},
- {'D', "nnnnwwnnwn"},
- {'E', "wnnnwwnnnn"},
- {'F', "nnwnwwnnnn"},
- {'G', "nnnnnwwnwn"},
- {'H', "wnnnnwwnnn"},
- {'I', "nnwnnwwnnn"},
- {'J', "nnnnwwwnnn"},
- {'K', "wnnnnnnwwn"},
- {'L', "nnwnnnnwwn"},
- {'M', "wnwnnnnwnn"},
- {'N', "nnnnwnnwwn"},
- {'O', "wnnnwnnwnn"},
- {'P', "nnwnwnnwnn"},
- {'Q', "nnnnnnwwwn"},
- {'R', "wnnnnnwwnn"},
- {'S', "nnwnnnwwnn"},
- {'T', "nnnnwnwwnn"},
- {'U', "wwnnnnnnwn"},
- {'V', "nwwnnnnnwn"},
- {'W', "wwwnnnnnnn"},
- {'X', "nwnnwnnnwn"},
- {'Y', "wwnnwnnnnn"},
- {'Z', "nwwnwnnnnn"},
- {'-', "nwnnnnwnwn"},
- {'.', "wwnnnnwnnn"},
- {' ', "nwwnnnwnnn"},
- {'*', "nwnnwnwnnn"},
- {'$', "nwnwnwnnnn"},
- {'/', "nwnwnnnwnn"},
- {'+', "nwnnnwnwnn"},
- {'%', "nnnwnwnwnn"},
- };
- int i0,iNum0=sizeof(infs)/sizeof(infs[0]);
- int i1;
- for(i0=0;i0<iNum0;i0++)
- {
- IntString&inf=infs[i0];
- if(inf.ch==ch)
- {
- for(i1=0;i1<10;i1++)
- {
- if(inf.psz[i1]=='w')
- *pb+=2;
- if(i1%2==0)
- *pb+=1;
- pb++;
- }
- return pb;
- }
- }
- return 0;
- }
- };
|