java程序引用res文件下的图片_java图片开源框架tesseract调用OCR实现图片文件识别代码下载...

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;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值