天天看點

Java數字圖像處理基礎知識 - 必讀

寫了很多篇關于圖像處理的文章,沒有一篇介紹Java 2D的圖像處理API,文章讨論和提及的

API都是基于JDK6的,首先來看Java中如何組織一個圖像對象BufferedImage的,如圖:

一個BufferedImage的像素資料儲存在Raster中,ColorModel裡面儲存顔色空間,類型等

資訊,目前Java隻支援一下三種圖像格式- JPG,PNG,GIF,如何向讓Java支援其它格式,首

先要 完成Java中的圖像讀寫接口,然後打成jar,加上啟動參數- Xbootclasspath/p

newimageformatIO.jar即可。

Java中如何讀寫一個圖像檔案,使用ImageIO對象即可。讀圖像檔案的代碼如下:

[java] view

plaincopy

  1. File file = new File("D:\\test\\blue_flower.jpg");  
  2. BufferedImage image = ImageIO.read(file);  

寫圖像檔案的代碼如下:

  1. File outputfile = new File("saved.png");  
  2. ImageIO.write(bufferedImage, "png",outputfile);  

從BufferedImage對象中讀取像素資料的代碼如下:

  1. int type= image.getType();  
  2. if ( type ==BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB )  
  3.      return (int [])image.getRaster().getDataElements(x, y, width, height, pixels );  
  4. else  
  5.     return image.getRGB( x, y, width, height, pixels, 0, width );  

首先擷取圖像類型,如果不是32位的INT型資料,直接讀寫RGB值即可,否則需要從Raster

對象中讀取。

往BufferedImage對象中寫入像素資料同樣遵守上面的規則。代碼如下:

  1.    image.getRaster().setDataElements(x, y, width, height, pixels );  
  2.    image.setRGB(x, y, width, height, pixels, 0, width );  

讀取圖像可能因為圖像檔案比較大,需要一定時間的等待才可以,Java Advance Image

Processor API提供了MediaTracker對象來跟蹤圖像的加載,同步其它操作,使用方法如下:

  1. MediaTracker tracker = new MediaTracker(this); //初始化對象  
  2. tracker.addImage(image_01, 1); // 加入要跟蹤的BufferedImage對象image_001  
  3. tracker.waitForID(1, 10000) // 等待10秒,讓iamge_01圖像加載  

從一個32位int型資料cARGB中讀取圖像RGB顔色值的代碼如下:

  1. int alpha = (cARGB >> 24)& 0xff; //透明度通道  
  2. int red = (cARGB >> 16) &0xff;  
  3. int green = (cARGB >> 8) &0xff;  
  4. int blue = cARGB & 0xff;  

将RGB顔色值寫入成一個INT型資料cRGB的代碼如下:

  1. cRGB = (alpha << 24) | (red<< 16) | (green << 8) | blue;  

建立一個BufferedImage對象的代碼如下:

  1. BufferedImage image = newBufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB);  

一個完整的源代碼Demo如下:

  1. package com.gloomyfish.swing;  
  2. import java.awt.BorderLayout;  
  3. import java.awt.Dimension;  
  4. import java.awt.Graphics;  
  5. import java.awt.Graphics2D;  
  6. import java.awt.RenderingHints;  
  7. import java.awt.image.BufferedImage;  
  8. import java.io.File;  
  9. import java.io.IOException;  
  10. import javax.imageio.ImageIO;  
  11. import javax.swing.JComponent;  
  12. import javax.swing.JFrame;  
  13. public class PlasmaDemo extends JComponent {    
  14.     /**  
  15.      *   
  16.      */    
  17.     private static final long serialVersionUID = -2236160343614397287L;    
  18.     private BufferedImage image = null;    
  19.     private int size = 256;  
  20.     public PlasmaDemo() {    
  21.         super();    
  22.         this.setOpaque(false);    
  23.     }    
  24.     protected void paintComponent(Graphics g) {    
  25.         Graphics2D g2 = (Graphics2D)g;    
  26.         g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);    
  27.         g2.drawImage(getImage(), 5, 5, image.getWidth(), image.getHeight(), null);    
  28.     private BufferedImage getImage() {    
  29.         if(image == null) {    
  30.             image = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);    
  31.             int[] rgbData = new int[size*size];    
  32.             generateNoiseImage(rgbData);    
  33.             setRGB(image, 0, 0, size, size, rgbData);  
  34.             File outFile = new File("plasma.jpg");  
  35.             try {  
  36.                 ImageIO.write(image, "jpg", outFile);  
  37.             } catch (IOException e) {  
  38.                 e.printStackTrace();  
  39.             }  
  40.         }    
  41.         return image;    
  42.     public void generateNoiseImage(int[] rgbData) {    
  43.         int index = 0;    
  44.         int a = 255;    
  45.         int r = 0;    
  46.         int g = 0;    
  47.         int b = 0;    
  48.         for(int row=0; row<size; row++) {    
  49.             for(int col=0; col<size; col++) {    
  50.                 // set random color value for each pixel    
  51.                 r = (int)(128.0 + (128.0 * Math.sin((row + col) / 8.0)));    
  52.                 g = (int)(128.0 + (128.0 * Math.sin((row + col) / 8.0)));    
  53.                 b = (int)(128.0 + (128.0 * Math.sin((row + col) / 8.0)));    
  54.                 rgbData[index] = ((clamp(a) & 0xff) << 24) |    
  55.                                 ((clamp(r) & 0xff) << 16)  |    
  56.                                 ((clamp(g) & 0xff) << 8)   |    
  57.                                 ((clamp(b) & 0xff));    
  58.                 index++;    
  59.             }    
  60.     private int clamp(int rgb) {    
  61.         if(rgb > 255)    
  62.             return 255;    
  63.         if(rgb < 0)    
  64.             return 0;    
  65.         return rgb;    
  66.     }      
  67.     public void setRGB( BufferedImage image, int x, int y, int width, int height, int[] pixels ) {    
  68.         int type = image.getType();    
  69.         if ( type == BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB )    
  70.             image.getRaster().setDataElements( x, y, width, height, pixels );    
  71.         else    
  72.             image.setRGB( x, y, width, height, pixels, 0, width );    
  73.     public static void main(String[] args) {    
  74.         JFrame frame = new JFrame("Noise Art Panel");    
  75.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    
  76.         frame.getContentPane().setLayout(new BorderLayout());    
  77.         // Display the window.    
  78.         frame.getContentPane().add(new PlasmaDemo(), BorderLayout.CENTER);    
  79.         frame.setPreferredSize(new Dimension(400 + 25,450));    
  80.         frame.pack();    
  81.         frame.setVisible(true);    
  82. }