Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
H
haoban-manage3.0
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
haoban3.0
haoban-manage3.0
Commits
30a41d42
Commit
30a41d42
authored
Oct 28, 2024
by
王祖波
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化imageIo读取
parent
f09012d8
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
30 additions
and
11 deletions
+30
-11
ImageCombined.java
...haoban/manage/service/context/combined/ImageCombined.java
+30
-11
No files found.
haoban-manage3-service/src/main/java/com/gic/haoban/manage/service/context/combined/ImageCombined.java
View file @
30a41d42
...
@@ -9,11 +9,15 @@ import org.slf4j.Logger;
...
@@ -9,11 +9,15 @@ import org.slf4j.Logger;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
import
javax.imageio.ImageIO
;
import
javax.imageio.ImageIO
;
import
javax.imageio.ImageReader
;
import
javax.imageio.stream.ImageInputStream
;
import
java.awt.*
;
import
java.awt.*
;
import
java.awt.image.BufferedImage
;
import
java.awt.image.BufferedImage
;
import
java.io.ByteArrayOutputStream
;
import
java.io.ByteArrayOutputStream
;
import
java.io.File
;
import
java.io.File
;
import
java.io.IOException
;
import
java.net.URL
;
import
java.net.URL
;
import
java.util.Iterator
;
import
java.util.concurrent.TimeUnit
;
import
java.util.concurrent.TimeUnit
;
/**
/**
...
@@ -38,11 +42,11 @@ public class ImageCombined {
...
@@ -38,11 +42,11 @@ public class ImageCombined {
private
static
final
int
QR_WIDTH_REFERENCE
=
136
;
private
static
final
int
QR_WIDTH_REFERENCE
=
136
;
private
static
final
String
LARGE_IMAGE_URL
=
"https://
jhdmyx-1251519181.cos.ap-shanghai.myqcloud.com/image/material_content-2e81265ed1bf4c4d8a623d6b04942767.jpe
g?imageView2/2/w/1080/h/10800/format/jpg"
;
private
static
final
String
LARGE_IMAGE_URL
=
"https://
wcy-1251519181.cos.ap-shanghai.myqcloud.com/image/material_content-0e89ce4dc0454998a16e5295a32e13ba.jp
g?imageView2/2/w/1080/h/10800/format/jpg"
;
private
static
final
String
QR_CODE_URL
=
"https://gicinner-1251519181.cos.ap-shanghai.myqcloud.com/image/material_content-4ffc77073ca1476fb264bf1be9f11383.png"
;
private
static
final
String
QR_CODE_URL
=
"https://gicinner-1251519181.cos.ap-shanghai.myqcloud.com/image/material_content-4ffc77073ca1476fb264bf1be9f11383.png"
;
private
static
final
String
STORE_NAME
=
"门店CCA"
;
private
static
final
String
STORE_NAME
=
"门店CCA"
;
private
static
final
String
GUIDE_NAME
=
"宇智222为您推荐"
;
private
static
final
String
GUIDE_NAME
=
"宇智222为您推荐"
;
private
static
final
String
OUTPUT_PATH
=
"/Users/wang/Downloads/output_image
3
.jpg"
;
private
static
final
String
OUTPUT_PATH
=
"/Users/wang/Downloads/output_image
4
.jpg"
;
static
{
static
{
ImageIO
.
scanForPlugins
();
// 初始化图像插件
ImageIO
.
scanForPlugins
();
// 初始化图像插件
...
@@ -70,7 +74,7 @@ public class ImageCombined {
...
@@ -70,7 +74,7 @@ public class ImageCombined {
//压缩图片的参数
//压缩图片的参数
imageUrl
=
imageUrl
+
"?imageView2/2/w/1080/h/10800/format/jpg"
;
imageUrl
=
imageUrl
+
"?imageView2/2/w/1080/h/10800/format/jpg"
;
BufferedImage
largeImage
=
loadImageWithCache
(
imageUrl
);
BufferedImage
largeImage
=
loadImageWithCache
(
imageUrl
);
BufferedImage
qrCodeImage
=
ImageIO
.
read
(
new
URL
(
qrCodeUrl
)
);
BufferedImage
qrCodeImage
=
loadImageWithReader
(
qrCodeUrl
);
if
(
largeImage
==
null
||
qrCodeImage
==
null
)
{
if
(
largeImage
==
null
||
qrCodeImage
==
null
)
{
return
null
;
return
null
;
}
}
...
@@ -84,16 +88,33 @@ public class ImageCombined {
...
@@ -84,16 +88,33 @@ public class ImageCombined {
}
}
}
}
private
static
BufferedImage
loadImageWithReader
(
String
url
)
{
try
{
URL
imageUrl
=
new
URL
(
url
);
ImageInputStream
input
=
ImageIO
.
createImageInputStream
(
imageUrl
.
openStream
());
Iterator
<
ImageReader
>
readers
=
ImageIO
.
getImageReaders
(
input
);
if
(
readers
.
hasNext
())
{
ImageReader
reader
=
readers
.
next
();
reader
.
setInput
(
input
,
true
);
return
reader
.
read
(
0
);
}
}
catch
(
IOException
e
)
{
logger
.
error
(
"读取异常: {}"
,
url
,
e
);
}
return
null
;
}
/**
/**
* 从缓存中加载图像
* 从缓存中加载图像
*/
*/
private
static
BufferedImage
loadImageWithCache
(
String
url
)
{
private
static
BufferedImage
loadImageWithCache
(
String
url
)
{
return
imageCache
.
get
(
url
,
key
->
{
return
imageCache
.
get
(
url
,
key
->
{
try
{
try
{
logger
.
info
(
"加载图片buffered:{},key:{}"
,
url
,
key
);
logger
.
info
(
"加载图片buffered:{},key:{}"
,
url
,
key
);
return
ImageIO
.
read
(
new
URL
(
key
));
return
loadImageWithReader
(
key
);
// 使用读取器方法
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
logger
.
info
(
"获取图片异常"
,
e
);
logger
.
error
(
"获取图片异常"
,
e
);
return
null
;
return
null
;
}
}
});
});
...
@@ -104,7 +125,7 @@ public class ImageCombined {
...
@@ -104,7 +125,7 @@ public class ImageCombined {
logger
.
info
(
"合成图片开始"
);
logger
.
info
(
"合成图片开始"
);
int
width
=
image
.
getWidth
();
int
width
=
image
.
getWidth
();
int
height
=
image
.
getHeight
();
int
height
=
image
.
getHeight
();
int
qrWidth
=
QR_WIDTH_REFERENCE
;
if
(
showStyle
==
1
)
{
if
(
showStyle
==
1
)
{
if
(
height
>
MAX_HEIGHT_NO_WHITE_SPACE
)
{
if
(
height
>
MAX_HEIGHT_NO_WHITE_SPACE
)
{
width
=
width
*
MAX_HEIGHT_NO_WHITE_SPACE
/
height
;
width
=
width
*
MAX_HEIGHT_NO_WHITE_SPACE
/
height
;
...
@@ -124,10 +145,8 @@ public class ImageCombined {
...
@@ -124,10 +145,8 @@ public class ImageCombined {
Graphics2D
g
=
combinedImage
.
createGraphics
();
Graphics2D
g
=
combinedImage
.
createGraphics
();
g
.
drawImage
(
resizedImage
,
0
,
0
,
null
);
g
.
drawImage
(
resizedImage
,
0
,
0
,
null
);
if
(
showStyle
==
1
)
{
int
qrWidth
=
width
*
QR_WIDTH_REFERENCE
/
750
;
qrWidth
=
width
*
QR_WIDTH_REFERENCE
/
750
;
if
(
width
<=
MIN_WIDTH
)
{
qrWidth
=
136
;
}
}
BufferedImage
resizedQRCode
=
Thumbnails
.
of
(
qrCode
).
size
(
qrWidth
,
qrWidth
).
asBufferedImage
();
BufferedImage
resizedQRCode
=
Thumbnails
.
of
(
qrCode
).
size
(
qrWidth
,
qrWidth
).
asBufferedImage
();
...
...
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