Image Filter : Image Filter « 2D Graphics GUI « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » 2D Graphics GUI » Image Filter 




Image Filter
  
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BandCombineOp;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.awt.image.ByteLookupTable;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import java.awt.image.LookupOp;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
import java.net.URL;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

public class Java2DExample extends JFrame {
  private JMenu filterMenu = new JMenu("Image Filters");

  private ImagePanel imagePanel;

  private MyFilter invertFilter = new InvertFilter();

  private MyFilter sharpenFilter = new SharpenFilter();

  private MyFilter blurFilter = new BlurFilter();

  private MyFilter colorFilter = new ColorFilter();

  public Java2DExample() {
    super("Java 2D Image Processing Demo");
    imagePanel = new ImagePanel(Java2DExample.class.getResource("yourImage.png"));

    JMenuBar menuBar = new JMenuBar();
    setJMenuBar(menuBar);
    filterMenu.setMnemonic('I');

    JMenuItem originalMenuItem = new JMenuItem("Display Original");
    originalMenuItem.setMnemonic('O');

    originalMenuItem.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent action) {
        imagePanel.displayOriginalImage();
      }

    });

    JMenuItem invertMenuItem = createMenuItem("Invert"'I', invertFilter);
    JMenuItem sharpenMenuItem = createMenuItem("Sharpen"'S', sharpenFilter);
    JMenuItem blurMenuItem = createMenuItem("Blur"'B', blurFilter);
    JMenuItem changeColorsMenuItem = createMenuItem("Change Colors"'C', colorFilter);

    filterMenu.add(originalMenuItem);
    filterMenu.add(invertMenuItem);
    filterMenu.add(sharpenMenuItem);
    filterMenu.add(blurMenuItem);
    filterMenu.add(changeColorsMenuItem);

    menuBar.add(filterMenu);

    getContentPane().add(imagePanel, BorderLayout.CENTER);

  }

  public JMenuItem createMenuItem(String menuItemName, char mnemonic, final MyFilter filter) {
    JMenuItem menuItem = new JMenuItem(menuItemName);
    menuItem.setMnemonic(mnemonic);
    menuItem.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent action) {
        imagePanel.applyFilter(filter);
      }

    });
    return menuItem;
  }

  public static void main(String args[]) {
    Java2DExample application = new Java2DExample();
    application.setDefaultCloseOperation(EXIT_ON_CLOSE);
    application.pack();
    application.setVisible(true);
  }
}

interface MyFilter {
  public abstract BufferedImage processImage(BufferedImage image);
}

class BlurFilter implements MyFilter {
  public BufferedImage processImage(BufferedImage image) {
    float[] blurMatrix = 1.0f 9.0f1.0f 9.0f1.0f 9.0f1.0f 9.0f1.0f 9.0f,
        1.0f 9.0f1.0f 9.0f1.0f 9.0f1.0f 9.0f };
    BufferedImageOp blurFilter = new ConvolveOp(new Kernel(33, blurMatrix),
        ConvolveOp.EDGE_NO_OP, null);
    return blurFilter.filter(image, null);
  }
}

class ImagePanel extends JPanel {

  private BufferedImage displayImage;

  private BufferedImage originalImage;

  private Image image;

  public ImagePanel(URL imageURL) {
    image = Toolkit.getDefaultToolkit().createImage(imageURL);
    MediaTracker mediaTracker = new MediaTracker(this);
    mediaTracker.addImage(image, 0);

    try {
      mediaTracker.waitForAll();
    catch (InterruptedException e) {
      e.printStackTrace();
    }
    originalImage = new BufferedImage(image.getWidth(null), image.getHeight(null),
        BufferedImage.TYPE_INT_RGB);
    displayImage = originalImage;
    Graphics2D graphics = displayImage.createGraphics();
    graphics.drawImage(image, null, null);

  }

  public void applyFilter(MyFilter filter) {
    displayImage = filter.processImage(displayImage);
    repaint();
  }

  public void displayOriginalImage() {
    displayImage = new BufferedImage(image.getWidth(null), image.getHeight(null),
        BufferedImage.TYPE_INT_RGB);

    Graphics2D graphics = displayImage.createGraphics();
    graphics.drawImage(originalImage, null, null);
    repaint();
  }
  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D graphics = (Graphics2Dg;
    graphics.drawImage(displayImage, 00null);
  }
  public Dimension getPreferredSize() {
    return new Dimension(displayImage.getWidth(), displayImage.getHeight());
  }
  public Dimension getMinimumSize() {
    return getPreferredSize();
  }
}

class SharpenFilter implements MyFilter {
  public BufferedImage processImage(BufferedImage image) {
    float[] sharpenMatrix = 0.0f, -1.0f0.0f, -1.0f5.0f, -1.0f0.0f, -1.0f0.0f };
    BufferedImageOp sharpenFilter = new ConvolveOp(new Kernel(33, sharpenMatrix),
        ConvolveOp.EDGE_NO_OP, null);
    return sharpenFilter.filter(image, null);
  }
}

class InvertFilter implements MyFilter {
  public BufferedImage processImage(BufferedImage image) {
    byte[] invertArray = new byte[256];

    for (int counter = 0; counter < 256; counter++)
      invertArray[counter(byte) (255 - counter);

    BufferedImageOp invertFilter = new LookupOp(new ByteLookupTable(0, invertArray)null);
    return invertFilter.filter(image, null);

  }
}

class ColorFilter implements MyFilter {
  public BufferedImage processImage(BufferedImage image) {
    float[][] colorMatrix = { { 1f0f0f }0.5f1.0f0.5f }0.2f0.4f0.6f } };
    BandCombineOp changeColors = new BandCombineOp(colorMatrix, null);
    Raster sourceRaster = image.getRaster();
    WritableRaster displayRaster = sourceRaster.createCompatibleWritableRaster();
    changeColors.filter(sourceRaster, displayRaster);
    return new BufferedImage(image.getColorModel(), displayRaster, true, null);

  }
}
           
         
    
  














Related examples in the same category
1.ImageComparator compares a byte[] for equality by creating 2 hashes for the bytearray and comparing thoose hashes.
2.Gray scale image operationGray scale image operation
3.Image Color Operation: dark, bright, contrast, negativeImage Color Operation: dark, bright, contrast, negative
4.Dithering a 24-bit RGB image to a monochrome (1-bit or bilevel) image
5.If the image has transparent pixels
6.Copy Raster
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.