Java:最快的文字绘制方法? [英] Java: Fastest way to draw text?

查看:80
本文介绍了Java:最快的文字绘制方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个仅用文本创建图像的程序(例如,在白色正方形上写 hello并存储图像),这听起来很简单,但是必须尽快完成。我尝试了Java2D库,但是绘制到BufferedImage上只需要2秒钟即可绘制图像,甚至不保存或显示图像。我也尝试过基于Java的CAPTCHA生成器,但是它们花费的时间太长(5秒)。



这看起来很简单,只能绘制文本,但是我很沮丧

是否可以通过某些命令行选项(例如分配更多的内存或优先)?是我应该使用一个特定的Java库,还是我应该知道的一些怪异的Java2D库,以使处理速度更快?



这是我的整个程序。我在Eclipse中运行它:

  import java.awt。*; 
import java.awt.image.BufferedImage;

公共类SimpleGraphics {
public static void main(String [] args){
long time = System.currentTimeMillis();

字符串消息= Hello world;
int width = 100;
int高度= 100;
BufferedImage img =新的BufferedImage(宽度,高度,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()-时间); //一致地〜2秒
}
}


解决方案

我从命令行运行我的代码(在Windows 7上使用JDK8),我大约用了300毫秒。



我将代码修改为以下内容:

  import java.awt。*; 
import java.awt.image.BufferedImage;

公共类SimpleGraphics {
public static void main(String [] args){
long time = System.currentTimeMillis();

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

System.out.println(System.currentTimeMillis()-时间); //一致地〜2秒
}

public static void createImage()
{
String message = Hello world;
int width = 100;
int高度= 100;
BufferedImage img =新的BufferedImage(宽度,高度,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);
}
}

我仍然可以保持300ms左右。



所以问题不在于绘画代码。



我不知道为什么会得到2秒,但显然加载类需要一些开销。因此,我只能建议分批创建图像,以减少时间。


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.

这篇关于Java:最快的文字绘制方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆