欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

java将字体输出成图片格式_JAVA IO流中,能否将一个字符串以图片的格式输出出来呢,即字符串显示在图片上...

发布时间:2024/9/27 47 豆豆
生活随笔 收集整理的这篇文章主要介绍了 java将字体输出成图片格式_JAVA IO流中,能否将一个字符串以图片的格式输出出来呢,即字符串显示在图片上... 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

展开全部

执行成功后会在D盘根目录生成32313133353236313431303231363533e59b9ee7ad9431333332616433一张名为image的jpg格式的图片,图片上以红色Serif体写着“你好”两个字——

import java.awt.Color;

import java.awt.Font;

import java.awt.Graphics2D;

import java.awt.font.FontRenderContext;

import java.awt.geom.Rectangle2D;

import java.awt.image.BufferedImage;

import java.io.File;

import javax.imageio.ImageIO;

public class CreateImage {

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

int width = 100;

int height = 100;

String s = "你好";

File file = new File("d:/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将字体输出成图片格式_JAVA IO流中,能否将一个字符串以图片的格式输出出来呢,即字符串显示在图片上...的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。