java生成一张图片
2024-08-31 03:59:24
public class CreateImage {
public static void main(String[] args) throws Exception{
int width = 100;
int height = 100;
String s = "你好"; File file = new File("/Users/tengxin/Pictures/image.jpg"); Font font = new Font("Serif", Font.BOLD, 10);
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D)bi.getGraphics();
g2.setBackground(Color.WHITE);
g2.clearRect(0, 0, width, height);
g2.setPaint(Color.RED); FontRenderContext context = g2.getFontRenderContext();
Rectangle2D bounds = font.getStringBounds(s, context);
double x = (width - bounds.getWidth()) / 2;
double y = (height - bounds.getHeight()) / 2;
double ascent = -bounds.getY();
double baseY = y + ascent; g2.drawString(s, (int)x, (int)baseY); ImageIO.write(bi, "jpg", file);
}
}
最新文章
- centos,nginx安装备忘
- JDBC中的PreparedStatement
- docker进入容器方法
- Centos yum install
- 使用(Drawable)资源——图片资源
- 浅谈数据结构vector
- 微信跳转ticket值怎么得到?浏览器跳到微信?哪里有微信跳转接口?跳转功能能用多久?
- Ubuntu下创建XFS文件系统的LVM
- Hadoop开启后jps显示只有jps
- java中LIst转换成Json
- 洛谷P5162 WD与积木 [DP,NTT]
- idea 自动换行
- MYSQLi数据访问查询数据
- Hash 迭代程序构造器要求字符串参数--错误解决
- Codeforces Beta Round #67 (Div. 2)
- 使用spring单元调试出错initializationError
- 测试word发表博客
- java-mybaits-011-mybatis-拦截器计算耗时
- 【转】python基础-编码与解码
- 2018年UI设计趋势概览