Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
F
fill-system
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
廖旭伟
fill-system
Commits
a2c89b7c
Commit
a2c89b7c
authored
Dec 25, 2024
by
赵啸非
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修改图片转换
parent
ae32497e
Pipeline
#2874
canceled with stages
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
76 additions
and
12 deletions
+76
-12
fill-manager/src/main/java/com/mortals/xhx/common/utils/WordUtil.java
.../src/main/java/com/mortals/xhx/common/utils/WordUtil.java
+76
-12
No files found.
fill-manager/src/main/java/com/mortals/xhx/common/utils/WordUtil.java
View file @
a2c89b7c
...
...
@@ -7,6 +7,7 @@ package com.mortals.xhx.common.utils;
**/
import
com.aspose.words.Document
;
import
com.aspose.words.GraphicsQualityOptions
;
import
com.aspose.words.ImageSaveOptions
;
import
com.aspose.words.SaveFormat
;
import
com.mortals.framework.exception.AppException
;
...
...
@@ -24,6 +25,7 @@ import javax.imageio.stream.ImageInputStream;
import
java.awt.*
;
import
java.awt.image.BufferedImage
;
import
java.io.*
;
import
java.net.URL
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Map
;
...
...
@@ -92,11 +94,19 @@ public class WordUtil {
//doc.save(os, SaveFormat.JPEG);
ImageSaveOptions
options
=
new
ImageSaveOptions
(
SaveFormat
.
JPEG
);
options
.
setResolution
(
256
);
GraphicsQualityOptions
qualityOptions
=
new
GraphicsQualityOptions
();
RenderingHints
renderingHints
=
new
RenderingHints
(
RenderingHints
.
KEY_ANTIALIASING
,
RenderingHints
.
VALUE_ANTIALIAS_ON
);
qualityOptions
.
setRenderingHints
(
renderingHints
);
options
.
setGraphicsQualityOptions
(
qualityOptions
);
options
.
setJpegQuality
(
10
);
options
.
setPrettyFormat
(
true
);
options
.
setUseAntiAliasing
(
true
);
options
.
setUseHighQualityRendering
(
true
);
int
pageCount
=
doc
.
getPageCount
();
log
.
info
(
"转换的页数:{}"
,
pageCount
);
log
.
info
(
"转换的页数:{}"
,
pageCount
);
List
<
BufferedImage
>
imageList
=
new
ArrayList
<
BufferedImage
>();
for
(
int
i
=
0
;
i
<
pageCount
;
i
++)
{
OutputStream
output
=
new
ByteArrayOutputStream
();
...
...
@@ -104,18 +114,19 @@ public class WordUtil {
doc
.
save
(
output
,
options
);
ImageInputStream
imageInputStream
=
ImageIO
.
createImageInputStream
(
parse
(
output
));
imageList
.
add
(
ImageIO
.
read
(
imageInputStream
));
log
.
info
(
"转换{}页完成!"
,
i
);
log
.
info
(
"转换{}页完成!"
,
i
);
}
log
.
info
(
"转换结束,开始拼接图片:{}"
,
pageCount
);
BufferedImage
mergeImage
=
mergeImage
(
false
,
imageList
);
log
.
info
(
"转换结束,开始拼接图片:{}"
,
pageCount
);
//BufferedImage mergeImage = mergeImage(false, imageList);
BufferedImage
mergeImage
=
moreToOne
(
imageList
);
Thumbnails
.
of
(
mergeImage
)
Thumbnails
.
of
(
mergeImage
)
.
scale
(
1
f
)
.
outputQuality
(
0.
5
f
).
toFile
(
file
);
.
outputQuality
(
0.
8
f
).
toFile
(
file
);
//
ImageIO.write(mergeImage, "jepg", file);
//
ImageIO.write(mergeImage, "jepg", file);
// 转换结束后时间
/* doc.save(jpegPath);*/
/* doc.save(jpegPath);*/
doc
.
cleanup
();
long
now
=
System
.
currentTimeMillis
();
log
.
info
(
"文件转换结束,共耗时:"
+
((
now
-
old
)
/
1000.0
)
+
"秒"
);
...
...
@@ -139,9 +150,6 @@ public class WordUtil {
}
public
static
void
pdfToImages
(
String
filePath
,
String
jpegPath
)
{
log
.
info
(
String
.
format
(
"pdf to images文件转换开始:%s"
,
DateUtils
.
getCurrStrDateTime
()));
// 转换开始前时间
...
...
@@ -237,6 +245,62 @@ public class WordUtil {
return
destImage
;
}
/**
* 将多张图片拼接成一张长图
*
* @return 拼接后的图片BufferedImage
*/
public
static
BufferedImage
moreToOne
(
List
<
BufferedImage
>
imgs
)
{
//传入图片少于1张直接返回
int
len
=
imgs
.
size
();
if
(
len
<
1
)
{
return
null
;
}
//使用一个数组将图片装起来
BufferedImage
[]
bufferedImages
=
new
BufferedImage
[
len
];
int
[][]
imageArrays
=
new
int
[
len
][];
for
(
int
i
=
0
;
i
<
len
;
i
++)
{
try
{
//文件地址,可直接转换
bufferedImages
[
i
]
=
Thumbnails
.
of
(
imgs
.
get
(
i
))
//.size(400, 600)
.
width
(
595
)
.
outputQuality
(
0.8f
)
.
asBufferedImage
();
}
catch
(
Exception
e
)
{
log
.
error
(
"图片转换失败"
,
e
);
}
//获取图片宽高
int
width
=
bufferedImages
[
i
].
getWidth
();
int
height
=
bufferedImages
[
i
].
getHeight
();
// 从图片中读取RGB
imageArrays
[
i
]
=
new
int
[
width
*
height
];
imageArrays
[
i
]
=
bufferedImages
[
i
].
getRGB
(
0
,
0
,
width
,
height
,
imageArrays
[
i
],
0
,
width
);
}
//拼接图片的宽高
int
dstHeight
=
0
;
int
dstWidth
=
bufferedImages
[
0
].
getWidth
();
for
(
BufferedImage
bufferedImage
:
bufferedImages
)
{
dstWidth
=
Math
.
max
(
dstWidth
,
bufferedImage
.
getWidth
());
dstHeight
+=
bufferedImage
.
getHeight
();
}
// 生成新图片
BufferedImage
imageNew
=
null
;
try
{
imageNew
=
new
BufferedImage
(
dstWidth
,
dstHeight
,
BufferedImage
.
TYPE_INT_RGB
);
int
newHeight
=
0
;
for
(
int
i
=
0
;
i
<
bufferedImages
.
length
;
i
++)
{
imageNew
.
setRGB
(
0
,
newHeight
,
dstWidth
,
bufferedImages
[
i
].
getHeight
(),
imageArrays
[
i
],
0
,
dstWidth
);
newHeight
+=
bufferedImages
[
i
].
getHeight
();
}
}
catch
(
Exception
e
)
{
log
.
error
(
"图片拼接出错"
,
e
);
}
return
imageNew
;
}
public
static
ByteArrayInputStream
parse
(
OutputStream
out
)
throws
Exception
{
ByteArrayOutputStream
baos
=
new
ByteArrayOutputStream
();
...
...
@@ -247,7 +311,7 @@ public class WordUtil {
}
public
static
void
main
(
String
[]
args
)
{
String
docPath
=
"E:\\pic\\doc\\
3
.docx"
;
String
docPath
=
"E:\\pic\\doc\\
new
.docx"
;
String
jpgPath
=
"E:\\pic\\doc\\3.jpg"
;
...
...
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