【转】Java image的文件获取、显示、像素分解、合成
实现image的文件获取、显示、像素分解、合成。
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package graph;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.Transparency;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* 常用图像处理
* 作者:何险峰
*
* 2008-10-25
*/
public class Img {
/**
* 读入图片文件
* @param fnm
* @return BufferedImage
*/
public BufferedImage getImg(String fnm) {
BufferedImage bi = null;
try {
bi = ImageIO.read(new File(fnm));
} catch (Exception e) {
e.printStackTrace();
}
return bi;
}
/**
* 图片存盘
* @param fnm
* @param img
*/
public void mkImgFile(String fnm, BufferedImage img) {
try {
ImageIO.write(img, "jpg", new File(fnm));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 返回象元的RGB数组
* @param image
* @param x
* @param y
* @return int[3] RGB数组
*/
public static int[] getSplitRGB(BufferedImage image, int x, int y) {
int[] rgb = null;
if (image != null && x < image.getWidth() && y < image.getHeight()) {
rgb = new int[3];
int pixel = image.getRGB(x, y);
rgb = getSplitRGB(pixel);
}
return rgb;
}
/**
* 返回象元的RGB数组
* @param pixel
* @return
*/
public static int[] getSplitRGB(int pixel) {
int[] rgbs = new int[3];
rgbs[ 0] = (pixel & 0xff0000) &g