`
mizhao1984
  • 浏览: 88163 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

linux BufferedImage 生成图片中文乱码

阅读更多

 1 package org.activiti.examples.processdefinitions;
 2 
 3 import java.awt.Font;
 4 import java.awt.FontMetrics;
 5 import java.awt.Graphics2D;
 6 import java.awt.image.BufferedImage;
 7 import java.io.File;
 8 import java.io.IOException;
 9 
10 import javax.imageio.ImageIO;
11 
12 public class Test {
13 
14     /**
15      * @param args
16      */
17     public static void main(String[] args) {
18         int width = 100;
19         int height = 100;
20         String text = "爱我中华";
21         int x = 0;
22         int y = 0;
23         BufferedImage processDiagram = new BufferedImage(100100,
24                 BufferedImage.TYPE_INT_ARGB);
25         Graphics2D g = (Graphics2D) processDiagram.createGraphics();
26         Font font = new Font("宋体", Font.BOLD, 12);
27         g.setFont(font);
28         FontMetrics fontMetrics = g.getFontMetrics();
29         int textX = x + ((width - fontMetrics.stringWidth(text)) / 2);
30         int textY = y + ((height - fontMetrics.getHeight()) / 2)
31                 + fontMetrics.getHeight();
32         g.drawString(text, textX, textY);
33         File outFile = new File("d:/newfile.png");
34         try {
35             ImageIO.write(processDiagram, "png", outFile);
36         } catch (IOException e) {
37             // TODO Auto-generated catch block
38             e.printStackTrace();
39         }// 写图片
40     }
41 
42 }
如果要部署到linux环境的话,需要将相关的字体文件传到服务器的jdk下即可。

我的本地字体文件地址是“C:\WINDOWS\Fonts\simsun.ttc”,
上传到服务器地址是:“/usr/jdk/instances/jdk1.5.0/jre/lib/fonts/simsun.ttc”
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics