www.pudn.com > try2LPR-1.0.rar > LPR.cpp


/* 
* Copyright (c) 2003, try2it.com 
* All rights reserved. 
* 
* This program is free software; you can redistribute it and/or modify it under 
* the terms of the GNU General Public License as published by the Free Software 
* Foundation; either version 2 of the License, or (at your option) any later 
* version. 
*  
* This program is distributed in the hope that it will be useful, but WITHOUT 
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 
* details. 
*  
* You should have received a copy of the GNU General Public License along with 
* this program; if not, write to the Free Software Foundation, Inc., 59 Temple 
* Place - Suite 330, Boston, MA 02111-1307, USA. 
* 
* 文件名称:Lpr.cpp 
* 文件标识:LPR-02-01 
* 摘要:牌照识别的主出口Dll 
* 
* 当前版本:1.0 
* 作者:try2it.com 
* 开始日期:2003年09月15日 
* 完成日期:2003年09月30日 
*/ 
 
#include "stdafx.h" 
#include "../include/types.h" 
#include  
#include "LPRHelper.h" 
#include "LPRProcess.h" 
#include "LPR.h" 
#include "../Reco/Reco.h" 
#include "../Cap/Cap.h" 
 
#pragma comment(lib, "..\\lib\\Reco") 
#pragma comment(lib, "..\\lib\\Cap") 
/*------------------------------------------------------------------ 
* 以下声明全局变量 
------------------------------------------------------------------*/ 
HWND g_hWnd = NULL;          // 全局调用窗口句柄 
const VERSION = 0x0101;      // 版本号 
char g_szFileName[MAX_PATH]; // 配置文件名 
 
BOOL APIENTRY DllMain( HANDLE hModule,  
                       DWORD  ul_reason_for_call,  
                       LPVOID lpReserved 
					 ) 
{ 
	char pModuleFileName[MAX_PATH]; 
	int i; 
 
	switch (ul_reason_for_call) 
	{ 
		case DLL_PROCESS_ATTACH: 
 
			memset(pModuleFileName, 0, MAX_PATH); 
			memset(g_szFileName, 0, MAX_PATH); 
 
			GetModuleFileName((HMODULE)hModule, pModuleFileName, MAX_PATH); 
 
			i = strlen(pModuleFileName); 
			while(pModuleFileName[i]!='\\' || i<=0) 
			{ 
				i--; 
			} 
 
			memcpy(g_szFileName, pModuleFileName, i+1); 
			 
			strcat(g_szFileName, "GDCLPR.ini"); 
 
			ReadParam(g_szFileName); 
			//SaveParam(80,220,40,160,100,0,0,0,0,g_szFileName); 
			break; 
 
		case DLL_THREAD_ATTACH: 
			break; 
		case DLL_THREAD_DETACH: 
			break; 
        case DLL_PROCESS_DETACH: 
			break; 
    } 
    return TRUE; 
} 
 
LPR_API int CALLAGREEMENT InitLPRDll(HWND hWnd, WORD *Version) 
{ 
	InitCapDll(hWnd, Version); 
	g_hWnd = hWnd; 
	*Version = VERSION; 
	return 1; 
} 
 
LPR_API int CALLAGREEMENT GetLPRAVI() 
{ 
	return GetAVI(); 
} 
 
LPR_API int CALLAGREEMENT GetLPR(PLPInfo pLPInfo, BYTE* pImgBuff) 
{ 
	TColorImg ColorImg;      
	TColorImg CutColorImg;   
	TColorImg LPColorImg;    
 
	TGrayImg InteImg;        
	TGrayImg MedianImg;      
	TGrayImg LPGrayImg;      
	TGrayImg LPTwoImg;       
	TGrayImg RectifyImg;     
	TGrayImg AccurateImg;    
	TGrayImg LPCharImg[MAX_LPCHAR_COUNT];  
 
	TGrayImg  TempGrayImg; 
 
	TColorID ColorID;        
	int CharCount;          
 
	double MaxBlackRatio, MinBlackRatio;  
	int Threshold; 
 
	RECT   Rect;             
	RECT   AccurateRect;     
	RECT   LPCharRect[MAX_LPCHAR_COUNT];   
 
	RECT   CutRect;          
	int LeftOffset, RightOffset, TopOffset, BottomOffset; 
 
	char LPNum[MAX_LPCHAR_COUNT]; 
 
	int res = 0; 
	int i; 
 
	BYTE *pBuff = NULL; 
	DWORD dwBufferSize = 0; 
 
	__try 
	{ 
		__try 
		{ 
			strcpy(pLPInfo->LPNum, "??????\0"); 
			pLPInfo->LPColor = LP_BLUE; 
			pLPInfo->LPRect.left = 0; 
			pLPInfo->LPRect.right = 0;			 
			pLPInfo->LPRect.top = 0; 
			pLPInfo->LPRect.bottom = 0; 
 
			if (pImgBuff==NULL) 
			{ 
				res = GetImg(NULL, &dwBufferSize); 
				if (0 == res) 
					__leave; 
 
				pBuff = (BYTE*)malloc(dwBufferSize); 
				if (pBuff == NULL) 
				{ 
					res = 0; 
					__leave; 
				} 
 
				res = GetImg(pBuff, &dwBufferSize); 
				if (0 == res) 
					__leave; 
 
				pImgBuff = pBuff; 
			} 
 
			res = LoadImgToArray(&ColorImg, pImgBuff); 
			if (0 == res) 
				__leave; 
 
			CutRect = GetManualCutRect(&LeftOffset, &RightOffset, &TopOffset, &BottomOffset, ColorImg.Width, ColorImg.Height); 
			res = GetSubColorImg(&CutColorImg, &ColorImg, CutRect); 
			if (0 == res) 
				__leave; 
 
			res = ColorImg_Free(&ColorImg); 
			if (0 == res) 
				__leave; 
 
			TempGrayImg = ColorToGray(CutColorImg); 
			res = InteEqualize(&InteImg, &TempGrayImg); 
			if (0 == res) 
				__leave; 
 
			res = MedianFilter(&MedianImg, &InteImg, 3, 3, 1, 1); 
			if (0 == res) 
				__leave; 
 
			res = GrayImg_Free(&InteImg); 
			if (0 == res) 
				__leave; 
 
			res = GetLPPos(&Rect, &MedianImg); 
			if (0 == res) 
				__leave; 
 
			res = GetSubColorImg(&LPColorImg, &CutColorImg, Rect); 
			if (0 == res) 
				__leave; 
 
			res = GetSubImg(&LPGrayImg, &MedianImg, Rect); 
			if (0 == res) 
				__leave; 
 
			res = ColorImg_Free(&CutColorImg); 
			if (0 == res) 
				__leave; 
 
			res = GrayImg_Free(&MedianImg); 
			if (0 == res) 
				__leave; 
 
			res = RecoLPColor(&ColorID, &LPColorImg); 
			if (0 == res) 
				return 0; 
 
			switch (ColorID) 
			{ 
			case LP_BLACK: 
			case LP_BLUE: 
 
			    MaxBlackRatio = 0.80; 
				MinBlackRatio = 0.75; 
 
				break; 
 
			case LP_WHITE: 
			case LP_YELLOW: 
 
				res = Invert(&LPGrayImg, &LPGrayImg); 
				if (0 == res) 
					__leave; 
 
				MaxBlackRatio = 0.65; 
				MinBlackRatio = 0.60; 
 
				break; 
 
			} 
			res = TwoPixelLPImg(&LPTwoImg, &LPGrayImg, MaxBlackRatio, MinBlackRatio, &Threshold); 
			if (0 == res) 
				__leave; 
 
			res = ColorImg_Free(&LPColorImg); 
			if (0 == res) 
				__leave; 
 
			res = GrayImg_Free(&LPGrayImg); 
			if (0 == res) 
				__leave; 
 
			res = RectifyLP(&RectifyImg, &LPTwoImg, &LPTwoImg); 
			if (0 == res) 
				__leave; 
 
			res = GrayImg_Free(&LPTwoImg); 
			if (0 == res) 
				__leave; 
 
			res = GetLPAccuratePos(&AccurateRect, &RectifyImg); 
			if (0 == res) 
				__leave; 
 
			res = GetSubImg(&AccurateImg, &RectifyImg, AccurateRect); 
			if (0 == res) 
				__leave; 
 
			res = GrayImg_Free(&RectifyImg); 
			if (0 == res) 
				__leave; 
 
			res = GetLPCharPos(&CharCount, LPCharRect, &AccurateImg); 
			if (0 == res) 
				__leave; 
 
			for (i=0; i=0; i--) 
			{ 
				res = GetSubImg(&LPCharImg[MAX_LPCHAR_CURRENT_COUNT-1+i-(CharCount-1)], &AccurateImg, LPCharRect[i]); 
				if (0 == res) 
					__leave; 
			} 
 
			res = GrayImg_Free(&AccurateImg); 
			if (0 == res) 
				__leave; 
 
			for (i=0; i=0; i--) 
			{ 
				res = GrayImg_Free(&LPCharImg[MAX_LPCHAR_CURRENT_COUNT-1+i-(CharCount-1)]); 
				if (0 == res) 
					__leave; 
			} 
 
			strcpy(pLPInfo->LPNum, LPNum); 
			pLPInfo->LPColor = ColorID; 
			pLPInfo->LPRect.left = Rect.left+LeftOffset; 
			pLPInfo->LPRect.right = Rect.right+LeftOffset;			 
			pLPInfo->LPRect.top = Rect.top+TopOffset; 
			pLPInfo->LPRect.bottom = Rect.bottom+TopOffset; 
 
		} 
		__finally 
		{ 
			if (pBuff != NULL) 
			{ 
				free(pBuff); 
				pBuff = NULL; 
			} 
 
			FreeAllMemNode(); 
		} 
	} 
	__except(EXCEPTION_EXECUTE_HANDLER) 
	{ 
	} 
 
	return res; 
} 
 
LPR_API int CALLAGREEMENT SetLPR() 
{ 
	return 1; 
} 
 
LPR_API int CALLAGREEMENT SetLPRCap() 
{ 
	return SetCap(); 
} 
 
LPR_API int CALLAGREEMENT SetLPRReco() 
{ 
	return SetReco(); 
}