Java绘制文字,Java:最快的文字绘制方法?

I'm trying to write a program that just creates an image out of text (e.g. write "hello" on a white square and store the image), which sounds simple but it must be done quickly. I tried the Java2D library but drawing onto a BufferedImage takes ~2 seconds to just draw the image, not even save or display it. I also tried Java-based CAPTCHA generators but they take much too long (5 seconds).

This seems like simple enough task to just draw text, but I'm frustrated that I can't do this faster than 2 seconds.

Is there a way I can do this faster on my machine by some command line options (e.g. allocating more memory or priority)? Is there a particular Java library I ought to use, or some weird quirk to Java2D I should be aware of to make things faster?

This is my entire program. I run this in Eclipse:

import java.awt.*;

import java.awt.image.BufferedImage;

public class SimpleGraphics {

public static void main(String[] args) {

long time = System.currentTimeMillis();

String message = "Hello world";

int width = 100;

int height = 100;

BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);

Graphics2D graphics = img.createGraphics();

graphics.setColor(Color.black);

graphics.setFont(new Font("TimesRoman", Font.BOLD, 12));

FontMetrics fontMetrics = graphics.getFontMetrics();

int stringWidth = fontMetrics.stringWidth(message);

int stringHeight = fontMetrics.getAscent();

graphics.drawString(message, (width - stringWidth) / 2, height / 2 + stringHeight / 4);

System.out.println(System.currentTimeMillis() - time); //consistently ~2 seconds

}

}

解决方案

I run my code from the command line (using JDK8 on Windows 7) and I get around 300ms.

I modified your code to the following:

import java.awt.*;

import java.awt.image.BufferedImage;

public class SimpleGraphics {

public static void main(String[] args) {

long time = System.currentTimeMillis();

for (int i = 0; i < 100; i++)

createImage();

System.out.println(System.currentTimeMillis() - time); //consistently ~2 seconds

}

public static void createImage()

{

String message = "Hello world";

int width = 100;

int height = 100;

BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);

Graphics2D graphics = img.createGraphics();

graphics.setColor(Color.black);

graphics.setFont(new Font("TimesRoman", Font.BOLD, 12));

FontMetrics fontMetrics = graphics.getFontMetrics();

int stringWidth = fontMetrics.stringWidth(message);

int stringHeight = fontMetrics.getAscent();

graphics.drawString(message, (width - stringWidth) / 2, height / 2 + stringHeight / 4);

}

}

I still get around 300ms.

So the problem is not with the painting code.

I don't know why you get 2 seconds, but there is obviously some overhead to loading the class. So all I can suggest is to do your image creation in batches to minimize the time.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值