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]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章