You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
287 lines
8.5 KiB
C++
287 lines
8.5 KiB
C++
#include "StdAfx.h"
|
|
#include "ObjBarCode.h"
|
|
#include "GlobalFunction.h"
|
|
#include "LogMgr.h"
|
|
#ifdef __TBAR_CODE__
|
|
#include "tbarcode51.h"
|
|
#endif
|
|
#include "ObjPline.h"
|
|
#include "FontTypeMgr.h"
|
|
|
|
//调试用-- 输出结果
|
|
void PrintResult(vector<vector<CBarCodeModule>> &ModuleVec)
|
|
{
|
|
ofstream *pStream = gLogMgr->GetDebugOfstream();
|
|
vector<vector<CBarCodeModule>>::iterator iter = ModuleVec.begin();
|
|
vector<vector<CBarCodeModule>>::iterator iter_end = ModuleVec.end();
|
|
for(;iter!=iter_end;iter++)
|
|
{
|
|
vector<CBarCodeModule>::iterator iter1 = (*iter).begin();
|
|
vector<CBarCodeModule>::iterator iter1_end = (*iter).end();
|
|
for(;iter1!=iter1_end;iter1++)
|
|
{
|
|
(*pStream)<<(*iter1).IsBlack();
|
|
}
|
|
(*pStream)<<endl;
|
|
}
|
|
}
|
|
|
|
CObjBarCode::CObjBarCode(void)
|
|
{
|
|
m_ModuleW = 1;
|
|
m_ModuleH = 1;
|
|
}
|
|
|
|
CObjBarCode::~CObjBarCode(void)
|
|
{
|
|
}
|
|
CObjBarCode::CObjBarCode(CObjBarCode &rhs)
|
|
:CObjString(rhs)
|
|
{
|
|
CreatExt();
|
|
}
|
|
CString CObjBarCode::GetStr()
|
|
{
|
|
CString str = "条码";
|
|
return str;
|
|
}
|
|
//获取当前文本
|
|
CString CObjBarCode::GetCurString()
|
|
{
|
|
//可变文本更新
|
|
m_ValStr.UpdateValStr();
|
|
return m_ValStr.GetString();
|
|
}
|
|
//参数绑定到对话框
|
|
void CObjBarCode::BindingDlg()
|
|
{
|
|
#ifdef __DLG_PANE_VAR_STRING__
|
|
GetFrame()->m_DlgVariableString.BindingObjString(this);
|
|
#endif
|
|
}
|
|
void CObjBarCode::CreatExt()
|
|
{
|
|
//清空容器
|
|
m_ObjContainer.Clear();
|
|
//获取字符串
|
|
CString Str = GetCurString();
|
|
//获取条码轮廓数据
|
|
CreatBarCodeEdge(Str,m_BasePt1,m_ObjContainer);
|
|
}
|
|
|
|
#if 1
|
|
//创建条码轮廓
|
|
void CObjBarCode::CreatBarCodeEdge(CString Str,Dbxy BasePt,CObjContainer &ObjContainer)
|
|
{
|
|
#ifdef __DLG_PANE_TEST__
|
|
#ifdef __TBAR_CODE__
|
|
m_BasePt = BasePt;
|
|
CTbarcode51 &Tbarcode51 = GetFrame()->m_DlgTest.GetTbarcode51();
|
|
|
|
vector<vector<CBarCodeModule>> ModuleVec;
|
|
CFontTypeMgr &pFontTypeMgr = CFontTypeMgr::Instance();
|
|
int idx = pFontTypeMgr.GetCurFontTypeIdx();
|
|
gLogMgr->WriteDebugLog("idx", "=", idx);
|
|
Tbarcode51.put_BarCode(pFontTypeMgr.GetCurFontTypeIdx());//类型
|
|
Tbarcode51.put_Text(Str);//text 内容
|
|
Tbarcode51.BCDraw((long)GetCurViewPtr()->GetHDC(),0,0,0,200);//在view 上绘制出来
|
|
if(AnlalysisBarCord(ModuleVec))
|
|
{
|
|
//生成轮廓数据到ObjContainer
|
|
CreatEdge(ModuleVec,ObjContainer);
|
|
}
|
|
#endif
|
|
#endif
|
|
}
|
|
//分析条码保存到矩阵ModuleVec
|
|
bool CObjBarCode::AnlalysisBarCord(vector<vector<CBarCodeModule>> &ModuleVec)
|
|
{
|
|
int Rows = 0;
|
|
#ifdef __DLG_PANE_TEST__
|
|
#ifdef __TBAR_CODE__
|
|
CTbarcode51 &Tbarcode51 = GetFrame()->m_DlgTest.GetTbarcode51();
|
|
//先读取行数
|
|
Rows = Tbarcode51.get_CountRows();
|
|
#endif
|
|
#endif
|
|
ofstream *pStream = gLogMgr->GetDebugOfstream();
|
|
(*pStream)<<"[Rows] = "<<Rows<<endl;
|
|
if(Rows<1)
|
|
{
|
|
(*pStream)<<"[生成错误] "<<endl;
|
|
return false;
|
|
}
|
|
else if(Rows ==1)
|
|
{
|
|
(*pStream)<<"[一维条码] "<<endl;
|
|
}
|
|
else
|
|
{
|
|
(*pStream)<<"[二维条码] "<<endl;
|
|
}
|
|
//先解析有多少列-------------------------------------
|
|
int ModuleSize = 3;//每个模块的尺寸(像素) (modulWidth 的值设置为800)
|
|
int startX=1;//barcode 控件的坐标
|
|
int startY=1;
|
|
int endX = 1000;
|
|
int endY = 400;
|
|
|
|
if(Rows>1)
|
|
{
|
|
endX = startX + Rows*ModuleSize;
|
|
endY = startY + Rows*ModuleSize;
|
|
}
|
|
|
|
int ColCnt = 0;
|
|
int Pos1 = startX;
|
|
int Pos2 = startX;
|
|
|
|
int CurY = startY;
|
|
//生成一个空行,方便生成轮廓
|
|
CreatSpaceLine(ModuleVec,Rows+2);
|
|
for(int i = 0;i<Rows;i++)//每次解析一行
|
|
{
|
|
vector<CBarCodeModule> vec;
|
|
ModuleVec.push_back(vec);
|
|
int size = ModuleVec.size();
|
|
AnlalysisOneRow(ModuleVec[size-1],startX,endX,CurY,ModuleSize);
|
|
CurY += ModuleSize;
|
|
}
|
|
//生成一个空行,方便生成轮廓
|
|
CreatSpaceLine(ModuleVec,Rows+2);
|
|
return !ModuleVec.empty();
|
|
}
|
|
//生成一个空行,方便生成轮廓
|
|
void CObjBarCode::CreatSpaceLine(vector<vector<CBarCodeModule>> &ModuleVec,int cnt)
|
|
{
|
|
vector<CBarCodeModule> vec;
|
|
ModuleVec.push_back(vec);
|
|
int size = ModuleVec.size();
|
|
for(int i=0;i<cnt;i++)
|
|
{
|
|
CBarCodeModule ModuleSpace(false);
|
|
ModuleVec[size-1].push_back(ModuleSpace);
|
|
}
|
|
}
|
|
//解析一行
|
|
void CObjBarCode::AnlalysisOneRow(vector<CBarCodeModule> &vec,int startX,int endX,int curY,int ModuleSize)
|
|
{
|
|
ofstream *pStream = gLogMgr->GetDebugOfstream();
|
|
HDC hdc = GetCurViewPtr()->GetHDC();
|
|
COLORREF Pixelcolor;//像素点的颜色
|
|
|
|
CBarCodeModule ModuleSpace(false);
|
|
vec.push_back(ModuleSpace);
|
|
for(int i = startX;i<endX;i +=ModuleSize)
|
|
{
|
|
//读取当前像素的颜色
|
|
Pixelcolor = GetPixel(hdc,i,curY);
|
|
if(RGB_RED == Pixelcolor)
|
|
{
|
|
CBarCodeModule Module(true);
|
|
vec.push_back(Module);
|
|
}
|
|
else
|
|
{
|
|
CBarCodeModule Module(false);
|
|
vec.push_back(Module);
|
|
}
|
|
}
|
|
vec.push_back(ModuleSpace);
|
|
}
|
|
//根据二维阵列来生成轮廓数据
|
|
void CObjBarCode::CreatEdge(vector<vector<CBarCodeModule>> &ModuleVec,CObjContainer &ObjContainer)
|
|
{
|
|
if(ModuleVec.empty())
|
|
return;
|
|
//垂直方向--------------------------------------------------------------------------------------------------------------------
|
|
int MaxCol = ModuleVec[0].size();
|
|
int MaxRow = ModuleVec.size();
|
|
for(int Col=1;Col<MaxCol;Col++)//每一列
|
|
{
|
|
int Start = -1;
|
|
int End = -1;
|
|
bool flg = false;
|
|
for(int Row=0;Row<MaxRow;Row++)//每一行
|
|
{
|
|
//黑白相交
|
|
if((ModuleVec[Row][Col].IsBlack()&&!ModuleVec[Row][Col-1].IsBlack())||(!ModuleVec[Row][Col].IsBlack()&&ModuleVec[Row][Col-1].IsBlack()))
|
|
{
|
|
if(-1==Start)
|
|
Start = Row;
|
|
End = Row;
|
|
if(Row == MaxRow-1 && (-1!=Start))
|
|
flg = true;
|
|
}
|
|
else if(-1 !=Start)
|
|
{
|
|
flg = true;
|
|
}
|
|
if(flg)
|
|
{
|
|
Dbxy pt1,pt2;
|
|
pt1.x = m_BasePt.x + m_ModuleW*Col;
|
|
pt1.y = m_BasePt.y - m_ModuleH*Start;
|
|
pt2.x = m_BasePt.x + m_ModuleW*Col;
|
|
pt2.y = m_BasePt.y - m_ModuleH*(End+1);
|
|
|
|
//创建线段
|
|
CObjPline *Pline = new CObjPline;
|
|
CDataPoint DataPoint1(pt1);
|
|
Pline->AddDataPoint(DataPoint1);
|
|
CDataPoint DataPoint2(pt2);
|
|
Pline->AddDataPoint(DataPoint2);
|
|
ObjContainer.AddObject(Pline);
|
|
|
|
Start = -1;
|
|
End = -1;
|
|
flg = false;
|
|
}
|
|
}
|
|
}
|
|
//水平方向--------------------------------------------------------------------------------------------------------------------
|
|
for(int Row=1;Row<MaxRow;Row++)//每一列
|
|
{
|
|
int Start = -1;
|
|
int End = -1;
|
|
bool flg = false;
|
|
for(int Col=0;Col<MaxCol;Col++)//每一行
|
|
{
|
|
//黑白相交
|
|
if((ModuleVec[Row][Col].IsBlack()&&!ModuleVec[Row-1][Col].IsBlack())||(!ModuleVec[Row][Col].IsBlack()&&ModuleVec[Row-1][Col].IsBlack()))
|
|
{
|
|
if(-1==Start)
|
|
Start = Col;
|
|
End = Col;
|
|
if(Col == MaxCol-1 && (-1!=Start))
|
|
flg = true;
|
|
}
|
|
else if(-1 !=Start)
|
|
{
|
|
flg = true;
|
|
}
|
|
if(flg)
|
|
{
|
|
Dbxy pt1,pt2;
|
|
pt1.x = m_BasePt.x + m_ModuleW*Start;
|
|
pt1.y = m_BasePt.y - m_ModuleH*Row;
|
|
pt2.x = m_BasePt.x + m_ModuleW*(End+1);
|
|
pt2.y = m_BasePt.y - m_ModuleH*Row;
|
|
|
|
//创建线段
|
|
CObjPline *Pline = new CObjPline;
|
|
CDataPoint DataPoint1(pt1);
|
|
Pline->AddDataPoint(DataPoint1);
|
|
CDataPoint DataPoint2(pt2);
|
|
Pline->AddDataPoint(DataPoint2);
|
|
ObjContainer.AddObject(Pline);
|
|
|
|
Start = -1;
|
|
End = -1;
|
|
flg = false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
#endif
|