qq_16994437 2015-08-22 08:16 采纳率: 0%
浏览 1612
已结题

关于文字扫描录入功能的代码,求大神帮忙修改

/** 取得每个字符的边界***/
void CPOCRNuberDlg::GetCharRect(HBITMAP bit, RECT rect,int type, CDC* pDC )
{
CString strTemp2;
CBitmap bitmap;
bitmap.m_hObject = CopyImage(bit,IMAGE_BITMAP,0,0,LR_MONOCHROME);

//CDC *pDC = GetDC();
CDC mDC;

mDC.CreateCompatibleDC( pDC );
mDC.SetBoundsRect( &rect, DCB_RESET);
mDC.SelectObject(bitmap.m_hObject);

BOOL bIsChar = false;
corInfo = new OCR_Info[50];
int index = 0;

COLORREF colPix;
int        iR;
int        iG;

/************** 取出每个数字所占的Rect **************/
// 列扫描

CString str;
for (int i = rect.left; i < rect.right ; i++)
{
    for (int j = rect.top - 250 ; j < rect.bottom - 250; j++)
    {
        colPix = mDC.GetPixel(i,j);
        iG = GetGValue(colPix);

        // 字符开始
        if ( (iG == 0) && (!bIsChar) )
        {
            corInfo[index].rect.left = i;
            corInfo[index].Index = index;
            bIsChar = true;

            break;
        }
        // 字符继续
        else if ((iG == 0) && (bIsChar))
        {
            break;
        }
        // 字符结束
        else if ((iG == 255) && ( j > rect.bottom -252) && (bIsChar))
        {
            corInfo[index].rect.right = i-1;
            if ((corInfo[index].rect.right - corInfo[index].rect.left) > 3)
                index++;
            bIsChar = false;
            break;
        }

    }    
}

CClientDC lDC(this);

// 行扫描
bIsChar = false;
int iBZ =0;
for ( int iinx = 0; iinx < index; iinx++)
{
for (int ii = rect.top-250 ; ii < rect.bottom -250; ii++)
{
for (int j = corInfo[iinx].rect.left ; j < corInfo[iinx].rect.right ; j++)
{
colPix = mDC.GetPixel(j,ii);

iG = GetGValue(colPix);

// 字符顶端开始
if ( (iG == 0) && (!bIsChar) )
{
corInfo[iinx].rect.top = ii;

bIsChar = true;

break;

}
// 字符继续
else if ((iG == 0) && (bIsChar))
{
break;
}
// 字符底端结束
else if ((iG == 255) && ( j > corInfo[iinx].rect.right -2 ) && (bIsChar))
{

corInfo[iinx].rect.bottom = ii-1;

bIsChar = false;
break;
}

}

}
}

/****** 位置确定完成,每个字符的位置信息写在 corInfo.rect 中********/

char m_pFilePath[255];
CStdioFile fInfo;

strcpy(m_pFilePath,"D:\\info.ini");
fInfo.Open( m_pFilePath, CFile::modeCreate | CFile::modeWrite );

CDC NewDC;    
NewDC.CreateCompatibleDC(pDC);
CBitmap memBmp;
memBmp.CreateCompatibleBitmap( &mDC, 14, 20 );
NewDC.SelectObject( memBmp );

CString strField;
CString strtemp;
char cNum[281];
int iNumIndex = 0;
int iErrCount[10];
int iECount =0;
int iBlack = 0;
int idx = 0;
int ww =0;
int w = 0;

GetCurrentDirectory(255,m_pFilePath);
strcat(m_pFilePath,HOCRINI);
//strcat(m_pFilePath,"\\stdinfo.ini");

// 逐个扫描,识别

for (int l = 0; l<index; l++)
{

NewDC.StretchBlt( 0,0,14,20,
&mDC,corInfo[l].rect.left +1,
corInfo[l].rect.top +1,
corInfo[l].rect.right - corInfo[l].rect.left -1,
corInfo[l].rect.bottom - corInfo[l].rect.top -1,
SRCCOPY);

    for (w = 0 ;w< 10; w++)
        iErrCount[w] = 0;

    for (int itmp = 0; itmp < 10; itmp++)
    {
        strtemp.Format("%d",itmp);
        GetPrivateProfileString("Number",strtemp,"",
                    cNum,281,m_pFilePath);
        for(int iy = 0; iy < 20; iy ++)
        {
            for(int ix = 0; ix<14;ix++)
            {
                colPix = NewDC.GetPixel(ix,iy);
                iR = GetRValue(colPix);
                if (iR == 255)
                    iR = 49;
                else
                    iR = 48;
                if ( iR != cNum[iNumIndex])
                    iErrCount[itmp]++;                    
                iNumIndex++;                    
            }                
        }        
        iNumIndex = 0;            
    }
    iECount = iErrCount[0];
    idx = 0;
    for(ww =0; ww< 10; ww++)
    {
        if (iErrCount[ww] < iECount)
        {
            iECount = iErrCount[ww];
            idx = ww;
        }
    }
    // 区别1
    if (idx != 1)
    {
        for (int itx = 7; itx < 9; itx++)
        {
            for (int ity = 0; ity < 20; ity++)
            {
                colPix = NewDC.GetPixel(itx,ity);
                iR = GetRValue(colPix);
                if (iR == 0)
                    iBlack++;
            }
        }
        if (iBlack > 39)
        {
            idx = 1;
        }
        iBlack = 0;
    }
    // 再将0 和8 进行判断
    if ((idx == 0) | (idx ==8))
    {
        for (int ity = 8; ity < 10; ity++)
        {
            for (int itx = 2; itx < 12; itx++)
            {
                colPix = NewDC.GetPixel(itx,ity);
                iR = GetRValue(colPix);
                if (iR == 0)
                    iBlack++;
            }
        }
        if (iBlack > 14)
            idx = 8;
        else
            idx = 0;
        iBlack = 0;
    }
    // 再将5 和6 进行判断
    if (( idx == 5) | (idx == 6))
    {

    }
    str.Format("%d",idx);
    strTemp2 += str;
}

delete [] corInfo;   

if (type == 1)
    m_ctrlCardNum.SetWindowText( strTemp2 );        
else
    m_ctrlCardPWD.SetWindowText( strTemp2 );        

}

  • 写回答

2条回答 默认 最新

  • devmiao 2015-08-22 23:31
    关注

    自己用调试器检查下,看看每一步执行有没有问题

    评论

报告相同问题?

悬赏问题

  • ¥15 用三极管设计—个共射极放大电路
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示