Our Feeds

Saturday, 7 March 2015

AJITH KP

Add Graphical Component to Image using Java

          Hi guys... It is very simple program to parse an image and add new graphical components to image. Here I used Graphics2D package to add a rectangle to highlight face... Hope you enjoy this tutorial and share it with your friends.



VIDEO TUTORIAL


SOURCE CODE


import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class MarkImage extends JFrame {
    JButton btn = new JButton("Open File");
    JPanel imagePanel = new JPanel();
    JPanel menuPanel = new JPanel();
    double h, w;
    MarkImage()//Constructor
    {
        super("Mark Image -- Coded By Ajith Kp [ ajithkp560 ]");
        btn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser fc = new JFileChooser();
                int ret = fc.showOpenDialog(MarkImage.this);
                if(ret==JFileChooser.APPROVE_OPTION)
                {
                    File imgf = fc.getSelectedFile();
                    imagePanel.removeAll();
                    try {
                        setPic(imgf);
                    } catch (IOException ex) {
                    }
                }
            }
        });
        menuPanel.add(btn);
        add(menuPanel, BorderLayout.NORTH);
        add(imagePanel, BorderLayout.CENTER);
    }
    
    public void setPic(File imgf) throws IOException
    {
        BufferedImage myImg = ImageIO.read(imgf);
        double x = myImg.getWidth();
        double y = myImg.getHeight();
        h = imagePanel.getHeight();
        w= imagePanel.getWidth();
        if(x>w || y>h)
        {
            double ox = w/x, oy = h/y;
            System.out.println(ox+" "+oy);
            if(ox<oy)
            {
                x = x*ox;
                y = y*ox;
            }
            else
            {
                x = x*oy;
                y = y*oy;
            }
        }
        
        Graphics2D g2 = myImg.createGraphics();
        g2.setColor(Color.red);
        g2.setStroke(new BasicStroke(20));
        g2.drawRect(650, 220, 600, 600);
        
        Image newimg = myImg.getScaledInstance((int)x, (int)y,  Image.SCALE_SMOOTH);
        JLabel picLabel = new JLabel(new ImageIcon(newimg));
        imagePanel.add(picLabel);
        imagePanel.revalidate();
        imagePanel.repaint();
    }
    
    public static void main(String[] args) {
        MarkImage dn = new MarkImage();
        dn.setVisible(true);
        dn.setSize(1200, 800);
        dn.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    
}