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.p_w_picpath.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.p_w_picpathio.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 p_w_picpath = new BufferedImage(imgSize, imgSize,
081
BufferedImage.TYPE_4BYTE_ABGR_PRE);
082
083
Graphics g=p_w_picpath.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 = p_w_picpath.getWidth() / 2 - stringWidth / 2;
094
int y = p_w_picpath.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(p_w_picpath, "png", fos);
102
fos.close();
103
} catch (Exception e) {
104
e.printStackTrace();
105
}
106
107
}
108
109
}
java awt实现 fontawesome转png
精选 转载上一篇:java导出csv 代码
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
VUE中实现文字转拼音
在vue2中,实现将文字转为拼音的使用案例
ruby标签 文字转拼音 拼音 vue转拼音