如何使用Java2D创建硬件加速的映像? [英] How can I create a hardware-accelerated image with Java2D?

查看:175
本文介绍了如何使用Java2D创建硬件加速的映像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个快速图像生成器,它可以完成大量2D变换和形状渲染,所以我试图使用BufferedImage,然后获取Graphics2D对象来执行我的所有绘制。我现在主要关心的是如何快速创建一个BufferedImage,像这样:

();
GraphicsConfiguration gc = ge.getDefaultScreenDevice()。getDefaultConfiguration();
BufferedImage bImage = gc.createCompatibleImage(width,height,Transparency.TRANSLUCENT);
Graphics2D graphics = bImage.createGraphics();

然而,如果我这样做:

 的System.out.println(bImage.getCapabilities(GC).isAccelerated()); 

输出总是 false ,即使我用 - Dsun.java2d.opengl = True打印该行:

 为屏幕上的默认配置启用OpenGL管道0 

我正在做一个BufferedImage,因为最后我想用ImageIO.write将它保存到一个PNG文件(bImage, PNG,file);



我可以创建一个VolatileImage,它会说它是加速的,但ImageIO在尝试保存时不喜欢它,不能投射到RenderedImage。有关如何获得可以存储到磁盘的加速映像的任何想法?此外,我不想创建一个VolatileImage和复制到BufferedImage来保存,因为我的图像真的很大,我会遇到内存不足的问题...

解决方案

从我的调查和阅读Sun / Oracle 2D教程中,我发现:

创建一个加速图像可以使用创建为pbuffer的易失性图像。有优点和缺点,在您尝试获取栅格像素数据之前,VolatileImages速度很快。在那一刻,他们成为一个正常的缓冲图像。它们也是不稳定的,不能保证它在渲染结束时可用。要执行此操作,请执行:

  GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
GraphicsConfiguration gc = ge.getDefaultScreenDevice()。getDefaultConfiguration();
VolatileImage vImage = gc.createCompatibleVolatileImage(width,height,Transparency.TRANSLUCENT);

这将为您提供加速图像,但无法直接使用ImageIO直接写入说一个PNG文件。

为了使用一个正常的BufferedImage,你需要做类似的事情:

  GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
GraphicsConfiguration gc = ge.getDefaultScreenDevice()。getDefaultConfiguration();
BufferedImage img = gc.createCompatibleImage(width,height,Transparency.TRANSLUCENT);
img.setAccelerationPriority(1);

这会创建一个没有加速的BufferedImage,但是您应该通过传递给JVM 1在setAccelerationPriority中。您可以阅读 Java教程的2D路径< a>。


I'm trying to create a fast image generator that does lots of 2d transformations and shape rendering, so I'm trying to use a BufferedImage and then acquire the Graphics2D object to perform all my drawing. My main concern now is to make is really fast so I'm creating a BufferedImage like this:

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsConfiguration gc = ge.getDefaultScreenDevice().getDefaultConfiguration();
BufferedImage bImage = gc.createCompatibleImage(width, height, Transparency.TRANSLUCENT);
Graphics2D graphics = bImage.createGraphics();

However if I do:

System.out.println(bImage.getCapabilities(gc).isAccelerated());

The output is always false, even if I start the JVM with -Dsun.java2d.opengl=True which prints the line:

OpenGL pipeline enabled for default config on screen 0

I'm doing a BufferedImage because in the end I want to save it to a PNG file with ImageIO.write(bImage, "PNG", file);

I can create a VolatileImage which will say that it is accelerated but ImageIO doesn't like it when trying to save, saying that that image cannot be casted to RenderedImage. Any ideas on how to get an accelerated image that can be stored to disk? Also I don't want to create a VolatileImage and the copy to a BufferedImage to save since my images are really big and I'll run into out of memory issues...

解决方案

From my investigation and reading the Sun/Oracle 2D tutorial this is what I've found:

To create an accelerated image one can either use the volatile image, which is created as a pbuffer. There are advantages and disadvantages, VolatileImages are fast until you try to get the raster pixel data. At that moment they become a normal bufferedImage. Also they are volatile and there is no guarantee that it will be available in the end of your rendering. To do this you execute:

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsConfiguration gc = ge.getDefaultScreenDevice().getDefaultConfiguration();
VolatileImage vImage = gc.createCompatibleVolatileImage(width, height, Transparency.TRANSLUCENT);

This will get you a accelerated image, however it is not possible to use the ImageIO to write it directly to say a PNG file.

In order to use a normal BufferedImage you need to do something like:

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsConfiguration gc = ge.getDefaultScreenDevice().getDefaultConfiguration();
BufferedImage img = gc.createCompatibleImage(width, height, Transparency.TRANSLUCENT);
img.setAccelerationPriority(1);

This creates a BufferedImage which is not accelerated but you give the hint to the JVM that is should by passing 1 in setAccelerationPriority. This you can read on the 2D trail of the Java Tutorial.

这篇关于如何使用Java2D创建硬件加速的映像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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