Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Fastest way to draw text?

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
    }
}
like image 621
Booley Avatar asked Jun 30 '26 15:06

Booley


2 Answers

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.

like image 102
camickr Avatar answered Jul 03 '26 05:07

camickr


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
}
}
like image 27
gpasch Avatar answered Jul 03 '26 04:07

gpasch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!