Chinaunix首页 | 论坛 | 博客
  • 博客访问: 18201
  • 博文数量: 14
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 145
  • 用 户 组: 普通用户
  • 注册时间: 2014-06-04 16:01
文章分类
文章存档

2014年(14)

我的朋友

分类: Java

2014-06-05 17:55:37

java awt实现 fontawesome转png   


001
package net.yunstudio.demo;
002

003
import java.awt.Color;
004
import java.awt.Font;
005
import java.awt.FontMetrics;
006
import java.awt.Graphics;
007
import java.awt.image.BufferedImage;
008
import java.io.BufferedReader;
009
import java.io.File;
010
import java.io.FileInputStream;
011
import java.io.FileOutputStream;
012
import java.io.IOException;
013
import java.io.InputStreamReader;
014

015
import javax.imageio.ImageIO;
016

017
public class Font2png {
018
    public static Font loadFont(String fontFileName, float fontSize) // 第一个参数是外部字体名,第二个是字体大小
019
    {
020
        try {
021
            File file = new File(fontFileName);
022
            if(!file.exists()){
023
                System.out.println("找不到字体文件");
024
            }
025
             
026
            FileInputStream aixing = new FileInputStream(file);
027
            Font dynamicFont = Font.createFont(Font.TRUETYPE_FONT, aixing);
028
            Font dynamicFontPt = dynamicFont.deriveFont(fontSize);
029
            aixing.close();
030
            return dynamicFontPt;
031
        } catch (Exception e)// 异常处理
032
        {
033
            e.printStackTrace();
034
            return new java.awt.Font("宋体", Font.PLAIN, (int)fontSize);
035
        }
036
    }
037

038
    public static void main(String[] args) {
039
        float fontSize=24;
040
        int color=0xff000000;
041
        String out=new File("").getAbsolutePath();
042
        String fontPath=out+"/fontawesome-webfont.ttf";
043
        System.out.println(fontPath);
044
        String name="icon.png";
045
        String text="\uF02c";
046
        int paddind=2;
047
        InputStreamReader isr=new InputStreamReader(System.in);
048
        BufferedReader br=new BufferedReader(isr);
049
        try {
050
            args=br.readLine().split("\\s+");
051
        } catch (IOException e1) {
052
            e1.printStackTrace();
053
        }
054
         
055
        if(args!=null){
056
            for (int i = 0; i < args.length; i+=2) {
057
                if(i==args.length-1){
058
                    continue;
059
                }
060
                String key=args;
061
                String value=args[i+1];
062
                if("--name".equals(key)){
063
                    name=value;
064
                }else if("--text".equals(key)){
065
                    text=value;
066
                }if("--padding".equals(key)){
067
                    paddind=Integer.parseInt(value);
068
                }else if("--fontpath".equals(key)){
069
                    fontPath=value;
070
                }else if ("--out".equals(key)) {
071
                    out=value;
072
                }else if("--size".equals(key)){
073
                    fontSize=Float.valueOf(value);
074
                }else if ("--color".equals(key)) {
075
                    color=Integer.decode(value);
076
                }
077
            }
078
        }
079
        int imgSize=(int) (paddind*2+fontSize);
080
        BufferedImage image = new BufferedImage(imgSize, imgSize,
081
                BufferedImage.TYPE_4BYTE_ABGR_PRE);
082
         
083
        Graphics g=image.getGraphics();
084
         
085
        Font font=loadFont(fontPath, fontSize);
086
        g.setFont(font);
087
        g.setColor(new Color(color,true));
088

089
        FontMetrics fm = g.getFontMetrics();
090
        int stringWidth = fm.stringWidth(text);
091
        int stringAscent = fm.getAscent();
092
        int stringDescent = fm.getDescent ();
093
        int x = image.getWidth() / 2 - stringWidth / 2;
094
        int y = image.getHeight() / 2 + (stringAscent - stringDescent) / 2;
095
         
096
        g.drawString(text, x, y);
097
         
098
        FileOutputStream fos=null;
099
        try {
100
            fos=new FileOutputStream(out+'/'+name);
101
            ImageIO.write(image, "png", fos);
102
            fos.close();
103
        } catch (Exception e) {
104
            e.printStackTrace();
105
        }
106

107
    }
108

109
}
阅读(389) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~