Java: Fastest way to draw text?

廉价感情. 提交于 2019-12-24 18:39:55

问题


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
    }
}

回答1:


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.




回答2:


Timing each line individually gives a big jump at fontMetrics - so there is your culprit. I dont know if other measuring classes (lineMetrics) will give you shorter time

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);
    System.out.println(System.currentTimeMillis() - time); //consistently ~2 seconds

    Graphics2D graphics = img.createGraphics();
    graphics.setColor(Color.black);
    graphics.setFont(new Font("TimesRoman", Font.BOLD, 12));
    System.out.println("1 "+(System.currentTimeMillis() - time)); //consistently ~2 seconds

    FontMetrics fontMetrics = graphics.getFontMetrics();
    System.out.println("2 "+(System.currentTimeMillis() - time)); //consistently ~2 seconds
    int stringWidth = fontMetrics.stringWidth(message);
    System.out.println("3 "+(System.currentTimeMillis() - time)); //consistently ~2 seconds
    int stringHeight = fontMetrics.getAscent();
    System.out.println(System.currentTimeMillis() - time); //consistently ~2 seconds

    graphics.drawString(message, (width - stringWidth) / 2, height / 2 + stringHeight / 4);
    System.out.println(System.currentTimeMillis() - time); //consistently ~2 seconds
}
}


来源:https://stackoverflow.com/questions/33943574/java-fastest-way-to-draw-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!