影像第三方上传优化;影像资源请求和OCR识别

This commit is contained in:
ap007 2022-07-25 09:31:45 +08:00
parent 8ef28c5c3c
commit 75df22d142
12 changed files with 470 additions and 38 deletions

View File

@ -45,12 +45,36 @@
String sButtons[][] = {
{flag,"All","Button","保存","保存所有修改","save()","","","",""},
{"true","All","Button","·µ»Ø","·µ»ØÁбí","returnList()","","","",""}
{"true","All","Button","返回","返回列表","returnList()","","","",""},
{"true","","Button","识别发票","识别发票","invoiceOrcDo()","","","",""}
};
sButtonPosition = "north";
%><%@ include file="/Frame/resources/include/ui/include_info.jspf"%>
<script type="text/javascript">
function invoiceOrcDo(){
let sResult = RunJavaMethodTrans("com.ample.icms.service.ImageOcrService","invoiceOrcDo","contractId=<%=ContractId%>,flowNo=FundPaymentCarFlow");
if(sResult!=null&&sResult.error_code==0&&sResult.result.发票号码!=''){
const resultList = sResult.result;
let Invoice_code = resultList.发票号码;
let Invoice_number = resultList.发票代码;
let Invoice_date = resultList.开票日期;
Invoice_date = Invoice_date.substr(0,4)+'/'+Invoice_date.substr(4,2)+'/'+Invoice_date.substr(6,2);
let Invoice_money = resultList.不含税价小写;
let tax_due = resultList.增值税税额;
let tax_rate = resultList.增值税税率或征收税率;
setItemValue(0,0,"Invoice_code", Invoice_code);
setItemValue(0,0,"Invoice_number", Invoice_number);
setItemValue(0,0,"Invoice_date", Invoice_date);
setItemValue(0,0,"Invoice_money", Invoice_money);
setItemValue(0,0,"tax_due", tax_due);
//setItemValue(0,0,"tax_rate", tax_rate);
alert('识别成功');
}else{
alert('识别失败');
}
}
//自动计算税金及开票金额合计
function checkInvoiceMoney(){
var Invoice_money = parseFloat(getItemValue(0,getRow(0),"Invoice_money"));

View File

@ -45,7 +45,7 @@
{btnflag,"","Button","查询签约结果","查询签约结果","getStatus()","","","","btn_icon_obtain",""},
{btnflag,"","Button","重新发送短信","重新发送短信","sendMessageAgain()","","","","btn_icon_refresh",""},
{signs,"","Button","公司盖章","公司盖章","sign()","","","","btn_icon_refresh",""},
{"true","","Button","上传合同","上传合同","uploadContract()","","","","btn_icon_refresh",""},
{"true","","Button","上传合同","暂时测试上传用按钮","uploadContract()","","","","btn_icon_refresh",""},
};
%><%@include file="/Frame/resources/include/ui/include_list.jspf"%>
<style>
@ -57,8 +57,8 @@
function uploadContract(){
//因为要传到付款阶段的合同套系所以这里的flowNo固定为FundPaymentCarFlow
var sParams="ProjectId=<%=projectId%>,flowUnid=<%=flowunid%>,projectNo=<%=ProjectNo%>,flowNo=FundPaymentCarFlow";
var sReturn = RunJavaMethod("com.ample.icms.service.ImageUploadService","upload",sParams);
alert(sReturn);
var sReturn = RunJavaMethodTrans("com.ample.icms.service.ImageUploadService","upload",sParams);
// alert(sReturn);
}
//测试电子签约
function sign(){
@ -202,6 +202,9 @@
}else{
sReturn = RunJavaMethodTrans("com.tenwa.channelportal.action.ContractSignAction","contractSignStatus",sParams);
}
if(sReturn=='已完成'){
uploadContract();
}
AsDebug.showMessage("提示",sReturn,"","",true,'','','',function(){
<%-- var sReturns = RunJavaMethodTrans("com.tenwa.channelportal.action.ContractSignAction","downloadDoc","ProjectId=<%=projectId%>,CurUserID=<%=CurUser.getUserID()%>"); --%>
window.location.reload();

View File

@ -110,7 +110,6 @@
}
function getRequestParam(){
let appInfo = RunJavaMethod("com.ample.icms.util.GetInfoUtil", "getAppInfoByFlowUnidRJM", "flowUnid=<%=sFlowUnid%>");
debugger;
let index = appInfo.indexOf("@");
if(index==-1){
alert(appInfo);

View File

@ -21,12 +21,12 @@
dwTemp.Style = "2";//freeform
dwTemp.ReadOnly = "-2";//怜뗍친駕
dwTemp.genHTMLObjectWindow(CurPage.getParameter("FlowUnid"));
String sButtons[][] = {
//{"true","All","Button","괏닸","괏닸杰唐錦맣","as_save(0)","","","",""},
//{String.valueOf(!com.amarsoft.are.lang.StringX.isSpace(sPrevUrl)),"All","Button","럿쀼","럿쀼죗깊","returnList()","","","",""}
{"true","","Button","栗죕헌데","栗죕헌데","openFileList()","","","","btn_icon_saveNew",""},
{"true","","Button","마운<EBA788>헝栗죕헌데","栗죕헌데","openPayFileList()","","","","btn_icon_saveNew",""},
{"0010".equals(sPhaseNo)?"false":"true","","Button","마운<EBA788>헝栗죕헌데","栗죕헌데","openPayFileList()","","","","btn_icon_saveNew",""},
};
//sButtonPosition = "south";
%><%@ include file="/Frame/resources/include/ui/include_info.jspf"%>

View File

@ -16,14 +16,13 @@ public class IcmsRelationInit extends BaseBussiness {
BizObject boLPIT = JBOFactory.getBizObjectManager(LB_PROJECT_INFO_TEMP.CLASS_NAME,tx).createQuery("flowunid=:flowunid").setParameter("flowunid",this.getAttribute("ObjectNo").toString()).getSingleResult(false);
String projectNo = boLPIT.getAttribute("project_no").toString();
String productId = boLPIT.getAttribute("product_id").toString();
SqlObject sql_icms = new SqlObject("select flowno,app_code,app_name,is_must from icms_prd_rela where productid='"+productId+"' group by flowno,app_code");
SqlObject sql_icms = new SqlObject("select flowno,app_code,app_name from icms_prd_rela where productid='"+productId+"' group by flowno,app_code");
ASResultSet rs = Sqlca.getASResultSet(sql_icms);
while (rs.next()){
String flowNo = rs.getString("flowno");
String appCode = rs.getString("app_code");
String appName = rs.getString("app_name");
String isMust = rs.getString("is_must");
SqlObject sql_rela = new SqlObject("insert into icms_rela (id,project_no,flow_no,app_code,app_name,is_must) values(REPLACE(UUID(), '-', ''),'"+projectNo+"','"+flowNo+"','"+appCode+"','"+appName+"','"+isMust+"')");
SqlObject sql_rela = new SqlObject("insert into icms_rela (id,project_no,flow_no,app_code,app_name) values(REPLACE(UUID(), '-', ''),'"+projectNo+"','"+flowNo+"','"+appCode+"','"+appName+"')");
Sqlca.executeSQL(sql_rela);
}
String sMessage="true";

View File

@ -0,0 +1,13 @@
package com.ample.icms.query;
import com.ample.icms.service.ImageService;
public class ImageInfo extends ImageService {
public String getResult(){
this.setCode("ECM0010");
String result = this.doPost();
return result;
}
}

View File

@ -0,0 +1,163 @@
package com.ample.icms.scan;
import com.ample.icms.util.PropertiesUtil;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import sun.misc.BASE64Encoder;
import java.io.*;
import java.net.URL;
public class ImageOcr {
public enum ocrType{
id_card,bank_card_ocr,vehicle_invoice_ocr
}
public String doPost(String fileUrl,ocrType ocrType) {
String responseResult = null;
//图像以base64编码传输方式
try {
responseResult = imagePostBybase64(PropertiesUtil.get("OcrUrl")+ocrType, fileUrl);
} catch (Exception e) {
e.printStackTrace();
}
return responseResult;
}
public String imagePostBybase64(String httpUrl, String destFile) {
HttpPost httpPost = new HttpPost(httpUrl);
CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpResponse responseRes = null;
String resultStr = null;
try {
//image参数为在服务端获取的key通过image这个参数可以获取到传递的字节流,这里不一定就是image,你的服务端使用什么这里就对应给出什么参数即可
StringEntity stringEntity = new StringEntity("image=" + getImgStrByUrl(destFile,false));
stringEntity.setContentType("application/x-www-form-urlencoded");
httpPost.setEntity(stringEntity);
responseRes = httpclient.execute(httpPost);
int status = responseRes.getStatusLine().getStatusCode();
if (status == 200) {
byte[] content;
try {
content = getContent(responseRes);
resultStr = new String(content, "utf-8");
System.out.println("httpPost返回的结果==:" + resultStr);
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (resultStr != null) {
return resultStr;
} else {
return "";
}
}
/**
* 将图片转换成Base64编码
*
* @param imgFile 待处理图片
* @return
*/
public String getImgStrByUrl(String imgFile,boolean isLocal) throws IOException {
//将图片文件转化为字节数组字符串并对其进行Base64编码处理
InputStream in = null;
byte[] data = null;
if(isLocal){
data = getImageFromLocalByUrl(imgFile);
}else{
data = getImageFromNetByUrl(imgFile);
}
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);
}
private byte[] getContent(HttpResponse response) throws IOException {
InputStream result = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
result = resEntity.getContent();
return readInputStream(result);
}
} catch (IOException e) {
e.printStackTrace();
throw new IOException("getContent异常", e);
} finally {
out.close();
if (result != null) {
result.close();
}
}
return null;
}
/**
* 根据地址获得数据的字节流
*
* @param strUrl
* 本地连接地址
* @return
*/
public byte[] getImageFromLocalByUrl(String strUrl) throws IOException {
File imageFile = new File(strUrl);
InputStream inStream = new FileInputStream(imageFile);
byte[] btImg = readInputStream(inStream);//得到图片的二进制数据
return btImg;
}
/**
* 根据地址获得数据的字节流(相当于 URL.openStream()源码)
*
* @param strUrl
* 网络连接地址
* @return
*/
public byte[] getImageFromNetByUrl(String strUrl) throws IOException {
URL url = new URL(strUrl);
/*HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5 * 1000);
InputStream inStream = conn.getInputStream();// 通过输入流获取图片数据*/
InputStream inStream = url.openStream();
byte[] btImg = readInputStream(inStream);// 得到图片的二进制数据
return btImg;
}
/**
* 从输入流中获取数据
*
* @param inStream
* 输入流
* @return
* @throws Exception
*/
public byte[] readInputStream(InputStream inStream) throws IOException {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[10240];
int len = 0;
while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
inStream.close();
return outStream.toByteArray();
}
}

View File

@ -19,13 +19,14 @@ import java.util.ArrayList;
import java.util.List;
public class ImageUpload extends ImageService {
private String nodeId;
private List<ImagePage> pageList;
private static final String FileSavePath = PropertiesUtil.get("fileSavePathTemp");
public List<ImageNode> addNodes(List<ImagePage> pages) throws Exception {
ImageNode node = new ImageNode();
//融资租赁合同套系
node.setId("APZLJJ000400");
node.setId(nodeId);
node.setAction("ADD");
node.setPage(pages);
List<ImageNode> nodes = new ArrayList<>();
@ -77,4 +78,11 @@ public class ImageUpload extends ImageService {
this.pageList = pageList;
}
public String getNodeId() {
return nodeId;
}
public void setNodeId(String nodeId) {
this.nodeId = nodeId;
}
}

View File

@ -0,0 +1,100 @@
package com.ample.icms.service;
import com.amarsoft.are.jbo.JBOException;
import com.amarsoft.are.jbo.JBOTransaction;
import com.ample.icms.query.ImageInfo;
import com.ample.icms.util.GetInfoUtil;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import java.sql.SQLException;
public class ImageInfoService {
private String projectId;
private String contractId;
private String projectNo;
private String flowNo;
public String getFileUrl(JBOTransaction tx) throws Exception {
String result = this.getResponseResult(tx);
//APZLJJ000400 APZLJJ000060
String FileUrl = this.getFileUrlByResultAnalysis(result,"APZLJJ000060");
return FileUrl;
}
public String getResponseResult(JBOTransaction tx) throws SQLException, JBOException {
GetInfoUtil gif = new GetInfoUtil();
this.setProjectNo(gif.getProjectNoByContractId(contractId,tx));
String appInfo = gif.getAppInfoByProjectNo(projectNo,flowNo,tx);
String[] app = appInfo.split("@");
String appCode= app[0];
String appName= app[1];
ImageInfo ii = new ImageInfo();
ii.setAppCode(appCode);
ii.setAppName(appName);
ii.setBusiNo(projectNo);
String result = ii.getResult();
return result;
}
public String getFileUrlByResultAnalysis(String result,String folderCode) throws Exception {
if("0000".equals(result)){
throw new Exception("连接影像系统失败,请及时联系管理员!");
}else {
Document dom = DocumentHelper.parseText(result);
Element root = dom.getRootElement();
Element node = (Element) root.selectSingleNode("//NODE[@ID='"+folderCode+"']");
/*List nodes = root.element("SYD").element("doc").element("VTREE").elements("NODE");
for (Iterator it = nodes.iterator(); it.hasNext();){
Element elm = (Element) it.next();
}*/
if(node==null){
throw new Exception("未找到对应的文件信息");
}
String pageId = node.element("LEAF").getTextTrim();
if("".equals(pageId)||pageId==null){
throw new Exception("未找到对应的图片信息");
}
Element page = (Element) root.selectSingleNode("//PAGE[@PAGEID='"+pageId+"']");
String pageUrl = page.attributeValue("PAGE_URL");
System.out.println("OCR文件路径"+pageUrl);
return pageUrl;
}
}
public String getProjectId() {
return projectId;
}
public void setProjectId(String projectId) {
this.projectId = projectId;
}
public String getProjectNo() {
return projectNo;
}
public void setProjectNo(String projectNo) {
this.projectNo = projectNo;
}
public String getFlowNo() {
return flowNo;
}
public void setFlowNo(String flowNo) {
this.flowNo = flowNo;
}
public String getContractId() {
return contractId;
}
public void setContractId(String contractId) {
this.contractId = contractId;
}
}

View File

@ -0,0 +1,54 @@
package com.ample.icms.service;
import com.amarsoft.are.jbo.JBOTransaction;
import com.ample.icms.scan.ImageOcr;
public class ImageOcrService {
private String projectId;
private String contractId;
private String projectNo;
private String flowNo;
public String invoiceOrcDo(JBOTransaction tx) throws Exception {
ImageInfoService iis = new ImageInfoService();
iis.setContractId(contractId);
iis.setFlowNo(flowNo);
String fileUrl = iis.getFileUrl(tx);
ImageOcr io = new ImageOcr();
String result = io.doPost(fileUrl, ImageOcr.ocrType.vehicle_invoice_ocr);
return result;
}
public String getProjectId() {
return projectId;
}
public void setProjectId(String projectId) {
this.projectId = projectId;
}
public String getProjectNo() {
return projectNo;
}
public void setProjectNo(String projectNo) {
this.projectNo = projectNo;
}
public String getFlowNo() {
return flowNo;
}
public void setFlowNo(String flowNo) {
this.flowNo = flowNo;
}
public String getContractId() {
return contractId;
}
public void setContractId(String contractId) {
this.contractId = contractId;
}
}

View File

@ -1,18 +1,19 @@
package com.ample.icms.service;
import com.amarsoft.are.jbo.BizObject;
import com.amarsoft.are.jbo.BizObjectManager;
import com.amarsoft.are.jbo.JBOException;
import com.amarsoft.are.jbo.JBOFactory;
import com.amarsoft.are.jbo.*;
import com.amarsoft.are.util.StringFunction;
import com.amarsoft.dict.als.manage.NameManager;
import com.ample.icms.bean.image.ImagePage;
import com.ample.icms.query.ImageCount;
import com.ample.icms.scan.ImageUpload;
import com.ample.icms.util.GetInfoUtil;
import com.ample.icms.util.PropertiesUtil;
import com.tenwa.officetempalte.exportcallback.impl.ZipUtils;
import jbo.app.tenwa.doc.LB_DOC_CONTRACT_LIST;
import org.apache.commons.io.FileUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import java.io.File;
import java.io.FileOutputStream;
@ -22,11 +23,13 @@ import java.util.List;
public class ImageUploadService {
private String projectId;
private String flowUnid;
private String projectNo;
private String flowNo;
private String appCode;
private String appName;
private String projectNo;
private final String FILE_SAVE_PATH_TEMP = PropertiesUtil.get("fileSavePathTemp");
public List<BizObject> getFileBo() throws JBOException {
BizObjectManager bom = JBOFactory.getBizObjectManager(LB_DOC_CONTRACT_LIST.CLASS_NAME);
public List<BizObject> getFileBo(JBOTransaction tx) throws JBOException {
BizObjectManager bom = JBOFactory.getBizObjectManager(LB_DOC_CONTRACT_LIST.CLASS_NAME,tx);
List<BizObject> bos = bom.createQuery("sign_type='COMPLETE' and file_flag='yes' and project_id=:projectId and flow_unid=:flowUnid")
.setParameter("projectId",projectId).setParameter("flowUnid",flowUnid).getResultList(false);
return bos;
@ -65,29 +68,34 @@ public class ImageUploadService {
}
return pages;
}
public String[] getAppInfo() throws Exception {
public void getAppInfo(JBOTransaction tx) throws Exception {
GetInfoUtil gif = new GetInfoUtil();
String appInfo = gif.getAppInfoByProjectNo(projectNo,flowNo);
String appInfo = gif.getAppInfoByProjectNo(projectNo,flowNo,tx);
String[] app = appInfo.split("@");
return app;
this.setAppCode(app[0]);
this.setAppName(app[1]);
}
public String upload() throws Exception {
public String upload(JBOTransaction tx) throws Exception {
/**
* 1.找出需要上传的影像
* 2.在指定路径添加对应xml文件
* 3.打包上传
* 4.删除复制文件和打包文件
* 1.判断是否有合同套系这个文件分类
* 2.找到对应的文件并复制过去
* 3.生成对应的xml
* 4.打包上传
* 5.删除复制文件和打包文件
*/
List<BizObject> boList = this.getFileBo();
this.getAppInfo(tx);
String fileCode = "APZLJJ000400";//APZLJJ000400为其它,APZLJJ000080为合同套系在影像系统查找核心系统已经没有对应信息
if(this.isHasContractFolder()){
fileCode = "APZLJJ000080";
}
ImageUpload imageUp = new ImageUpload();
imageUp.setAppCode(appCode);
imageUp.setAppName(appName);
imageUp.setBusiNo(projectNo);
imageUp.setNodeId(fileCode);
List<BizObject> boList = this.getFileBo(tx);
List<File> files = this.getFile(boList);
List<ImagePage> pageList = this.getPages(boList);
ImageUpload imageUp = new ImageUpload();
String[] appInfo = this.getAppInfo();
imageUp.setAppCode(appInfo[0]);
imageUp.setAppName(appInfo[1]);
imageUp.setBusiNo(projectNo);
imageUp.setPageList(pageList);
String xmlUrl = imageUp.createXMl();
@ -99,7 +107,7 @@ public class ImageUploadService {
String result = imageUp.doUpload(zipUrl);
String returnCode = result.substring(0,result.indexOf("@"));
String returnMsg = result.substring(result.indexOf("@")+1,result.length());
// deleteTemp();
deleteTemp();
return returnMsg;
}
/**
@ -113,6 +121,44 @@ public class ImageUploadService {
}
}
public boolean isHasContractFolder() throws Exception {
ImageCount ic = new ImageCount();
ic.setAppCode(appCode);
ic.setAppName(appName);
ic.setBusiNo(projectNo);
String countResult = ic.getResult();
if("0000".equals(countResult)){
String responseMsg = "连接影像系统失败,请及时联系管理员!";
throw new Exception(responseMsg);
}
Document dom = DocumentHelper.parseText(countResult);
Element root = dom.getRootElement();
String responseCode = root.element("RESPONSE_CODE").getTextTrim();
if("400".equals(responseCode)){
String responseMsg = root.element("RESPONSE_MSG").getTextTrim();
throw new Exception(responseMsg);
}
if(root.toString().contains("APZLJJ000080")){
return true;
}
//todo
/* 以后再改良先用上面的判断下
String fileCode = "APZLJJ000400";//APZLJJ000400为其它,APZLJJ000080为合同套系在影像系统查找核心系统已经没有对应信息
List nodes = root.element("ATREE_ALL").elements("NODE");
for (Iterator it = nodes.iterator(); it.hasNext();) {
Element elm = (Element) it.next();
String newFileCode = elm.element("DOC_CODE").getTextTrim();
String newFileName = elm.element("DOC_NAME").getTextTrim();
//APZLJJ000080为合同套系
if("APZLJJ000080".equals(newFileCode)||newFileName.contains("合同套系")){
fileCode = "APZLJJ000080";
break;
}
}
*/
return false;
}
public String getProjectId() {
return projectId;
}
@ -144,4 +190,20 @@ public class ImageUploadService {
public void setFlowNo(String flowNo) {
this.flowNo = flowNo;
}
public String getAppCode() {
return appCode;
}
public void setAppCode(String appCode) {
this.appCode = appCode;
}
public String getAppName() {
return appName;
}
public void setAppName(String appName) {
this.appName = appName;
}
}

View File

@ -2,9 +2,9 @@ package com.tenwa.officetempalte.exportcallback.impl;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@ -46,20 +46,27 @@ public class ZipUtils {
}
}
public void toZip(List<File> srcFiles, OutputStream out) throws RuntimeException {
//有些地方需要将文件转换为gbk编码为了之前别的引用可以继续用默认设为utf-8原ZipOutputStream(OutputStream out)构造方法默认就是utf-8
this.toZip(srcFiles,out,Charset.forName("UTF-8"));
}
/**
* 压缩成ZIP 方法2
* @param srcFiles
* 需要压缩的文件列表
* @param out
* 压缩文件输出流
* @param cs
* 编码格式
* @throws RuntimeException
* 压缩失败会抛出运行时异常
*/
public void toZip(List<File> srcFiles, OutputStream out) throws RuntimeException {
public void toZip(List<File> srcFiles, OutputStream out,Charset cs) throws RuntimeException {
long start = System.currentTimeMillis();
ZipOutputStream zos = null;
try {
zos = new ZipOutputStream(out);
zos = new ZipOutputStream(out,cs);
for (File srcFile : srcFiles) {
byte[] buf = new byte[BUFFER_SIZE];
zos.putNextEntry(new ZipEntry(srcFile.getName()));