font awesome java_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

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值