Java实现 图片水印或者文字水印
在原图之上再画一张图片(drawImage)或者文本文字(drawString),Java SE中提供了 java.awt.image包和javax.imageio包可以实现非常方便的实现 图片水印或者文字水印。
1.图片水印
import java.awt.AlphaComposite; import java.awt.Graphics2D; import java.awt.Image; import java.awt.RenderingHints; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; import javax.imageio.ImageIO; import javax.swing.ImageIcon; /** * 生成图片水印 * @author Ricky Fung * */ public class WaterMarkIconTest { /** * @param args */ public static void main(String[] args) { File srcImageFile = new File("D:/test/myblog.png"); File logoImageFile = new File("D:/test/logo.png"); File outputImageFile = new File("D:/test/myblog_water_icon.jpg"); File outputRoateImageFile = new File("D:/test/myblog_water_icon_rotate.jpg"); createWaterMarkByIcon(srcImageFile, logoImageFile, outputImageFile); createWaterMarkByIcon(srcImageFile, logoImageFile, outputRoateImageFile, 45); } public static void createWaterMarkByIcon(File srcImageFile, File logoImageFile, File outputImageFile) { createWaterMarkByIcon(srcImageFile, logoImageFile, outputImageFile, 0); } public static void createWaterMarkByIcon(File srcImageFile, File logoImageFile, File outputImageFile, double degree) { OutputStream os = null; try { Image srcImg = ImageIO.read(srcImageFile); BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null), srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB); Graphics2D g = buffImg.createGraphics(); g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g.drawImage( srcImg.getScaledInstance(srcImg.getWidth(null), srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null); ImageIcon logoImgIcon = new ImageIcon(ImageIO.read(logoImageFile)); Image logoImg = logoImgIcon.getImage(); //旋转 if (degree>0) { g.rotate(Math.toRadians(degree), (double) buffImg.getWidth() / 2, (double) buffImg.getWidth() / 2); } float alpha = 0.8f; // 透明度 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha)); //水印 的位置 g.drawImage(logoImg, buffImg.getWidth()/3, buffImg.getHeight()/2, null); g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER)); g.dispose(); os = new FileOutputStream(outputImageFile); // 生成图片 ImageIO.write(buffImg, "JPG", os); } catch (Exception e) { e.printStackTrace(); } finally { try { if (null != os) os.close(); } catch (Exception e) { e.printStackTrace(); } } } }
2.文字水印
import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.Image; import java.awt.RenderingHints; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; import javax.imageio.ImageIO; /** * 生成文字水印 * @author Ricky Fung * */ public class WaterMarkTextTest { /** * @param args */ public static void main(String[] args) { File srcImgFile = new File("D:/test/myblog.png"); String logoText = "[ 水印文字 电话邦 ]"; File outputImageFile = new File("D:/test/myblog_water_text.jpg"); File outputRotateImageFile = new File("D:/test/myblog_water_text_rotate.jpg"); createWaterMarkByText(srcImgFile, logoText, outputImageFile); createWaterMarkByText(srcImgFile, logoText, outputRotateImageFile, 45); } public static void createWaterMarkByText(File srcImgFile, String logoText, File outputImageFile) { createWaterMarkByText(srcImgFile, logoText, outputImageFile, 0); } public static void createWaterMarkByText(File srcImgFile, String logoText, File outputImageFile, double degree) { OutputStream os = null; try { Image srcImg = ImageIO.read(srcImgFile); BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null), srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB); Graphics2D g = buffImg.createGraphics(); g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g.drawImage( srcImg.getScaledInstance(srcImg.getWidth(null), srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null); if (degree>0) { g.rotate(Math.toRadians(degree), (double) buffImg.getWidth() / 2, (double) buffImg.getHeight() / 2); } g.setColor(Color.RED); g.setFont(new Font("宋体", Font.BOLD, 36)); float alpha = 0.8f; g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha)); g.drawString(logoText, buffImg.getWidth()/3, buffImg.getHeight()/2); g.dispose(); os = new FileOutputStream(outputImageFile); // 生成图片 ImageIO.write(buffImg, "JPG", os); } catch (Exception e) { e.printStackTrace(); } finally { try { if (null != os) os.close(); } catch (Exception e) { e.printStackTrace(); } } } }