图片添加文字水印和图片水印

  1 /**
  2  * 〈〉
  3  *
  4  * @author WatermelonBoy
  5  */
  6 public class WaterBase {
  7     // 水印透明度
  8     private static float alpha = 0.9f;
  9     // 水印横向位置
 10     private static int positionWidth = 150;
 11     // 水印纵向位置
 12     private static int positionHeight = 100;
 13     // 水印文字字体
 14     private static Font font = new Font("宋体", Font.BOLD, 30);
 15     // 水印文字颜色
 16     private static Color color = Color.red;
 17 
 18     public static void setImageMarkOptions(float alpha, int positionWidth, int positionHeight, Font font, Color color) {
 19         if (alpha != 0.0f) WaterBase.alpha = alpha;
 20         if (positionWidth != 0) WaterBase.positionWidth = positionWidth;
 21         if (positionHeight != 0) WaterBase.positionHeight = positionHeight;
 22         if (font != null) WaterBase.font = font;
 23         if (color != null) WaterBase.color = color;
 24     }
 25 
 26     public static void main(String[] args) throws IOException {
 27         String srcImgPath = "F:\GitLocalStorage\xiakesong\services\house-service\src\main\resources\wallhaven.jpg";
 28         String iconPath = "F:\GitLocalStorage\xiakesong\services\house-service\src\main\resources\icon.jpg";
 29         String targerIconPath2 = "F:\GitLocalStorage\xiakesong\services\house-service\src\main\resources\targetImg.jpg";
 30         String logoText = "@侠客颂";
 31 
 32         //可通过此方法调试间距,颜色.不适用则默认初始化数据
 33         //setImageMarkOptions(0.9f, 250, 100, null, null);
 34 
 35         /**给图片添加水印图片,水印图片旋转0*/
 36         //markImageByIcon(iconPath, srcImgPath, targerIconPath2, 0);
 37 
 38         /**给图片添加水印文字,水印图片旋转-45*/
 39         //markImageByText(logoText, srcImgPath, targerIconPath2, -45);
 40 
 41         /**给图片添加水印文字Hutool工具类*/
 42         FileInputStream fileInputStream = new FileInputStream(new File("gg"));
 43         FileOutputStream fileOutputStream = new FileOutputStream(new File(targerIconPath2));
 44 
 45         BufferedImage srcImg = ImageIO.read(fileInputStream);
 46         int height = srcImg.getHeight() - 100;
 47         int width = srcImg.getWidth() - 200;
 48         fileInputStream = new FileInputStream(new File(srcImgPath));
 49         ImgUtil.pressText(fileInputStream, fileOutputStream, logoText, Color.CYAN, new Font("微软雅黑", Font.BOLD, 60), 0, -600, 0.2f);
 50         fileInputStream.close();
 51         fileOutputStream.close();
 52 
 53     }
 54 
 55     //读取图片转为Base64字符串
 56     public static String TuImage(String args) {
 57 
 58         String imgFile = args;//待处理的图片
 59         InputStream in = null;
 60         byte[] data = null;
 61         //读取图片字节数组
 62         try {
 63             in = new FileInputStream(imgFile);
 64             data = new byte[in.available()];
 65             in.read(data);
 66             in.close();
 67         } catch (IOException e) {
 68             e.printStackTrace();
 69         }
 70         //对字节数组Base64编码
 71         BASE64Encoder encoder = new BASE64Encoder();
 72         return encoder.encode(data);
 73     }
 74 
 75     public static String markImageByIcon(String iconPath, String srcImgPath, String targerPath, Integer degree) {
 76 
 77         BASE64Encoder base64en = new BASE64Encoder();
 78         BASE64Decoder base64de = new BASE64Decoder();
 79         InputStream is = null;
 80         OutputStream os = null;
 81         byte[] b;
 82         try {
 83             //针对上送的图片是Base64场景
 84             b = base64de.decodeBuffer(TuImage(srcImgPath));
 85             is = new java.io.ByteArrayInputStream(b);
 86             BufferedImage srcImg = ImageIO.read(is);
 87 
 88 
 89             BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null), srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
 90 
 91             // 1、得到画笔对象
 92             Graphics2D g = buffImg.createGraphics();
 93 
 94             // 2、设置对线段的锯齿状边缘处理
 95             g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
 96             g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null), srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null);
 97             // 3、设置水印旋转
 98             if (null != degree) {
 99                 g.rotate(Math.toRadians(degree), (double) buffImg.getWidth() / 2, (double) buffImg.getHeight() / 2);
100             }
101 
102             // 4、水印图片的路径 水印图片一般为gif或者png的,这样可设置透明度(本地地址可直接填写,服务器地址ImageIcon导包位置一定要正确)
103             ImageIcon imgIcon = new ImageIcon(iconPath);
104 
105             // 5、得到Image对象
106             Image img = imgIcon.getImage();
107 
108             g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
109 
110             // 6、水印图片的位置
111             g.drawImage(img, positionWidth, positionHeight, null);
112             g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
113             // 7、释放资源
114             g.dispose();
115 
116             // 8、生成图片
117             os = new FileOutputStream(targerPath);
118             ImageIO.write(buffImg, "JPG", os);
119 
120             //把图片转换为字节
121             ByteArrayOutputStream bot = new ByteArrayOutputStream();
122             JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bot);
123             encoder.encode(buffImg);
124             byte[] rstByte = bot.toByteArray();
125             srcImgPath = base64en.encode(rstByte);
126             System.out.println("图片完成添加水印图片");
127             return srcImgPath;
128 
129         } catch (Exception e) {
130             e.printStackTrace();
131         } finally {
132             try {
133                 if (null != os)
134                     os.close();
135                 if (null != is)
136                     is.close();
137             } catch (Exception e) {
138                 e.printStackTrace();
139             }
140         }
141         return targerPath;
142     }
143 
144     /**
145      * 给图片添加水印文字
146      *
147      * @param logoText   水印文字
148      * @param srcImgPath 源图片路径
149      * @param targerPath 目标图片路径
150      */
151     public static void markImageByText(String logoText, String srcImgPath,
152                                        String targerPath) {
153         markImageByText(logoText, srcImgPath, targerPath, null);
154     }
155 
156     /**
157      * 给图片添加水印文字、可设置水印文字的旋转角度
158      *
159      * @param logoText
160      * @param srcImgPath
161      * @param targerPath
162      * @param degree
163      */
164     public static void markImageByText(String logoText, String srcImgPath,
165                                        String targerPath, Integer degree) {
166 
167         BASE64Decoder base64de = new BASE64Decoder();
168         InputStream is = null;
169         OutputStream os = null;
170         byte[] b;
171         try {
172             // 1、源图片
173             //针对上送的图片是Base64场景
174             b = base64de.decodeBuffer(TuImage(srcImgPath));
175             is = new java.io.ByteArrayInputStream(b);
176             BufferedImage srcImg = ImageIO.read(is);
177             int height = srcImg.getHeight() - 100;
178             int width = srcImg.getWidth() - 200;
179             System.out.println(height);
180             System.out.println(width);
181             setImageMarkOptions(0.9f, width, height, null, null);
182 
183             //Image srcImg = ImageIO.read(new File(srcImgPath));
184             BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null), srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
185 
186             // 2、得到画笔对象
187             Graphics2D g = buffImg.createGraphics();
188             // 3、设置对线段的锯齿状边缘处理
189             g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
190             g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null), srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null);
191             // 4、设置水印旋转
192             if (null != degree) {
193                 g.rotate(Math.toRadians(degree), (double) buffImg.getWidth() / 2, (double) buffImg.getHeight() / 2);
194             }
195             // 5、设置水印文字颜色
196             g.setColor(color);
197             // 6、设置水印文字Font
198             g.setFont(font);
199             // 7、设置水印文字透明度
200             g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
201             // 8、第一参数->设置的内容,后面两个参数->文字在图片上的坐标位置(x,y)
202             g.drawString(logoText, positionWidth, positionHeight);
203             // 9、释放资源
204             g.dispose();
205             // 10、生成图片
206             os = new FileOutputStream(targerPath);
207             ImageIO.write(buffImg, "JPG", os);
208 
209             System.out.println("图片完成添加水印文字");
210 
211         } catch (Exception e) {
212             e.printStackTrace();
213         } finally {
214             try {
215                 if (null != is)
216                     is.close();
217             } catch (Exception e) {
218                 e.printStackTrace();
219             }
220             try {
221                 if (null != os)
222                     os.close();
223             } catch (Exception e) {
224                 e.printStackTrace();
225             }
226         }
227     }
228 
229 }
原文地址:https://www.cnblogs.com/codecodeandcode/p/14236701.html