1 packagemichael.io.image;2
3 importjava.awt.AlphaComposite;4 importjava.awt.Graphics2D;5 importjava.awt.Image;6 importjava.awt.RenderingHints;7 importjava.awt.image.BufferedImage;8 importjava.io.File;9 importjava.io.FileInputStream;10 importjava.io.FileOutputStream;11 importjava.io.InputStream;12 importjava.io.OutputStream;13
14 importjavax.imageio.ImageIO;15 importjavax.swing.ImageIcon;16
17 importcom.sun.image.codec.jpeg.JPEGCodec;18 importcom.sun.image.codec.jpeg.JPEGImageDecoder;19 importcom.sun.image.codec.jpeg.JPEGImageEncoder;20
21 /**
22 * 图片水印23 * @bloghttp://sjsky.iteye.com
24 *@authorMichael25 */
26 public classImageMarkLogoByIcon {27
28 /**
29 *@paramargs30 */
31 public static voidmain(String[] args) {32 String srcImgPath = "d:/test/michael/myblog_01.png";33 String iconPath = "d:/test/michael/blog_logo.png";34 String targerPath = "d:/test/michael/img_mark_icon.jpg";35 String targerPath2 = "d:/test/michael/img_mark_icon_rotate.jpg";36 //给图片添加水印
37 ImageMarkLogoByIcon.markImageByIcon(iconPath, srcImgPath, targerPath);38 //给图片添加水印,水印旋转-45
39 ImageMarkLogoByIcon.markImageByIcon(iconPath, srcImgPath, targerPath2,40 -45);41
42 }43
44 /**
45 * 给图片添加水印46 *@paramiconPath 水印图片路径47 *@paramsrcImgPath 源图片路径48 *@paramtargerPath 目标图片路径49 */
50 public static voidmarkImageByIcon(String iconPath, String srcImgPath,51 String targerPath) {52 markImageByIcon(iconPath, srcImgPath, targerPath, null);53 }54
55 /**
56 * 给图片添加水印、可设置水印图片旋转角度57 *@paramiconPath 水印图片路径58 *@paramsrcImgPath 源图片路径59 *@paramtargerPath 目标图片路径60 *@paramdegree 水印图片旋转角度61 */
62 public static voidmarkImageByIcon(String iconPath, String srcImgPath,63 String targerPath, Integer degree) {64 OutputStream os = null;65 try{66 Image srcImg = ImageIO.read(newFile(srcImgPath));67
68 BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),69 srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);70
71 //得到画笔对象72 //Graphics g= buffImg.getGraphics();
73 Graphics2D g =buffImg.createGraphics();74
75 //设置对线段的锯齿状边缘处理
76 g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,77 RenderingHints.VALUE_INTERPOLATION_BILINEAR);78
79 g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null), srcImg80 .getHeight(null), Image.SCALE_SMOOTH), 0, 0, null);81
82 if (null !=degree) {83 //设置水印旋转
84 g.rotate(Math.toRadians(degree),85 (double) buffImg.getWidth() / 2, (double) buffImg86 .getHeight() / 2);87 }88
89 //水印图象的路径 水印一般为gif或者png的,这样可设置透明度
90 ImageIcon imgIcon = newImageIcon(iconPath);91
92 //得到Image对象。
93 Image img =imgIcon.getImage();94
95 float alpha = 0.5f; //透明度
96 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,97 alpha));98
99 //表示水印图片的位置
100 g.drawImage(img, 150, 300, null);101
102 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));103
104 g.dispose();105
106 os = newFileOutputStream(targerPath);107
108 //生成图片
109 ImageIO.write(buffImg, "JPG", os);110
111 System.out.println("图片完成添加Icon印章。。。。。。");112 } catch(Exception e) {113 e.printStackTrace();114 } finally{115 try{116 if (null !=os)117 os.close();118 } catch(Exception e) {119 e.printStackTrace();120 }121 }122 }123 }