public
class
ImgFactory
{
private
Logger
log
=
LoggerFactory
.
getLogger
(
ImgFactory
.
class
);
private
File
_saveFile
;
private
BufferedImage
_sourceBufferedImage
;
public
static
final
int
LETF
=
1
;
public
static
final
int
RIGHT
=
2
;
public
static
final
int
CENTER
=
3
;
public
static
final
int
ABOVE
=
4
;
public
static
final
int
BELOW
=
5
;
private
BufferedImage
_waterMarker
;
private
int
markerX
;
private
int
markerY
;
private
int
markerWidth
;
private
int
markerHeight
;
public
ImgFactory
(
String
soursePath
,
String
savePath
)
throws
IOException
{
this
(
new
File
(
soursePath
),
new
File
(
savePath
));
}
public
ImgFactory
(
File
_sourseFile
,
File
_saveFile
)
throws
IOException
{
if
(
_sourseFile
==
null
||
_saveFile
==
null
){
throw
new
NullPointerException
(
"图片文件不能为null"
);
}
if
(!
_sourseFile
.
exists
()){
throw
new
FileNotFoundException
(
_sourseFile
.
getAbsolutePath
()+
"not found!"
);
}
this
.
_sourceBufferedImage
=
ImageIO
.
read
(
_sourseFile
);
this
.
_saveFile
=
_saveFile
;
}
public
void
compress
(
int
width
,
int
height
)
throws
IOException
{
//---缩放图片
Image
_compressImage
=
_sourceBufferedImage
.
getScaledInstance
(
width
,
height
,
Image
.
SCALE_SMOOTH
);
//----图像透明,设置
BufferedImage
_bufferedImage
=
new
BufferedImage
(
width
,
height
,
this
.
_sourceBufferedImage
.
getType
());
Graphics
graphics
=
_bufferedImage
.
getGraphics
();
Graphics2D
graphics2d
=(
Graphics2D
)
graphics
;
//---绘制图片
graphics2d
.
drawImage
(
_compressImage
,
0
,
0
,
null
);
saveImg
(
_bufferedImage
);
}
public
void
compress
(
float
radio
)
throws
IOException
{
int
_width
=
_sourceBufferedImage
.
getWidth
();
int
_height
=
_sourceBufferedImage
.
getHeight
();
float
_toWidth
=
_width
;
float
_toHeight
=
_height
;
if
(
radio
!=
0
){
_toWidth
*=
radio
;
_toHeight
*=
radio
;
}
compress
(
Math
.
round
(
_toWidth
),
Math
.
round
(
_toHeight
));
}
public
void
setWaterMarker
(
String
imgPath
,
int
x
,
int
y
,
int
width
,
int
height
)
throws
IOException
{
this
.
_waterMarker
=
ImageIO
.
read
(
new
File
(
imgPath
));
this
.
markerX
=
x
;
this
.
markerY
=
y
;
this
.
markerWidth
=
width
;
this
.
markerHeight
=
height
;
}
public
void
zoom
(
int
width
,
int
height
,
boolean
isCompress
)
throws
IOException
{
if
(
isCompress
){
compress
(
width
,
height
);
}
else
{
BufferedImage
_bufferedImage
=
new
BufferedImage
(
width
,
height
,
this
.
_sourceBufferedImage
.
getType
());
Graphics
graphics
=
_bufferedImage
.
getGraphics
();
Graphics2D
graphics2d
=(
Graphics2D
)
graphics
;
//---绘制图片
graphics2d
.
drawImage
(
this
.
_sourceBufferedImage
,
0
,
0
,
width
,
height
,
null
);
//--
saveImg
(
_bufferedImage
);
}
}
public
void
zoom
(
float
radio
,
boolean
isCompress
)
throws
IOException
{
int
_width
=
_sourceBufferedImage
.
getWidth
();
int
_height
=
_sourceBufferedImage
.
getHeight
();
float
_toWidth
=
_width
;
float
_toHeight
=
_height
;
if
(
radio
!=
0
){
_toWidth
*=
radio
;
_toHeight
*=
radio
;
}
zoom
(
Math
.
round
(
_toWidth
),
Math
.
round
(
_toHeight
)
,
isCompress
);
}
public
String
getFileSuffix
(
File
file
)
{
if
(
file
==
null
)
{
return
null
;
}
return
getFileSuffix
(
file
.
getName
());
}
public
String
getFileSuffix
(
String
fileName
)
{
if
(
fileName
==
null
){
return
null
;
}
String
suffix
=
""
;
int
i
=
fileName
.
lastIndexOf
(
'.'
);
if
(
i
>
0
&&
i
<
fileName
.
length
()
-
1
)
{
suffix
=
fileName
.
substring
(
i
+
1
);
}
return
suffix
;
}
public
void
setSourceFile
(
File
sourseFile
)
throws
IOException
{
this
.
_sourceBufferedImage
=
ImageIO
.
read
(
sourseFile
);
}
public
void
setSourceFile
(
String
path
)
throws
IOException
{
setSaveFile
(
new
File
(
path
));
}
public
void
setSaveFile
(
File
_saveFile
)
{
this
.
_saveFile
=
_saveFile
;
}
public
void
setSaveFile
(
String
path
){
setSaveFile
(
new
File
(
path
));
}
public
void
saveImg
(
BufferedImage
bufferedImage
)
throws
IOException
{
Graphics2D
graphics2d
=(
Graphics2D
)
bufferedImage
.
getGraphics
();
//----水印图片不等于NULL,给图片添加水印
if
(
this
.
_waterMarker
!=
null
){
if
(
bufferedImage
.
getWidth
()
<
this
.
markerWidth
||
bufferedImage
.
getHeight
()
<
this
.
markerHeight
){
log
.
error
(
"图片小于水印图片大小!添加失败"
);
}
else
{
//---绘制水印
graphics2d
.
drawImage
(
this
.
_waterMarker
,
markerX
,
markerY
,
markerWidth
,
markerHeight
,
null
);
}
}
graphics2d
.
dispose
();
ImageIO
.
write
(
bufferedImage
,
getFileSuffix
(
_saveFile
),
_saveFile
);
}
//---测试
public
static
void
main
(
String
[]
args
)
{
try
{
ImgFactory
ifs
=
new
ImgFactory
(
"D:/1.png"
,
"D:/2.png"
);
ifs
.
setWaterMarker
(
"D:/3.png"
,
0
,
0
,
50
,
50
);
ifs
.
zoom
(
0.5f
,
false
);
System
.
out
.
println
(
"成功!!!"
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)