Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
C
common
Project
Project
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
曲欣亮
common
Commits
2fba11ca
Commit
2fba11ca
authored
Dec 14, 2023
by
张永
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
上传图片根据压缩
parent
028a8da9
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
180 additions
and
1 deletion
+180
-1
pom.xml
pom.xml
+8
-0
FileUtil.java
src/main/java/com/egolm/common/FileUtil.java
+6
-0
ImageUtil.java
src/main/java/com/egolm/common/ImageUtil.java
+43
-0
StringUtil.java
src/main/java/com/egolm/common/StringUtil.java
+1
-1
ThumbnailsUtil.java
src/main/java/com/egolm/common/ThumbnailsUtil.java
+122
-0
No files found.
pom.xml
View file @
2fba11ca
...
@@ -150,6 +150,14 @@
...
@@ -150,6 +150,14 @@
<version>
74.1
</version>
<version>
74.1
</version>
</dependency>
</dependency>
<!-- 简体转繁体end -->
<!-- 简体转繁体end -->
<!-- 图片压缩 https://www.cnblogs.com/yinjing/p/12157562.html-->
<dependency>
<groupId>
net.coobird
</groupId>
<artifactId>
thumbnailator
</artifactId>
<version>
0.4.20
</version>
</dependency>
</dependencies>
</dependencies>
...
...
src/main/java/com/egolm/common/FileUtil.java
View file @
2fba11ca
...
@@ -527,6 +527,12 @@ public class FileUtil {
...
@@ -527,6 +527,12 @@ public class FileUtil {
public
static
File
createFile
(
String
name
)
{
public
static
File
createFile
(
String
name
)
{
return
FileUtil
.
createFile
(
new
File
(
name
));
return
FileUtil
.
createFile
(
new
File
(
name
));
}
}
public
static
void
main
(
String
[]
args
)
{
String
name
=
"d:/data/erp/excel/QY68011/"
;
createFolder
(
name
);
}
public
static
File
createFile
(
File
file
)
{
public
static
File
createFile
(
File
file
)
{
File
folder
=
file
.
getParentFile
();
File
folder
=
file
.
getParentFile
();
...
...
src/main/java/com/egolm/common/ImageUtil.java
0 → 100644
View file @
2fba11ca
package
com
.
egolm
.
common
;
import
java.io.File
;
import
java.io.IOException
;
import
java.util.HashSet
;
import
java.util.Set
;
public
class
ImageUtil
{
//压缩图片
public
static
boolean
compressImage
(
String
sourceImagePath
,
String
outputImagePath
,
int
desFileSize
)
{
Set
<
String
>
extSet
=
new
HashSet
<
String
>();
extSet
.
add
(
"jpg"
);
extSet
.
add
(
"png"
);
System
.
out
.
println
(
"desFileSize----"
+
desFileSize
);
String
extName
=
FileUtil
.
getExtName
(
new
File
(
sourceImagePath
)).
toLowerCase
();
if
(!
extSet
.
contains
(
extName
))
{
return
false
;
}
byte
[]
images
=
ThumbnailsUtil
.
compressPicForScale
(
sourceImagePath
,
outputImagePath
,
desFileSize
);
if
(
images
!=
null
)
{
File
dFile
=
new
File
(
outputImagePath
);
if
(
dFile
.
exists
())
{
return
true
;
}
}
return
false
;
}
public
static
void
main
(
String
[]
args
)
throws
IOException
{
float
imageSizeKB
=
100
;
String
sourceImagePath
=
"C:\\Users\\zhangyong\\Desktop\\jpg\\9.png"
;
String
outputImagePath
=
"C:\\Users\\zhangyong\\Desktop\\jpg\\9_"
+
imageSizeKB
+
".png"
;
boolean
res
=
compressImage
(
sourceImagePath
,
outputImagePath
,
50
);
System
.
out
.
println
(
res
);
}
}
src/main/java/com/egolm/common/StringUtil.java
View file @
2fba11ca
...
@@ -1232,7 +1232,7 @@ public class StringUtil {
...
@@ -1232,7 +1232,7 @@ public class StringUtil {
public
static
void
main
(
String
[]
args
)
{
public
static
void
main
(
String
[]
args
)
{
String
original
=
"
\"VendorName\": \"萬里行有限公司\",
"
;
String
original
=
"
采购订单收货,支持无订单快捷收货,收货后增加商铺库存。应用业务参数::ShowCheckMsg(制单时提醒是否审核)
"
;
String
result
=
ZhConverterUtil
.
toTraditional
(
original
);
String
result
=
ZhConverterUtil
.
toTraditional
(
original
);
System
.
out
.
println
(
"opecc : "
+
result
);
System
.
out
.
println
(
"opecc : "
+
result
);
String
result1
=
ICU4JConverter
(
original
);
String
result1
=
ICU4JConverter
(
original
);
...
...
src/main/java/com/egolm/common/ThumbnailsUtil.java
0 → 100644
View file @
2fba11ca
package
com
.
egolm
.
common
;
import
java.awt.Graphics
;
import
java.awt.GraphicsConfiguration
;
import
java.awt.GraphicsDevice
;
import
java.awt.GraphicsEnvironment
;
import
java.awt.HeadlessException
;
import
java.awt.Image
;
import
java.awt.Toolkit
;
import
java.awt.Transparency
;
import
java.awt.image.BufferedImage
;
import
java.io.ByteArrayInputStream
;
import
java.io.ByteArrayOutputStream
;
import
java.io.File
;
import
javax.swing.ImageIcon
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
net.coobird.thumbnailator.Thumbnails
;
//https://blog.csdn.net/hechurui/article/details/129042805
//
public
class
ThumbnailsUtil
{
private
static
Logger
logger
=
LoggerFactory
.
getLogger
(
ThumbnailsUtil
.
class
);
//desFileSize 指定多少KB
public
static
byte
[]
compressPicForScale
(
String
filePath
,
String
compressPath
,
long
desFileSize
)
{
System
.
out
.
println
(
"【图片压缩开始】"
);
byte
[]
imageBytes
=
new
byte
[
0
];
try
{
imageBytes
=
FileUtil
.
fileToBytes
(
new
File
(
filePath
));
//判断大小,如果小于desFileSize kb,不用压缩,如果大于等于 desFileSize kb,需要压缩
if
(
imageBytes
==
null
||
imageBytes
.
length
<=
0
||
imageBytes
.
length
<
desFileSize
*
1024
)
{
return
imageBytes
;
}
long
srcSize
=
imageBytes
.
length
;
double
accuracy
=
getAccuracy
(
srcSize
/
1024
);
System
.
out
.
println
(
"accuracy : "
+
accuracy
);
// 防止压缩后图片变红
Image
src
=
Toolkit
.
getDefaultToolkit
().
getImage
(
filePath
);
BufferedImage
image
=
toBufferedImage
(
src
);
// 第一次压缩
Thumbnails
.
of
(
image
).
scale
(
accuracy
).
toFile
(
compressPath
);
imageBytes
=
FileUtil
.
fileToBytes
(
new
File
(
compressPath
));
System
.
out
.
println
(
"imageBytes length : "
+
imageBytes
.
length
+
"------desFileSize * 1024: "
+
desFileSize
*
1024
);
while
(
imageBytes
.
length
>
desFileSize
*
1024
)
{
ByteArrayInputStream
inputStream
=
new
ByteArrayInputStream
(
imageBytes
);
ByteArrayOutputStream
outputStream
=
new
ByteArrayOutputStream
(
imageBytes
.
length
);
Thumbnails
.
of
(
inputStream
).
scale
(
accuracy
).
outputQuality
(
accuracy
).
toOutputStream
(
outputStream
);
imageBytes
=
outputStream
.
toByteArray
();
System
.
out
.
println
(
"imageBytes length : "
+
imageBytes
.
length
);
}
System
.
out
.
println
(
"【图片压缩结束】 图片原大小= "
+
srcSize
/
1024
+
" kb | 压缩后大小= "
+
imageBytes
.
length
/
1024
+
" kb"
);
}
catch
(
Exception
e
)
{
System
.
out
.
println
(
"【图片压缩】msg=图片压缩失败!"
+
e
);
}
return
imageBytes
;
}
/**
* 自动调节精度(经验数值)
*
* @param size 源图片大小
* @return 图片压缩质量比
*/
private
static
double
getAccuracy
(
long
size
)
{
double
accuracy
;
if
(
size
<
900
)
{
accuracy
=
0.85
;
}
else
if
(
size
<
2047
)
{
accuracy
=
0.6
;
}
else
if
(
size
<
3275
)
{
accuracy
=
0.44
;
}
else
{
accuracy
=
0.4
;
}
return
accuracy
;
}
public
static
BufferedImage
toBufferedImage
(
Image
image
)
{
if
(
image
instanceof
BufferedImage
)
{
return
(
BufferedImage
)
image
;
}
// This code ensures that all the pixels in the image are loaded
image
=
new
ImageIcon
(
image
).
getImage
();
BufferedImage
bimage
=
null
;
GraphicsEnvironment
ge
=
GraphicsEnvironment
.
getLocalGraphicsEnvironment
();
try
{
int
transparency
=
Transparency
.
OPAQUE
;
GraphicsDevice
gs
=
ge
.
getDefaultScreenDevice
();
GraphicsConfiguration
gc
=
gs
.
getDefaultConfiguration
();
bimage
=
gc
.
createCompatibleImage
(
image
.
getWidth
(
null
),
image
.
getHeight
(
null
),
transparency
);
}
catch
(
HeadlessException
e
)
{
// The system does not have a screen
}
if
(
bimage
==
null
)
{
// Create a buffered image using the default color model
int
type
=
BufferedImage
.
TYPE_INT_RGB
;
bimage
=
new
BufferedImage
(
image
.
getWidth
(
null
),
image
.
getHeight
(
null
),
type
);
}
// Copy image to buffered image
Graphics
g
=
bimage
.
createGraphics
();
// Paint the image onto the buffered image
g
.
drawImage
(
image
,
0
,
0
,
null
);
g
.
dispose
();
return
bimage
;
}
public
static
void
main
(
String
[]
args
)
{
String
s
=
"C:\\Users\\zhangyong\\Desktop\\jpg\\2023_12_08_09_21_32_21723102_11.jpg"
;
String
d
=
"C:\\Users\\zhangyong\\Desktop\\jpg\\2023_12_08_09_21_32_21723102_12.jpg"
;
compressPicForScale
(
s
,
d
,
1
);
}
}
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