JAVA IO流中,能否将一个字符串以图片的格式输出出来呢,即字符串显示在图片上
2个回答
展开全部
执行成功后会在D盘根目录生成一张名为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);
}
}
2013-05-14
展开全部
那是图片的流吧
是想在图片是添加文字?
BufferedImage
是想在图片是添加文字?
BufferedImage
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询