import com.baidu.aip.ocr.AipOcr;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.json.JSONObject;
import javax.swing.;
import java.awt.;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
@author xuexiaojun
@creat 2019-11-01 9:24
*/
public class OcrTestMetherd{
public static final String APP_ID = “17076882”;
public static final String API_KEY = “pMNo0g74SMVwdj5dgGSegyTk”;
public static final String SECRET_KEY = “9BPYVVqYb9wTuyPSBMlRGSKH06xkDhSU”;
public static JSONObject getImageValueData(String imagePath) {
// 初始化一个AipOcr
JSONObject res = null;
try {
AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
// 调用接口
HashMap
//是否翻转
options.put(“detect_direction”, “true”);
res = client.basicGeneral(imagePath, options);
// System.out.println(res.toString(2));
} catch (Exception e) {
System.out.println("=read image failed=");
}
return res;
}
/***
private static int getProductComponyValue(CustomsDeclaration customsDeclaration, List
String value;
getProductComponyNoValue(customsDeclaration, stringStringMap);
for (int j = i + 1; j < dataList.size(); j++) {
i = j;
stringStringMap = dataList.get(j);
value = stringStringMap.get(“words”).trim();
StringBuffer sb = new StringBuffer();
if (value.contains(“公司”)) {
customsDeclaration.setProductCompony(dataList.get(i).get(“words”).replace(“锎”, “铜”));
} else if (value.contains(“运费”) || value.contains(“运港”) || value.contains(“合同协议号”) || value.contains(“贸易国”) || value.contains(“抵国”)) {
i–;
break;
}
}
return i;
}
//读取报关单位信息
private static void getDeclareUnitInfoValue(CustomsDeclaration customsDeclaration, List
String value;
value = dataList.get(i).get(“words”).replaceAll("\)", “”);
Pattern p = Pattern.compile("[0-9A-Za-z]");
Matcher matcher = p.matcher(value);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
sb.append(matcher.group());
}
customsDeclaration.setDeclareUnitno(sb.toString());
value = value.length() <= (value.indexOf(sb.toString()) + sb.toString().length()) ? value : value.substring(value.indexOf(sb.toString()) + sb.toString().length());
customsDeclaration.setDeclareUnit(value);
}
//读取货物相关信息
private static int getGoodsInfoValue(CustomsDeclaration customsDeclaration, Set goodsInfoList, List
Map
String value;
StringBuffer allGoodNoSb = new StringBuffer();
StringBuffer allGoodNameSb = new StringBuffer();
for (int j = i + 1; j < dataList.size(); j++) {
i = j;
stringStringMap = dataList.get(j);
value = stringStringMap.get(“words”).trim();
if (value.contains(“特殊关系确认”) || value.contains(“价格影响确认”) || value.contains(“报关人员”) || (j == dataList.size() - 1)) {
if (allGoodNameSb.length() > 0) {
customsDeclaration.setAllgoodsCnname(allGoodNameSb.substring(0, allGoodNameSb.lastIndexOf(",")));
}
if (allGoodNoSb.length() > 0) {
customsDeclaration.setAllgoodsNo(allGoodNoSb.substring(0, allGoodNoSb.lastIndexOf(",")));
}
break;
}
if (value.contains(".") || value.contains(“千克”) || value.contains(“征免”) || value.contains(“征兔”)) {
continue;
}
Pattern p = Pattern.compile("[0-9]{7,10}");
Matcher matcher = p.matcher(value);
Pattern pStart = Pattern.compile("1{7,10}.");
Matcher matcherStart = pStart.matcher(value);
Pattern pWord = Pattern.compile("[\u4e00-\u9fa5]");
Matcher matcherWord = pWord.matcher(value);
if (matcher.find() && matcherStart.matches() && matcherWord.find() && !value.contains("(")) {
String goodNo = matcher.group();
if (goodNo.equals(value)){
continue;
}
value = value.length() <= (value.indexOf(goodNo) + goodNo.length()) ? value : value.substring(value.indexOf(goodNo) + goodNo.length());
if (value.contains(“转”)){
value = value.replace(“6”, “E”);
}
if (value.startsWith(“0”) && value.length() > 1){
goodNo = goodNo.substring(1) + “0”;
value = value.substring(1);
}
String[] subArray = {"%", “#”, “)”, "", “+”, “,”, “|”, “°”, “”"};
for (String s : subArray) {
if (value.startsWith(s)){
value = value.substring(1);
}
}
Pattern pNum = Pattern.compile("[0-9a-zA-z]{1,4}");
Matcher matcherNum = pNum.matcher(value);
if (matcherNum.matches()){
continue;
}
if (goodNo.length() == 7){
goodNo = goodNo + “000”;
}else if (goodNo.length() == 9){
goodNo = goodNo + “0”;
}else if (goodNo.length() == 8){
goodNo = goodNo + “00”;
}
allGoodNoSb.append(goodNo).append(",");
allGoodNameSb.append(value).append(",");
customsDeclaration.setGoodsNo(goodNo);
customsDeclaration.setGoodsCnname(value);
Goodsinfo goodsInfo = new OcrTestMetherd().new Goodsinfo(goodNo, value, “”);
goodsInfoList.add(goodsInfo);
continue;
}
//境内货源地
stringStringMap = dataList.get(j);
value = stringStringMap.get(“words”).trim();
Pattern pResource = Pattern.compile("[0-9]{3,}");
Matcher matcherResource = pResource.matcher(value);
if (matcherResource.find() && (value.contains("(") || value.contains(")"))) {
value = value.replaceAll("\(", “”).replaceAll("\)", “”);
Pattern pResourceNo = Pattern.compile("[0-9]{3,}");
Matcher mResourceNo = pResourceNo.matcher(value);
while (mResourceNo.find()) {
String goodsSourceno = mResourceNo.group();
customsDeclaration.setGoodsSourceno(goodsSourceno);
value = value.length() <= (value.indexOf(goodsSourceno) + goodsSourceno.length()) ? value : value.substring(value.indexOf(goodsSourceno) + goodsSourceno.length());
customsDeclaration.setGoodsSource(value.replace(“照章征税”, “”).replace(“全免”, “”));
}
}
}
return i;
}
//读取毛重信息
private static int getTotleWeightValue(CustomsDeclaration customsDeclaration, List
Map
String value;
for (int j = i + 1; j < dataList.size() - 3; j++) {
stringStringMap = dataList.get(j);
value = stringStringMap.get(“words”);
if (value.contains(“单证及编号”)){
break;
}
if (value.contains(“集装箱标箱数及号码”) || value.contains(“商品名称及规格”)){
break;
}
i = j;
if (value.contains(“杂费”)){
stringStringMap = dataList.get(j + 3);
value = stringStringMap.get(“words”).trim().replaceAll(“±”, “4”);
Double weight = null;
try {
weight = Double.valueOf(value);
customsDeclaration.setTotalWeight(BigDecimal.valueOf(weight));
} catch (NumberFormatException e) {
// logger.info("=weight format failed={}", value);
}
continue;
}
Pattern p = Pattern.compile("[A-Za-z]{1,}");
Matcher matcher = p.matcher(value);
if (matcher.matches()) {
stringStringMap = dataList.get(j - 2);
value = stringStringMap.get(“words”).trim().replaceAll(“±”, “4”);
Double weight = null;
try {
weight = Double.valueOf(value);
customsDeclaration.setTotalWeight(BigDecimal.valueOf(weight));
} catch (NumberFormatException e) {
// logger.info("=weight format failed={}", value);
}
break;
}
}
return i;
}
//读取生产销售单位信息
private static void getProductComponyNoValue(CustomsDeclaration customsDeclaration, Map
String value;
value = stringStringMap.get(“words”);
Pattern p = Pattern.compile("[a-zA-Z0-9]");
Matcher matcher = p.matcher(value);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
sb.append(matcher.group());
}
value = sb.toString();
customsDeclaration.setProductComponyno(value);
}
//读取收货人信息
private static void getReceiveShipperNoValue(CustomsDeclaration customsDeclaration, String value) {
Pattern p = Pattern.compile("[a-zA-Z0-9]");
Matcher matcher = p.matcher(value);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
sb.append(matcher.group());
}
value = sb.toString();
customsDeclaration.setReceiveShipperNo(value);
}
//读取发货人信息
private static void setShipperNoValue(CustomsDeclaration customsDeclaration, String value) {
Pattern p = Pattern.compile("[a-zA-Z0-9]");
Matcher matcher = p.matcher(value);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
sb.append(matcher.group());
}
value = sb.toString();
customsDeclaration.setShipperNo(value);
}
public static void main(String[] args) {
JFileChooser fileChooser = new JFileChooser("\");
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int returnVal = fileChooser.showOpenDialog(fileChooser);
if(returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
List list = Lists.newArrayList();
List fileList = Lists.newArrayList();
if (file != null){
List filePathList = Lists.newArrayList();
getFileList(file.getAbsolutePath(), fileList, “”);
}
for(int i = 0; i < fileList.size(); i++){
CustomsDeclaration customsDeclaration = new OcrTestMetherd().new CustomsDeclaration();
customsDeclaration.setFilepath(new File(fileList.get(i)).getName());
Set goodsInfo = Sets.newHashSet();
JSONObject res = getImageValueData(fileList.get(i));
dealImageJSONObjectData(res, customsDeclaration, goodsInfo);
list.add(customsDeclaration);
}
String[][] playerInfo = new String[6][6];
int i = 0;
for (CustomsDeclaration customsDeclaration : list) {
playerInfo[i][0] = customsDeclaration.getShipperCnname();
playerInfo[i][1] = customsDeclaration.getShipperNo();
playerInfo[i][2] = customsDeclaration.getGoodsCnname();
playerInfo[i][3] = customsDeclaration.getGoodsNo();
playerInfo[i][4] = customsDeclaration.getDeclareUnit();
playerInfo[i][5] = customsDeclaration.getFilepath();
i++;
}
OcrTestMetherd com = new OcrTestMetherd();
JFrame f = new JFrame();
// 创建表格中的横标题
String[] Names = {"发货人", "发货人no", "商品名", "商品no", "申报单位", "图片名"};
// 以Names和playerInfo为参数,创建一个表格
JTable table = new JTable(playerInfo, Names);
// 设置此表视图的首选大小
table.setPreferredScrollableViewportSize(new Dimension(700, 100));
// 将表格加入到滚动条组件中
JScrollPane scrollPane = new JScrollPane(table);
f.getContentPane().add(scrollPane, BorderLayout.CENTER);
// 再将滚动条组件添加到中间容器中
f.setTitle("表格测试窗口");
f.pack();
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
public static void getFileList(String path, List fileList, String pdfDirPath){
try {
if (new File(path).isDirectory()){
File[] fileArray = new File(path).listFiles();
for (File file : fileArray) {
if (file.isDirectory()){
getFileList(file.getAbsolutePath(), fileList, pdfDirPath);
}else{
fileList.add(file.getAbsolutePath());
}
}
}else if(new File(path).isFile()){
// if (path.endsWith(".pdf") || path.endsWith(".PDF")){
// List filePathListNew = PdfBoxTask.extractImagesAndReturn(new File(path), pdfDirPath);
// fileList.addAll(filePathListNew);
// }
fileList.add(path);
}
} catch (Exception e) {
// logger.error("=get ocrFileList failed=", e);
}
}
class CustomsDeclaration {
/**
* 图片信息ID
/
private Long customsDeclarationId;
/*
* 订单id
/
private Long orderId;
/*
* 报关单类型
/
private String ocrType;
/*
* 箱号
/
private String containerNo;
/*
* 图片文本内容
/
private String textContent;
/*
* 图片路径
/
private String filepath;
/*
* 境内发货人
/
private String shipperCnname;
/*
* 境内发货人编号
/
private String shipperNo;
/*
* 收货人
/
private String receiveShipperCnname;
/*
* 收货人编号
/
private String receiveShipperNo;
/*
* 申报日期
/
private Date declareDate;
/*
* 生产销售单位编号
/
private String productComponyno;
/*
* 生产销售单位
/
private String productCompony;
/*
* 运抵国(地区)
/
private String arriveCountry;
/*
* 毛重(千克)
/
private BigDecimal totalWeight;
/*
* 商品编号
/
private String goodsNo;
/*
* 商品名称
/
private String goodsCnname;
/*
* 全部商品编号
/
private String allgoodsNo;
/*
* 全部商品名称
/
private String allgoodsCnname;
/*
* 境内货源地
/
private String goodsSource;
/*
* 境内货源地编号
/
private String goodsSourceno;
/*
* 申报单位编号
/
private String declareUnitno;
/*
* 申报单位
/
private String declareUnit;
/*
* 集装箱标箱数及号码
/
private String countAndNo;
/*
* 备注
/
private String remark;
/*
* 添加人id
/
private Long addUserId;
/*
* 添加时间
/
private Date addTime;
/*
* 修改人id
/
private Long modifyUserId;
/*
* 最后更新时间
*/
private Date modifyTime;
/**
* 发站
*/
private String departureStation;
/**
* 到站
*/
private String arriveStation;
/**
* 托运公司名称
*/
private String shipperCompanyName;
/**
* 收货公司名称
*/
private String arriveCompanyName;
/**
* 承运人确定重量
*/
private BigDecimal carrierSureWeight;
/**
* 班次类型
*/
private String orderType;
private String containerType;
private Long caRequirePlanId;
private String caPlanNo;
public Long getCaRequirePlanId() {
return caRequirePlanId;
}
public void setCaRequirePlanId(Long caRequirePlanId) {
this.caRequirePlanId = caRequirePlanId;
}
public String getCaPlanNo() {
return caPlanNo;
}
public void setCaPlanNo(String caPlanNo) {
this.caPlanNo = caPlanNo;
}
public String getContainerType() {
return containerType;
}
public void setContainerType(String containerType) {
this.containerType = containerType;
}
public String getDepartureStation() {
return departureStation;
}
public void setDepartureStation(String departureStation) {
this.departureStation = departureStation;
}
public String getArriveStation() {
return arriveStation;
}
public void setArriveStation(String arriveStation) {
this.arriveStation = arriveStation;
}
public String getShipperCompanyName() {
return shipperCompanyName;
}
public void setShipperCompanyName(String shipperCompanyName) {
this.shipperCompanyName = shipperCompanyName;
}
public String getArriveCompanyName() {
return arriveCompanyName;
}
public void setArriveCompanyName(String arriveCompanyName) {
this.arriveCompanyName = arriveCompanyName;
}
public BigDecimal getCarrierSureWeight() {
return carrierSureWeight;
}
public void setCarrierSureWeight(BigDecimal carrierSureWeight) {
this.carrierSureWeight = carrierSureWeight;
}
public Long getCustomsDeclarationId() {
return customsDeclarationId;
}
public void setCustomsDeclarationId(Long customsDeclarationId) {
this.customsDeclarationId = customsDeclarationId;
}
public Long getOrderId() {
return orderId;
}
public void setOrderId(Long orderId) {
this.orderId = orderId;
}
public String getOcrType() {
return ocrType;
}
public void setOcrType(String ocrType) {
this.ocrType = ocrType;
}
public String getContainerNo() {
return containerNo;
}
public void setContainerNo(String containerNo) {
this.containerNo = containerNo;
}
public String getTextContent() {
return textContent;
}
public void setTextContent(String textContent) {
this.textContent = textContent;
}
public String getFilepath() {
return filepath;
}
public void setFilepath(String filepath) {
this.filepath = filepath;
}
public String getShipperCnname() {
return shipperCnname;
}
public void setShipperCnname(String shipperCnname) {
this.shipperCnname = shipperCnname;
}
public String getShipperNo() {
return shipperNo;
}
public void setShipperNo(String shipperNo) {
this.shipperNo = shipperNo;
}
public Date getDeclareDate() {
return declareDate;
}
public void setDeclareDate(Date declareDate) {
this.declareDate = declareDate;
}
public String getProductComponyno() {
return productComponyno;
}
public void setProductComponyno(String productComponyno) {
this.productComponyno = productComponyno;
}
public String getProductCompony() {
return productCompony;
}
public void setProductCompony(String productCompony) {
this.productCompony = productCompony;
}
public String getArriveCountry() {
return arriveCountry;
}
public void setArriveCountry(String arriveCountry) {
this.arriveCountry = arriveCountry;
}
public BigDecimal getTotalWeight() {
return totalWeight;
}
public void setTotalWeight(BigDecimal totalWeight) {
this.totalWeight = totalWeight;
}
public String getGoodsNo() {
return goodsNo;
}
public void setGoodsNo(String goodsNo) {
this.goodsNo = goodsNo;
}
public String getGoodsCnname() {
return goodsCnname;
}
public void setGoodsCnname(String goodsCnname) {
this.goodsCnname = goodsCnname;
}
public String getAllgoodsNo() {
return allgoodsNo;
}
public void setAllgoodsNo(String allgoodsNo) {
this.allgoodsNo = allgoodsNo;
}
public String getAllgoodsCnname() {
return allgoodsCnname;
}
public void setAllgoodsCnname(String allgoodsCnname) {
this.allgoodsCnname = allgoodsCnname;
}
public String getGoodsSource() {
return goodsSource;
}
public void setGoodsSource(String goodsSource) {
this.goodsSource = goodsSource;
}
public String getGoodsSourceno() {
return goodsSourceno;
}
public void setGoodsSourceno(String goodsSourceno) {
this.goodsSourceno = goodsSourceno;
}
public String getDeclareUnitno() {
return declareUnitno;
}
public void setDeclareUnitno(String declareUnitno) {
this.declareUnitno = declareUnitno;
}
public String getDeclareUnit() {
return declareUnit;
}
public void setDeclareUnit(String declareUnit) {
this.declareUnit = declareUnit;
}
public String getCountAndNo() {
return countAndNo;
}
public void setCountAndNo(String countAndNo) {
this.countAndNo = countAndNo;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public Long getAddUserId() {
return addUserId;
}
public void setAddUserId(Long addUserId) {
this.addUserId = addUserId;
}
public Date getAddTime() {
return addTime;
}
public void setAddTime(Date addTime) {
this.addTime = addTime;
}
public Long getModifyUserId() {
return modifyUserId;
}
public void setModifyUserId(Long modifyUserId) {
this.modifyUserId = modifyUserId;
}
public Date getModifyTime() {
return modifyTime;
}
public void setModifyTime(Date modifyTime) {
this.modifyTime = modifyTime;
}
public String getReceiveShipperCnname() {
return receiveShipperCnname;
}
public void setReceiveShipperCnname(String receiveShipperCnname) {
this.receiveShipperCnname = receiveShipperCnname;
}
public String getReceiveShipperNo() {
return receiveShipperNo;
}
public void setReceiveShipperNo(String receiveShipperNo) {
this.receiveShipperNo = receiveShipperNo;
}
public String getOrderType() {
return orderType;
}
public void setOrderType(String orderType) {
this.orderType = orderType;
}
@Override
public String toString() {
return "CustomsDeclaration{" +
"customsDeclarationId=" + customsDeclarationId +
", orderId=" + orderId +
", ocrType='" + ocrType +
", containerNo='" + containerNo +
", textContent='" + textContent +
", filepath='" + filepath +
", shipperCnname='" + shipperCnname +
", shipperNo='" + shipperNo +
", receiveShipperCnname='" + receiveShipperCnname +
", receiveShipperNo='" + receiveShipperNo +
", declareDate=" + declareDate +
", productComponyno='" + productComponyno +
", productCompony='" + productCompony +
", arriveCountry='" + arriveCountry +
", totalWeight=" + totalWeight +
", goodsNo='" + goodsNo +
", goodsCnname='" + goodsCnname +
", allgoodsNo='" + allgoodsNo +
", allgoodsCnname='" + allgoodsCnname +
", goodsSource='" + goodsSource +
", goodsSourceno='" + goodsSourceno +
", declareUnitno='" + declareUnitno +
", declareUnit='" + declareUnit +
", countAndNo='" + countAndNo +
", remark='" + remark +
", addUserId=" + addUserId +
", addTime=" + addTime +
", modifyUserId=" + modifyUserId +
", modifyTime=" + modifyTime +
", departureStation='" + departureStation +
", arriveStation='" + arriveStation +
", shipperCompanyName='" + shipperCompanyName +
", arriveCompanyName='" + arriveCompanyName +
", carrierSureWeight=" + carrierSureWeight +
", orderType='" + orderType +
'}';
}
}
class Goodsinfo {
public Goodsinfo(String goodsNo, String goodsCnname, String goodsEnname) {
this.goodsNo = goodsNo;
this.goodsCnname = goodsCnname;
this.goodsEnname = goodsEnname;
}
public Goodsinfo() {
}
/**
* 商品id
*/
private Long goodinfoId;
/**
* 商品编号
*/
private String goodsNo;
/**
* 商品中文名
*/
private String goodsCnname;
/**
* 商品英文名
*/
private String goodsEnname;
/**
* 备注
*/
private String remark;
/**
* 添加人id
*/
private Long addUserId;
/**
* 添加时间
*/
private Date addTime;
/**
* 修改人id
*/
private Long modifyUserId;
/**
* 最后更新时间
*/
private Date modifyTime;
public Long getGoodinfoId() {
return goodinfoId;
}
public void setGoodinfoId(Long goodinfoId) {
this.goodinfoId = goodinfoId;
}
public String getGoodsNo() {
return goodsNo;
}
public void setGoodsNo(String goodsNo) {
this.goodsNo = goodsNo;
}
public String getGoodsCnname() {
return goodsCnname;
}
public void setGoodsCnname(String goodsCnname) {
this.goodsCnname = goodsCnname;
}
public String getGoodsEnname() {
return goodsEnname;
}
public void setGoodsEnname(String goodsEnname) {
this.goodsEnname = goodsEnname;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public Long getAddUserId() {
return addUserId;
}
public void setAddUserId(Long addUserId) {
this.addUserId = addUserId;
}
public Date getAddTime() {
return addTime;
}
public void setAddTime(Date addTime) {
this.addTime = addTime;
}
public Long getModifyUserId() {
return modifyUserId;
}
public void setModifyUserId(Long modifyUserId) {
this.modifyUserId = modifyUserId;
}
public Date getModifyTime() {
return modifyTime;
}
public void setModifyTime(Date modifyTime) {
this.modifyTime = modifyTime;
}
@Override
public String toString() {
return "Goodsinfo{" +
"goodinfoId=" + goodinfoId +
", goodsNo=" + goodsNo +
", goodsCnname=" + goodsCnname +
", goodsEnname=" + goodsEnname +
", remark=" + remark +
", addUserId=" + addUserId +
", addTime=" + addTime +
", modifyUserId=" + modifyUserId +
", modifyTime=" + modifyTime +
"}";
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Goodsinfo goodsinfo = (Goodsinfo) o;
return goodsNo.equals(goodsinfo.goodsNo) && goodsCnname.equals(goodsinfo.goodsCnname);
}
@Override
public int hashCode() {
return goodsNo.hashCode() + goodsCnname.hashCode();
}
}
}
0-9 ↩︎