Java2D API视觉特效

1.一个不错的例子
[url=http://fivedots.coe.psu.ac.th/~ad/jg/ch04/index.html]Killer Game Programming in Java 第5章和第6章 Images, Visual Effects, and Animation[/url]
这本游戏编程书的第5和第6章讲了一个例子,涉及到了图像视觉特效的许多例子,可以优先参考。效果图如下:
[img]http://dl2.iteye.com/upload/attachment/0105/0139/86394d59-ca12-35dc-b384-003bcbf0bbcf.gif[/img]

使用的方法大致有以下这些:
1) Graphics.drawImage(Image img, int x, int y, int width, int height, ImageObserver observer)
Graphics.drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer)
可用来放大缩小图像,水平垂直翻转图像。
2) AlphaComposite
阿尔法通道,利用透明度使图像淡出
3) AffineTransformOp
仿射变换,可用来360度旋转图像。
4) ConvolveOp
卷积操作,可用来模糊、锐化、边缘检测。
5) LookupOp
利用查找表,做出使图像变红。
6) RescaleOp
利用线性方程,做出使图像变红、变亮、反色效果。
7) BandCombineOp
利用矩阵,混合图像颜色。
8) BufferedImage.getRGB() BufferedImage.setRGB()
逐个像素变换。效果有传送(像素逐渐消失)、消灭(像素逐渐变黄,变红)

具体参考源码实验体会即可。

2. 神奇的卷积
利用卷积(ConvolveOp),提供一个矩阵,就可以做出各种效果。
以下例子使用6个矩阵,做出了6种效果(图见代码下面),从左到右,从上到下依次为:
原图、模糊、锐化
边缘检测、浮雕、运动模糊。

public static final int[] matrixDimension ={3,3,3,3,3,9};
public static float ninth = 1.0f / 9.0f;
public static final float[] ORIGINAL = {
0.f, 0.f, 0.f,
0.f, 1.f, 0.f,
0.f, 0.f, 0.f};
public static final float[] BLUR = {
ninth, ninth, ninth,
ninth, ninth, ninth,
ninth, ninth, ninth};
public static final float[] SHARPEN = {
0.f, -1.f, 0.f,
-1.f, 5.f, -1.f,
0.f, -1.f, 0.f};
public static final float[] EDGE_DETECT = {
-1.f, -1.f, -1.f,
-1.f, 8.f, -1.f,
-1.f, -1.f, -1.f};
public static final float[] EMBOSS = {
-1.f, -1.f, 0.f,
-1.f, 0.f, 1.f,
0.f, 1.f, 1.f};
public static final float[] MOTION_BLUR = {
ninth, 0, 0, 0, 0, 0, 0, 0, 0,
0, ninth, 0, 0, 0, 0, 0, 0, 0,
0, 0, ninth, 0, 0, 0, 0, 0, 0,
0, 0, 0, ninth, 0, 0, 0, 0, 0,
0, 0, 0, 0, ninth, 0, 0, 0, 0,
0, 0, 0, 0, 0, ninth, 0, 0, 0,
0, 0, 0, 0, 0, 0, ninth, 0, 0,
0, 0, 0, 0, 0, 0, 0, ninth, 0,
0, 0, 0, 0, 0, 0, 0, 0, ninth};

[img]http://dl2.iteye.com/upload/attachment/0105/0141/6d966366-ff44-320b-ba2b-16fe475d91c0.jpg[/img]

以上程序我都附在附件里了,可以下载,

3.参考资料
以下2篇文章是不错的CG入门文章,介绍了图片的模糊、锐化、边缘检测。
[url=http://www.jhlabs.com/ip/blurring.html]Blurring for Beginners[/url]
[url=http://lodev.org/cgtutor/filtering.html]Image Filtering[/url]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章