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

 我来答
iamKweny
2013-05-15 · 超过13用户采纳过TA的回答
知道答主
回答量:25
采纳率:0%
帮助的人:37.1万
展开全部

执行成功后会在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
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式