1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.io.File; import java.util.ArrayList; import javax.imageio.ImageIO; import javax.swing.*; public class ImageClipper extends JFrame{ private JButton bopen=null; private JPanel pmain=null; private String fname=null; private JButton bremove=null; private BufferedImage bufimage=null; private Color pixcolor[][]=null; private ArrayList<Point> allpoint=null; private boolean mclicked=false; private Point tempoint=null; private boolean mmoved=false; private MouseHandler mh=null; private Polygon tempoly=null; public ImageClipper(){ super("Background Remover"); setSize(800,600); setLayout(new BorderLayout()); bopen=new JButton("Open"); bopen.addActionListener(new ButtonHandler()); pmain=new WallImage(); //pmain.setPreferredSize(new Dimension(800,500)); mh=new MouseHandler(); pmain.addMouseListener(mh); pmain.addMouseMotionListener(mh); bremove=new JButton("Remove"); bremove.addActionListener(new ButtonHandler()); add("North",bopen); add("Center",pmain); add("South",bremove); allpoint=new ArrayList<Point>(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); show(); } class WallImage extends JPanel{ public void paintComponent(Graphics g){ super.paintComponent(g); Graphics2D g2=(Graphics2D)g; if(bufimage!=null){ g2.drawImage(bufimage,0,0,pmain); } int px[]=null; int py[]=null; if(allpoint.size()>0){ px=new int[allpoint.size()]; py=new int[allpoint.size()]; for(int i=0;i<allpoint.size();i++){ px[i]=(allpoint.get(i)).x; py[i]=(allpoint.get(i)).y; } g2.setColor(Color.YELLOW); g2.drawPolyline(px,py,allpoint.size()); } if(allpoint.size()>0&&mmoved==true){ g2.setColor(Color.YELLOW); g2.drawLine((allpoint.get(allpoint.size()-1)).x,(allpoint.get(allpoint.size()-1)).y,tempoint.x,tempoint.y); } if(allpoint.size()>0&&mclicked==true){ if(allpoint.size()>2&& tempoint.x<=(allpoint.get(0).x)+5&& tempoint.x>=(allpoint.get(0).x)-5&& tempoint.y<=(allpoint.get(0).y)+5&& tempoint.y>=(allpoint.get(0).y)-5){ (allpoint.get(allpoint.size()-1)).x=allpoint.get(0).x; (allpoint.get(allpoint.size()-1)).y=allpoint.get(0).y; g2.drawImage(bufimage,0,0,pmain); px=new int[allpoint.size()]; py=new int[allpoint.size()]; for(int i=0;i<allpoint.size();i++){ px[i]=(allpoint.get(i)).x; py[i]=(allpoint.get(i)).y; } g2.setColor(Color.ORANGE); g2.drawPolygon(px,py,allpoint.size()); tempoly=new Polygon(px,py,allpoint.size()); pmain.removeMouseListener(mh); pmain.removeMouseMotionListener(mh); } else{ g2.drawPolyline(px,py,allpoint.size()); mclicked=false; } } } } class ButtonHandler implements ActionListener{ public void actionPerformed(ActionEvent e){ if(e.getActionCommand()=="Open"){ FileDialog fd=new FileDialog(ImageClipper.this,"Open Image File",FileDialog.LOAD); fd.setDirectory("C:\\"); fd.setVisible(true); fname=fd.getDirectory()+fd.getFile(); try{ bufimage=ImageIO.read(new File(fname)); }catch(Exception ex){ } allpoint.clear(); tempoint=new Point(); mclicked=false; mmoved=false; pmain.addMouseListener(mh); pmain.addMouseMotionListener(mh); pmain.repaint(); } if(e.getActionCommand()=="Remove"){ Graphics2D g2=(Graphics2D)pmain.getGraphics(); g2.setBackground(Color.WHITE); g2.clearRect(0,0,pmain.getWidth(),pmain.getHeight()); g2.setClip(tempoly); g2.drawImage(bufimage,pmain.getX(),pmain.getY(),pmain); } } } class MouseHandler implements MouseMotionListener, MouseListener{ public void mouseMoved(MouseEvent e){ if(!allpoint.isEmpty()){ tempoint=e.getPoint(); mmoved=true; repaint(); } } public void mouseDragged(MouseEvent e){ } public void mouseEntered(MouseEvent e){ } public void mouseExited(MouseEvent e){ } public void mouseReleased(MouseEvent e){ } public void mousePressed(MouseEvent e){ } public void mouseClicked(MouseEvent e){ allpoint.add(e.getPoint()); mclicked=true; repaint(); } } public static void main(String[] args){ new ImageClipper(); } } |
Sunday, April 3, 2016
Image Clipping Tool
This small app removes unwanted background from any image. Two methods in the Graphics class, drawPolyline and drawPolygon are used in this program. This enables the user to clip an arbitrary shape out of an image.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment