tusharjoshi

Image convert to Grayscale

Feb 11th, 2014
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.63 KB | None | 0 0
  1. package javaapplication6;
  2.  
  3. import java.awt.image.BufferedImage;
  4. import java.awt.image.ColorConvertOp;
  5. import java.io.IOException;
  6. import javax.imageio.ImageIO;
  7. import javax.swing.ImageIcon;
  8. import javax.swing.JFileChooser;
  9.  
  10. /**
  11.  *
  12.  * @author tusharjoshi
  13.  */
  14. public class MainWindow extends javax.swing.JFrame {
  15.    
  16.     /**
  17.      * Creates new form MainWindow
  18.      */
  19.     public MainWindow() {
  20.         initComponents();
  21.     }
  22.  
  23.     /**
  24.      * This method is called from within the constructor to initialize the form.
  25.      * WARNING: Do NOT modify this code. The content of this method is always
  26.      * regenerated by the Form Editor.
  27.      */
  28.     @SuppressWarnings("unchecked")
  29.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  30.     private void initComponents() {
  31.  
  32.         imageLabel = new javax.swing.JLabel();
  33.         chooseButton = new javax.swing.JButton();
  34.  
  35.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  36.  
  37.         chooseButton.setText("Choose");
  38.         chooseButton.setActionCommand("Choose");
  39.         chooseButton.addActionListener(new java.awt.event.ActionListener() {
  40.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  41.                 chooseButtonActionPerformed(evt);
  42.             }
  43.         });
  44.  
  45.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  46.         getContentPane().setLayout(layout);
  47.         layout.setHorizontalGroup(
  48.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  49.             .addGroup(layout.createSequentialGroup()
  50.                 .addGap(19, 19, 19)
  51.                 .addComponent(imageLabel)
  52.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  53.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  54.                 .addContainerGap(303, Short.MAX_VALUE)
  55.                 .addComponent(chooseButton)
  56.                 .addContainerGap())
  57.         );
  58.         layout.setVerticalGroup(
  59.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  60.             .addGroup(layout.createSequentialGroup()
  61.                 .addContainerGap()
  62.                 .addComponent(imageLabel)
  63.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 259, Short.MAX_VALUE)
  64.                 .addComponent(chooseButton)
  65.                 .addContainerGap())
  66.         );
  67.  
  68.         pack();
  69.     }// </editor-fold>                        
  70.  
  71.     private void chooseButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
  72.         // TODO add your handling code here:
  73.         JFileChooser fileChooser = new JFileChooser();
  74.         fileChooser.setDialogTitle("Open image?");
  75.         fileChooser.showOpenDialog(this);
  76.         try {
  77.             //writes new file
  78.             BufferedImage image = ImageIO.read(fileChooser.getSelectedFile());
  79.            
  80.             ImageIcon icon = new ImageIcon(image);
  81.             imageLabel.setIcon(icon);
  82.            
  83.             BufferedImage image2 = new BufferedImage(image.getWidth(),
  84.                     image.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
  85.  
  86.             // convert colored image to grayscale
  87.             ColorConvertOp grayScale = new ColorConvertOp(image.getColorModel().getColorSpace(),image2.getColorModel().getColorSpace(),null);
  88.             grayScale.filter(image,image2);
  89.             imageLabel.setIcon(new ImageIcon(image2));
  90.         }
  91.         catch (IOException f) {
  92.             System.out.println("Saving failed! Could not save image.");
  93.         }
  94.        
  95.     }                                            
  96.  
  97.     /**
  98.      * @param args the command line arguments
  99.      */
  100.     public static void main(String args[]) {
  101.         /* Set the Nimbus look and feel */
  102.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  103.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  104.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  105.          */
  106.         try {
  107.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  108.                 if ("Nimbus".equals(info.getName())) {
  109.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  110.                     break;
  111.                 }
  112.             }
  113.         } catch (ClassNotFoundException ex) {
  114.             java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  115.         } catch (InstantiationException ex) {
  116.             java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  117.         } catch (IllegalAccessException ex) {
  118.             java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  119.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  120.             java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  121.         }
  122.         //</editor-fold>
  123.  
  124.         /* Create and display the form */
  125.         java.awt.EventQueue.invokeLater(new Runnable() {
  126.             public void run() {
  127.                 new MainWindow().setVisible(true);
  128.             }
  129.         });
  130.     }
  131.  
  132.     // Variables declaration - do not modify                    
  133.     private javax.swing.JButton chooseButton;
  134.     private javax.swing.JLabel imageLabel;
  135.     // End of variables declaration                  
  136. }
Add Comment
Please, Sign In to add comment