java实现图片加水印效果
图片加水印代码,这些代码不常用,但是用到的时候需要注意的地方也挺多的,每次都重写比较麻烦,记下来备忘。代码是图片加水印的一般流程,可根据实际项目需要自行修改。
注:代码在JPG和PNG格式图片下测试通过,其他图片格式请自行测试和修改
import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.geom.AffineTransform; import java.awt.image.BufferedImage; import java.io.File; import java.util.Iterator; import javax.imageio.IIOImage; import javax.imageio.ImageIO; import javax.imageio.ImageReader; import javax.imageio.ImageWriteParam; import javax.imageio.ImageWriter; import javax.imageio.stream.FileImageInputStream; import javax.imageio.stream.FileImageOutputStream; import org.junit.Test; public class Wjt { @Test public void t3() throws Exception { File rootDir = new File("C:/Users/Administrator/Desktop/tem1"); String[] fa = { "1.png", "2.jpg" }; for (String s : fa) { String f = s; { FileImageInputStream fileImageInputStream = new FileImageInputStream(new File(rootDir, f)); Iterator<ImageReader> imageReaders = ImageIO.getImageReaders(fileImageInputStream); //根据图片文件获取读取器 ImageReader imageReader = imageReaders.hasNext() ? imageReaders.next() : null; //检测读取器是否存在 imageReader.setInput(fileImageInputStream); //设置读取器输入流 BufferedImage bufferedImage = imageReader.read(0); //从读取器取BufferedImage,这里只有一个,所以可以直接写0 { //开始加水印 Graphics2D graphics2d = bufferedImage.createGraphics(); //取画笔 graphics2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); //设置画笔文字抗锯齿 graphics2d.transform(AffineTransform.getTranslateInstance(bufferedImage.getWidth() / 2, bufferedImage.getHeight() / 2)); //将画笔移动到图片中央 graphics2d.transform(AffineTransform.getRotateInstance(Math.toRadians(45))); //将画笔旋转一定弧度 float radius = Double.valueOf(Math.sqrt(bufferedImage.getWidth() * bufferedImage.getWidth() + bufferedImage.getHeight() * bufferedImage.getHeight()) / 2).floatValue(); //计算图片外切圆半径 Font waterMarkerFont = new Font("微软雅黑", Font.PLAIN, bufferedImage.getHeight() / 25); //设置画笔要用的字体,这里可以设置字体样式和大小 graphics2d.setFont(waterMarkerFont); //设置画笔字体 graphics2d.setColor(Color.WHITE); //设置画笔颜色 graphics2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.3F)); //设置画笔操作透明度 FontMetrics fontMetrics = graphics2d.getFontMetrics(); //取画笔字体信息 String waterMarker = "THIS IS WATERMARK."; //水印文字 int stringWidth = fontMetrics.stringWidth(waterMarker); //计算水印文字宽度,很多人卡在了这一步 for (float i = -radius; i <= radius; i += (stringWidth + bufferedImage.getHeight() / 25)) { //循环添加水印,这里可以设置水印水平距离,单位:px for (float j = -radius; j <= radius; j += (fontMetrics.getHeight() + bufferedImage.getHeight() / 25)) { //这里可以设置水印垂直距离,单位:px graphics2d.drawString(waterMarker, i, j); //画水印 } } graphics2d.dispose(); //释放画笔资源 } ImageWriter imageWriter = ImageIO.getImageWriter(imageReader); //根据图片文件读取器获取图片文件写入器 String[] split = f.split("\\."); FileImageOutputStream fileOutputStream = new FileImageOutputStream(new File(rootDir, split[0] + "r." + split[1])); //生成目标图片文件路径 imageWriter.setOutput(fileOutputStream); //设置写入器输出流 IIOImage iioImage = new IIOImage(bufferedImage, null, null); ImageWriteParam imageWriteParam = imageWriter.getDefaultWriteParam(); if (imageReader.getFormatName().equals("JPEG")) { //jpg格式图片可设置输出图片文件的质量 imageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); //这一行固定这样写,跟下一行搭配 imageWriteParam.setCompressionQuality(0.947F); //跟上一行搭配,设置输出图片文件的质量 } imageWriter.write(null, iioImage, imageWriteParam); //写文件 imageWriter.dispose(); //关闭写入器 imageReader.dispose(); //关闭读取器 } } } }
代码流程在注释中写的很详细了,不多做解释。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)