package com.handler;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import com.bean.Card;
import com.ocr.OCR;
import com.util.CardInfoMatchUtil;
import com.util.CardParamStore;
import com.util.ImageFormatUtil;
public class CardDiscern {
	/**
	 * 单个图片识别处理
	 * @param cardPath 图片路径
	 * @param sucOutPath 处理成功存放路径
	 * @param faiOutPath 处理失败存放路径
	 * @return
	 */
	public static int discernHandler(String cardPath, String sucOutPath, String faiOutPath){
		int res = 0;
		OCR ocr = new OCR();
		String cardName = null;
		String cardNumber = null;
		//String imageFormat = cardPath.split("\\.")[1].toString();
		String imageFormat = ImageFormatUtil.getFormatInFile(new File(cardPath));
		if ("NoImg".equals(imageFormat)) {
			res = 10;
			return res;
		}else if("IOException".equals(imageFormat)){
			res = 9;
			return res;
		}
		int sucTotal = ParamOutput.getSucTotal();
		int failTotal = ParamOutput.getFailTotal();
		String outFile = null;
			try {
				String cardContent = ocr.recognizeText(new File(cardPath), imageFormat);
				String[] infos = cardContent.split("\\n+");
				for (String s : infos) {
					if (s.startsWith("姓名")) { //拿出以“姓名”开头的字符串操作
						//cardName = s.split("\\s+")[1].toString().trim();
						cardName = s.trim();
					}
					if (s.startsWith("公民身份号码")) {//拿出以“公民身份号码”开头的字符串操作
						cardNumber = s.split("\\s+")[1].toString().trim();
					}
				}
				if (cardName == null || cardNumber == null) {//未成功识别
					failTotal += 1;
					CardParamStore.getInstance().getParamMap().put("failTotal", ""+failTotal);
					outFile = faiOutPath + "\\" + cardPath.substring(cardPath.lastIndexOf("\\")+1); 
					res = 1;
				}else{
					boolean matchInfo = CardInfoMatchUtil.matchInfo(cardName, cardNumber);					
					if (matchInfo) { //识别名字或号码有误
						failTotal += 1;
						CardParamStore.getInstance().getParamMap().put("failTotal", ""+failTotal);
						outFile = faiOutPath + "\\" +  cardPath.substring(cardPath.lastIndexOf("\\")+1); 
						res = 2;
					}else {//成功识别
						sucTotal += 1;
						CardParamStore.getInstance().getParamMap().put("sucTotal", ""+sucTotal);
						Card card = new Card(cardName, cardNumber);
						CardParamStore.getInstance().getSucInfoMap().put(""+sucTotal, card);
						outFile = sucOutPath + "\\" +  cardName + cardNumber + "." + imageFormat;
						res = 0;
					}
				}
				CardOutputHandler.output(cardPath, outFile);
				return res;
			} catch (FileNotFoundException e) {
				res = 8;
				return res;
			} catch (RuntimeException e) {
				res = Integer.parseInt(e.getMessage());
				return res;
			} catch (IOException e) {
				res = 9;
				return res;
			} catch (InterruptedException e) {
				res = 3;
				return res;
			}
	}
	
}
 最近下载更多
最近下载更多
                
                okliu123456     LV24
                2021年12月14日
            
            
        
                zzs269285304     LV7
                2020年6月30日
            
            
        
                570662438     LV8
                2019年11月1日
            
            
        
                xtm123     LV18
                2019年10月12日
            
            
        
                CLATZJ     LV19
                2019年7月12日
            
            
        
                a278975044     LV7
                2019年5月18日
            
            
        
                tess123     LV1
                2019年5月17日
            
            
        
                fuxiyuan     LV6
                2019年5月8日
            
            
        
                913024733     LV15
                2019年4月16日
            
            
        
                zouzuofa     LV1
                2019年2月21日
            
            
         最近浏览更多
最近浏览更多
                
                lyn520     LV3
                4月24日
            
            
        
                星空Starry-Sky     LV6
                2024年3月1日
            
            
        
                tr1314qq     LV17
                2024年2月5日
            
            
        
                liyonggang     LV2
                2024年1月19日
            
            
        
                1837157674lgw    
                2023年11月13日
            
            
                    暂无贡献等级
            
        
                1257592068     LV6
                2023年9月18日
            
            
        
                huang163zi     LV1
                2023年4月26日
            
            
        
                微微笑的笑笑     LV3
                2023年4月11日
            
            
        
                我真的是小明     LV10
                2023年3月21日
            
            
        
                晴风8815     LV2
                2023年2月28日
            
            
        
 
                 
                 
                