Commit 1cbfe5b1 by 雷紫添

添加ocr

parent e30aa59f
package com.founder.controller;
import com.alibaba.fastjson.JSONObject;
import com.founder.service.OcrService;
import org.apache.ibatis.annotations.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import sun.misc.BASE64Encoder;
@RestController
@RequestMapping(value="/ocr",produces = "application/json; charset=utf-8")
public class OcrController {
@Autowired
private OcrService ocrjService;
@ResponseBody
@PostMapping("/InvoiceOcr")
public JSONObject query(HttpServletRequest request, MultipartFile file) {
byte[] fileByte = null;
try {
fileByte = file.getBytes();
} catch (IOException e) {
}
BASE64Encoder encoder = new BASE64Encoder();
String voiceBase64= encoder.encode(fileByte);
JSONObject j = new JSONObject();
j.put("msg",ocrjService.getOcr(voiceBase64));
return j;
}
}
package com.founder.service;
import com.founder.model.Jccj;
import org.springframework.web.multipart.MultipartFile;
import java.util.Base64;
import java.util.Map;
/**
* Created by wangxing on 2019/8/14.
*/
public interface OcrService {
String getOcr(String encoder);
}
package com.founder.service.impl;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.founder.service.OcrService;
import com.founder.util.InvoiceOcr;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.springframework.stereotype.Service;
/**
* Created by wangxing on 2019/8/14.
*/
@Service
public class OcrServiceImpl implements OcrService {
@Override
public String getOcr(String encoder) {
InvoiceOcr ac = new InvoiceOcr(apiURL);
JSONArray arry = new JSONArray();
JSONObject j = new JSONObject();
arry.add(encoder);
j.put("images",arry);
String result = ac.post(j.toJSONString());
System.out.println(result);
return result;
}
//接口地址
private static String apiURL = "http://47.92.223.200:8866/predict/chinese_ocr_db_crnn_server";
//private Log logger = LogFactory.getLog(this.getClass());
private HttpClient httpClient = null;
private HttpPost method = new HttpPost(apiURL);
private long startTime = 0L;
private long endTime = 0L;
private int status = 0;
}
package com.founder.util;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import sun.misc.BASE64Encoder;
public class InvoiceOcr {
//接口地址
private static String apiURL = "http://47.92.223.200:8866/predict/chinese_ocr_db_crnn_server";
//private Log logger = LogFactory.getLog(this.getClass());
private HttpClient httpClient = null;
private HttpPost method = null;
private long startTime = 0L;
private long endTime = 0L;
private int status = 0;
/**
* 接口地址
*
* @param url
*/
public InvoiceOcr(String url) {
if (url != null) {
this.apiURL = url;
}
if (apiURL != null) {
httpClient = new DefaultHttpClient();
method = new HttpPost(apiURL);
}
}
/**
* 调用 API
*
* @param parameters
* @return
*/
public String post(String parameters) {
String body = null;
if (method != null & parameters != null && !"".equals(parameters.trim())) {
try {
// 建立一个NameValuePair数组,用于存储欲传送的参数
method.addHeader("Content-type","application/json");
method.setHeader("Accept", "application/json");
method.setEntity(new StringEntity(parameters, Charset.forName("UTF-8")));
startTime = System.currentTimeMillis();
Date date = new Date();
HttpResponse response = httpClient.execute(method);
DateFormat df2 = DateFormat.getDateTimeInstance();//可以精确到时分秒
System.out.println(df2.format(date)+"开始时间");
endTime = System.currentTimeMillis();
int statusCode = response.getStatusLine().getStatusCode();
//logger.info("statusCode:" + statusCode);
//logger.info("调用API 花费时间(单位:毫秒):" + (endTime - startTime));
if (statusCode != HttpStatus.SC_OK) {
//logger.error("Method failed:" + response.getStatusLine());
status = 1;
}
body = EntityUtils.toString(response.getEntity(),"utf-8");
} catch (IOException e) {
// 网络错误
status = 3;
System.out.println(e);
} finally {
//logger.info("调用接口状态:" + status);
}
}
Date date1 = new Date();
DateFormat df3 = DateFormat.getDateTimeInstance();//可以精确到时分秒
System.out.println(df3.format(date1)+"结束时间");
return body;
}
public static void main(String[] args) throws ParseException {
InvoiceOcr ac = new InvoiceOcr(apiURL);
JSONArray arry = new JSONArray();
JSONObject j = new JSONObject();
arry.add(imageToBase64("D:\\44.png"));
j.put("images",arry);
String result = ac.post(j.toJSONString());
System.out.println(result);
}
public static String imageToBase64(String path) {
byte[] data = null;
// 读取图片字节数组
try {
InputStream in = new FileInputStream(path);
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
// 对字节数组Base64编码
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);// 返回Base64编码过的字节数组字符串
}
/**
* 0.成功 1.执行方法失败 2.协议错误 3.网络错误
*
* @return the status
*/
public int getStatus() {
return status;
}
/**
* @param status
* the status to set
*/
public void setStatus(int status) {
this.status = status;
}
/**
* @return the startTime
*/
public long getStartTime() {
return startTime;
}
/**
* @return the endTime
*/
public long getEndTime() {
return endTime;
}
}
...@@ -4,7 +4,8 @@ spring: ...@@ -4,7 +4,8 @@ spring:
jackson: jackson:
date-format: yyyy-MM-dd HH:mm:ss date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8 time-zone: GMT+8
mvc:
static-path-pattern: /**
mybatis: mybatis:
#type-aliases-package: com.founder.interservice.model #type-aliases-package: com.founder.interservice.model
...@@ -24,3 +25,5 @@ ssdw: 140000000000 ...@@ -24,3 +25,5 @@ ssdw: 140000000000
server: server:
port: 9061 port: 9061
@charset "UTF-8";
/*css 初始化 */
html,
body,
ul,
li,
ol,
dl,
dd,
dt,
p,
h1,
h2,
h3,
h4,
h5,
h6,
form,
fieldset,
legend,
img {
margin: 0;
padding: 0;
}
fieldset,
img,
input,
button {
border: none;
padding: 0;
margin: 0;
outline-style: none;
}
/*清除边框外发光 */
ul,
ol {
list-style: none;
/* 清除左侧小圆点标注 */
}
input {
padding-top: 0;
padding-bottom: 0;
font-family: "SimSun", "宋体";
}
select,
input {
vertical-align: middle;
}
select,
input,
textarea {
font-size: 12px;
margin: 0;
}
textarea {
resize: none;
/*防止拖动*/
}
img {
border: 0;
vertical-align: middle;
/* 去掉图片底部默认的3像素空白缝隙*/
}
table {
border-collapse: collapse;
/* 边框合并 */
}
body {
font: 12px/150% Arial, Verdana, "\5b8b\4f53";
/* 宋体 */
color: #333;
background: #fff
}
.clearfix:before,
.clearfix:after {
content: "";
display: table;
/* 清除浮动 */
}
.clearfix:after {
clear: both;
/* 清除浮动 */
}
.clearfix {
*zoom: 1;
/*IE/7/6*/
}
a {
color: #666;
text-decoration: none;
/* 清除下划线 */
}
h1,
h2,
h3,
h4,
h5,
h6 {
text-decoration: none;
font-weight: normal;
font-size: 100%;
}
s,
i,
em {
font-style: normal;
text-decoration: none;
}
.col-red {
color: #C81623!important;
}
/*公共类*/
.w {
width: 1210px;
margin: 0 auto;
/*版心 提取 */
}
.fl {
float: left
}
.fr {
float: right
}
.al {
text-align: left
}
.ac {
text-align: center
}
.ar {
text-align: right
}
.hide {
display: none
}
.show{
display: block;
}
\ No newline at end of file
This source diff could not be displayed because it is too large. You can view the blob instead.
.icon-blank{
background:url('icons/blank.gif') no-repeat center center;
}
.icon-add{
background:url('icons/edit_add.png') no-repeat center center;
}
.icon-edit{
background:url('icons/pencil.png') no-repeat center center;
}
.icon-clear{
background:url('icons/clear.png') no-repeat center center;
}
.icon-remove{
background:url('icons/edit_remove.png') no-repeat center center;
}
.icon-save{
background:url('icons/filesave.png') no-repeat center center;
}
.icon-cut{
background:url('icons/cut.png') no-repeat center center;
}
.icon-ok{
background:url('icons/ok.png') no-repeat center center;
}
.icon-no{
background:url('icons/no.png') no-repeat center center;
}
.icon-cancel{
background:url('icons/cancel.png') no-repeat center center;
}
.icon-reload{
background:url('icons/reload.png') no-repeat center center;
}
.icon-search{
background:url('icons/search.png') no-repeat center center;
}
.icon-print{
background:url('icons/print.png') no-repeat center center;
}
.icon-help{
background:url('icons/help.png') no-repeat center center;
}
.icon-undo{
background:url('icons/undo.png') no-repeat center center;
}
.icon-redo{
background:url('icons/redo.png') no-repeat center center;
}
.icon-back{
background:url('icons/back.png') no-repeat center center;
}
.icon-sum{
background:url('icons/sum.png') no-repeat center center;
}
.icon-tip{
background:url('icons/tip.png') no-repeat center center;
}
.icon-filter{
background:url('icons/filter.png') no-repeat center center;
}
.icon-man{
background:url('icons/man.png') no-repeat center center;
}
.icon-lock{
background:url('icons/lock.png') no-repeat center center;
}
.icon-more{
background:url('icons/more.png') no-repeat center center;
}
.icon-mini-add{
background:url('icons/mini_add.png') no-repeat center center;
}
.icon-mini-edit{
background:url('icons/mini_edit.png') no-repeat center center;
}
.icon-mini-refresh{
background:url('icons/mini_refresh.png') no-repeat center center;
}
.icon-large-picture{
background:url('icons/large_picture.png') no-repeat center center;
}
.icon-large-clipart{
background:url('icons/large_clipart.png') no-repeat center center;
}
.icon-large-shapes{
background:url('icons/large_shapes.png') no-repeat center center;
}
.icon-large-smartart{
background:url('icons/large_smartart.png') no-repeat center center;
}
.icon-large-chart{
background:url('icons/large_chart.png') no-repeat center center;
}
body {
font-family:verdana,helvetica,arial,sans-serif;
padding:20px;
font-size:12px;
margin:0;
}
h2 {
font-size:18px;
font-weight:bold;
margin:0;
margin-bottom:15px;
}
.demo-info{
padding:0 0 12px 0;
}
.demo-tip{
display:none;
}
.label-top{
display: block;
height: 22px;
line-height: 22px;
vertical-align: middle;
}
\ No newline at end of file
.icon-blank{
background:url('icons/blank.gif') no-repeat center center;
}
.icon-add{
background:url('icons/edit_add.png') no-repeat center center;
}
.icon-edit{
background:url('icons/pencil.png') no-repeat center center;
}
.icon-clear{
background:url('icons/clear.png') no-repeat center center;
}
.icon-remove{
background:url('icons/edit_remove.png') no-repeat center center;
}
.icon-save{
background:url('icons/filesave.png') no-repeat center center;
}
.icon-cut{
background:url('icons/cut.png') no-repeat center center;
}
.icon-ok{
background:url('icons/ok.png') no-repeat center center;
}
.icon-no{
background:url('icons/no.png') no-repeat center center;
}
.icon-cancel{
background:url('icons/cancel.png') no-repeat center center;
}
.icon-reload{
background:url('icons/reload.png') no-repeat center center;
}
.icon-search{
background:url('icons/search.png') no-repeat center center;
}
.icon-print{
background:url('icons/print.png') no-repeat center center;
}
.icon-help{
background:url('icons/help.png') no-repeat center center;
}
.icon-undo{
background:url('icons/undo.png') no-repeat center center;
}
.icon-redo{
background:url('icons/redo.png') no-repeat center center;
}
.icon-back{
background:url('icons/back.png') no-repeat center center;
}
.icon-sum{
background:url('icons/sum.png') no-repeat center center;
}
.icon-tip{
background:url('icons/tip.png') no-repeat center center;
}
.icon-filter{
background:url('icons/filter.png') no-repeat center center;
}
.icon-man{
background:url('icons/man.png') no-repeat center center;
}
.icon-lock{
background:url('icons/lock.png') no-repeat center center;
}
.icon-mini-add{
background:url('icons/mini_add.png') no-repeat center center;
}
.icon-mini-edit{
background:url('icons/mini_edit.png') no-repeat center center;
}
.icon-mini-refresh{
background:url('icons/mini_refresh.png') no-repeat center center;
}
.icon-large-picture{
background:url('icons/large_picture.png') no-repeat center center;
}
.icon-large-clipart{
background:url('icons/large_clipart.png') no-repeat center center;
}
.icon-large-shapes{
background:url('icons/large_shapes.png') no-repeat center center;
}
.icon-large-smartart{
background:url('icons/large_smartart.png') no-repeat center center;
}
.icon-large-chart{
background:url('icons/large_chart.png') no-repeat center center;
}
@charset "utf-8";
html,body{width:100%;height:100%;}
html,body,p,div,h1,h2,h3,h4,h5{margin:0;padding:0;}
.lf{float: left;}
.rg{float:right;}
.container-box{width:900px;height:600px;border-radius: 5px;box-shadow: 2px 2px 23px #909090;}
.tong-h2{/* width:100%; */padding-left:20px;height:40px;line-height:40px;color:#fff;background:#000;}
.box-shadow{box-shadow: 2px 2px 23px #909090;margin:20px 8px;padding:10px;height:500px;}
.content-tong{}
.lf-box{width:250px;}
.rg-box{width:575px;}
.a_demo_one {
background-color:#3bb3e0;
padding:10px;
position:relative;
font-family: 'Open Sans', sans-serif;
font-size:12px;
text-decoration:none;
color:#fff;
border: solid 1px #186f8f;
background-image: linear-gradient(bottom, rgb(44,160,202) 0%, rgb(62,184,229) 100%);
background-image: -o-linear-gradient(bottom, rgb(44,160,202) 0%, rgb(62,184,229) 100%);
background-image: -moz-linear-gradient(bottom, rgb(44,160,202) 0%, rgb(62,184,229) 100%);
background-image: -webkit-linear-gradient(bottom, rgb(44,160,202) 0%, rgb(62,184,229) 100%);
background-image: -ms-linear-gradient(bottom, rgb(44,160,202) 0%, rgb(62,184,229) 100%);
background-image: -webkit-gradient(
linear,
left bottom,
left top,
color-stop(0, rgb(44,160,202)),
color-stop(1, rgb(62,184,229))
);
-webkit-box-shadow: inset 0px 1px 0px #7fd2f1, 0px 1px 0px #fff;
-moz-box-shadow: inset 0px 1px 0px #7fd2f1, 0px 1px 0px #fff;
box-shadow: inset 0px 1px 0px #7fd2f1, 0px 1px 0px #fff;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
-o-border-radius: 5px;
border-radius: 5px;
}
.a_demo_one::before {
content:"";
display:block;
position:absolute;
width:100%;
height:100%;
padding:8px;
left:-8px;
top:-8px;
z-index:-1;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
-o-border-radius: 5px;
border-radius: 5px;
}
.a_demo_one:active {
padding-bottom:9px;
padding-left:10px;
padding-right:10px;
padding-top:11px;
top:1px;
background-image: linear-gradient(bottom, rgb(62,184,229) 0%, rgb(44,160,202) 100%);
background-image: -o-linear-gradient(bottom, rgb(62,184,229) 0%, rgb(44,160,202) 100%);
background-image: -moz-linear-gradient(bottom, rgb(62,184,229) 0%, rgb(44,160,202) 100%);
background-image: -webkit-linear-gradient(bottom, rgb(62,184,229) 0%, rgb(44,160,202) 100%);
background-image: -ms-linear-gradient(bottom, rgb(62,184,229) 0%, rgb(44,160,202) 100%);
background-image: -webkit-gradient(
linear,
left bottom,
left top,
color-stop(0, rgb(62,184,229)),
color-stop(1, rgb(44,160,202))
);
}
.serach-box{width:400px;height:26px;line-height: 26px;padding:2px 5px;border-radius: 5px;margin-right:20px;}
\ No newline at end of file
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment