Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
I
image-backend
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
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
陈皓
image-backend
Commits
9b3f9b97
Commit
9b3f9b97
authored
Apr 07, 2024
by
陈皓
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
init
parent
5ecec3eb
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
18 changed files
with
592 additions
and
214 deletions
+592
-214
config-server/src/main/resources/properties/config-wanpro.yml
+46
-0
imgproc-server/pom.xml
+1
-1
imgproc-server/src/main/java/com/zq/imgproc/controller/ApiController.java
+44
-6
imgproc-server/src/main/java/com/zq/imgproc/service/ApiService.java
+0
-0
imgproc-server/src/main/java/com/zq/imgproc/utils/BendUtil.java
+1
-1
imgproc-server/src/main/java/com/zq/imgproc/utils/Deskew.java
+14
-14
imgproc-server/src/main/java/com/zq/imgproc/utils/Deskew2.java
+170
-0
imgproc-server/src/main/java/com/zq/imgproc/vo/ApiDetectionVO.java
+5
-5
imgproc-server/src/main/resources/application.yml
+12
-8
interface-server/src/main/java/com/zq/im/modules/api/service/IApiLogic.java
+30
-49
interface-server/src/main/java/com/zq/im/modules/api/service/impl/ApiV1Logic.java
+47
-56
interface-server/src/main/java/com/zq/im/modules/im/controller/ImgProcController.java
+25
-7
interface-server/src/main/java/com/zq/im/modules/im/feign/ImgprocFeign.java
+30
-0
interface-server/src/main/java/com/zq/im/modules/im/service/ImgProcService.java
+41
-1
interface-server/src/main/java/com/zq/im/modules/im/vo/DetectionResVo.java
+5
-5
interface-server/src/main/java/com/zq/im/modules/im/vo/OptimizationVO.java
+56
-0
interface-server/src/main/java/com/zq/im/utils/V1Demo.java
+56
-61
pom.xml
+9
-0
No files found.
config-server/src/main/resources/properties/config-wanpro.yml
0 → 100644
View file @
9b3f9b97
#Redis配置
redis.url
:
127.0.0.1
redis.port
:
6379
redis.password
:
#数据库源配置
db
:
cloud
:
username
:
root
password
:
Dk2019!23456
driver-class-name
:
com.mysql.cj.jdbc.Driver
url
:
cloud
:
jdbc:mysql://119.45.183.210:13308/image?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
#jwt
jwt
:
header
:
Authorization
# 令牌前缀
token-start-with
:
Bearer
# 必须使用最少88位的Base64对该令牌进行编码
base64-secret
:
ZmQ0ZGI5NjQ0MDQwY2I4MjMxY2Y3ZmI3MjdhN2ZmMjNhODViOTg1ZGE0NTBjMGM4NDA5NzYxMjdjOWMwYWRmZTBlZjlhNGY3ZTg4Y2U3YTE1ODVkZDU5Y2Y3OGYwZWE1NzUzNWQ2YjFjZDc0NGMxZWU2MmQ3MjY1NzJmNTE0MzI=
# 令牌过期时间 此处单位/毫秒 ,默认2小时,可在此网站生成 https://www.convertworld.com/zh-hans/time/milliseconds.html
token-validity-in-seconds
:
7200000
# 在线用户key
online-key
:
online-token-
# 验证码
code-key
:
code-key-
# token 续期检查时间范围(默认30分钟,单位默认毫秒),在token即将过期的一段时间内用户操作了,则给用户的token续期
detect
:
1800000
# 续期时间范围,默认 1小时,这里单位毫秒
renew
:
3600000
#线程池配置
task
:
pool
:
# 是否启用线程池
enable
:
true
# 核心线程池大小
core-pool-size
:
5
# 最大线程数
max-pool-size
:
15
# 队列容量
queue-capacity
:
20
# 活跃时间
keep-alive-seconds
:
30
imgproc-server/pom.xml
View file @
9b3f9b97
...
...
@@ -86,7 +86,7 @@
<artifactId>
spring-cloud-starter-bootstrap
</artifactId>
</dependency>
<!-- OpenCv -->
<!-- mvn install:install-file -Dfile=
./src/main/resources
/lib/opencv-460.jar -DgroupId=org.opencv -DartifactId=opencv -Dversion=4.6.0 -Dpackaging=jar -->
<!-- mvn install:install-file -Dfile=
D:/project/image-backend/config
/lib/opencv-460.jar -DgroupId=org.opencv -DartifactId=opencv -Dversion=4.6.0 -Dpackaging=jar -->
<dependency>
<groupId>
org.opencv
</groupId>
<artifactId>
opencv
</artifactId>
...
...
imgproc-server/src/main/java/com/zq/imgproc/controller/ApiController.java
View file @
9b3f9b97
...
...
@@ -6,14 +6,17 @@ import com.zq.common.utils.AssertUtils;
import
com.zq.common.vo.ResultVo
;
import
com.zq.imgproc.service.ApiService
;
import
com.zq.imgproc.vo.OptimizationReq
;
import
com.zq.imgproc.vo.OptimizationVO
;
import
io.swagger.annotations.Api
;
import
io.swagger.annotations.ApiOperation
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.
beans.factory.annotation.Value
;
import
org.springframework.
web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.RequestBody
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
import
java.io.IOException
;
/**
* <p>
* 图片处理API
...
...
@@ -27,11 +30,6 @@ import org.springframework.web.bind.annotation.RestController;
@RestController
public
class
ApiController
{
@Value
(
"${imgconfig.opencv}"
)
String
opencvUrl
;
@Value
(
"${imgconfig.deskew}"
)
String
deskewUrl
;
private
final
ApiService
service
;
@Autowired
public
ApiController
(
ApiService
service
)
{
...
...
@@ -52,4 +50,44 @@ public class ApiController {
return
service
.
detection
(
req
);
}
@ApiOperation
(
"图片优化"
)
@PostMapping
(
"/imageOptimization"
)
public
ResultVo
<
OptimizationVO
>
imageOptimization
(
@RequestBody
OptimizationReq
req
)
throws
IOException
{
AssertUtils
.
hasText
(
req
.
getFileContent
(),
"缺少文件内容"
);
AssertUtils
.
hasText
(
req
.
getFileName
(),
"缺少文件名"
);
return
ResultVo
.
success
(
service
.
optimization
(
req
));
}
@ApiOperation
(
"图片纠偏"
)
@AnonymousPostMapping
(
"/deskew"
)
public
ResultVo
<
String
>
deskew
(
@RequestBody
OptimizationReq
req
)
throws
Exception
{
AssertUtils
.
hasText
(
req
.
getFileContent
(),
"缺少文件内容"
);
AssertUtils
.
hasText
(
req
.
getFileName
(),
"缺少文件名"
);
return
service
.
deskew
(
req
);
}
@ApiOperation
(
"图片去黑边"
)
@AnonymousPostMapping
(
"/removeBlack"
)
public
ResultVo
<
String
>
removeBlack
(
@RequestBody
OptimizationReq
req
)
throws
Exception
{
AssertUtils
.
hasText
(
req
.
getFileContent
(),
"缺少文件内容"
);
AssertUtils
.
hasText
(
req
.
getFileName
(),
"缺少文件名"
);
return
service
.
removeBlack
(
req
);
}
@ApiOperation
(
"图片灰度化"
)
@AnonymousPostMapping
(
"/gray"
)
public
ResultVo
<
String
>
gray
(
@RequestBody
OptimizationReq
req
)
throws
Exception
{
AssertUtils
.
hasText
(
req
.
getFileContent
(),
"缺少文件内容"
);
AssertUtils
.
hasText
(
req
.
getFileName
(),
"缺少文件名"
);
return
service
.
gray
(
req
);
}
@ApiOperation
(
"图片边缘检测"
)
@AnonymousPostMapping
(
"/canny"
)
public
ResultVo
<
String
>
canny
(
@RequestBody
OptimizationReq
req
)
throws
Exception
{
AssertUtils
.
hasText
(
req
.
getFileContent
(),
"缺少文件内容"
);
AssertUtils
.
hasText
(
req
.
getFileName
(),
"缺少文件名"
);
return
service
.
canny
(
req
);
}
}
imgproc-server/src/main/java/com/zq/imgproc/service/ApiService.java
View file @
9b3f9b97
This diff is collapsed.
Click to expand it.
imgproc-server/src/main/java/com/zq/imgproc/utils/BendUtil.java
View file @
9b3f9b97
...
...
@@ -39,7 +39,7 @@ public class BendUtil {
BendResult
res
=
null
;
try
{
String
result
=
HttpUtil
.
post
(
MAIN
_URL
,
param
,
1000
*
20
);
String
result
=
HttpUtil
.
post
(
WANPRO
_URL
,
param
,
1000
*
20
);
res
=
parseResult
(
result
);
}
catch
(
Exception
e
)
{
log
.
error
(
"图片弯曲检测失败!"
,
e
);
...
...
imgproc-server/src/main/java/com/zq/imgproc/utils/Deskew.java
View file @
9b3f9b97
...
...
@@ -10,17 +10,19 @@ import java.util.HashMap;
import
java.util.List
;
import
java.util.Map
;
import
static
org
.
opencv
.
imgproc
.
Imgproc
.
LINE_AA
;
public
class
Deskew
{
public
static
void
main
(
String
[]
args
)
{
System
.
load
(
"
D:/project/imgproc/lib
/opencv_java460.dll"
);
Mat
image
=
Imgcodecs
.
imread
(
"C:/Users/11419/Desktop/test/
dark.pn
g"
);
System
.
load
(
"
C:/Users/11419/Desktop/test
/opencv_java460.dll"
);
Mat
image
=
Imgcodecs
.
imread
(
"C:/Users/11419/Desktop/test/
8.jp
g"
);
long
start
=
System
.
currentTimeMillis
();
int
angle
=
getDeskewAngle
(
image
);
System
.
out
.
println
(
"耗时为:"
+
String
.
valueOf
(
System
.
currentTimeMillis
()
-
start
));
System
.
out
.
println
(
"耗时为:"
+
(
System
.
currentTimeMillis
()
-
start
));
System
.
out
.
println
(
angle
);
Mat
roateMat
=
rotate
(
image
,
angle
);
Imgcodecs
.
imwrite
(
"C:/Users/11419/Desktop/test/
bend2res.pn
g"
,
roateMat
);
Imgcodecs
.
imwrite
(
"C:/Users/11419/Desktop/test/
res.jp
g"
,
roateMat
);
}
public
static
Integer
getDeskewAngle
(
Mat
src
)
{
...
...
@@ -51,14 +53,14 @@ public class Deskew {
// 5. 霍夫变换,通过步长为1,角度为PI/180来搜索可能的直线
Imgproc
.
HoughLinesP
(
canny
,
lines
,
1
,
Math
.
PI
/
180
,
threshold
,
minLineLength
,
maxLineGap
);
//
// 测试画图
//
Mat mat = src.clone();
//
Scalar color = new Scalar(0, 0, 255);
//
for (int i = 0; i < lines.rows(); i++) {
//
double[] line = lines.get(i, 0);
//
Imgproc.line(mat, new Point(line[0], line[1]), new Point(line[2], line[3]), color,3, LINE_AA);
//
}
// Imgcodecs.imwrite("C:\\Users\\11419\\Desktop\\test\\bend2Test
.png", mat);
// 测试画图
Mat
mat
=
src
.
clone
();
Scalar
color
=
new
Scalar
(
0
,
0
,
255
);
for
(
int
i
=
0
;
i
<
lines
.
rows
();
i
++)
{
double
[]
line
=
lines
.
get
(
i
,
0
);
Imgproc
.
line
(
mat
,
new
Point
(
line
[
0
],
line
[
1
]),
new
Point
(
line
[
2
],
line
[
3
]),
color
,
3
,
LINE_AA
);
}
Imgcodecs
.
imwrite
(
"C:\\Users\\11419\\Desktop\\test\\test1
.png"
,
mat
);
// 计算倾斜角度
List
<
Integer
>
angelList
=
new
ArrayList
<>();
...
...
@@ -150,6 +152,4 @@ public class Deskew {
return
rotatedImage
;
}
}
imgproc-server/src/main/java/com/zq/imgproc/utils/Deskew2.java
0 → 100644
View file @
9b3f9b97
package
com
.
zq
.
imgproc
.
utils
;
import
cn.hutool.core.convert.Convert
;
import
org.opencv.core.*
;
import
org.opencv.imgcodecs.Imgcodecs
;
import
org.opencv.imgproc.Imgproc
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
import
static
org
.
opencv
.
imgproc
.
Imgproc
.
LINE_AA
;
/**
* <p>
*
* </p>
*
* @author chenhao
* @since 2024/2/20
*/
public
class
Deskew2
{
public
static
void
main
(
String
[]
args
)
{
System
.
load
(
"C:/Users/11419/Desktop/test/opencv_java460.dll"
);
Mat
image
=
Imgcodecs
.
imread
(
"C:/Users/11419/Desktop/test/8.jpg"
);
long
start
=
System
.
currentTimeMillis
();
int
angle
=
getDeskewAngle
(
image
);
System
.
out
.
println
(
"耗时为:"
+
(
System
.
currentTimeMillis
()
-
start
));
System
.
out
.
println
(
angle
);
Mat
roateMat
=
rotate
(
image
,
angle
);
Imgcodecs
.
imwrite
(
"C:/Users/11419/Desktop/test/res.jpg"
,
roateMat
);
}
public
static
Integer
getDeskewAngle
(
Mat
src
)
{
// 图片灰度化
Mat
gray
=
src
.
clone
();
Imgproc
.
cvtColor
(
src
,
gray
,
Imgproc
.
COLOR_BGR2GRAY
);
// 2.高斯滤波,降噪
Mat
gaussianBlur
=
gray
.
clone
();
Imgproc
.
GaussianBlur
(
gray
,
gaussianBlur
,
new
Size
(
3
,
3
),
0
);
Mat
kernel
=
Imgproc
.
getStructuringElement
(
Imgproc
.
MORPH_RECT
,
new
Size
(
5
,
5
));
// 3. 图片腐蚀
Mat
erode
=
gaussianBlur
.
clone
();
Imgproc
.
erode
(
gaussianBlur
,
erode
,
kernel
);
// 4. 图片膨胀
Mat
dilate
=
erode
.
clone
();
Imgproc
.
dilate
(
erode
,
dilate
,
kernel
);
// 5. 边缘检测
Mat
canny
=
dilate
.
clone
();
Imgproc
.
Canny
(
dilate
,
canny
,
50
,
150
);
// 6. 霍夫变换得到线条
// 霍夫变换得到线条
Mat
lines
=
new
Mat
();
//累加器阈值参数,小于设置值不返回
int
threshold
=
30
;
//最低线段长度,低于设置值则不返回
double
minLineLength
=
100
;
//间距小于该值的线当成同一条线
double
maxLineGap
=
10
;
// 霍夫变换,通过步长为0.8,角度为PI/180来搜索可能的直线
Imgproc
.
HoughLinesP
(
canny
,
lines
,
0.8
,
Math
.
PI
/
180
,
threshold
,
minLineLength
,
maxLineGap
);
// 测试画图
Mat
mat
=
src
.
clone
();
Scalar
color
=
new
Scalar
(
0
,
0
,
255
);
for
(
int
i
=
0
;
i
<
lines
.
rows
();
i
++)
{
double
[]
line
=
lines
.
get
(
i
,
0
);
Imgproc
.
line
(
mat
,
new
Point
(
line
[
0
],
line
[
1
]),
new
Point
(
line
[
2
],
line
[
3
]),
color
,
3
,
LINE_AA
);
}
Imgcodecs
.
imwrite
(
"C:\\Users\\11419\\Desktop\\test\\test1.png"
,
mat
);
// 计算倾斜角度
List
<
Integer
>
angelList
=
new
ArrayList
<>();
for
(
int
i
=
0
;
i
<
lines
.
rows
();
i
++)
{
double
[]
line
=
lines
.
get
(
i
,
0
);
int
k
=
calculateAngle
(
line
[
0
],
line
[
1
],
line
[
2
],
line
[
3
]);
angelList
.
add
(
k
);
}
if
(
angelList
.
isEmpty
())
{
return
0
;
}
gray
.
release
();
dilate
.
release
();
canny
.
release
();
lines
.
release
();
// 可能还得需要考虑方差来决定选择平均数还是众数
return
most
(
angelList
);
}
/**
* 求数组众数
*
* @param angelList 数组
* @return 数组众数
*/
private
static
int
most
(
List
<
Integer
>
angelList
)
{
if
(
angelList
.
isEmpty
())
{
return
0
;
}
int
res
=
0
;
int
max
=
Integer
.
MIN_VALUE
;
Map
<
Integer
,
Integer
>
map
=
new
HashMap
<>();
for
(
int
i
:
angelList
)
{
map
.
put
(
i
,
map
.
getOrDefault
(
i
,
0
)
+
1
);
}
for
(
Integer
i
:
map
.
keySet
())
{
int
count
=
map
.
get
(
i
);
if
(
count
>
max
)
{
max
=
count
;
res
=
i
;
}
}
return
res
;
}
/**
* 计算直线的倾斜角
*/
private
static
int
calculateAngle
(
double
x1
,
double
y1
,
double
x2
,
double
y2
)
{
double
dx
=
x2
-
x1
;
double
dy
=
y2
-
y1
;
if
(
Math
.
abs
(
dx
)
<
1
e
-
4
)
{
return
90
;
}
else
if
(
Math
.
abs
(
dy
)
<
1
e
-
4
)
{
return
0
;
}
else
{
double
radians
=
Math
.
atan2
(
dy
,
dx
);
double
degrees
=
Math
.
toDegrees
(
radians
);
return
Convert
.
toInt
(
Math
.
round
(
degrees
));
}
}
/**
* 图片旋转
*
* @param image 输入图片
* @param angle 旋转角度
* @return 输出图片
*/
public
static
Mat
rotate
(
Mat
image
,
double
angle
)
{
int
w
=
image
.
cols
();
int
h
=
image
.
rows
();
if
(
angle
<
0
)
{
angle
=
360
+
angle
;
}
Point
center
=
new
Point
((
double
)
w
/
2
,
(
double
)
h
/
2
);
double
scale
=
1.0
;
Mat
rotationMatrix
=
Imgproc
.
getRotationMatrix2D
(
center
,
angle
,
scale
);
// 设置填充颜色为白色
Scalar
backgroundColor
=
new
Scalar
(
255
,
255
,
255
);
Mat
rotatedImage
=
new
Mat
();
Imgproc
.
warpAffine
(
image
,
rotatedImage
,
rotationMatrix
,
new
Size
(
w
,
h
),
Imgproc
.
INTER_LINEAR
,
Core
.
BORDER_CONSTANT
,
backgroundColor
);
rotationMatrix
.
release
();
return
rotatedImage
;
}
}
imgproc-server/src/main/java/com/zq/imgproc/vo/ApiDetectionVO.java
View file @
9b3f9b97
...
...
@@ -35,17 +35,17 @@ public class ApiDetectionVO {
@ApiModelProperty
(
"DPI"
)
private
Integer
dpi
;
@ApiModelProperty
(
"图片
清晰度
"
)
private
Double
clarity
;
@ApiModelProperty
(
"图片
亮度值
"
)
private
Double
brightness
;
@ApiModelProperty
(
"图片弯曲置信度"
)
private
Double
bend
;
@ApiModelProperty
(
"图片
亮度值
"
)
private
Integer
brightness
;
@ApiModelProperty
(
"图片
清晰度
"
)
private
Double
clarity
;
@ApiModelProperty
(
"图片偏离角度"
)
private
Integer
deskewA
ngel
;
private
Integer
a
ngel
;
@ApiModelProperty
(
"图片黑边数量"
)
private
Integer
black
;
...
...
imgproc-server/src/main/resources/application.yml
View file @
9b3f9b97
...
...
@@ -82,17 +82,20 @@ mybatis-plus:
ip
:
local-parsing
:
true
# 内网
#imgconfig:
# opencv: /opt/services/tianjin-backend/lib/opencv_java460.so
# deskew: /opt/services/tianjin-backend/lib/Deskew/Bin/deskew
# deskewpy: /opt/services/tianjin-backend/lib/correct.py
imgconfig
:
opencv
:
/opt/tianjin/lib/opencv_java460.so
deskew
:
/opt/tianjin/lib/Deskew/Bin/deskew
deskewpy
:
/opt/tianjin/lib/correct.py
# 172.28.1.171
#imgconfig:
# opencv: D:/project/image-backend/config/lib/opencv_java460.dll
# deskew: C:/Users/11419/Desktop/Deskew/Bin/deskew.exe
# deskewpy: D:/project/imgproc/lib/correct.py
# opencv: /opt/tianjin/lib/opencv_java460.so
# deskew: /opt/tianjin/lib/Deskew/Bin/deskew
# deskewpy: /opt/tianjin/lib/correct.py
# 本地
imgconfig
:
opencv
:
D:/project/image-backend/config/lib/opencv_java460.dll
deskew
:
C:/Users/11419/Desktop/Deskew/Bin/deskew.exe
deskewpy
:
D:/project/imgproc/lib/correct.py
\ No newline at end of file
interface-server/src/main/java/com/zq/im/modules/api/service/IApiLogic.java
View file @
9b3f9b97
...
...
@@ -20,54 +20,35 @@ public interface IApiLogic extends IApiCommon {
/**
* 图片质量检测
*/
ApiResp
detection
(
ApiForm
form
);
//
// /**
// * 图片纠偏
// *
// * @param form
// * @return
// */
// ApiResp deskew(ApiForm form);
//
// /**
// * 去黑边
// *
// * @param form
// * @return
// */
// ApiResp removeBlack(ApiForm form);
//
// /**
// * 图片灰度化
// *
// * @param form
// * @return
// */
// ApiResp gray(ApiForm form);
//
// /**
// * 图片边缘检测
// *
// * @param form
// * @return
// */
// ApiResp canny(ApiForm form);
//
// /**
// * 图片弯曲矫正
// *
// * @param form
// * @return
// */
// ApiResp correct(ApiForm form);
//
// /**
// * 图片优化
// *
// * @param form
// * @return
// */
// ApiResp imageOptimization(ApiForm form);
ApiResp
pictureQualityDetection
(
ApiForm
form
);
/**
* 图片优化
*/
ApiResp
imageOptimization
(
ApiForm
form
);
/**
* 图片质量检测
*/
ApiResp
deskew
(
ApiForm
form
);
/**
* 图片去黑边
*/
ApiResp
removeBlack
(
ApiForm
form
);
/**
* 图片灰度化
*/
ApiResp
gray
(
ApiForm
form
);
/**
* 图片边缘检测
*/
ApiResp
canny
(
ApiForm
form
);
}
interface-server/src/main/java/com/zq/im/modules/api/service/impl/ApiV1Logic.java
View file @
9b3f9b97
...
...
@@ -41,66 +41,57 @@ public class ApiV1Logic extends BaseApiLogic implements IApiLogic {
@Override
@OpenApi
(
"检测图片信息"
)
public
ApiResp
d
etection
(
ApiForm
form
)
{
public
ApiResp
pictureQualityD
etection
(
ApiForm
form
)
{
ImageReq
req
=
form
.
toBean
(
ImageReq
.
class
);
AssertUtils
.
hasText
(
req
.
getFileContent
(),
"缺少文件内容"
);
AssertUtils
.
hasText
(
req
.
getFileName
(),
"缺少文件名"
);
return
ApiUtils
.
toApiResp
(
form
,
imgProcService
.
detection
(
req
));
return
ApiUtils
.
toApiResp
(
form
,
imgProcService
.
pictureQualityDetection
(
req
));
}
@Override
@OpenApi
(
"图片优化"
)
public
ApiResp
imageOptimization
(
ApiForm
form
)
{
ImageReq
req
=
form
.
toBean
(
ImageReq
.
class
);
AssertUtils
.
hasText
(
req
.
getFileContent
(),
"缺少文件内容"
);
AssertUtils
.
hasText
(
req
.
getFileName
(),
"缺少文件名"
);
return
ApiUtils
.
toApiResp
(
form
,
imgProcService
.
imageOptimization
(
req
));
}
@Override
@OpenApi
(
"图片纠偏"
)
public
ApiResp
deskew
(
ApiForm
form
)
{
ImageReq
req
=
form
.
toBean
(
ImageReq
.
class
);
AssertUtils
.
hasText
(
req
.
getFileContent
(),
"缺少文件内容"
);
AssertUtils
.
hasText
(
req
.
getFileName
(),
"缺少文件名"
);
return
ApiUtils
.
toApiResp
(
form
,
imgProcService
.
deskew
(
req
));
}
@Override
@OpenApi
(
"图片去黑边"
)
public
ApiResp
removeBlack
(
ApiForm
form
)
{
ImageReq
req
=
form
.
toBean
(
ImageReq
.
class
);
AssertUtils
.
hasText
(
req
.
getFileContent
(),
"缺少文件内容"
);
AssertUtils
.
hasText
(
req
.
getFileName
(),
"缺少文件名"
);
return
ApiUtils
.
toApiResp
(
form
,
imgProcService
.
removeBlack
(
req
));
}
@Override
@OpenApi
(
"图片灰度化"
)
public
ApiResp
gray
(
ApiForm
form
)
{
ImageReq
req
=
form
.
toBean
(
ImageReq
.
class
);
AssertUtils
.
hasText
(
req
.
getFileContent
(),
"缺少文件内容"
);
AssertUtils
.
hasText
(
req
.
getFileName
(),
"缺少文件名"
);
return
ApiUtils
.
toApiResp
(
form
,
imgProcService
.
gray
(
req
));
}
@Override
@OpenApi
(
"图片边缘检测"
)
public
ApiResp
canny
(
ApiForm
form
)
{
ImageReq
req
=
form
.
toBean
(
ImageReq
.
class
);
AssertUtils
.
hasText
(
req
.
getFileContent
(),
"缺少文件内容"
);
AssertUtils
.
hasText
(
req
.
getFileName
(),
"缺少文件名"
);
return
ApiUtils
.
toApiResp
(
form
,
imgProcService
.
canny
(
req
));
}
//
// @ApiMethod(name = "图片纠偏", filterRequestField = "fileContent")
// @Override
// public ApiResp deskew(ApiForm form) {
// DetectionVo vo = form.toBean(DetectionVo.class);
// AssertUtils.hasText(vo.getFileContent(), "缺少文件内容");
// AssertUtils.hasText(vo.getFilename(), "缺少文件名");
// return ApiUtils.toApiResp(form, imgProcService.apiService(vo, "imageCorrection"));
// }
//
// @ApiMethod(name = "去黑边", filterRequestField = "fileContent")
// @Override
// public ApiResp removeBlack(ApiForm form) {
// DetectionVo vo = form.toBean(DetectionVo.class);
// AssertUtils.hasText(vo.getFileContent(), "缺少文件内容");
// AssertUtils.hasText(vo.getFilename(), "缺少文件名");
// return ApiUtils.toApiResp(form, imgProcService.apiService(vo, "removeBlack"));
// }
//
// @ApiMethod(name = "图片灰度化", filterRequestField = "fileContent")
// @Override
// public ApiResp gray(ApiForm form) {
// DetectionVo vo = form.toBean(DetectionVo.class);
// AssertUtils.hasText(vo.getFileContent(), "缺少文件内容");
// AssertUtils.hasText(vo.getFilename(), "缺少文件名");
// return ApiUtils.toApiResp(form, imgProcService.apiService(vo, "gray"));
// }
//
// @ApiMethod(name = "图片边缘检测", filterRequestField = "fileContent")
// @Override
// public ApiResp canny(ApiForm form) {
// DetectionVo vo = form.toBean(DetectionVo.class);
// AssertUtils.hasText(vo.getFileContent(), "缺少文件内容");
// AssertUtils.hasText(vo.getFilename(), "缺少文件名");
// return ApiUtils.toApiResp(form, imgProcService.apiService(vo, "gray"));
// }
//
// @ApiMethod(name = "图片弯曲矫正", filterRequestField = "fileContent")
// @Override
// public ApiResp correct(ApiForm form) {
// DetectionVo vo = form.toBean(DetectionVo.class);
// AssertUtils.hasText(vo.getFileContent(), "缺少文件内容");
// AssertUtils.hasText(vo.getFilename(), "缺少文件名");
// return ApiUtils.toApiResp(form, imgProcService.apiService(vo, "correct"));
// }
//
// @ApiMethod(name = "图片优化", filterRequestField = "fileContent")
// @Override
// public ApiResp imageOptimization(ApiForm form) {
// DetectionVo vo = form.toBean(DetectionVo.class);
// AssertUtils.hasText(vo.getFileContent(), "缺少文件内容");
// AssertUtils.hasText(vo.getFilename(), "缺少文件名");
// return ApiUtils.toApiResp(form, imgProcService.imageOptimization(vo, "imageOptimization"));
// }
}
interface-server/src/main/java/com/zq/im/modules/im/controller/ImgProcController.java
View file @
9b3f9b97
package
com
.
zq
.
im
.
modules
.
im
.
controller
;
import
cn.hutool.core.codec.Base64
;
import
cn.hutool.core.io.FileUtil
;
import
com.zq.im.modules.im.req.ImageReq
;
import
com.zq.im.modules.im.service.ImgProcService
;
import
com.zq.im.modules.system.vo.ResultVo
;
...
...
@@ -7,10 +9,9 @@ import com.zq.im.utils.AssertUtils;
import
io.swagger.annotations.Api
;
import
lombok.RequiredArgsConstructor
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.RequestBody
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
import
org.springframework.web.bind.annotation.*
;
import
java.io.File
;
/**
* <p>
...
...
@@ -29,12 +30,29 @@ public class ImgProcController {
private
final
ImgProcService
service
;
@PostMapping
(
value
=
"/
d
etection"
)
public
ResultVo
<?>
d
etection
(
@RequestBody
ImageReq
req
)
{
@PostMapping
(
value
=
"/
pictureQualityD
etection"
)
public
ResultVo
<?>
pictureQualityD
etection
(
@RequestBody
ImageReq
req
)
{
AssertUtils
.
hasText
(
req
.
getFileContent
(),
"缺少文件内容"
);
AssertUtils
.
hasText
(
req
.
getFileName
(),
"缺少文件名"
);
return
ResultVo
.
success
(
service
.
detection
(
req
));
return
ResultVo
.
success
(
service
.
pictureQualityDetection
(
req
));
}
@PostMapping
(
value
=
"/demo/{filename}"
)
public
ResultVo
<?>
demo
(
@PathVariable
String
filename
)
{
File
file
=
FileUtil
.
file
(
"/data/img/"
+
filename
);
if
(!
file
.
exists
())
{
return
ResultVo
.
fail
(
"文件不存在!"
);
}
if
(!
FileUtil
.
extName
(
file
).
equals
(
"jpg"
)
&&
!
FileUtil
.
extName
(
file
).
equals
(
"png"
))
{
return
ResultVo
.
fail
(
"暂不支持文件类型!"
);
}
ImageReq
req
=
ImageReq
.
builder
()
.
fileName
(
filename
)
.
fileContent
(
Base64
.
encode
(
file
))
.
build
();
return
ResultVo
.
success
(
service
.
pictureQualityDetection
(
req
));
}
}
interface-server/src/main/java/com/zq/im/modules/im/feign/ImgprocFeign.java
View file @
9b3f9b97
...
...
@@ -2,6 +2,7 @@ package com.zq.im.modules.im.feign;
import
com.zq.im.modules.im.req.ImageReq
;
import
com.zq.im.modules.im.vo.DetectionResVo
;
import
com.zq.im.modules.im.vo.OptimizationVO
;
import
com.zq.im.modules.system.vo.ResultVo
;
import
org.springframework.cloud.openfeign.FeignClient
;
import
org.springframework.web.bind.annotation.PostMapping
;
...
...
@@ -20,4 +21,33 @@ public interface ImgprocFeign {
@PostMapping
(
value
=
"/detection"
)
ResultVo
<
DetectionResVo
>
detection
(
ImageReq
req
);
/**
* 图片优化
*/
@PostMapping
(
value
=
"/imageOptimization"
)
ResultVo
<
OptimizationVO
>
imageOptimization
(
ImageReq
req
);
/**
* 图片纠偏
*/
@PostMapping
(
value
=
"/deskew"
)
ResultVo
<
String
>
deskew
(
ImageReq
req
);
/**
* 图片去黑边
*/
@PostMapping
(
value
=
"/removeBlack"
)
ResultVo
<
String
>
removeBlack
(
ImageReq
req
);
/**
* 图片灰度化
*/
@PostMapping
(
value
=
"/gray"
)
ResultVo
<
String
>
gray
(
ImageReq
req
);
/**
* 图片边缘检测
*/
@PostMapping
(
value
=
"/canny"
)
ResultVo
<
String
>
canny
(
ImageReq
req
);
}
interface-server/src/main/java/com/zq/im/modules/im/service/ImgProcService.java
View file @
9b3f9b97
...
...
@@ -3,6 +3,7 @@ package com.zq.im.modules.im.service;
import
com.zq.im.modules.im.feign.ImgprocFeign
;
import
com.zq.im.modules.im.req.ImageReq
;
import
com.zq.im.modules.im.vo.DetectionResVo
;
import
com.zq.im.modules.im.vo.OptimizationVO
;
import
com.zq.im.modules.system.vo.ResultVo
;
import
lombok.RequiredArgsConstructor
;
import
lombok.extern.slf4j.Slf4j
;
...
...
@@ -25,7 +26,7 @@ public class ImgProcService {
@Autowired
ImgprocFeign
feign
;
public
ResultVo
<?>
d
etection
(
ImageReq
req
)
{
public
ResultVo
<?>
pictureQualityD
etection
(
ImageReq
req
)
{
ResultVo
<
DetectionResVo
>
resultVo
=
feign
.
detection
(
req
);
if
(!
resultVo
.
isSuccess
())
{
return
ResultVo
.
fail
(
resultVo
.
getErrMsg
());
...
...
@@ -33,4 +34,43 @@ public class ImgProcService {
return
resultVo
;
}
public
ResultVo
<?>
imageOptimization
(
ImageReq
req
)
{
ResultVo
<
OptimizationVO
>
resultVo
=
feign
.
imageOptimization
(
req
);
if
(!
resultVo
.
isSuccess
())
{
return
ResultVo
.
fail
(
resultVo
.
getErrMsg
());
}
return
resultVo
;
}
public
ResultVo
<?>
deskew
(
ImageReq
req
)
{
ResultVo
<
String
>
resultVo
=
feign
.
deskew
(
req
);
if
(!
resultVo
.
isSuccess
())
{
return
ResultVo
.
fail
(
resultVo
.
getErrMsg
());
}
return
resultVo
;
}
public
Object
removeBlack
(
ImageReq
req
)
{
ResultVo
<
String
>
resultVo
=
feign
.
removeBlack
(
req
);
if
(!
resultVo
.
isSuccess
())
{
return
ResultVo
.
fail
(
resultVo
.
getErrMsg
());
}
return
resultVo
;
}
public
Object
gray
(
ImageReq
req
)
{
ResultVo
<
String
>
resultVo
=
feign
.
gray
(
req
);
if
(!
resultVo
.
isSuccess
())
{
return
ResultVo
.
fail
(
resultVo
.
getErrMsg
());
}
return
resultVo
;
}
public
Object
canny
(
ImageReq
req
)
{
ResultVo
<
String
>
resultVo
=
feign
.
canny
(
req
);
if
(!
resultVo
.
isSuccess
())
{
return
ResultVo
.
fail
(
resultVo
.
getErrMsg
());
}
return
resultVo
;
}
}
interface-server/src/main/java/com/zq/im/modules/im/vo/DetectionResVo.java
View file @
9b3f9b97
...
...
@@ -36,17 +36,17 @@ public class DetectionResVo {
@ApiModelProperty
(
"DPI"
)
private
Integer
dpi
;
@ApiModelProperty
(
"图片
清晰度
"
)
private
Double
clarity
;
@ApiModelProperty
(
"图片
亮度值
"
)
private
Double
brightness
;
@ApiModelProperty
(
"图片弯曲置信度"
)
private
Double
bend
;
@ApiModelProperty
(
"图片
亮度值
"
)
private
Integer
brightness
;
@ApiModelProperty
(
"图片
清晰度
"
)
private
Double
clarity
;
@ApiModelProperty
(
"图片偏离角度"
)
private
Integer
deskewA
ngel
;
private
Integer
a
ngel
;
@ApiModelProperty
(
"图片黑边数量"
)
private
Integer
black
;
...
...
interface-server/src/main/java/com/zq/im/modules/im/vo/OptimizationVO.java
0 → 100644
View file @
9b3f9b97
package
com
.
zq
.
im
.
modules
.
im
.
vo
;
import
io.swagger.annotations.ApiModelProperty
;
import
lombok.AllArgsConstructor
;
import
lombok.Builder
;
import
lombok.Data
;
import
lombok.NoArgsConstructor
;
/**
* <p>
* 图片优化结果类
* </p>
*
* @author chenhao
* @since 2023/4/20
*/
@NoArgsConstructor
@AllArgsConstructor
@Data
@Builder
public
class
OptimizationVO
{
@ApiModelProperty
(
"base64图片"
)
private
String
fileContent
;
@ApiModelProperty
(
"文件名称"
)
private
String
filename
;
@ApiModelProperty
(
"是否经过亮度调整"
)
private
boolean
isBrightness
;
@ApiModelProperty
(
"图片原始平均亮度值"
)
private
double
originalBrightnessVal
;
@ApiModelProperty
(
"图片修正后的平均亮度值"
)
private
double
brightnessVal
;
@ApiModelProperty
(
"是否经过清晰度调整"
)
private
boolean
isClarity
;
@ApiModelProperty
(
"图片原始清晰度值"
)
private
double
originalClarityVal
;
@ApiModelProperty
(
"图片清晰度调整过后的清晰度值"
)
private
double
clarityVal
;
@ApiModelProperty
(
"是否经过黑边处理"
)
private
boolean
isRemoveBlack
;
@ApiModelProperty
(
"图片纠偏的角度,纠偏出错返回-1"
)
private
double
deskewAngel
;
@ApiModelProperty
(
"图片是否经过弯曲矫正"
)
private
boolean
isCorrect
;
}
interface-server/src/main/java/com/zq/im/utils/V1Demo.java
View file @
9b3f9b97
...
...
@@ -5,7 +5,6 @@ import cn.hutool.core.convert.Convert;
import
cn.hutool.core.date.DatePattern
;
import
cn.hutool.core.date.DateUtil
;
import
cn.hutool.core.io.FileUtil
;
import
cn.hutool.core.net.url.UrlQuery
;
import
cn.hutool.core.util.ArrayUtil
;
import
cn.hutool.core.util.IdUtil
;
import
cn.hutool.core.util.URLUtil
;
...
...
@@ -14,11 +13,10 @@ import cn.hutool.http.HttpRequest;
import
cn.hutool.http.HttpResponse
;
import
cn.hutool.json.JSONObject
;
import
cn.hutool.json.JSONUtil
;
import
com.zq.im.modules.im.vo.DetectionResVo
;
import
lombok.
Data
;
import
io.swagger.annotations.ApiModelProperty
;
import
lombok.
*
;
import
java.io.File
;
import
java.io.InputStream
;
import
java.util.Date
;
import
java.util.HashMap
;
import
java.util.Map
;
...
...
@@ -41,6 +39,7 @@ public class V1Demo {
private
static
final
String
APP_ID
=
"ec3f54b398e64fbeb9ed00bb0144a91b"
;
private
static
final
String
APP_SECRET
=
"4c0d6c2a99e541c8a1affc3c8d8dcddb"
;
private
static
final
String
URL
=
"http://127.0.0.1:9900/api/v1/action"
;
// private static final String URL = "http://172.28.1.171:8855/api/v1/action";
public
static
ApiInfo
getApiInfo
()
{
ApiInfo
apiInfo
=
API_INFO_MAP
.
get
(
APP_ID
);
...
...
@@ -66,12 +65,12 @@ public class V1Demo {
HttpRequest
request
=
HttpRequest
.
post
(
URL
)
.
contentType
(
"application/x-www-form-urlencoded"
)
.
form
(
params
);
System
.
out
.
println
(
"请求报文 => "
+
UrlQuery
.
of
(
params
));
//
System.out.println("请求报文 => " + UrlQuery.of(params));
HttpResponse
execut
e
=
request
.
execute
();
String
body
=
execut
e
.
body
();
System
.
out
.
println
(
"响应报文 => "
+
body
);
int
status
=
execut
e
.
getStatus
();
@Cleanup
HttpResponse
respons
e
=
request
.
execute
();
String
body
=
respons
e
.
body
();
//
System.out.println("响应报文 => " + body);
int
status
=
respons
e
.
getStatus
();
if
(
status
==
200
)
{
JSONObject
obj
=
JSONUtil
.
parseObj
(
body
);
Integer
code
=
obj
.
getInt
(
"code"
);
...
...
@@ -108,14 +107,14 @@ public class V1Demo {
// POST的Content-Type默认是application/x-www-form-urlencoded,有文件则是multipart/form-data
HttpRequest
request
=
HttpRequest
.
post
(
URL
)
.
form
(
params
);
System
.
out
.
println
(
"请求报文 => "
+
UrlQuery
.
of
(
params
));
//
System.out.println("请求报文 => " + UrlQuery.of(params));
HttpResponse
execut
e
=
request
.
execute
();
String
body
=
execut
e
.
body
();
System
.
out
.
println
(
"响应报文 => "
+
body
);
HttpResponse
respons
e
=
request
.
execute
();
String
body
=
respons
e
.
body
();
//
System.out.println("响应报文 => " + body);
// 失败 => {"apiNo":"09c1ad82ec0f4b2d80cae0cfb1d7059b","code":"103","msg":"调用方法异常","timestamp":1638176339560,"data":null,"success":false}
// 成功 => {"apiNo":"bc070a7c31ac4b8eb1180b2d82a2096b","code":"200","msg":"成功","timestamp":1638176552353,"data":{"userId":"123","username":"admin@gxfy.com"},"success":true}
int
status
=
execut
e
.
getStatus
();
int
status
=
respons
e
.
getStatus
();
if
(
status
==
200
)
{
JSONObject
obj
=
JSONUtil
.
parseObj
(
body
);
Integer
code
=
obj
.
getInt
(
"code"
);
...
...
@@ -127,51 +126,7 @@ public class V1Demo {
}
/**
* 下载文件
*
* @param method
* @param bizContentMap
* @param sessionKey
* @return
*/
public
static
InputStream
download
(
String
method
,
Map
<
String
,
Object
>
bizContentMap
,
String
sessionKey
)
{
String
apiNo
=
IdUtil
.
simpleUUID
();
String
timestamp
=
DateUtil
.
format
(
DateUtil
.
date
(),
DatePattern
.
PURE_DATETIME_PATTERN
);
//JSON字符串
String
bizContent
=
bizContentMap
==
null
?
""
:
JSONUtil
.
toJsonStr
(
bizContentMap
);
Map
<
String
,
Object
>
params
=
new
HashMap
<>(
6
);
params
.
put
(
"appId"
,
APP_ID
);
params
.
put
(
"apiNo"
,
apiNo
);
params
.
put
(
"method"
,
method
);
params
.
put
(
"timestamp"
,
timestamp
);
params
.
put
(
"bizContent"
,
URLUtil
.
encode
(
Base64
.
encode
(
bizContent
)));
params
.
put
(
"sign"
,
getSign
(
bizContent
,
method
,
timestamp
,
apiNo
,
sessionKey
,
null
,
null
));
// POST的Content-Type默认是application/x-www-form-urlencoded,有文件则是multipart/form-data
HttpRequest
request
=
HttpRequest
.
post
(
URL
)
.
form
(
params
);
System
.
out
.
println
(
"请求报文 => "
+
UrlQuery
.
of
(
params
));
HttpResponse
execute
=
request
.
execute
();
if
(!
execute
.
isOk
())
{
String
body
=
execute
.
body
();
System
.
out
.
println
(
"响应报文 => "
+
body
);
return
null
;
}
return
execute
.
bodyStream
();
}
/**
* 获取签名
*
* @param bizContent
* @param method
* @param timestamp
* @param apiNo
* @param sessionKey
* @return
*/
public
static
String
getSign
(
String
bizContent
,
String
method
,
String
timestamp
,
String
apiNo
,
String
sessionKey
,
File
file
,
File
[]
fileList
)
{
// 参与签名的参数排序
...
...
@@ -209,12 +164,14 @@ public class V1Demo {
System
.
out
.
println
(
sessionKey
);
// String sessionKey = "860a013f4e7344bca5869b361a186648";
Map
<
String
,
Object
>
params
=
new
HashMap
<>(
2
);
params
.
put
(
"filename"
,
"6.png"
);
params
.
put
(
"fileContent"
,
Base64
.
encode
(
FileUtil
.
file
(
"C:\\Users\\11419\\Desktop\\test\\bend1.jpg"
)));
Object
o
=
doPost
(
"detection"
,
params
,
sessionKey
);
params
.
put
(
"filename"
,
"8.jpg"
);
params
.
put
(
"fileContent"
,
Base64
.
encode
(
FileUtil
.
file
(
"C:\\Users\\11419\\Desktop\\图片检测\\test\\8.jpg"
)));
for
(
int
i
=
0
;
i
<
500
;
i
++)
{
Object
o
=
doPost
(
"pictureQualityDetection"
,
params
,
sessionKey
);
DetectionResVo
convert
=
Convert
.
convert
(
DetectionResVo
.
class
,
o
);
System
.
out
.
println
(
JSONUtil
.
toJsonStr
(
convert
));
}
}
@Data
public
static
class
ApiInfo
{
...
...
@@ -230,6 +187,44 @@ public class V1Demo {
private
String
unit
;
private
Date
dueTime
;
}
@NoArgsConstructor
@AllArgsConstructor
@Data
@Builder
public
static
class
DetectionResVo
{
@ApiModelProperty
(
"文件名称"
)
private
String
fileName
;
@ApiModelProperty
(
"图片大小"
)
private
Double
size
;
@ApiModelProperty
(
"水平分辨率"
)
private
Integer
widthResolution
;
@ApiModelProperty
(
"垂直分辨率"
)
private
Integer
heightResolution
;
@ApiModelProperty
(
"DPI"
)
private
Integer
dpi
;
@ApiModelProperty
(
"图片亮度值"
)
private
Double
brightness
;
@ApiModelProperty
(
"图片弯曲置信度"
)
private
Double
bend
;
@ApiModelProperty
(
"图片清晰度"
)
private
Double
clarity
;
@ApiModelProperty
(
"图片偏离角度"
)
private
Integer
angel
;
@ApiModelProperty
(
"图片黑边数量"
)
private
Integer
black
;
}
...
...
pom.xml
View file @
9b3f9b97
...
...
@@ -101,6 +101,15 @@
</activation>
</profile>
<profile>
<!--172.28.1.171外网环境-->
<id>
wanpro
</id>
<properties>
<profiles.active>
wanpro
</profiles.active>
<logging.level>
info
</logging.level>
<register.url>
http://admin:GXfy2022@172.28.1.171:8800/eureka/
</register.url>
</properties>
</profile>
<profile>
<!--生产环境-->
<id>
product
</id>
<properties>
...
...
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