写了很多篇关于图像处理的文章,没有一篇介绍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对象中读取像素数据的代码如下:
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 int type= image.getType(); 2 if ( type ==BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB ) 3 image.getRaster().setDataElements(x, y, width, height, pixels ); 4 else 5 image.setRGB(x, y, width, height, pixels, 0, width );
读取图像可能因为图像文件比较大,需要一定时间的等待才可以,Java Advance Image
Processor API提供了MediaTracker对象来跟踪图像的加载,同步其它操作,使用方法如下:
MediaTracker tracker = new MediaTracker(this); //初始化对象 http://www.cnblogs.com/roucheng/ tracker.addImage(image_01, 1); // 加入要跟踪的BufferedImage对象image_001 tracker.waitForID(1, 10000) // 等待10秒,让iamge_01图像加载
从一个32位int型数据cARGB中读取图像RGB颜色值的代码如下:
1 int alpha = (cARGB >> 24)& 0xff; //透明度通道 http://www.cnblogs.com/roucheng/ 2 int red = (cARGB >> 16) &0xff; 3 int green = (cARGB >> 8) &0xff; 4 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如下:
1 package com.gloomyfish.swing; 2 3 import java.awt.BorderLayout; 4 import java.awt.Dimension; 5 import java.awt.Graphics; 6 import java.awt.Graphics2D; 7 import java.awt.RenderingHints; 8 import java.awt.image.BufferedImage; 9 import java.io.File; 10 import java.io.IOException; 11 12 import javax.imageio.ImageIO; 13 import javax.swing.JComponent; 14 import javax.swing.JFrame; 15 16 public class PlasmaDemo extends JComponent { 17 18 /** 19 * 20 */ 21 private static final long serialVersionUID = -2236160343614397287L; 22 private BufferedImage image = null; 23 private int size = 256; 24 25 public PlasmaDemo() { 26 super(); 27 this.setOpaque(false); 28 } 29 30 protected void paintComponent(Graphics g) { 31 Graphics2D g2 = (Graphics2D)g; 32 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 33 g2.drawImage(getImage(), 5, 5, image.getWidth(), image.getHeight(), null); 34 } 35 36 private BufferedImage getImage() { 37 if(image == null) { 38 image = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB); 39 int[] rgbData = new int[size*size]; 40 generateNoiseImage(rgbData); 41 setRGB(image, 0, 0, size, size, rgbData); 42 File outFile = new File("plasma.jpg"); 43 try { 44 ImageIO.write(image, "jpg", outFile); 45 } catch (IOException e) { 46 e.printStackTrace(); 47 } 48 } 49 return image; 50 } 51 52 public void generateNoiseImage(int[] rgbData) { 53 int index = 0; 54 int a = 255; 55 int r = 0; 56 int g = 0; 57 int b = 0; 58 59 for(int row=0; row<size; row++) { 60 for(int col=0; col<size; col++) { 61 // set random color value for each pixel 62 r = (int)(128.0 + (128.0 * Math.sin((row + col) / 8.0))); 63 g = (int)(128.0 + (128.0 * Math.sin((row + col) / 8.0))); 64 b = (int)(128.0 + (128.0 * Math.sin((row + col) / 8.0))); 65 66 rgbData[index] = ((clamp(a) & 0xff) << 24) | 67 ((clamp(r) & 0xff) << 16) | 68 ((clamp(g) & 0xff) << 8) | 69 ((clamp(b) & 0xff)); 70 index++; 71 } 72 } 73 74 } 75 76 private int clamp(int rgb) { 77 if(rgb > 255) 78 return 255; 79 if(rgb < 0) 80 return 0; 81 return rgb; 82 } 83 84 public void setRGB( BufferedImage image, int x, int y, int width, int height, int[] pixels ) { 85 int type = image.getType(); 86 if ( type == BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB ) 87 image.getRaster().setDataElements( x, y, width, height, pixels ); 88 else 89 image.setRGB( x, y, width, height, pixels, 0, width ); 90 } 91 92 public static void main(String[] args) { 93 JFrame frame = new JFrame("Noise Art Panel"); 94 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 95 frame.getContentPane().setLayout(new BorderLayout()); 96 97 // Display the window. http://www.cnblogs.com/roucheng/ 98 frame.getContentPane().add(new PlasmaDemo(), BorderLayout.CENTER); 99 frame.setPreferredSize(new Dimension(400 + 25,450)); 100 frame.pack(); 101 frame.setVisible(true); 102 } 103 }