代码改变世界

图片添加文字水印

2018-01-26 15:24  如是我所闻  阅读(342)  评论(0编辑  收藏  举报

测试代码

  note:如果linux环境下中文乱码,参考这篇文章:http://blog.csdn.net/itdragons/article/details/72625041

byte[] markImageByte=ImageRemarkUtil.pressText("水印文字", imgByte, "宋体", "#CD6600", 22, 0.1f, true);

 

工具类

package com.company.wx.util;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.font.GlyphVector;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;

import javax.imageio.ImageIO;

import org.apache.log4j.Logger;

/**
 * @ClassName: ImageRemarkUtil
 * @Description: TODO(图片水印工具类)
 * @author chenzhangwei
 * @date 2017年12月11日下午3:32:16
 */
public class ImageRemarkUtil {
    static Logger log=Logger.getLogger(ImageRemarkUtil.class);
    /**
      * 斜水印,重复水印,文字
      * @param pressText  文字
      * @param targetImg  目标图片字节数组
      * @param fontName 字体名称
      * @param colorStr 字体颜色字符串,格式如:#29944f
      * @param fontSize  字体大小
      * @param alpha 透明度(0.1-0.9)
      * @param carelessness true为字体实心,false为字体空心
      * @return
      */
     public static byte[] pressText(String pressText, byte[] fileByte, String fontName, String colorStr, int fontSize, float alpha,boolean carelessness) {
      ByteArrayOutputStream bos=new ByteArrayOutputStream();     
      try {
       Image src = ImageIO.read(new ByteArrayInputStream(fileByte));
       
       //图片宽度
       int width = src.getWidth(null);
       //图片高度
       int height = src.getHeight(null);
       
       BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
       
       Graphics2D g2d  = image.createGraphics();
       //绘原图
       g2d.drawImage(src, 0, 0, width, height, null);
       //比例
       g2d.scale(1, 1);
       
       g2d.addRenderingHints(new RenderingHints(
         RenderingHints.KEY_ANTIALIASING,
         RenderingHints.VALUE_ANTIALIAS_ON));
       g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
         RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
       
       
       //颜色
       Color color = Color.decode(colorStr);
       
       //字体
       Font font = new Font(fontName, Font.PLAIN, fontSize);
       
       
       GlyphVector fontGV = font.createGlyphVector(g2d.getFontRenderContext(),
         pressText);
       Rectangle size = fontGV
         .getPixelBounds(g2d.getFontRenderContext(), 0, 0);
       Shape textShape = fontGV.getOutline();
       double textWidth = size.getWidth();
       double textHeight = size.getHeight();
       AffineTransform rotate45 = AffineTransform
         .getRotateInstance(Math.PI / 4d);
       Shape rotatedText = rotate45.createTransformedShape(textShape);
       g2d.setPaint(new GradientPaint(0, 0, color,
       image.getWidth() / 2, image.getHeight() / 2,color));
       
       //透明度
       g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
       
       
       double yStep = Math.sqrt(textWidth * textWidth / 2) + 5;
       for (double x = -textHeight * 3; x < image.getWidth(); x += (textHeight*5)) {
        double y = -yStep;
        for (; y < image.getHeight(); y += (yStep*1.5)) {
         g2d.draw(rotatedText);
         if(carelessness)//字体实心
         {
          g2d.fill(rotatedText);
         }
         g2d.translate(0, yStep*1.5);
        }
        g2d.translate(textHeight * 5, -(y + yStep));
       }
       
       g2d.dispose();
       ImageIO.write(image, "jpg", bos);
       return bos.toByteArray();
      } catch (Exception e) {
          log.error("水印添加抛出异常:"+e);
      }
      return null;
     }

}


package com.company.wx.util;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.font.GlyphVector;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;

import javax.imageio.ImageIO;

import org.apache.log4j.Logger;

/**
 * @ClassName: ImageRemarkUtil
 * @Description: TODO(图片水印工具类)
 * @author chenzhangwei
 * @date 2017年12月11日下午3:32:16
 */
public class ImageRemarkUtil {
    static Logger log=Logger.getLogger(ImageRemarkUtil.class);
    /**
      * 斜水印,重复水印,文字
      * @param pressText  文字
      * @param targetImg  目标图片
      * @param fontName 字体名称
      * @param colorStr 字体颜色字符串,格式如:#29944f
      * @param fontSize  字体大小
      * @param alpha 透明度(0.1-0.9)
      * @param carelessness true为字体实心,false为字体空心
      * @return
      */
     public static byte[] pressText(String pressText, byte[] fileByte, String fontName, String colorStr, int fontSize, float alpha,boolean carelessness) {
      ByteArrayOutputStream bos=new ByteArrayOutputStream();    
      try {
       Image src = ImageIO.read(new ByteArrayInputStream(fileByte));
       
       //图片宽度
       int width = src.getWidth(null);
       //图片高度
       int height = src.getHeight(null);
       
       BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
       
       Graphics2D g2d  = image.createGraphics();
       //绘原图
       g2d.drawImage(src, 0, 0, width, height, null);
       //比例
       g2d.scale(1, 1);
       
       g2d.addRenderingHints(new RenderingHints(
         RenderingHints.KEY_ANTIALIASING,
         RenderingHints.VALUE_ANTIALIAS_ON));
       g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
         RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
       
       
       //颜色
       Color color = Color.decode(colorStr);
       
       //字体
       Font font = new Font(fontName, Font.PLAIN, fontSize);
       
       
       GlyphVector fontGV = font.createGlyphVector(g2d.getFontRenderContext(),
         pressText);
       Rectangle size = fontGV
         .getPixelBounds(g2d.getFontRenderContext(), 0, 0);
       Shape textShape = fontGV.getOutline();
       double textWidth = size.getWidth();
       double textHeight = size.getHeight();
       AffineTransform rotate45 = AffineTransform
         .getRotateInstance(Math.PI / 4d);
       Shape rotatedText = rotate45.createTransformedShape(textShape);
       g2d.setPaint(new GradientPaint(0, 0, color,
       image.getWidth() / 2, image.getHeight() / 2,color));
       
       //透明度
       g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
       
       
       double yStep = Math.sqrt(textWidth * textWidth / 2) + 5;
       for (double x = -textHeight * 3; x < image.getWidth(); x += (textHeight*5)) {
        double y = -yStep;
        for (; y < image.getHeight(); y += (yStep*1.5)) {
         g2d.draw(rotatedText);
         if(carelessness)//字体实心
         {
          g2d.fill(rotatedText);
         }
         g2d.translate(0, yStep*1.5);
        }
        g2d.translate(textHeight * 5, -(y + yStep));
       }
       
       g2d.dispose();
       ImageIO.write(image, "jpg", bos);
       return bos.toByteArray();
      } catch (Exception e) {
          log.error("水印添加抛出异常:"+e);
      }
      return null;
     }

}