⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wenzishibie.txt

📁 字符和字符之间都有间隔
💻 TXT
字号:
/** 取得每个字符的边界***/
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 );
/*    
// 标志定义
    iBZ =0;
    BOOL bBZ = false;    

    
    for (int bn = 0; bn<index; bn++)
    {
        NewDC.StretchBlt( 0,0,14,20,
                        &mDC,corInfo[bn].rect.left +1,
                        corInfo[bn].rect.top +1,
                        corInfo[bn].rect.right - corInfo[bn].rect.left -1,
                        corInfo[bn].rect.bottom - corInfo[bn].rect.top -1,
                        SRCCOPY);

        
        for (int yy = 0; yy <20; yy ++)
        {
            for (int xx = 0; xx <14;xx++)
            {
                colPix = NewDC.GetPixel(xx,yy);
                iR = GetRValue(colPix);    
                if (iR == 255) 
                    str = "□";
                else
                    str ="■";
                fInfo.WriteString(str);
            }
            fInfo.WriteString("\n");
        }
        str.Format("NEXT%d\n",bn);
        fInfo.WriteString(str);
    }
/************************** OCR **************************/
    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;
        /*
        if (type == 1)
            //m_CardNum += str;
        else
            //m_CardPWD += str;
        */
    }
    
/*****************************************/
    delete [] corInfo;    

    if (type == 1)
        m_ctrlCardNum.SetWindowText( strTemp2 );        
    else
        m_ctrlCardPWD.SetWindowText( strTemp2 );        
    //::ReleaseDC(m_hWnd,(HDC)pDC);
    //UpdateData(false);    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -