java awt實現 fontawesome轉png

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
        {http://www.huiyi8.com/gundongdaima/zuoyou/
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
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章