用Java实现给图片添加文字水印-源码分享

直接上源码:

单张瓦片添加单个文字水印、可设置水印文字内容,字体,颜色,旋转角度,透明度

controller层


import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.geovis.domain.DataObject;
import com.geovis.domain.TileWatermark;
import com.geovis.service.TileService;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.annotation.Resource;
import javax.imageio.ImageIO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


@RestController
@Api(description= "瓦片")
@RequestMapping("/api/v1")
public class TileController {
	
	private final Logger logger = LoggerFactory.getLogger(TileController.class);
	
	@Resource
	private TileService tileService;
	/**
	 * 单张瓦片添加单个文字水印、可设置水印文字内容,字体,颜色,旋转角度,透明度
	 * @param tileWatermark
	 * @return
	 * @throws Exception
	 */
	//@HystrixCommand
	@ApiOperation(value = "单张瓦片添加单个文字水印",notes = "可设置水印文字内容,位置,字体,颜色,旋转角度,透明度")
	@GetMapping(value = "/watermarks")
	public Map watermarks(@RequestBody TileWatermark tileWatermark) throws Exception {
			
		
		Map watermark = new ConcurrentHashMap();
		String resultInfo = tileService.watermarkBySingleText(tileWatermark);
		watermark.put("resultInfo", resultInfo);
		return watermark;
	}
}

实体类

import java.awt.Color;
import java.io.Serializable;

import io.swagger.annotations.ApiParam;

public class TileWatermark implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 8870574699118240793L;

	//需要加水印的瓦片路径
	private String tilePath;
	//添加水印后瓦片输出路径
	private String tileOutputPath;
	//输出瓦片名称
	private String tileOutputName;
	//输出瓦片类型
	private String tileOutputType;
	//水印文字内容
	private String watermarkContent;
	//水印文字位置
	private String watermarkPosition;
	//水印文字字体类型
	private String watermarkFontType;
	//水印文字字体风格
	private String watermarkFontStyle;
	//水印文字字体大小
	private Integer watermarkFontSize;
	//水印文字颜色
	private Color watermarkColor;
	//水印文字旋转角度,null表示不旋转
	private Integer watermarkDegree;
	//水印文字透明度
	private float watermarkAlpha;
	
	public String getTilePath() {
		return tilePath;
	}
	public void setTilePath(String tilePath) {
		this.tilePath = tilePath;
	}
	public String getTileOutputPath() {
		return tileOutputPath;
	}
	public void setTileOutputPath(String tileOutputPath) {
		this.tileOutputPath = tileOutputPath;
	}
	public String getTileOutputName() {
		return tileOutputName;
	}
	public void setTileOutputName(String tileOutputName) {
		this.tileOutputName = tileOutputName;
	}
	public String getTileOutputType() {
		return tileOutputType;
	}
	public void setTileOutputType(String tileOutputType) {
		this.tileOutputType = tileOutputType;
	}
	public String getWatermarkContent() {
		return watermarkContent;
	}
	public void setWatermarkContent(String watermarkContent) {
		this.watermarkContent = watermarkContent;
	}
	public String getWatermarkPosition() {
		return watermarkPosition;
	}
	public void setWatermarkPosition(String watermarkPosition) {
		this.watermarkPosition = watermarkPosition;
	}
	public String getWatermarkFontType() {
		return watermarkFontType;
	}
	public void setWatermarkFontType(String watermarkFontType) {
		this.watermarkFontType = watermarkFontType;
	}
	public String getWatermarkFontStyle() {
		return watermarkFontStyle;
	}
	public void setWatermarkFontStyle(String watermarkFontStyle) {
		this.watermarkFontStyle = watermarkFontStyle;
	}
	public Integer getWatermarkFontSize() {
		return watermarkFontSize;
	}
	public void setWatermarkFontSize(Integer watermarkFontSize) {
		this.watermarkFontSize = watermarkFontSize;
	}
	public Color getWatermarkColor() {
		return watermarkColor;
	}
	public void setWatermarkColor(Color watermarkColor) {
		this.watermarkColor = watermarkColor;
	}
	public Integer getWatermarkDegree() {
		return watermarkDegree;
	}
	public void setWatermarkDegree(Integer watermarkDegree) {
		this.watermarkDegree = watermarkDegree;
	}
	public float getWatermarkAlpha() {
		return watermarkAlpha;
	}
	public void setWatermarkAlpha(float watermarkAlpha) {
		this.watermarkAlpha = watermarkAlpha;
	}
	
	
}

service


import java.awt.Color;

import com.geovis.domain.TileFuse;
import com.geovis.domain.TileWatermark;


public interface TileService {

	String watermarkBySingleText(TileWatermark tileWatermark);
	
		  
}

service实现层

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.springframework.stereotype.Service;

import com.geovis.domain.TileFuse;
import com.geovis.domain.TileWatermark;
import com.geovis.service.TileService;


@Service
public class TileServiceImpl implements TileService {

	@Override
	public String watermarkBySingleText(TileWatermark tileWatermark) {
		
		Color watermarkColor = tileWatermark.getWatermarkColor();
		Integer watermarkDegree = tileWatermark.getWatermarkDegree();
		float watermarkAlpha = tileWatermark.getWatermarkAlpha();
		String watermarkContent = tileWatermark.getWatermarkContent();
		String tileOutputPath = tileWatermark.getTileOutputPath();
		String tileOutputName = tileWatermark.getTileOutputName();
		String tileOutputType = tileWatermark.getTileOutputType();
		String resultinfo = "瓦片添加单个水印出錯";
		
		try {
			// 读取原瓦片信息
			File file = new File(tileWatermark.getTilePath());
			if (!file.isFile()) {
				return file + " 不是一个瓦片文件!";
			}
			Image tile = ImageIO.read(file);
			int width = tile.getWidth(null);
			int height = tile.getHeight(null);
			
			// 加水印
			BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
			
			// 得到画笔对象
			Graphics2D g = bufferedImage.createGraphics();
			g.drawImage(tile, 0, 0, width, height, null);
			// 设置对线段的锯齿状边缘处理
			// g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);

			// g.drawImage(tile.getScaledInstance(tile.getWidth(null),tile.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null);
			
			// 设置水印字体样式
			Font font = new Font("微软雅黑", Font.PLAIN, 50);
			g.setFont(font);
			
			// 根据图片的背景设置水印颜色
			g.setColor(watermarkColor);
			// 设置水印旋转角度
			if (null != watermarkDegree) {
				// 设置水印旋转
				g.rotate(Math.toRadians(watermarkDegree), (double) bufferedImage.getWidth() / 2, (double) bufferedImage.getHeight() / 2);
			}

			// 设置水印透明度
			g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, watermarkAlpha));

			// 水印位置
			int x = width / 2;
			int y = height / 2;
			g.drawString(watermarkContent, x, y);

			g.dispose();
			// 输出图片
			File sf = new File(tileOutputPath, tileOutputName + "." + tileOutputType);
			ImageIO.write(bufferedImage, tileOutputType, sf); // 保存图片
			resultinfo = "瓦片添加单个水印成功";
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return resultinfo;
	}
	
}

 

你可能感兴趣的:(java)