后台生成水印图片前端展示背景图

需求:

后台生成一张图片:参数 工号+ip;

返回到前台作为背景水印图。

后端代码:

package com.hb.kfcenter.kfFlow.controller;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.RespectBinding;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping(value = "/watermarkImage")
public class WatermarkImageController {
	
	@RequestMapping("/creatWatermarkImage")
	@RespectBinding
	public static void method(HttpServletResponse response,String marl,String fontType) {
//		long time = System.currentTimeMillis();
		if(StringUtils.isEmpty(marl)){
			marl = "查询工号:无,IP地址:无";
		}
		if(StringUtils.isEmpty(fontType)){
			fontType = "黑体";
		}
		try {
			//图片宽高
			int wt = 350;
			int ht = 300;
			BufferedImage image = new BufferedImage(wt, ht, BufferedImage.TYPE_INT_RGB);
			// 得到画笔
			Graphics2D g = (Graphics2D) image.createGraphics();
			Font font = new Font(fontType, Font.BOLD, 18);
			// 设置背景颜色 白色
			g.setClip(0, 0, wt, ht);
			g.setColor(Color.WHITE);
			g.fillRect(0, 0, wt, ht);
			
			Rectangle clip = g.getClipBounds();
			FontMetrics fm = g.getFontMetrics();
			int ascent = fm.getAscent();
			int descent = fm.getDescent();
			int y = (clip.height - (ascent + descent)) / 2 + ascent;
			g.setFont(font);


			//旋转
			int radians = -35;
			AffineTransform affineTransform = new AffineTransform();
			affineTransform.rotate(Math.toRadians(radians), 0, 0);
			Font rotatedFont = font.deriveFont(affineTransform);
			g.setFont(rotatedFont);

			//文字模糊处理
//			g.setPaint(new Color(0, 0, 0, 64));// 阴影颜色
//			g.drawString(marl, 20, (int) (y * 1.6));// 先绘制阴影
			
			//获取文字y中心
			//添加文字
			g.setPaint(Color.BLACK);// 正文颜色
			g.drawString(marl, 20, (int) (y * 1.6));// 画出字符串
			
			//关闭画笔
			g.dispose();
//			ImageIO.write(image, "png", new File("C:/Users/admin1/Desktop/pdf/"
//					+ wt1 + "-" + ht1 + ".png"));
			response.setContentType("png");
			ImageIO.write(image, "png", response.getOutputStream());
		} catch (IOException e) {
			e.printStackTrace();
		}
		long time2 = System.currentTimeMillis();
//		System.out.println(time-time2);
	}

}

前端页面加载:


以上是完整代码:

在过程中遇到的问题。

1、图片如何旋转

2、背景如何修改颜色

3、最主要的是前端如何加载

你可能感兴趣的:(demo,工具插件类,java)