Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
X
xzxt-api-report
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
雷紫添
xzxt-api-report
Commits
e13d6728
Commit
e13d6728
authored
Dec 13, 2021
by
lilei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
paddleOcr及lac调用服务接口
parent
dcef7317
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
205 additions
and
0 deletions
+205
-0
src/main/java/com/founder/util/PaddleController.java
+201
-0
src/main/resources/application-dev.yml
+4
-0
No files found.
src/main/java/com/founder/util/PaddleController.java
0 → 100644
View file @
e13d6728
package
com
.
founder
.
util
;
import
com.alibaba.fastjson.JSONArray
;
import
com.alibaba.fastjson.JSONObject
;
import
org.apache.http.HttpResponse
;
import
org.apache.http.HttpStatus
;
import
org.apache.http.ParseException
;
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
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.stereotype.Component
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.RequestParam
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
org.springframework.web.multipart.MultipartFile
;
import
sun.misc.BASE64Encoder
;
import
java.io.FileInputStream
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.nio.charset.Charset
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
/**
* @auther: Lilei
* @date: 2021/11/10 17:43
*/
@Component
@Controller
public
class
PaddleController
{
//接口地址
@Value
(
"${ocrURL}"
)
private
String
ocrURL
;
@Value
(
"${lacURL}"
)
private
String
lacURL
;
private
int
status
=
0
;
/**
* 调用 API
*
* @param parameters
* @return
*/
public
String
post
(
String
parameters
,
String
URL
)
{
String
body
=
""
;
HttpPost
method
=
new
HttpPost
(
URL
);
if
(
method
!=
null
&
parameters
!=
null
&&
!
""
.
equals
(
parameters
.
trim
()))
{
try
{
HttpClient
httpClient
=
new
DefaultHttpClient
();
// 建立一个NameValuePair数组,用于存储欲传送的参数
method
.
addHeader
(
"Content-type"
,
"application/json"
);
method
.
setHeader
(
"Accept"
,
"application/json"
);
method
.
setEntity
(
new
StringEntity
(
parameters
,
Charset
.
forName
(
"UTF-8"
)));
HttpResponse
response
=
httpClient
.
execute
(
method
);
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
;
e
.
printStackTrace
();
}
finally
{
//logger.info("调用接口状态:" + status);
}
}
return
body
;
}
public
String
getOcrText
(
byte
[]
base64Data
)
throws
ParseException
{
// PaddleOcrUtils ac = new PaddleOcrUtils();
JSONArray
arry
=
new
JSONArray
();
JSONObject
j
=
new
JSONObject
();;
BASE64Encoder
encoder
=
new
BASE64Encoder
();
String
encode
=
encoder
.
encode
(
base64Data
);
arry
.
add
(
encode
);
j
.
put
(
"images"
,
arry
);
String
httpresult
=
post
(
j
.
toJSONString
(),
ocrURL
);
String
text
=
""
;
if
(
httpresult
!=
null
&&
httpresult
!=
""
){
JSONObject
jsonObject
=
JSONObject
.
parseObject
(
httpresult
);
JSONArray
results
=
jsonObject
.
getJSONArray
(
"results"
);
JSONObject
result
=
results
.
getJSONObject
(
0
);
JSONArray
data
=
result
.
getJSONArray
(
"data"
);
for
(
Object
object:
data
)
{
Map
map
=
JSONObject
.
parseObject
(
object
.
toString
(),
Map
.
class
);
text
+=
map
.
get
(
"text"
);
}
}
return
text
;
}
/**
* 获取图片中的文字
*
* @param file
* @return
*/
@PostMapping
(
"/getPaddleOcrText"
)
@ResponseBody
public
Map
<
String
,
Object
>
getPaddleOcrText
(
@RequestParam
MultipartFile
file
){
String
text
=
""
;
try
{
byte
[]
bytes
=
file
.
getBytes
();
text
=
getOcrText
(
bytes
);
}
catch
(
Exception
e
){
e
.
printStackTrace
();
}
Map
<
String
,
Object
>
map
=
new
HashMap
<>();
map
.
put
(
"text"
,
text
);
return
map
;
}
public
List
<
String
>
getLacList
(
String
text
)
throws
ParseException
{
JSONObject
jsonParam
=
new
JSONObject
();
jsonParam
.
put
(
"text"
,
text
);
jsonParam
.
put
(
"batch_size"
,
1
);
String
httpresult
=
post
(
jsonParam
.
toJSONString
(),
lacURL
);
List
<
String
>
resultList
=
new
ArrayList
<>();
if
(
httpresult
!=
null
&&
httpresult
!=
""
){
JSONObject
jsonObject
=
JSONObject
.
parseObject
(
httpresult
);
JSONArray
results
=
jsonObject
.
getJSONArray
(
"results"
);
resultList
=
JSONObject
.
parseObject
(
results
.
toString
(),
List
.
class
);
}
return
resultList
;
}
/**
* 根据输入文字进行分词
*
* @param text
* @return
*/
@PostMapping
(
"/getPaddleLacList"
)
@ResponseBody
public
Map
<
String
,
Object
>
getPaddleLacList
(
@RequestParam
String
text
){
List
<
String
>
list
=
new
ArrayList
<>();
System
.
out
.
println
(
"这里"
+
text
);
System
.
out
.
println
();
try
{
list
=
getLacList
(
text
);
}
catch
(
Exception
e
){
e
.
printStackTrace
();
}
Map
<
String
,
Object
>
map
=
new
HashMap
<>();
map
.
put
(
"list"
,
list
);
return
map
;
}
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
;
}
}
src/main/resources/application-dev.yml
View file @
e13d6728
...
@@ -73,3 +73,7 @@ codemsg:
...
@@ -73,3 +73,7 @@ codemsg:
-
code
:
"
010104"
-
code
:
"
010104"
name
:
"
用户身份证信息有误"
name
:
"
用户身份证信息有误"
#PaddleOCR服务部署路径
ocrURL
:
http://39.99.224.27:8877/predict/chinese_ocr_db_crnn_mobile
#lac服务部署路径
lacURL
:
http://39.99.224.27:8877/predict/lac
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment