1. 程式人生 > >用Java實現給圖片新增文字水印-原始碼分享

用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<String, String> watermarks(@RequestBody TileWatermark tileWatermark) throws Exception {
			
		
		Map<String, String> watermark = new ConcurrentHashMap<String,String>();
		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;
	}
	
}