Image Color Render

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.filechooser.FileNameExtensionFilter;

public class ImageColorRenderFrame extends JFrame implements ActionListener,
  ChangeListener {

 private static final long serialVersionUID = -3239696245073823821L;

 public static void main(String[] args) {
  ImageColorRenderFrame frame = new ImageColorRenderFrame();
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setLocationRelativeTo(null);
  frame.setVisible(true);
 }

 private Image src;
 private Image image;
 private JMenuItem save;
 private JLabel imageLabel;
 private JRadioButtonMenuItem filter, reverseFilter;
 private JFileChooser chooser;
 private JSlider sliderR, sliderG, sliderB;

 public ImageColorRenderFrame() {
  setTitle("Image Color Filter ");
  setLayout(new BorderLayout());
  setSize(410, 310);
  // menu
  JMenuBar menubar = new JMenuBar();
  //
  JMenu mnuFile = new JMenu("File");
  mnuFile.setMnemonic('f');
  JMenuItem open = new JMenuItem("Open...", 'o');
  open.addActionListener(this);
  save = new JMenuItem("Save As...", 'a');
  save.setEnabled(false);
  save.addActionListener(this);
  JMenuItem exit = new JMenuItem("Exit", 'x');
  exit.addActionListener(this);
  mnuFile.add(open);
  mnuFile.addSeparator();
  mnuFile.add(save);
  mnuFile.addSeparator();
  mnuFile.add(exit);

  JMenu mnuOption = new JMenu("Option");
  mnuOption.setMnemonic('o');
  // ButtonGroup for radio buttons
  ButtonGroup optionGroup = new ButtonGroup();
  filter = new JRadioButtonMenuItem("Filter");
  filter.setMnemonic('f');
  filter.addActionListener(this);
  optionGroup.add(filter);
  reverseFilter = new JRadioButtonMenuItem("Render", true);
  reverseFilter.setMnemonic('r');
  reverseFilter.addActionListener(this);
  optionGroup.add(reverseFilter);
  mnuOption.add(filter);
  mnuOption.add(reverseFilter);

  //
  menubar.add(mnuFile);
  menubar.add(mnuOption);
  setJMenuBar(menubar);

  // chooser
  chooser = new JFileChooser();
  FileNameExtensionFilter filter = new FileNameExtensionFilter(
    "JPG & GIF & PNG ...", "jpg", "gif", "png");
  chooser.setFileFilter(filter);

  chooser.setCurrentDirectory(new File("."));
  // imageLabel
  JPanel panel = new JPanel();
  panel.setSize(400, 300);
  imageLabel = new JLabel();
  panel.add(imageLabel, BorderLayout.CENTER);
  add(panel, BorderLayout.CENTER);

  // slider
  JPanel p = new JPanel();
  p.setLayout(new GridLayout());
  sliderR = new JSlider(0, 0xff, 0);
  sliderR.setBorder(BorderFactory.createTitledBorder("Red"));
  sliderR.setMajorTickSpacing(50);
  sliderR.setMinorTickSpacing(10);
  sliderR.setPaintTicks(true);
  sliderR.setPaintLabels(true);
  sliderR.addChangeListener(this);
  sliderG = new JSlider(0, 0xff, 0);
  sliderG.setBorder(BorderFactory.createTitledBorder("Green"));
  sliderG.setMajorTickSpacing(50);
  sliderG.setMinorTickSpacing(10);
  sliderG.setPaintTicks(true);
  sliderG.setPaintLabels(true);
  sliderG.addChangeListener(this);
  sliderB = new JSlider(0, 0xff, 0);
  sliderB.setBorder(BorderFactory.createTitledBorder("Blue"));
  sliderB.setMajorTickSpacing(50);
  sliderB.setMinorTickSpacing(10);
  sliderB.setPaintTicks(true);
  sliderB.setPaintLabels(true);
  sliderB.addChangeListener(this);
  p.add(sliderR);
  p.add(sliderG);
  p.add(sliderB);
  add(p, BorderLayout.NORTH);
 }

 @Override
 public void stateChanged(ChangeEvent e) {
  JSlider slider = (JSlider) e.getSource();

  if (!slider.getValueIsAdjusting()) {
   slider.setToolTipText(String.valueOf(slider.getValue()));

   if (src != null) {
    ImageFilter filter = new ColorFilter(sliderR.getValue(),
      sliderG.getValue(), sliderB.getValue(),
      this.filter.isSelected());
    FilteredImageSource filteredSrc = new FilteredImageSource(
      src.getSource(), filter);
    image = Toolkit.getDefaultToolkit().createImage(filteredSrc);
    imageLabel.setIcon(new ImageIcon(image));
   }
  }

 }

 @Override
 public void actionPerformed(ActionEvent e) {
  if (e.getActionCommand() == "Open...") {
   int result = chooser.showOpenDialog(null);
   if (result == JFileChooser.APPROVE_OPTION) {
    String filename = chooser.getSelectedFile().getPath();
    src = new ImageIcon(filename).getImage();
    if (src != null) {
     imageLabel.setIcon(new ImageIcon(src));
     save.setEnabled(true);
    } else
     save.setEnabled(false);
    pack();
    setLocationRelativeTo(null);
   }
  }
  if (e.getActionCommand() == "Save As...") {
   int result = chooser.showSaveDialog(null);
   if (result == JFileChooser.APPROVE_OPTION) {
    String filename = chooser.getSelectedFile().getPath();
    File file = new File(filename);
    try {
     ImageIO.write(image2BufferedImage(image),
       getFileType(file), file);
    } catch (Exception ew) {
     JOptionPane.showMessageDialog(this, ew.getMessage(),
       "Error", JOptionPane.ERROR_MESSAGE);
    }
   }
  }
  if (e.getActionCommand() == "Exit")
   System.exit(0);
  if (e.getActionCommand() == "Filter") {
   sliderR.setValueIsAdjusting(false);
  }
  if (e.getActionCommand() == "Render") {
   sliderR.setValueIsAdjusting(true);
  }
 }

 public String getFileType(File file) {
  String filename = file.getName();
  return filename.substring(filename.lastIndexOf(".") + 1);
 }

 public BufferedImage image2BufferedImage(Image image) {
  image = new ImageIcon(image).getImage();
  BufferedImage bufferedImage = new BufferedImage(image.getWidth(null),
    image.getHeight(null), BufferedImage.TYPE_INT_RGB);
  Graphics g = bufferedImage.createGraphics();
  g.setColor(Color.white);
  g.fillRect(0, 0, image.getWidth(null), image.getHeight(null));
  g.drawImage(image, 0, 0, null);
  g.dispose();

  return bufferedImage;
 }

}

class ColorFilter extends RGBImageFilter {
 private int filterRgb;// 过滤值
 private boolean isFilter;// 是否反转过滤

 public ColorFilter(Color targetColor) {
  this(targetColor, true);
 }

 public ColorFilter(Color targetColor, boolean isFilter) {
  canFilterIndexColorModel = true;
  this.isFilter = isFilter;
  filterRgb = targetColor.getRGB();
 }

 public ColorFilter(int r, int g, int b) {
  this(0xff, r, g, b, true);
 }

 public ColorFilter(int r, int g, int b, boolean isFilter) {
  this(0xff, r, g, b, isFilter);
 }

 public ColorFilter(int a, int r, int g, int b) {
  this(a, r, g, b, true);
 }

 public ColorFilter(int a, int r, int g, int b, boolean isFilter) {
  canFilterIndexColorModel = true;
  this.isFilter = isFilter;
  filterRgb = a << 24 | (r << 16) | (g << 8) | b;
 }

 @Override
 public int filterRGB(int x, int y, int rgb) {
  return isFilter ? rgb & filterRgb : rgb | filterRgb;
 }

}

效果:

参考:http://blog.csdn.net/dahual/article/details/6673439

你可能感兴趣的:(Java)