Coding Lab - TechOnTechnology

Image Processing using Java




Program :

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Dialog;
import java.awt.image.DataBufferByte;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.ByteArrayOutputStream;


class Global {
public static int flag=0;
public static Image image=null;
public static BufferedImage bimage=null;
public static BufferedImage insertimage=null;
public static int imageinsertstatus=0;
}

public class ImageProcessing{
public static void main(String[] args) throws IOException{

final JFrame f = new JFrame("Image Processing");
f.setVisible(true);
f.setSize(700,700);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel mainPanel=new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));

final JLabel p=new JLabel();
p.setPreferredSize(new Dimension(600, 600));
p.setAlignmentX(Component.CENTER_ALIGNMENT);
mainPanel.add(p);


f.addMouseListener(new MouseListener() {
    @Override
    public void mouseClicked(MouseEvent e) {
        if( Global.insertimage != null)
        {
                ImageIcon icon = (ImageIcon)p.getIcon();
                Image img = (Image) icon.getImage();
                BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
                Graphics2D bGr = bimage.createGraphics();
                bGr.drawImage(img, 0, 0, null);
                bGr.dispose();
                BufferedImage sourceImage = bimage;
                BufferedImage overlayImage = Global.insertimage;
                Graphics2D g2Source = sourceImage.createGraphics();
                g2Source.drawImage(overlayImage, e.getX()-20, e.getY()-26,null);
                g2Source.dispose(); 
                p.setIcon(new ImageIcon(sourceImage));
                Global.insertimage = null;
        }
     }
    @Override
    public void mouseExited(MouseEvent e) {}
    @Override
    public void mouseEntered(MouseEvent e) {}
    @Override
    public void mouseReleased(MouseEvent e) {}
    @Override
    public void mousePressed(MouseEvent e) {}
});

mainPanel.add(Box.createRigidArea(new Dimension(0, 10)));

JPanel buttonPane = new JPanel();
buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.X_AXIS));
buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));


JButton openBtn = new JButton("Load Picture");
buttonPane.add(openBtn);

openBtn.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0)
        {
                Global.flag=0;
                Global.image=null;
                Global.bimage=null;
                Global.insertimage=null;
                Global.imageinsertstatus=0;
                JFileChooser fc = new JFileChooser();
                BufferedImage image=null;
                Image dimg=null;
                fc.setCurrentDirectory(new File("."));
                int retVal = fc.showOpenDialog(null);
                if (retVal == 0)
                {
                    File file = fc.getSelectedFile();
                    try { image = ImageIO.read(file); dimg= image.getScaledInstance(600, 600,Image.SCALE_SMOOTH); Global.image=dimg;Global.bimage=image; }
                    catch (IOException e1) { e1.printStackTrace(); }
                }
                p.setIcon(new ImageIcon(dimg));

        }
});

buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
JButton svBtn = new JButton("Save Picture");
buttonPane.add(svBtn);

svBtn.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0)
        {
        if( Global.image != null)
        {
                JFileChooser fc = new JFileChooser();
                int returnVal = fc.showSaveDialog(null);
                if (returnVal == JFileChooser.APPROVE_OPTION)
                {
                File file = fc.getSelectedFile();
                ImageIcon icon = (ImageIcon)p.getIcon();
                Image img = (Image) icon.getImage();   
                BufferedImage bimg = new BufferedImage(img.getWidth(null), img.getHeight(null),BufferedImage.TYPE_INT_RGB);
                Graphics2D gg = bimg.createGraphics();
                gg.drawImage(img, 0, 0, null);
                try { ImageIO.write(bimg, "jpg", file); JOptionPane.showMessageDialog(f,"File Saved","Message",JOptionPane.PLAIN_MESSAGE);}
                catch (IOException e1) { e1.printStackTrace(); }
                }
        }
        else
        {
        JOptionPane.showMessageDialog(f,"Image Not Loaded..","Message",JOptionPane.PLAIN_MESSAGE);
        }
    }
});

buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
JButton CBtn = new JButton("Change Colour");
Global.flag=0;
buttonPane.add(CBtn);
CBtn.addActionListener(new ActionListener() {
     @Override
     public void actionPerformed(ActionEvent arg0) {
         if( Global.image != null)
        {
            if (Global.flag == 0 ) {
                ImageIcon icon = (ImageIcon)p.getIcon();
                Image img = (Image) icon.getImage();   
                BufferedImage bimg = new BufferedImage(img.getWidth(null), img.getHeight(null),BufferedImage.TYPE_BYTE_GRAY);
                Graphics2D gg = bimg.createGraphics();
                gg.drawImage(img, 0, 0, null);
                p.setIcon(new ImageIcon(bimg));
                Global.flag = 1;
            } else {
                p.setIcon(new ImageIcon(Global.image));
                Global.flag = 0;
            }
        }
        else
        {
            JOptionPane.showMessageDialog(f,"Image Not Loaded..","Message",JOptionPane.PLAIN_MESSAGE);
        }
    }
});
mainPanel.add(buttonPane);

buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));

JButton GetBin = new JButton("Get Binary");
buttonPane.add(GetBin);
GetBin.addActionListener(new ActionListener() {
     @Override
     public void actionPerformed(ActionEvent arg0)
     {
         if( Global.image != null)
        {
            JFrame jf= new JFrame("Image in Binary");
            JTextArea comp = new JTextArea();
            JScrollPane sp=new JScrollPane(comp);
            comp.setBounds(20,20,200,50);
            //comp.setLineWrap(true);     //Gets Slow - For Horizontal
            jf.add(new JScrollPane(comp));
            jf.setSize(400,200);
            jf.setVisible(true);
            ImageIcon icon = (ImageIcon)p.getIcon();
            Image img = (Image) icon.getImage();
            BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
            Graphics2D bGr = bimage.createGraphics();
            bGr.drawImage(img, 0, 0, null);
            bGr.dispose();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] imageInByte=null;
            try {
                ImageIO.write( bimage, "jpg", baos );
                baos.flush();
                imageInByte = baos.toByteArray();
                baos.close();
                }
            catch (IOException e1) { e1.printStackTrace(); }
            StringBuilder sb = new StringBuilder(imageInByte.length * Byte.SIZE);
            for( int i = 0; i < Byte.SIZE * imageInByte.length; i++ )
            sb.append((imageInByte[i / Byte.SIZE] << i % Byte.SIZE & 0x80) == 0 ? '0' : '1');
            comp.append(""+sb);
        }
        else
        {
            JOptionPane.showMessageDialog(f,"Image Not Loaded..","Message",JOptionPane.PLAIN_MESSAGE);
        }
    }
});

buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
JButton IBtn = new JButton("Insert Snap");
Global.imageinsertstatus =0;
buttonPane.add(IBtn);
  IBtn.addActionListener(new ActionListener() {
     @Override
     public void actionPerformed(ActionEvent arg0) {
        if( Global.image != null)
        {
            JFileChooser fc = new JFileChooser();
            BufferedImage image=null;
            Image dimg=null;
                fc.setCurrentDirectory(new File("."));
                int retVal = fc.showOpenDialog(null);
                if (retVal == 0)
                {
                    File file = fc.getSelectedFile();
                    try { image = ImageIO.read(file); dimg= image.getScaledInstance(600, 600,Image.SCALE_SMOOTH); Global.insertimage=image;}
                    catch (IOException e1) { e1.printStackTrace(); }
                    JOptionPane.showMessageDialog(f,"Please Select Position,  where to insert Image","Message",JOptionPane.PLAIN_MESSAGE);
                    Global.imageinsertstatus = 0;
                }
        }
        else
        {
            JOptionPane.showMessageDialog(f,"Image Not Loaded..","Message",JOptionPane.PLAIN_MESSAGE);
        }
       }
  });

mainPanel.add(buttonPane);

buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
JButton EBtn = new JButton("Exit");
buttonPane.add(EBtn);
EBtn.addActionListener(new ActionListener() {
     @Override
     public void actionPerformed(ActionEvent arg0) {
        System.exit(0);
       }
  });

buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
mainPanel.add(buttonPane);
mainPanel.add(Box.createRigidArea(new Dimension(0, 10)));
f.add(mainPanel);
f.pack();
}
}

 

Name

Email *

Message *