Image Operations : Image « 2D Graphics GUI « Java

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. Email
14. Event
15. File Input Output
16. Game
17. Generics
18. Hibernate
19. I18N
20. J2EE
21. J2ME
22. JDK 6
23. JSP
24. JSTL
25. Language Basics
26. Network Protocol
27. PDF RTF
28. Reflection
29. Regular Expressions
30. Scripting
31. Security
32. Servlets
33. Spring
34. Swing Components
35. Swing JFC
36. SWT JFace Eclipse
37. Threads
38. Tiny Application
39. Velocity
40. Web Services SOA
41. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » 2D Graphics GUI » ImageScreenshots 
Image Operations
Image Operations

/*
 * Copyright (c) 2000 David Flanagan.  All rights reserved.
 * This code is from the book Java Examples in a Nutshell, 2nd Edition.
 * It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied.
 * You may study, use, and modify it for any non-commercial purpose.
 * You may distribute it non-commercially as long as you retain this notice.
 * For a commercial use license, or to purchase the book (recommended),
 * visit http://www.davidflanagan.com/javaexamples2.
 */

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.color.ColorSpace;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.awt.image.ByteLookupTable;
import java.awt.image.ColorConvertOp;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import java.awt.image.LookupOp;
import java.awt.image.RescaleOp;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;

/** A demonstration of various image processing filters */
public class ImageOps extends JPanel{
  static final int WIDTH = 600, HEIGHT = 675// Size of our example

  public String getName() {
    return "Image Processing";
  }

  public int getWidth() {
    return WIDTH;
  }

  public int getHeight() {
    return HEIGHT;
  }

  Image image;

  /** This constructor loads the image we will manipulate */
  public ImageOps() {
    java.net.URL imageurl = this.getClass().getResource("cover.gif");
    image = new javax.swing.ImageIcon(imageurl).getImage();
  }

  // These arrays of bytes are used by the LookupImageOp image filters below
  static byte[] brightenTable = new byte[256];

  static byte[] thresholdTable = new byte[256];
  static // Initialize the arrays
    for (int i = 0; i < 256; i++) {
      brightenTable[i(byte) (Math.sqrt(i / 255.0255);
      thresholdTable[i(byte) ((i < 225: i);
    }
  }

  // This AffineTransform is used by one of the image filters below
  static AffineTransform mirrorTransform;
  static // Create and initialize the AffineTransform
    mirrorTransform = AffineTransform.getTranslateInstance(1270);
    mirrorTransform.scale(-1.01.0)// flip horizontally
  }

  // These are the labels we'll display for each of the filtered images
  static String[] filterNames = new String[] { "Original""Gray Scale",
      "Negative""Brighten (linear)""Brighten (sqrt)""Threshold",
      "Blur""Sharpen""Edge Detect""Mirror""Rotate (center)",
      "Rotate (lower left)" };

  // The following BufferedImageOp image filter objects perform
  // different types of image processing operations.
  static BufferedImageOp[] filters = new BufferedImageOp[] {
      // 1) No filter here. We'll display the original image
      null,
      // 2) Convert to Grayscale color space
      new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY)null),
      // 3) Image negative. Multiply each color value by -1.0 and add 255
      new RescaleOp(-1.0f255fnull),
      // 4) Brighten using a linear formula that increases all color
      // values
      new RescaleOp(1.25f0null),
      // 5) Brighten using the lookup table defined above
      new LookupOp(new ByteLookupTable(0, brightenTable)null),
      // 6) Threshold using the lookup table defined above
      new LookupOp(new ByteLookupTable(0, thresholdTable)null),
      // 7) Blur by "convolving" the image with a matrix
      new ConvolveOp(new Kernel(33new float[] { .1111f.1111f,
          .1111f.1111f.1111f.1111f.1111f.1111f.1111f})),
      // 8) Sharpen by using a different matrix
      new ConvolveOp(new Kernel(33new float[] { 0.0f, -0.75f0.0f,
          -0.75f4.0f, -0.75f0.0f, -0.75f0.0f })),
      // 9) Edge detect using yet another matrix
      new ConvolveOp(new Kernel(33new float[] { 0.0f, -0.75f0.0f,
          -0.75f3.0f, -0.75f0.0f, -0.75f0.0f })),
      // 10) Compute a mirror image using the transform defined above
      new AffineTransformOp(mirrorTransform,
          AffineTransformOp.TYPE_BILINEAR),
      // 11) Rotate the image 180 degrees about its center point
      new AffineTransformOp(AffineTransform.getRotateInstance(Math.PI,
          6495), AffineTransformOp.TYPE_NEAREST_NEIGHBOR),
      // 12) Rotate the image 15 degrees about the bottom left
      new AffineTransformOp(AffineTransform.getRotateInstance(
          Math.PI / 120190),
          AffineTransformOp.TYPE_NEAREST_NEIGHBOR)};

  /** Draw the example */
  public void paint(Graphics g1) {
    Graphics2D g = (Graphics2D)g1;
    // Create a BufferedImage big enough to hold the Image loaded
    // in the constructor. Then copy that image into the new
    // BufferedImage object so that we can process it.
    BufferedImage bimage = new BufferedImage(image.getWidth(this), image
        .getHeight(this), BufferedImage.TYPE_INT_RGB);
    Graphics2D ig = bimage.createGraphics();
    ig.drawImage(image, 00this)// copy the image

    // Set some default graphics attributes
    g.setFont(new Font("SansSerif", Font.BOLD, 12))// 12pt bold text
    g.setColor(Color.green)// Draw in green
    g.translate(1010)// Set some margins

    // Loop through the filters
    for (int i = 0; i < filters.length; i++) {
      // If the filter is null, draw the original image, otherwise,
      // draw the image as processed by the filter
      if (filters[i== null)
        g.drawImage(bimage, 00this);
      else
        g.drawImage(filters[i].filter(bimage, null)00this);
      g.drawString(filterNames[i]0205)// Label the image
      g.translate(1370)// Move over
      if (i % == 3)
        g.translate(-137 4215)// Move down after 4
    }
  }
  public static void main(String[] a) {
    JFrame f = new JFrame();
    f.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
    f.setContentPane(new ImageOps());
    f.pack();
    f.setVisible(true);
  }
}

           
       
Related examples in the same category
1. Image size Image size
2. Image demoImage demo
3. Paint an IconPaint an Icon
4. Image Processing: Brightness and ContrastImage Processing: Brightness and Contrast
5. Image with mouse drag and move eventImage with mouse drag and move event
6. Image Animation and ThreadImage Animation and Thread
7. Image Color Gray EffectImage Color Gray Effect
8. Image BufferingImage Buffering
9. Image Effect: CombineImage Effect: Combine
10. AffineTransform demoAffineTransform demo
11. Image Effect: Rotate Image using DataBufferImage Effect: Rotate Image using DataBuffer
12. Image Effect: Sharpen, blurImage Effect: Sharpen, blur
13. Image scale
14. Image crop
15. Demonstrating the Drawing of an Image with a Convolve Operation
16. Demonstrating Use of the Image I/O Library
17. Adding Image-Dragging Behavior
18. Sending Image Objects through the ClipboardSending Image Objects through the Clipboard
19. Anti AliasAnti Alias
20. Image ViewerImage Viewer
21. Standalone Image Viewer - works with any AWT-supported format
22. Toolkit.getImage() which works the same in either Applet or Application
23. Image Processing Test Image Processing Test
24. Image Transfer TestImage Transfer Test
25. Double Buffered Image
26. Graband Fade: displays image and fades to black
27. Graband Fade with Rasters
28. Rotate Image 45 Degrees
29. Convert java.awt.image.BufferedImage to java.awt.Image
30. Filter image by multiplier its red, green and blue color
31. Drags within the imageDrags within the image
www___.___java___2___s.___c__o__m___ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.