-
测试一下查看全部
-
不公开,10秒查看全部
-
添加水印图片的思路查看全部
-
struts.xml 配置action查看全部
-
在struts.xml中添加新的action配置查看全部
-
struts.xml 配置文件查看全部
-
struts2框架整合 web文件查看全部
-
jar查看全部
-
java图片水印实用工具类查看全部
-
搭建框架查看全部
-
项目案例演示查看全部
-
项目结构。。。。。
查看全部 -
书写代码思路
查看全部 -
课程目标:
了解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;
}查看全部
举报