2015-11-26 57 views
0

我试图编写一个程序,只是创建一个文本的图像(例如在白色方块上写“你好”并存储图像),这听起来很简单,但它必须快速完成。我尝试了Java2D库,但是绘制到BufferedImage需要2秒钟来绘制图像,甚至不保存或显示它。我也尝试了基于Java的CAPTCHA生成器,但它们需要很长的时间(5秒)。Java:绘制文本的最快方法?

这似乎是简单的任务,只是绘制文本,但我很沮丧,我不能做到这一点比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 
    } 
} 
+2

它不需要2秒做任何基本的图形绘画或文件I/O。发布你的[SSCCE](http://sscce.org/)来证明问题,以便我们确切地看到你在做什么。我没有看到你发布的基本代码有什么问题,但是你如何使用代码的上下文很重要。 – camickr

+0

这段代码实际上是我主要方法中的一切。我在Eclipse中运行这个。 – Booley

+1

好吧,然后将所有代码复制到论坛中。例如,你如何计算“2秒”?如果我们无法运行代码,我们不知道问题是您的代码还是您的机器。 2秒钟可以加载JVM而不是实际的绘画时间。 – camickr

回答

2

我(在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); 
    } 
} 

我还是避开为300ms。

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

我不知道你为什么得到2秒,但显然有一些开销加载类。所以我可以建议的是分批进行图像创建,以缩短时间。

+0

我注意到,当我批量运行时,第一个图像需要2s才能加载,但其他所有内容都<10ms;很高兴知道类加载是问题而不是代码本身,但是批量执行仍然不切实际。我的java /机器有问题吗? – Booley

+0

不知道它是你的机器的IDE,平台还是Java版本。 – camickr

+0

我可以问你正在使用什么IDE /版本? (IDE的版本,就是这样) – Booley

0

单独定时每条线给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 
} 
} 
相关问题