Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
ChenBbMing
Java_278843
提交
37334b6f
J
Java_278843
项目概览
ChenBbMing
/
Java_278843
与 Fork 源项目一致
Fork自
inscode / Java
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
Java_278843
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
37334b6f
编写于
8月 04, 2023
作者:
C
cjm_01
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fri Aug 4 11:23:00 CST 2023 inscode
上级
a5df1b10
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
202 addition
and
0 deletion
+202
-0
NyImageUtils.java
NyImageUtils.java
+202
-0
未找到文件。
NyImageUtils.java
0 → 100644
浏览文件 @
37334b6f
import
java.awt.image.BufferedImage
;
import
java.io.ByteArrayOutputStream
;
import
java.io.File
;
import
java.io.FileInputStream
;
import
java.io.FileOutputStream
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.nio.file.Files
;
import
java.nio.file.Paths
;
import
java.util.ArrayList
;
import
java.util.List
;
import
javax.imageio.ImageIO
;
import
javax.swing.JComponent
;
import
javax.swing.JFileChooser
;
import
javax.swing.filechooser.FileFilter
;
import
nc.bs.logging.Logger
;
import
nc.vo.pub.BusinessException
;
import
org.apache.commons.net.util.Base64
;
import
org.apache.pdfbox.pdmodel.PDDocument
;
import
org.apache.pdfbox.rendering.PDFRenderer
;
import
org.apache.xmlbeans.impl.store.Path
;
import
sun.misc.BASE64Encoder
;
import
com.informix.util.dateUtil
;
import
com.lowagie.text.pdf.codec.Base64.OutputStream
;
import
com.spire.pdf.PdfDocument
;
import
com.spire.pdf.graphics.PdfImageType
;
@SuppressWarnings
(
"all"
)
public
class
NyImageUtils
{
public
static
List
<
String
>
getImageToString
(
JComponent
jComponent
)
throws
IOException
,
BusinessException
{
List
<
String
>
list
=
new
ArrayList
<>();
File
file
=
getFile
(
jComponent
);
if
(
file
==
null
)
{
return
null
;
}
Logger
.
error
(
"开始读取文件:"
+
file
.
getName
());
if
(
file
.
getName
().
endsWith
(
".pdf"
))
{
list
=
pdfToImage
(
file
);
}
else
if
(
file
.
getName
().
endsWith
(
".bmp"
))
{
list
.
add
(
Base64
.
encodeBase64String
(
bmpToImage
(
file
)));
}
else
{
byte
[]
bytes
=
imageToBytes
(
file
);
list
.
add
(
Base64
.
encodeBase64String
(
bytes
));
}
return
list
;
}
private
static
byte
[]
bmpToImage
(
File
file
)
{
byte
[]
data
=
null
;
File
newfile
=
null
;
String
result
=
null
;
try
{
String
path
=
file
.
getPath
();
result
=
path
.
replace
(
"bmp"
,
"jpg"
);
FileOutputStream
out
=
new
FileOutputStream
(
result
);
newfile
=
new
File
(
result
);
ImageIO
.
write
(
ImageIO
.
read
(
file
),
"jpg"
,
newfile
);
data
=
imageToBytes
(
newfile
);
// Files.deleteIfExists(Paths.get(result));
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
data
;
}
public
static
List
<
String
>
sprPdfToImage_stream
(
byte
[]
returnBytes
)
throws
BusinessException
{
List
<
String
>
list
=
new
ArrayList
<
String
>();
PdfDocument
pdf
=
new
PdfDocument
();
pdf
.
loadFromBytes
(
returnBytes
);
for
(
int
i
=
0
;
i
<
pdf
.
getPages
().
getCount
();
i
++)
{
// 将页面保存为图片,并设置DPI分辨率
BufferedImage
image
=
pdf
.
saveAsImage
(
i
,
PdfImageType
.
Bitmap
,
500
,
500
);
// 将图片保存为png格式
ByteArrayOutputStream
fileByte
=
new
ByteArrayOutputStream
();
try
{
ImageIO
.
write
(
image
,
"PNG"
,
fileByte
);
byte
[]
bytes
=
fileByte
.
toByteArray
();
list
.
add
(
Base64
.
encodeBase64String
(
bytes
));
}
catch
(
IOException
e
)
{
String
msg
=
"解析pdf文件出错,"
+
e
.
getMessage
();
Logger
.
error
(
msg
);
throw
new
BusinessException
(
msg
);
}
finally
{
pdf
.
close
();
}
}
return
list
;
}
public
static
List
<
String
>
pdfToImage_Sprpdf
(
File
file
)
throws
BusinessException
{
List
<
String
>
list
=
new
ArrayList
<
String
>();
PdfDocument
pdf
=
new
PdfDocument
();
pdf
.
loadFromFile
(
file
.
toString
());
for
(
int
i
=
0
;
i
<
pdf
.
getPages
().
getCount
();
i
++)
{
// 将页面保存为图片,并设置DPI分辨率
BufferedImage
image
=
pdf
.
saveAsImage
(
i
,
PdfImageType
.
Bitmap
,
500
,
500
);
// 将图片保存为png格式
ByteArrayOutputStream
fileByte
=
new
ByteArrayOutputStream
();
try
{
ImageIO
.
write
(
image
,
"PNG"
,
fileByte
);
byte
[]
bytes
=
fileByte
.
toByteArray
();
list
.
add
(
Base64
.
encodeBase64String
(
bytes
));
}
catch
(
IOException
e
)
{
String
msg
=
"解析pdf文件出错,"
+
e
.
getMessage
();
Logger
.
error
(
msg
);
throw
new
BusinessException
(
msg
);
}
finally
{
pdf
.
close
();
}
}
return
list
;
}
public
static
List
<
String
>
pdfToImage
(
File
file
)
throws
BusinessException
{
List
<
String
>
list
=
new
ArrayList
<
String
>();
PDDocument
doc
=
null
;
try
{
doc
=
PDDocument
.
load
(
file
);
PDFRenderer
renderer
=
new
PDFRenderer
(
doc
);
int
pageCount
=
doc
.
getNumberOfPages
();
for
(
int
i
=
0
;
i
<
pageCount
;
i
++)
{
BufferedImage
image
=
renderer
.
renderImage
(
i
,
2.0f
);
ByteArrayOutputStream
fileByte
=
new
ByteArrayOutputStream
();
ImageIO
.
write
(
image
,
"png"
,
fileByte
);
byte
[]
bytes
=
fileByte
.
toByteArray
();
list
.
add
(
Base64
.
encodeBase64String
(
bytes
));
}
return
list
;
}
catch
(
IOException
e
)
{
String
msg
=
"解析pdf文件出错,"
+
e
.
getMessage
();
Logger
.
error
(
msg
);
throw
new
BusinessException
(
msg
);
}
finally
{
if
(
null
!=
doc
)
{
try
{
doc
.
close
();
}
catch
(
IOException
e
)
{
}
}
}
}
private
static
byte
[]
imageToBytes
(
File
file
)
throws
IOException
{
if
(
file
==
null
||
!
file
.
exists
()
||
!
file
.
isFile
())
{
return
new
byte
[
0
];
}
byte
[]
bytes
=
null
;
FileInputStream
fis
=
null
;
try
{
fis
=
new
FileInputStream
(
file
);
bytes
=
new
byte
[(
int
)
file
.
length
()];
fis
.
read
(
bytes
);
return
bytes
;
}
finally
{
if
(
null
!=
fis
)
{
fis
.
close
();
}
}
}
private
static
File
getFile
(
JComponent
jComponent
)
{
File
file
=
null
;
JFileChooser
choose
=
new
JFileChooser
();
choose
.
setAcceptAllFileFilterUsed
(
false
);
choose
.
addChoosableFileFilter
(
new
FileFilter
()
{
public
boolean
accept
(
File
f
)
{
String
fileName
=
f
.
getName
();
return
f
.
isDirectory
()
||
fileName
.
endsWith
(
".pdf"
)
||
fileName
.
endsWith
(
".PDF"
)
||
fileName
.
endsWith
(
".gif"
)
||
fileName
.
endsWith
(
".GIF"
)
||
fileName
.
endsWith
(
".jpg"
)
||
fileName
.
endsWith
(
".JPG"
)
||
fileName
.
endsWith
(
".bmp"
)
||
fileName
.
endsWith
(
".BMP"
)
||
fileName
.
endsWith
(
".jpeg"
)
||
fileName
.
endsWith
(
".JPEG"
)
||
fileName
.
endsWith
(
".png"
)
||
fileName
.
endsWith
(
".PNG"
);
}
public
String
getDescription
()
{
return
"支持的文件格式:.pdf .gif .jpg .bmp .png .jpeg "
;
}
});
int
retValue
=
choose
.
showOpenDialog
(
jComponent
);
if
(
retValue
==
JFileChooser
.
APPROVE_OPTION
)
{
String
filePath
=
((
File
)
choose
.
getSelectedFile
()).
getAbsolutePath
();
/*
* if (filePath == null || "".equals(filePath) ||
* (filePath.indexOf(".xls") < 0 && filePath.indexOf(".xlsx") < 0))
* { MessageDialog.showErrorDlg(jComponent, "错误", "请选择EXCEL文件!");
* return null; }
*/
file
=
new
File
(
filePath
);
}
return
file
;
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录