Fork me on GitHub

Java实现图片添加水印

参考别人的感觉挺好玩,还没仔细研究,先上代码。

 1 package WaterMark;
 2 
 3 import javax.imageio.ImageIO;
 4 import java.awt.*;
 5 import java.awt.image.BufferedImage;
 6 import java.io.File;
 7 import java.io.FileOutputStream;
 8 import java.io.OutputStream;
 9 
10 /**
11  * Created by zhengbin06 on 2017/2/5.
12  */
13 public class WaterMarkFont {
14     /**
15      * @param args
16      */
17     public static void main(String[] args) {
18         
19         File srcImgFile = new File("/Users/zhengbin/Downloads/2.jpg");
20         String logoText = "www.cnblogs.com/zhengbin";
21         
22         File outputImageFile = new File("/Users/zhengbin/Downloads/2-1.jpg");
23         
24         File outputRotateImageFile = new File("/Users/zhengbin/Downloads/2-2.jpg");
25         
26         createWaterMarkByText(srcImgFile, logoText, outputImageFile);
27         
28         createWaterMarkByText(srcImgFile, logoText, outputRotateImageFile, 45);
29     }
30     
31     
32     public static void createWaterMarkByText(File srcImgFile, String logoText,
33                                              File outputImageFile) {
34         createWaterMarkByText(srcImgFile, logoText, outputImageFile, 0);
35     }
36     
37     public static void createWaterMarkByText(File srcImgFile, String logoText,
38                                              File outputImageFile, double degree) {
39         OutputStream os = null;
40         try {
41             Image srcImg = ImageIO.read(srcImgFile);
42             
43             // 定义图像的宽高和图像类型
44             BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),
45                     srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
46             
47             // 创建一个 Graphics2D,可以将它绘制到此 BufferedImage 中
48             // Graphics2D,它是用于在 Java(tm) 平台上呈现二维形状、文本和图像的基础类。
49             Graphics2D g = buffImg.createGraphics();
50             
51             g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
52                     RenderingHints.VALUE_INTERPOLATION_BILINEAR);
53             
54             g.drawImage(
55                     srcImg.getScaledInstance(srcImg.getWidth(null),
56                             srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0,
57                     null);
58             
59             if (degree>0) {
60                 // 旋转
61                 // 将用角度表示的角转换为近似相等的用弧度表示的角
62                 g.rotate(Math.toRadians(degree),
63                         (double) buffImg.getWidth() / 2,
64                         (double) buffImg.getHeight() / 2);
65             }
66             
67             g.setColor(Color.RED);
68             
69             g.setFont(new Font("宋体", Font.BOLD, 36));
70             
71             float alpha = 0.8f;
72             g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
73                     alpha));
74             
75             // 烙上水印
76             g.drawString(logoText, buffImg.getWidth()/3, buffImg.getHeight()/2);
77             
78             // 释放此图形的上下文以及它使用的所有系统资源
79             g.dispose();
80             
81             os = new FileOutputStream(outputImageFile);
82             
83             // 生成图片
84             ImageIO.write(buffImg, "JPG", os);
85             
86         } catch (Exception e) {
87             e.printStackTrace();
88         } finally {
89             try {
90                 if (null != os)
91                     os.close();
92             } catch (Exception e) {
93                 e.printStackTrace();
94             }
95         }
96     }
97 }

 

posted @ 2017-02-05 21:50  郑斌blog  阅读(1365)  评论(0编辑  收藏  举报