寫了很多篇關於影像處理的文章,沒有一篇介紹Java 2D的影像處理API,文章討論和提及的
API都是基於JDK6的,首先來看Java中如何組織一個映像對象BufferedImage的,
一個BufferedImage的像素資料儲存在Raster中,ColorModel裡面儲存色彩空間,類型等
資訊,當前Java只支援一下三種映像格式- JPG,PNG,GIF,如何向讓Java支援其它格式,首
先要 完成Java中的映像讀寫介面,然後打成jar,加上啟動參數- Xbootclasspath/p
newimageformatIO.jar即可。
Java中如何讀寫一個影像檔,使用ImageIO對象即可。讀影像檔的代碼如下:
File file = new File("D:\\test\\blue_flower.jpg");BufferedImage image = ImageIO.read(file);
寫影像檔的代碼如下:
File outputfile = new File("saved.png");ImageIO.write(bufferedImage, "png",outputfile);
從BufferedImage對象中讀取像素資料的代碼如下:
int type= image.getType();if ( type ==BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB ) return (int [])image.getRaster().getDataElements(x, y, width, height, pixels );else return image.getRGB( x, y, width, height, pixels, 0, width );
首先擷取映像類型,如果不是32位的INT型資料,直接讀寫RGB值即可,否則需要從Raster
對象中讀取。
往BufferedImage對象中寫入像素資料同樣遵守上面的規則。代碼如下:
int type= image.getType();if ( type ==BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB ) image.getRaster().setDataElements(x, y, width, height, pixels );else image.setRGB(x, y, width, height, pixels, 0, width );
讀取映像可能因為影像檔比較大,需要一定時間的等待才可以,Java Advance Image
Processor API提供了MediaTracker對象來跟蹤映像的載入,同步其它操作,使用方法如下:
MediaTracker tracker = new MediaTracker(this); //初始化對象tracker.addImage(image_01, 1); // 加入要跟蹤的BufferedImage對象image_001tracker.waitForID(1, 10000) // 等待10秒,讓iamge_01映像載入
從一個32位int型資料cARGB中讀取映像RGB顏色值的代碼如下:
int alpha = (cARGB >> 24)& 0xff; //透明度通道int red = (cARGB >> 16) &0xff;int green = (cARGB >> 8) &0xff;int blue = cARGB & 0xff;
將RGB顏色值寫入成一個INT型資料cRGB的代碼如下:
cRGB = (alpha << 24) | (red<< 16) | (green << 8) | blue;
建立一個BufferedImage對象的代碼如下:
BufferedImage image = newBufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB);
一個完整的原始碼Demo如下:
package com.gloomyfish.swing;import java.awt.BorderLayout;import java.awt.Dimension;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.RenderingHints;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import javax.imageio.ImageIO;import javax.swing.JComponent;import javax.swing.JFrame;public class PlasmaDemo extends JComponent { /** * */ private static final long serialVersionUID = -2236160343614397287L; private BufferedImage image = null; private int size = 256; public PlasmaDemo() { super(); this.setOpaque(false); } protected void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.drawImage(getImage(), 5, 5, image.getWidth(), image.getHeight(), null); } private BufferedImage getImage() { if(image == null) { image = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB); int[] rgbData = new int[size*size]; generateNoiseImage(rgbData); setRGB(image, 0, 0, size, size, rgbData); File outFile = new File("plasma.jpg"); try {ImageIO.write(image, "jpg", outFile);} catch (IOException e) {e.printStackTrace();} } return image; } public void generateNoiseImage(int[] rgbData) { int index = 0; int a = 255; int r = 0; int g = 0; int b = 0; for(int row=0; row<size; row++) { for(int col=0; col<size; col++) { // set random color value for each pixel r = (int)(128.0 + (128.0 * Math.sin((row + col) / 8.0))); g = (int)(128.0 + (128.0 * Math.sin((row + col) / 8.0))); b = (int)(128.0 + (128.0 * Math.sin((row + col) / 8.0))); rgbData[index] = ((clamp(a) & 0xff) << 24) | ((clamp(r) & 0xff) << 16) | ((clamp(g) & 0xff) << 8) | ((clamp(b) & 0xff)); index++; } } } private int clamp(int rgb) { if(rgb > 255) return 255; if(rgb < 0) return 0; return rgb; } public void setRGB( BufferedImage image, int x, int y, int width, int height, int[] pixels ) { int type = image.getType(); if ( type == BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB ) image.getRaster().setDataElements( x, y, width, height, pixels ); else image.setRGB( x, y, width, height, pixels, 0, width ); } public static void main(String[] args) { JFrame frame = new JFrame("Noise Art Panel"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(new BorderLayout()); // Display the window. frame.getContentPane().add(new PlasmaDemo(), BorderLayout.CENTER); frame.setPreferredSize(new Dimension(400 + 25,450)); frame.pack(); frame.setVisible(true); } }