import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.awt.image.LookupOp;
import java.awt.image.LookupTable;
import java.awt.image.ShortLookupTable;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ImageShading extends JFrame {
private JPanel contentPane;
private BorderLayout borderLayout1 = new BorderLayout();
// 添加的组件
private JPanel jpanel = new JPanel();
private JButton jb1 = new JButton();
private JButton jb2 = new JButton();
private JButton jb3 = new JButton();
private GridLayout gridLayout = new GridLayout();
// 创建ShowPanel类的实例
ShowPanel sp = new ShowPanel();
public void initial() {
// 设置各组件的属性
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(580, 440));
this.setTitle("图像的明暗处理演示");
jb1.setFont(new Font("Dialog", Font.BOLD, 13));
jb1.setText("Brighten");
jb1.addActionListener(new ActionListener() {// 添加事件监听器
public void actionPerformed(ActionEvent e) {
jb1_actionPerformed(e);
}
});
jb2.setFont(new Font("Dialog", Font.BOLD, 13));
jb2.setText("Darken");
jb2.addActionListener(new ActionListener() {// 添加事件监听器
public void actionPerformed(ActionEvent e) {
jb2_actionPerformed(e);
}
});
jb3.setFont(new Font("Dialog", Font.BOLD, 13));
jb3.setText("Reset");
jb3.addActionListener(new ActionListener() {// 添加事件监听器
public void actionPerformed(ActionEvent e) {
jb3_actionPerformed(e);
}
});
jpanel.setLayout(gridLayout);
jpanel.add(jb1, null);
jpanel.add(jb2, null);
jpanel.add(jb3, null);
contentPane.add(jpanel, BorderLayout.SOUTH);
// 在窗口中部添加ShowPanel面板
contentPane.add(sp, BorderLayout.CENTER);
this.setVisible(true);
}
public void jb1_actionPerformed(ActionEvent e) {
sp.brightImage();// 创建变亮查找表
sp.applyFilter();// 变亮源图像
sp.repaint();// 绘制变亮后的图像
}
public void jb2_actionPerformed(ActionEvent e) {
sp.darkImage();// 创建变暗查找表
sp.applyFilter();// 变暗源图像
sp.repaint();// 绘制变暗后的图像
}
public void jb3_actionPerformed(ActionEvent e) {
sp.reset();// 缓冲区图像为源图像
sp.repaint();// 绘制源图像
}
public static void main(String[] args) {
new ImageShading().initial();
}
}
class ShowPanel extends JPanel {
private BorderLayout borderLayout = new BorderLayout();
// 定义新属性
Image image;// 源图像
BufferedImage bi;// 缓冲区图像
Graphics2D g2d;// 图像上下文
LookupTable lut;// 查找表
public ShowPanel() {
this.setLayout(borderLayout);
loadImage();// 加载图片
createBufferedImage();// 创建缓冲区图像
this
.setSize(new Dimension(image.getWidth(this), image
.getHeight(this)));// 设置面板大小
}
public void loadImage() {
// 加载图片
image = this.getToolkit().getImage(
// 这里如果不填写一个存在的图片,程序会抛出空指针异常
ClassLoader.getSystemResource("hua.jpg"));// 创建Image对象
MediaTracker mt = new MediaTracker(this);
mt.addImage(image, 1);
try {
mt.waitForAll();
} catch (Exception e) {
e.printStackTrace();// 表示加载失败
}
if (image.getWidth(this) == -1) {
System.out.println("不能创建图象");// 如果不存在退出程序
System.exit(-1);
}
}
public void createBufferedImage() {
// 根据image对象创建一个缓冲区图像
bi = new BufferedImage(image.getWidth(this), image.getHeight(this),
BufferedImage.TYPE_INT_ARGB);
// 向缓冲区图像中输入原image对象的图象数据
g2d = bi.createGraphics();
g2d.drawImage(this.image, 0, 0, this);// 绘制缓冲区图像
}
public void brightImage() {
short[] brighten = new short[256];// 查找表的数据数组
short pixelValue;
for (int i = 0; i < 256; i++) {
pixelValue = (short) (i + 10);// 加亮源像素10个单位值
if (pixelValue > 255) {// 如果源像素加亮后的值超过255,则设为255
pixelValue = 255;
} else if (pixelValue < 0) {// 如果源像素加亮后的值低于0,则设为0
pixelValue = 0;
}
brighten[i] = pixelValue;// 赋值
}
lut = new ShortLookupTable(0, brighten);// 创建新的查找表,该表加亮源图像
}
public void darkImage() {
short[] darken = new short[256];// 查找表的数据数组
short pixelValue;
for (int i = 0; i < 256; i++) {
pixelValue = (short) (i - 10);// 变暗源像素10个单位值
if (pixelValue > 255) {// 如果源处理后的像素值超过255,则设为255
pixelValue = 255;
} else if (pixelValue < 0) {// 如果处理后的像素值低于0,则设为0
pixelValue = 0;
}
darken[i] = pixelValue;// 赋值
}
lut = new ShortLookupTable(0, darken);// 创建新的查找表,该表变暗源图像
}
public void reset() {
g2d.setColor(Color.black);
g2d.clearRect(0, 0, image.getWidth(this), image.getHeight(this));
g2d.drawImage(this.image, 0, 0, this);// 绘制缓冲区图像
}
public void applyFilter() {
LookupOp lop = new LookupOp(lut, null);// 根据查找表,创建查找过滤器
lop.filter(bi, bi);// 过滤图像
}
public void update(Graphics g) {
g.clearRect(0, 0, this.getWidth(), this.getHeight());
paintComponent(g);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.drawImage(bi, 0, 0, this);
}
}