文档章节

Tips for Fast 2D Image Processing in Java

刘少
 刘少
发布于 2016/04/11 21:20
字数 738
阅读 33
收藏 1

Swing and AWT

AWT is a heavyweight windowing API. Each control allocates it's own memory for rendering.
Swing is lightweight. Every component is a JWindow shares the same video buffer for
drawing.
Swing is double buffered. When you repaint a component, the drawings are written to the back
buffer of the root window. When the window has finished rendering, the back buffer is
swapped to the front. This provides fast, flicker free drawing with a small memory footprint.
Some APIs need to be heavyweight and have their own video memory. JOGL and Java3D
Canvases are examples of such components.

Tips for Faster Rendering

java.awt.RenderingHints allow the user to specify trade-offs between nice looking and fast
rendering. Turning off anti-aliasing or choosing a less expensive interpolation method can
speed things up.
Keep it simple. Don't draw elements that will be completely obscured by other elements. Don't
draw elements that will be offsreen. Don't use use curved shapes when rectangles will do.
Use clip areas to shrink the area of a component that needs rendering.
If using java.awt.geom.Area, convert to a GeneralPath before rendering or using as a clip area.
javax.swing.CellRendererPane can let you have Swing do most of the work for you. This is a
great way to draw JComponents anywhere onscreen during the paintComponent() call. Useful
for designing your own custom controls that display formatted data. This is the trick JTables
and JLists use to draw their content.

Animation

Animation is not much different from drawing regularly.
Each animated element should be described by a set of parameters. These parameters can be
updated over time. When an element's parameters change, it's appearance will change too.
Cache as much as possible. It's much faster to draw something to an offscreen buffer once and
then blit it back onscreen when needed than it is to redraw it from scratch each time.
Use lazy evaluation. When parameters change, set a flag saying that the element needs to
update it's appearance – but don't actually update the appearance until it next needs to be drawn.

Fast Images

Java has many ways to create and manipulate images.
Image ops are fastest when the source and destination images have the same data format.
Use GraphicsConfiguration.createCompatableImage() to create the image that best matches the
Component you are drawing to.
Java accelerates some image data processing with native code, but not others. If using
BufferedImages, use BufferedImage.TYPE_INT_ARGB or
BufferedImage.TYPE_4BYTE_ARGB to take advantage of this.
BufferedImage.TYPE_INT_ARGB seems to be fast in practice.

Volatile Images

BufferedImages are handled in software. VolatileImages exist in hardware memory.
As of Java 1.4, BufferedImages are automatically 'accelerated' – ie, the runtime engine
automatically converts them to Volatile images when it detects this will provide a speed
advantage.
VolatileImages are faster because they are stored in video hardware - but their content can be
lost at any time, so they need to be carefully monitored and updated.
Blits between images with incompatible data types have to be done in software. As a result,
accelerated BufferedImages become decelerated, which causes a slowdown. To ensure images
stay volatile, be careful to only perform blits with compatible images.

Writable Rasters

For intensive pixel operations, it might be best to use a WritableRaster.
A raster encapsulates a DataBuffer of pixel data, and a SampleModel that describes how the
data buffer is partitioned into individual pixels.
The DataBuffer can be quickly accessed and manipulated. Also, because it can be treated as
numerical data, you can apply unusual operations to it, such as Fourier analysis.
A WritableRaster can be combined with a ColorModel to produce a BufferedImage that can be
used in standard graphics routines.
It is a good idea to choose the default ColorModel to ensure JDK native code controls things
behind the scenes.
GradientPaint and TexturePaint use WritableRasters to render their content.

Procedural Shaders

Combine OpenGL with Java2D/Swing
Use JOGL pbuffers to create 3D images and render it to an offscreen buffer. Then blit the
buffer to screen.
OpenGL shaders provide dynamic, accelerated image filtering ops: Gaussian blurs,
brightness/contrast controls, embossing, bump map shading – pretty much any 2D filtering
operation.

JAI

An extensive library for fancy, fast image compositing and manipulation.
Much of it is accelerated in native code. Java interpreted code is used when native code for
your platform doesn't exist.
Allows use of huge images by partitioning large images into tiles.

本文转载自:http://www.kitfox.com

共有 人打赏支持
刘少
粉丝 13
博文 31
码字总数 12465
作品 0
海淀
程序员
私信 提问
Java 5 、6、 7中新特性

JDK5新特性(与1.4相比)【转】 1 循环 for (type variable : array){ body} for (type variable : arrayList){body} 而1.4必须是: for (int i = 0; i < array.length; i++){ type variabl......

thinkyoung
2014/10/14
0
0
Java 7 的新特性一览表

官方说是 7月28日 正式发布 Java 7 ,正常的话我们应该在 7月29日 看到这个版本。很快了,就两天时间。 发布之前让我们先来看看 Java 7 都有什么新特性吧。 Java 7 的架构图: 新特性一览表:...

红薯
2011/07/27
76.4K
83
Processing 2.0 发布,开源图形处理编程语言

用于创意艺术和视觉设计的开源编程语言和开发环境Processing发布了2.0版(代码托管在Github) ,主要新特性是用 OpenGL加速的渲染器替代基于软件的渲染器。 Processing项目诞生于2001年,其发...

oschina
2013/06/06
4.4K
2
20 Very Useful Java Code Snippets

Following are few very useful Java code snippets for Java developers. Few of them are written by me and few are taken from other code reference. Feel free to comment about the c......

pczhangtl
2014/03/30
0
0
JCR 223 在各种语言的实现

JSR 223 compliant scripting languages Added by Adrian David, last edited by Adrian David on May 08, 2008 (view change) The J2EE platform implements the java.script APIs, which a......

麦地兜兜
2012/12/15
408
1

没有更多内容

加载失败,请刷新页面

加载更多

容器服务

简介 容器服务提供高性能可伸缩的容器应用管理服务,支持用 Docker 和 Kubernetes 进行容器化应用的生命周期管理,提供多种应用发布方式和持续交付能力并支持微服务架构。 产品架构 容器服务...

狼王黄师傅
昨天
3
0
高性能应用缓存设计方案

为什么 不管是刻意或者偶尔看其他大神或者大师在讨论高性能架构时,自己都是认真的去看缓存是怎么用呢?认认真真的看完发现缓存这一块他们说的都是一个WebApp或者服务的缓存结构或者缓存实现...

呼呼南风
昨天
12
0
寻找一种易于理解的一致性算法(扩展版)

摘要 Raft 是一种为了管理复制日志的一致性算法。它提供了和 Paxos 算法相同的功能和性能,但是它的算法结构和 Paxos 不同,使得 Raft 算法更加容易理解并且更容易构建实际的系统。为了提升可...

Tiny熊
昨天
2
0
聊聊GarbageCollectionNotificationInfo

序 本文主要研究一下GarbageCollectionNotificationInfo CompositeData java.management/javax/management/openmbean/CompositeData.java public interface CompositeData { public Co......

go4it
昨天
3
0
阿里云ECS的1M带宽理解

本文就给大家科普下阿里云ECS的固定1M带宽的含义。 “下行带宽”和“上行带宽” 为了更好的理解,需要先给大家解释个词“下行带宽”和“上行带宽”: 下行带宽:粗略的解释就是下载数据的最大...

echojson
昨天
10
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部