簡體   English   中英

Java:最快的文字繪制方法?

[英]Java: Fastest way to draw text?

我正在嘗試編寫一個僅用文本創建圖像的程序(例如,在白色正方形上寫“ hello”並存儲圖像),這聽起來很簡單,但是必須盡快完成。 我嘗試了Java2D庫,但是繪制到BufferedImage上只需要2秒鍾即可繪制圖像,甚至不保存或顯示圖像。 我還嘗試了基於Java的CAPTCHA生成器,但是它們花費的時間太長(5秒)。

這看起來很簡單,只需要繪制文本即可,但是令我沮喪的是我不能快於2秒。

有什么辦法可以通過某些命令行選項(例如分配更多的內存或優先級)在我的計算機上更快地完成此操作? 有沒有我應該使用的特定Java庫,或者我應該意識到對Java2D的一些怪異的怪癖才能使事情變得更快?

這是我的整個程序。 我在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
    }
}

我從命令行運行代碼(在Windows 7上使用JDK8),大約需要300毫秒。

我將您的代碼修改為以下內容:

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

我仍然有300毫秒左右的時間。

因此,問題不在於繪畫代碼。

我不知道為什么會花2秒,但是加載類顯然有一些開銷。 因此,我只能建議分批創建映像,以最大程度地減少時間。

對每一行單獨計時會極大地提高fontMetrics的效率-因此有罪魁禍首。 我不知道其他測量類別(lineMetrics)是否會縮短您的時間

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM