Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
cust-api
Project
Project
Details
Activity
Releases
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
李伟
cust-api
Commits
75899fff
Commit
75899fff
authored
Dec 27, 2024
by
gaoyingwei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修改 图片上传压缩转换问题
parent
58a60b37
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
86 additions
and
4 deletions
+86
-4
pom.xml
pom.xml
+6
-0
AppMemInfoController.java
...isoft/app/appMemInfo/controller/AppMemInfoController.java
+80
-4
No files found.
pom.xml
View file @
75899fff
...
...
@@ -152,6 +152,12 @@
<version>
4.2
</version>
</dependency>
<dependency>
<groupId>
com.drewnoakes
</groupId>
<artifactId>
metadata-extractor
</artifactId>
<version>
2.14.0
</version>
</dependency>
</dependencies>
...
...
src/main/java/org/rcisoft/app/appMemInfo/controller/AppMemInfoController.java
View file @
75899fff
...
...
@@ -4,6 +4,9 @@ package org.rcisoft.app.appMemInfo.controller;
/*固定导入*/
import
com.baomidou.mybatisplus.core.metadata.IPage
;
import
com.drew.imaging.ImageMetadataReader
;
import
com.drew.metadata.Metadata
;
import
com.drew.metadata.exif.ExifIFD0Directory
;
import
io.swagger.v3.oas.annotations.Operation
;
import
io.swagger.v3.oas.annotations.Parameter
;
import
io.swagger.v3.oas.annotations.Parameters
;
...
...
@@ -36,6 +39,7 @@ import org.springframework.web.multipart.MultipartFile;
import
javax.imageio.ImageIO
;
import
javax.validation.Valid
;
import
java.awt.*
;
import
java.awt.image.BufferedImage
;
import
java.io.ByteArrayOutputStream
;
import
java.io.IOException
;
...
...
@@ -136,24 +140,28 @@ public class AppMemInfoController extends CyPaginationController<MemInfo> {
cyFileStorageService
.
filePath
(
file
,
temp
,
bucket
));
}
//图片压缩
private
static
MultipartFile
compressImage
(
MultipartFile
file
)
{
BufferedImage
image
=
null
;
try
{
image
=
ImageIO
.
read
(
file
.
getInputStream
());
// 设置压缩质量
float
quality
=
1.0f
;
// 获取图像的方向信息并进行旋转调整
image
=
fixImageOrientation
(
file
,
image
);
// // 设置压缩质量
// float quality = 1.0f;
// 调整图片尺寸,设置目标宽度和高度
int
targetWidth
=
800
;
// 目标宽度(可以根据需求调整)
int
targetHeight
=
600
;
// 目标高度(可以根据需求调整)
// 使用Imgscalr进行图像压缩(缩放)
BufferedImage
compressedImage
=
Scalr
.
resize
(
image
,
Scalr
.
Method
.
QUALITY
,
targetWidth
,
targetHeight
);
// BufferedImage compressedImage = Scalr.resize(image,Scalr.Method.QUALITY, Scalr.Mode.AUTOMATIC,500
);
// 将BufferedImage写入ByteArrayOutputStream
ByteArrayOutputStream
outputStream
=
new
ByteArrayOutputStream
();
String
originalFilename
=
file
.
getOriginalFilename
();
String
extension
=
originalFilename
.
substring
(
originalFilename
.
lastIndexOf
(
'.'
)
+
1
).
toLowerCase
();
ImageIO
.
write
(
compressedImage
,
extension
,
outputStream
);
// 假设原始图片格式为jpg
ImageIO
.
write
(
image
,
extension
,
outputStream
);
// 假设原始图片格式为jpg
// 将压缩后的图片数据转换为byte数组
byte
[]
compressedImageData
=
outputStream
.
toByteArray
();
...
...
@@ -167,4 +175,72 @@ public class AppMemInfoController extends CyPaginationController<MemInfo> {
return
file
;
}
/**
* 获取图像的 EXIF 方向标签
*/
private
static
int
getExifOrientation
(
MultipartFile
file
)
{
try
{
Metadata
metadata
=
ImageMetadataReader
.
readMetadata
(
file
.
getInputStream
());
ExifIFD0Directory
directory
=
metadata
.
getFirstDirectoryOfType
(
ExifIFD0Directory
.
class
);
if
(
directory
!=
null
)
{
Integer
orientationTag
=
directory
.
getInteger
(
ExifIFD0Directory
.
TAG_ORIENTATION
);
if
(
orientationTag
!=
null
)
{
return
orientationTag
;
}
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
1
;
// 默认方向,无旋转
}
/**
* 旋转图像
*/
private
static
BufferedImage
rotateImage
(
BufferedImage
image
,
int
angle
)
{
int
width
=
image
.
getWidth
();
int
height
=
image
.
getHeight
();
// 计算旋转后的新宽高
double
radians
=
Math
.
toRadians
(
angle
);
int
newWidth
=
(
int
)
Math
.
abs
(
width
*
Math
.
cos
(
radians
))
+
(
int
)
Math
.
abs
(
height
*
Math
.
sin
(
radians
));
int
newHeight
=
(
int
)
Math
.
abs
(
height
*
Math
.
cos
(
radians
))
+
(
int
)
Math
.
abs
(
width
*
Math
.
sin
(
radians
));
// 创建一个新的图像,背景色为白色或透明
BufferedImage
rotatedImage
=
new
BufferedImage
(
newWidth
,
newHeight
,
image
.
getType
());
Graphics2D
g2d
=
rotatedImage
.
createGraphics
();
// g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
//
// // 设置旋转中心
g2d
.
translate
((
newWidth
-
width
)
/
2
,
(
newHeight
-
height
)
/
2
);
// 居中
// g2d.rotate(radians, width / 2, height / 2); // 旋转
g2d
.
rotate
(
radians
,
width
/
2
,
height
/
2
);
// 绘制原图像
g2d
.
drawImage
(
image
,
0
,
0
,
null
);
g2d
.
dispose
();
return
rotatedImage
;
}
/**
* 根据 EXIF 信息修正图像方向
*/
private
static
BufferedImage
fixImageOrientation
(
MultipartFile
file
,
BufferedImage
image
)
throws
IOException
{
int
orientation
=
getExifOrientation
(
file
);
switch
(
orientation
)
{
case
3
:
return
rotateImage
(
image
,
180
);
case
6
:
return
rotateImage
(
image
,
90
);
case
8
:
return
rotateImage
(
image
,
270
);
default
:
return
image
;
// 无需旋转
}
}
}
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