java透明_java透明背景 - lilei个人空间 - OSCHINA - 中文开源技术交流社区

package top.lileix.test;

import java.awt.AlphaComposite;

import java.awt.BasicStroke;

import java.awt.Color;

import java.awt.Font;

import java.awt.Graphics2D;

import java.awt.Transparency;

import java.awt.image.BufferedImage;

import java.io.IOException;

import javax.imageio.ImageIO;

import javax.servlet.ServletOutputStream;

import javax.servlet.http.HttpServletResponse;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

@Controller

@EnableAutoConfiguration

public class App {

@RequestMapping("/")

@ResponseBody

void home(HttpServletResponse response, String s) {

int width = 55;

int height = 55;

int fontHeight = 55 / s.length()-3;

String drawStr = s;

BufferedImage buffImg = new BufferedImage(55, 55, BufferedImage.TYPE_INT_RGB);

Graphics2D gd = buffImg.createGraphics();

// 设置透明 start

buffImg = gd.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);

gd = buffImg.createGraphics();

// 设置透明 end

// 画圆

gd.setColor(Color.GRAY); // 设置颜色

gd.fillOval(0, 0, 54, 54);

gd.setFont(new Font("微软雅黑", Font.PLAIN, fontHeight)); // 设置字体

gd.setColor(Color.white); // 设置颜色

gd.drawString(drawStr, width / 2 - fontHeight * drawStr.length() / 2, (fontHeight + height) / 2-2); // 输出文字(中文横向居中)

response.setHeader("Pragma", "no-cache");

response.setHeader("Cache-Control", "no-cache");

response.setDateHeader("Expires", 0);

response.setContentType("image/jpeg");

ServletOutputStream sos = null;

try {

sos = response.getOutputStream();

ImageIO.write(buffImg, "png", sos);

} catch (Exception e) {

e.printStackTrace();

} finally {

if (sos != null) {

try {

sos.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

public static void main(String[] args) throws Exception {

SpringApplication.run(App.class, args);

}

}

springboot

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值