zthwr 2 роки тому
батько
коміт
1564621d10
1 змінених файлів з 155 додано та 0 видалено
  1. 155 0
      pu/pu/src/client/nc/pub/filesystem/newui/ThOCRAction.java

+ 155 - 0
pu/pu/src/client/nc/pub/filesystem/newui/ThOCRAction.java

@@ -0,0 +1,155 @@
+package nc.pub.filesystem.newui;
+
+import java.awt.event.ActionEvent;
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.swing.AbstractAction;
+import javax.swing.Action;
+
+import org.apache.commons.httpclient.HttpException;
+import nc.bs.framework.common.NCLocator;
+import nc.bs.framework.common.RuntimeEnv;
+import nc.bs.logging.Logger;
+import nc.funcnode.ui.action.AbstractNCAction;
+import nc.itf.pu.service.IInvoiceOCRMaintain;
+import nc.itf.th.IThOcrService;
+import nc.ui.ml.NCLangRes;
+import nc.ui.pub.beans.MessageDialog;
+import nc.ui.pub.bill.BillCardPanel;
+import nc.vo.pub.BusinessException;
+
+
+public class ThOCRAction extends AbstractNCAction{
+
+	/**
+	 * OCR识别
+	 */
+	private static final long serialVersionUID = -5178574564280732585L;
+	private static final String upurl="https://up.mypiaojia.com/api/invoiceApi/file/uploadFileSpilit";
+	private static final String inverurl="https://api.mypiaojia.com/api/invoiceApi/invoice/ocr";
+	private static final String appId="773676128773832704";
+	private static final Long   cid=773676128725336064l;
+	private static final String secret="6726dea9883f4f6bb2725ac8750d01de";
+	private static final String useid="2108";
+	
+	private ThFileContentPanel parentpanel ;
+	public ThOCRAction(ThFileContentPanel para){
+		parentpanel = para;
+		this.putValue(AbstractAction.NAME,"OCR识别");
+		this.putValue(Action.SHORT_DESCRIPTION, "OCR识别");
+	}
+
+	@Override
+	public void actionPerformed(ActionEvent actionevent) {
+		MessageDialog.showHintDlg(parentpanel, "提示","OCR识别!");
+		
+		String vdef17 =(String) parentpanel.editor_.getBillCardPanel().getHeadItem("vdef17").getValueObject();//附件id
+		
+		String filepath =  RuntimeEnv.getInstance().getNCHome()
+			      +File.separator +"uploadfile" + File.separator+ vdef17; // 文件保存的路径
+		
+		List<String> filewjlis=getFileNames(filepath);
+		
+		IThOcrService  ocrservice=(IThOcrService) NCLocator.getInstance().lookup(IThOcrService.class.getName());
+		
+//		IInvoiceOCRMaintain iInvoiceOCRMaintain = (IInvoiceOCRMaintain) NCLocator.getInstance().lookup(IInvoiceOCRMaintain.class.getName());
+		
+		
+		//获取token
+		String token="";
+		try {
+			 token=ocrservice.getToken(appId,cid,secret,useid);
+		} catch (HttpException e) {
+	//		Logger.error(e.getMessage(), e);
+			MessageDialog.showErrorDlg(parentpanel, NCLangRes.getInstance().getStrByID("pc", "FileManageUI-000000")/*错误*/
+					,"获取token:\n"+e.getMessage());
+			return;
+		} catch (IOException e) {
+			MessageDialog.showErrorDlg(parentpanel, NCLangRes.getInstance().getStrByID("pc", "FileManageUI-000000")/*错误*/
+					,"获取token:\n"+e.getMessage());
+			return;
+		} catch (BusinessException e) {
+			MessageDialog.showErrorDlg(parentpanel, NCLangRes.getInstance().getStrByID("pc", "FileManageUI-000000")/*错误*/
+					,"获取token:\n"+e.getMessage());
+			return;
+		}
+		
+		MessageDialog.showHintDlg(parentpanel, "提示","token:"+token);
+		StringBuffer sb=new StringBuffer();
+		StringBuffer sb2=new StringBuffer();
+		StringBuffer sb3=new StringBuffer();
+		for(int i=0;i<filewjlis.size();i++){
+			File file=new File(filepath+File.separator+filewjlis.get(i));
+			//发票上传
+			String idjson="";
+			try {
+				idjson=ocrservice.uploadInvoice(upurl,token, file);
+			} catch (Exception e) {
+				MessageDialog.showErrorDlg(parentpanel, NCLangRes.getInstance().getStrByID("pc", "FileManageUI-000000")/*错误*/
+						,"发票上传:\n"+e.getMessage());
+				return;
+			}
+			
+			sb.append(idjson).append("\n");
+			
+			//发票识别
+			String ocrinvoicejson=ocrservice.ocrIdentification(inverurl, token,idjson);
+			sb2.append(ocrinvoicejson).append("\n");
+			
+			
+//			JSONObject scanjb = iInvoiceOCRMaintain.startInvoiceOCR(file);
+//			sb3.append(scanjb.toString());
+			
+			//识别数据保存数据库
+		}
+		
+		MessageDialog.showHintDlg(parentpanel, "提示","发票上传json:\n"+sb.toString());
+		MessageDialog.showHintDlg(parentpanel, "提示","ocr识别json:\n"+sb2.toString());
+	//	MessageDialog.showHintDlg(parentpanel, "提示","ocr识别YY:\n"+sb3.toString());
+		
+		
+		
+		
+		
+		
+	}
+	
+	
+	/**
+     * 得到文件名称
+     *
+     * @param path 路径
+     * @return {@link List}<{@link String}>
+     */
+    private  List<String> getFileNames(String path) {
+        File file = new File(path);
+        if (!file.exists()) {
+            return null;
+        }
+        List<String> fileNames = new ArrayList<String>();
+        return getFileNames(file, fileNames);
+    }
+
+/**
+     * 得到文件名称
+     *
+     * @param file      文件
+     * @param fileNames 文件名
+     * @return {@link List}<{@link String}>
+     */
+    private  List<String> getFileNames(File file, List<String> fileNames) {
+        File[] files = file.listFiles();
+        for (File f : files) {
+            if (f.isDirectory()) {
+                getFileNames(f, fileNames);
+            } else {
+                fileNames.add(f.getName());
+            }
+        }
+        return fileNames;
+    }
+
+}