Advertisement
Guest User

Untitled

a guest
Feb 21st, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.10 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import javax.swing.event.*;
  5. import java.awt.geom.*;
  6. import java.awt.image.*;
  7. import java.awt.color.*;
  8.  
  9. /** A demonstration of various image processing filters */
  10. public class ImageOps implements GraphSample {
  11.     static final int WIDTH = 600, HEIGHT = 675;         // Size of our example
  12.     public String getName() {return "Image Processing";}// From GraphSample
  13.     public int getWidth() { return WIDTH; }             // From GraphSample
  14.     public int getHeight() { return HEIGHT; }           // From GraphSample
  15.  
  16.     Image image;
  17.  
  18.     /** This constructor loads the image we will manipulate */
  19.     public ImageOps() {
  20.     java.net.URL imageurl = this.getClass().getResource("cover.gif");
  21.     image = new javax.swing.ImageIcon(imageurl).getImage();
  22.     }
  23.    
  24.     // These arrays of bytes are used by the LookupImageOp image filters below
  25.     static byte[] brightenTable = new byte[256];
  26.     static byte[] thresholdTable = new byte[256];
  27.     static {  // Initialize the arrays
  28.     for(int i = 0; i < 256; i++) {
  29.         brightenTable[i] = (byte)(Math.sqrt(i/255.0)*255);
  30.         thresholdTable[i] = (byte)((i < 225)?0:i);
  31.     }
  32.     }
  33.  
  34.     // This AffineTransform is used by one of the image filters below
  35.     static AffineTransform mirrorTransform;
  36.     static {  // Create and initialize the AffineTransform
  37.     mirrorTransform = AffineTransform.getTranslateInstance(127, 0);
  38.     mirrorTransform.scale(-1.0, 1.0);  // flip horizontally
  39.     }
  40.  
  41.     // These are the labels we'll display for each of the filtered images
  42.     static String[] filterNames = new String[] {
  43.     "Original", "Gray Scale",  "Negative",  "Brighten (linear)",
  44.     "Brighten (sqrt)", "Threshold", "Blur", "Sharpen",
  45.     "Edge Detect", "Mirror", "Rotate (center)", "Rotate (lower left)"
  46.     };
  47.  
  48.     // The following BufferedImageOp image filter objects perform
  49.     // different types of image processing operations.
  50.     static BufferedImageOp[] filters = new BufferedImageOp[] {
  51.     // 1) No filter here.  We'll display the original image
  52.     null,
  53.     // 2) Convert to Grayscale color space
  54.     new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null),
  55.     // 3) Image negative.  Multiply each color value by -1.0 and add 255
  56.     new RescaleOp(-1.0f, 255f, null),
  57.     // 4) Brighten using a linear formula that increases all color values
  58.     new RescaleOp(1.25f, 0, null),
  59.     // 5) Brighten using the lookup table defined above
  60.     new LookupOp(new ByteLookupTable(0, brightenTable), null),
  61.     // 6) Threshold using the lookup table defined above
  62.     new LookupOp(new ByteLookupTable(0, thresholdTable), null),
  63.     // 7) Blur by "convolving" the image with a matrix
  64.     new ConvolveOp(new Kernel(3, 3, new float[] {  
  65.         .1111f,.1111f,.1111f,
  66.         .1111f,.1111f,.1111f,
  67.         .1111f,.1111f,.1111f,})),
  68.     // 8) Sharpen by using a different matrix
  69.     new ConvolveOp(new Kernel(3, 3, new float[] {  
  70.         0.0f, -0.75f, 0.0f,
  71.         -0.75f, 4.0f, -0.75f,
  72.         0.0f, -0.75f, 0.0f})),
  73.     // 9) Edge detect using yet another matrix
  74.     new ConvolveOp(new Kernel(3, 3, new float[] {  
  75.         0.0f,  -0.75f, 0.0f,
  76.         -0.75f, 3.0f, -0.75f,
  77.         0.0f,  -0.75f, 0.0f})),
  78.     // 10) Compute a mirror image using the transform defined above
  79.     new AffineTransformOp(mirrorTransform,AffineTransformOp.TYPE_BILINEAR),
  80.     // 11) Rotate the image 180 degrees about its center point
  81.     new AffineTransformOp(AffineTransform.getRotateInstance(Math.PI,64,95),
  82.                   AffineTransformOp.TYPE_NEAREST_NEIGHBOR),
  83.     // 12) Rotate the image 15 degrees about the bottom left
  84.     new AffineTransformOp(AffineTransform.getRotateInstance(Math.PI/12,
  85.                                 0, 190),
  86.                   AffineTransformOp.TYPE_NEAREST_NEIGHBOR),
  87.     };
  88.  
  89.     /** Draw the example */
  90.     public void draw(Graphics2D g, Component c) {
  91.     // Create a BufferedImage big enough to hold the Image loaded
  92.     // in the constructor.  Then copy that image into the new
  93.     // BufferedImage object so that we can process it.
  94.     BufferedImage bimage = new BufferedImage(image.getWidth(c),
  95.                          image.getHeight(c),
  96.                          BufferedImage.TYPE_INT_RGB);
  97.     Graphics2D ig = bimage.createGraphics();
  98.     ig.drawImage(image, 0, 0, c);  // copy the image
  99.  
  100.     // Set some default graphics attributes
  101.     g.setFont(new Font("SansSerif", Font.BOLD, 12));  // 12pt bold text
  102.     g.setColor(Color.green);                          // Draw in green
  103.     g.translate(10, 10);                              // Set some margins
  104.  
  105.     // Loop through the filters
  106.     for(int i = 0; i < filters.length; i++) {
  107.         // If the filter is null, draw the original image, otherwise,
  108.         // draw the image as processed by the filter
  109.         if (filters[i] == null) g.drawImage(bimage, 0, 0, c);
  110.         else g.drawImage(filters[i].filter(bimage, null), 0, 0, c);
  111.         g.drawString(filterNames[i], 0, 205);      // Label the image
  112.         g.translate(137, 0);                       // Move over
  113.         if (i % 4 == 3) g.translate(-137*4, 215);  // Move down after 4
  114.     }
  115.     }
  116. }
  117.  
  118. ////////////////////////////////////////////////////////////////////////////
  119. // Frame
  120. ////////////////////////////////////////////////////////////////////////////
  121. class GraphSampleFrame extends JFrame {
  122.     // The class name of the requested example
  123.     static final String classname = "ImageOps";
  124.     public GraphSampleFrame(final GraphSample[] examples) {
  125.     super("GraphSampleFrame");
  126.  
  127.     Container cpane = getContentPane();   // Set up the frame
  128.     cpane.setLayout(new BorderLayout());
  129.     final JTabbedPane tpane = new JTabbedPane(); // And the tabbed pane
  130.     cpane.add(tpane, BorderLayout.CENTER);
  131.  
  132.     // Add a menubar
  133.     JMenuBar menubar = new JMenuBar();         // Create the menubar
  134.     this.setJMenuBar(menubar);                 // Add it to the frame
  135.     JMenu filemenu = new JMenu("File");        // Create a File menu
  136.     menubar.add(filemenu);                     // Add to the menubar
  137.     JMenuItem quit = new JMenuItem("Quit");    // Create a Quit item
  138.     filemenu.add(quit);                        // Add it to the menu
  139.  
  140.     // Tell the Quit menu item what to do when selected
  141.     quit.addActionListener(new ActionListener() {
  142.         public void actionPerformed(ActionEvent e) { System.exit(0); }
  143.         });
  144.  
  145.     // In addition to the Quit menu item, also handle window close events
  146.     this.addWindowListener(new WindowAdapter() {
  147.         public void windowClosing(WindowEvent e) { System.exit(0); }
  148.         });
  149.  
  150.     // Insert each of the example objects into the tabbed pane
  151.     for(int i = 0; i < examples.length; i++) {
  152.         GraphSample e = examples[i];
  153.         tpane.addTab(e.getName(), new GraphSamplePane(e));
  154.     }
  155.     }
  156.  
  157.     /**
  158.      * This inner class is a custom Swing component that displays
  159.      * a GraphSample object.
  160.      */
  161.     public class GraphSamplePane extends JComponent {
  162.     GraphSample example;  // The example to display
  163.     Dimension size;           // How much space it requires
  164.    
  165.     public GraphSamplePane(GraphSample example) {
  166.         this.example = example;
  167.         size = new Dimension(example.getWidth(), example.getHeight());
  168.             setMaximumSize( size );
  169.     }
  170.  
  171.     /** Draw the component and the example it contains */
  172.     public void paintComponent(Graphics g) {
  173.         g.setColor(Color.white);                    // set the background
  174.         g.fillRect(0, 0, size.width, size.height);  // to white
  175.         g.setColor(Color.black);             // set a default drawing color
  176.         example.draw((Graphics2D) g, this);  // ask example to draw itself
  177.     }
  178.  
  179.     // These methods specify how big the component must be
  180.     public Dimension getPreferredSize() { return size; }
  181.     public Dimension getMinimumSize() { return size; }
  182.     }
  183.  
  184.     public static void main(String[] args) {
  185.     GraphSample[] examples = new GraphSample[1];
  186.  
  187.         // Try to instantiate the named GraphSample class
  188.         try {
  189.         Class exampleClass = Class.forName(classname);
  190.         examples[0] = (GraphSample) exampleClass.newInstance();
  191.         }
  192.         catch (ClassNotFoundException e) {  // unknown class
  193.         System.err.println("Couldn't find example: "  + classname);
  194.         System.exit(1);
  195.         }
  196.         catch (ClassCastException e) {      // wrong type of class
  197.         System.err.println("Class " + classname +
  198.                    " is not a GraphSample");
  199.         System.exit(1);
  200.         }
  201.         catch (Exception e) {  // class doesn't have a public constructor
  202.         // catch InstantiationException, IllegalAccessException
  203.         System.err.println("Couldn't instantiate example: " +
  204.                    classname);
  205.         System.exit(1);
  206.         }
  207.  
  208.     // Now create a window to display the examples in, and make it visible
  209.     GraphSampleFrame f = new GraphSampleFrame(examples);
  210.     f.pack();
  211.     f.setVisible(true);
  212.     }
  213. }
  214. ////////////////////////////////////////////////////////////////////////////
  215. // interface GraphSample
  216. ////////////////////////////////////////////////////////////////////////////
  217. /**
  218.  * This interface defines the methods that must be implemented by an
  219.  * object that is to be displayed by the GraphSampleFrame object
  220.  */
  221. interface GraphSample {
  222.     public String getName();                      // Return the example name
  223.     public int getWidth();                        // Return its width
  224.     public int getHeight();                       // Return its height
  225.     public void draw(Graphics2D g, Component c);  // Draw the example
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement