java图片水印处理

编程入门 行业动态 更新时间:2024-10-11 09:23:50

java图片<a href=https://www.elefans.com/category/jswz/34/1769835.html style=水印处理"/>

java图片水印处理

水印处理工具方法

效果

代码

import lombok.extern.slf4j.Slf4j;import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Date;/*** 图片添加水印*/
@Slf4j
public class ImgWaterMark {// 水印透明度private  float alpha = 0.9f;// 水印文字大小private   int fontSize = 28;// 水印文字字体private  Font font =  new Font(Font.DIALOG, Font.PLAIN, this.fontSize);;// 水印文字颜色private  Color color = Color.black;// 水印之间的间隔private   int xmove = 200;// 水印之间的间隔private   int ymove = 200;private int  degree = -40;private String logoText;private ByteArrayOutputStream out;//rgbpublic static Color color(int v1,int v2,int v3){return new Color(v1,v2,v3);}//文字加时间public static String  timeText(String logoText){SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");return logoText + sdf.format(new Date());}public ImgWaterMark(float alpha, int fontSize , Color color, int move, int degree, String logoText){this(logoText);this.alpha = alpha;this.fontSize = fontSize;this.font = new Font(Font.DIALOG, Font.PLAIN, this.fontSize);this.color = color;this.xmove = move;this.ymove = move;this.degree = degree;}public ImgWaterMark(String logoText){this();this.logoText = logoText;}public ImgWaterMark(){log.info("水印对象实例化");this.logoText = "水印测试";out = new ByteArrayOutputStream();}/*** 创建图片* @return*/public ImgWaterMark create(InputStream in,String fileType) throws Exception {log.info("开始创建水印");try {Image source = ImageIO.read(in);int srcWidth = source.getWidth(null);int srcHeight = source.getHeight(null);BufferedImage buffImg = new BufferedImage(srcWidth,srcHeight, BufferedImage.TYPE_INT_RGB);// 得到画笔对象Graphics2D g = buffImg.createGraphics();// 设置对线段的锯齿状边缘处理g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);g.drawImage(source.getScaledInstance(srcWidth, srcHeight, Image.SCALE_SMOOTH),0, 0, null);// 设置水印旋转g.rotate(Math.toRadians(degree), (double) buffImg.getWidth() / 2, (double) buffImg.getHeight() / 2);// 设置水印文字颜色g.setColor(color);// 设置水印文字Fontg.setFont(font);// 设置水印文字透明度g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));int x = -srcWidth / 2;int y = -srcHeight / 2;int markWidth = fontSize * getTextLength(logoText);// 字体长度int markHeight = fontSize;// 字体高度// 循环添加水印while (x < srcWidth * 1.5) {y = -srcHeight / 2;while (y < srcHeight * 1.5) {g.drawString(logoText, x, y);y += markHeight + ymove;}x += markWidth + xmove;}// 释放资源g.dispose();ImageIO.write(buffImg,fileType,out);log.info("创建水印结束");} catch (IOException e) {throw new IOException("文件读取失败");}return this;}/*** 获取文本长度。汉字为1:1,英文和数字为2:1*/private static int getTextLength(String text) {int length = text.length();for (int i = 0; i < text.length(); i++) {String s = String.valueOf(text.charAt(i));if (s.getBytes().length > 1) {length++;}}length = length % 2 == 0 ? length / 2 : length / 2 + 1;return length;}public byte[] getData() throws IOException {byte[] data = out.toByteArray();out.flush();out.close();return data;}public String getDataBase64() throws IOException {return Base64.getEncoder().encodeToString(getData());}}

测试代码

    public static void main(String[] args) throws Exception {FileInputStream fis = new FileInputStream("D:/imgtest/a.png");ImgWaterMark imgWaterMark = new ImgWaterMark();imgWaterMark.create(fis,"jpg");byte[] data = imgWaterMark.getData();System.out.println(data.length);FileOutputStream out = new FileOutputStream("D:/imgtest/b.png");out.write(data);out.flush();out.close();}

可以找个图片试一下哈

更多推荐

java图片水印处理

本文发布于:2024-03-09 05:40:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1724017.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:水印   图片   java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!