问题描述
有没有可用于从真正的字体(.ttf)中提取每个字符的java库?字体的每个字符,我想:
$ b
- 将其转换为图像
- 提取其代码(例如:Unicode值) li>
任何人都可以帮助向我展示上述目的的一些提示吗?
我想弄清楚,这样的应用程序是如何制作的:
public BufferedImage stringToBufferedImage(String s){
//首先,我们必须计算字符串的宽度和高度
BufferedImage img = new BufferedImage(1, BufferedImage.TYPE_4BYTE_ABGR);
图形g = img.getGraphics();
//设置绘制字符串时使用的字体
Font f = new Font(Tahoma,Font.PLAIN,48);
g.setFont(f);
//获取字符串可视范围
FontRenderContext frc = g.getFontMetrics()。getFontRenderContext();
Rectangle2D rect = f.getStringBounds(s,frc);
//释放资源
g.dispose();
//然后,我们必须在最后的图像上绘制字符串
//创建一个新的图像在哪里打印字符
img = new BufferedImage (int)Math.ceil(rect.getWidth()),(int)Math.ceil(rect.getHeight()),BufferedImage.TYPE_4BYTE_ABGR);
g = img.getGraphics();
g.setColor(Color.black); //否则文本将是白色
g.setFont(f);
//计算该字符串的x和y
FontMetrics fm = g.getFontMetrics();
int x = 0;
int y = fm.getAscent(); // getAscent()=基线
g.drawString(s,x,y);
//释放资源
g.dispose();
//返回图片
return img;
}
我认为没有办法得到所有的字符,创建一个字符串或者一个 char 数组,存储所有要转换为图像的字符。
使用所有键对 String 或 char [] 你想转换,你可以很容易地迭代它,并转换调用 stringToBufferedImage 方法,那么你可以做$ /
$ b $前一类=lang-java prettyprint-override> int charCode =(int)charactersMap.charAt(counter);
如果 charactersMap 是 String 或
int charCode =(int)charactersMap [计数器];
如果 charactersMap 是 char array
希望这有助于
Is there any java library that can be used to extract each character from a true type font (.ttf)?
Each character of the font, I want to:
- Convert it to the image
- Extract its code (ex: Unicode value)
Anyone can help to show me some tips on above purpose?
P.S: I want to figure out, how such this app made: http://www.softpedia.com/progScreenshots/CharMap-Screenshot-94863.html
This will convert a String to a BufferedImage:
public BufferedImage stringToBufferedImage(String s) { //First, we have to calculate the string's width and height BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_4BYTE_ABGR); Graphics g = img.getGraphics(); //Set the font to be used when drawing the string Font f = new Font("Tahoma", Font.PLAIN, 48); g.setFont(f); //Get the string visual bounds FontRenderContext frc = g.getFontMetrics().getFontRenderContext(); Rectangle2D rect = f.getStringBounds(s, frc); //Release resources g.dispose(); //Then, we have to draw the string on the final image //Create a new image where to print the character img = new BufferedImage((int) Math.ceil(rect.getWidth()), (int) Math.ceil(rect.getHeight()), BufferedImage.TYPE_4BYTE_ABGR); g = img.getGraphics(); g.setColor(Color.black); //Otherwise the text would be white g.setFont(f); //Calculate x and y for that string FontMetrics fm = g.getFontMetrics(); int x = 0; int y = fm.getAscent(); //getAscent() = baseline g.drawString(s, x, y); //Release resources g.dispose(); //Return the image return img; }
I think there isn't a way to get all the characters, you have to create a String or a char array where you store all the chars you want to convert to image.
Once you have the String or char[] with all keys you want to convert, you can easily iterate over it and convert call the stringToBufferedImage method, then you can do
int charCode = (int) charactersMap.charAt(counter);
if charactersMap is a String, or
int charCode = (int) charactersMap[counter];
if charactersMap is a char array
Hope this helps
这篇关于Java解析truetype字体提取每个字符作为图像&它的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!