项目结构。。。。。
书写代码思路
课程目标:
了解Java图片水印实现思路
掌握文字水印和图片水印的实现
掌握多图片批量水印的实现
while循环判断条件中的width和height应该是图片的宽和高,老师误写了
具体代码如下:
public String waterMark(File image, String imageFileName,
String uploadPath, String realUploadPath) {
String logoFileName = "logo_" + imageFileName;
OutputStream os = null;
try {
Image img = ImageIO.read(image);
int imgWidth = img.getWidth(null);
int imgHeight = img.getHeight(null);
BufferedImage bufferedImage = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bufferedImage.createGraphics();
g.drawImage(img, 0, 0, imgWidth, imgHeight, null);
g.setFont(new Font(FONT_NAME,FONT_STYLE,FONT_SIZE));
g.setColor(FONT_COLOR);
int width = FONT_SIZE*getTextLength(MARK_TEXT);
int height = FONT_SIZE;
//设置透明度
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,ALPH));
g.rotate(Math.toRadians(-30),bufferedImage.getWidth()/2,bufferedImage.getHeight()/2);
int x = -width/2;
int y = -height/2;
while(x<imgWidth*1.5){
y = -height/2;
while(y<imgHeight*1.5){
g.drawString(MARK_TEXT, x, y);
y += height + 70;
}
x += width+70;
}
g.dispose();
os = new FileOutputStream(realUploadPath+"/"+logoFileName);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
encoder.encode(bufferedImage);
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
if(os!=null)
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return uploadPath+"/"+logoFileName;
}
public int getTextLength(String text){
int length = text.length();
for (int i = 0; i < text.length(); i++) {
String s = String.valueOf(text.charAt(i));
if(s.getBytes().length>1){
length++;
}
}
length = length%2==0?length/2:length/2+1;
return length;
}
使用文字水印实现类
在waterMarkAction中添加如下代码
(注意:这里的类名和视频的不相同,但类的实现代码一样)
IMarkService markService = new TextMarkServiceImpl();
imageInfo.setLogoImageURL(markService.waterMark(image, imageFileName, uploadPath, realPath));
@Override
public String waterMark(File image, String imageFileName, String uploadPath,
String realUploadPath) {
String logoFileName = "logo_" + imageFileName;
OutputStream os = null;
try {
Image img = ImageIO.read(image);
int imgWidth = img.getWidth(null);
int imgHeight = img.getHeight(null);
BufferedImage bufferedImage = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bufferedImage.createGraphics();
g.drawImage(img, 0, 0, imgWidth, imgHeight, null);
g.setFont(new Font(FONT_NAME,FONT_STYLE,FONT_SIZE));
g.setColor(FONT_COLOR);
//设置透明度
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP));
int width = FONT_SIZE*getTextLength(MARK_TEXT);
int height = FONT_SIZE;
int widthDiff = imgWidth-width;
int heightDiff = imgHeight-height;
int x = X;
int y = Y;
x = x>widthDiff ? widthDiff : x;
y = y>heightDiff ? heightDiff : y;
g.drawString(MARK_TEXT, x, y+FONT_SIZE);
g.dispose();
os = new FileOutputStream(realUploadPath+"/"+logoFileName);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
encoder.encode(bufferedImage);
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
if(os!=null)
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return uploadPath+"/"+logoFileName;
}
public int getTextLength(String text){
int length = text.length();
for (int i = 0; i < text.length(); i++) {
String s = String.valueOf(text.charAt(i));
if(s.getBytes().length>1){
length++;
}
}
length = length%2==0?length/2:length/2+1;
return length;
}
提交页面
strtus.xml添加action配置
struts.xml
xml
jar
1