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流中,能否将一个字符串以图片的格式输出出来呢,即字符串显示在图片上...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: eclipse把tomcant用到一个项
- 下一篇: 卸载源码安装mysql_CentOS7下