首页 > 代码库 > java生成一张图片
java生成一张图片
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); } }
java生成一张图片
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。