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 | import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.io.File; import javax.imageio.ImageIO; import javax.swing.*; public class BackgroundRemover extends JFrame{ private JButton bopen=null; private JPanel pmain=null; private String fname=null; private JButton bstart=null; private BufferedImage bufimage=null; private BufferedImage grayimage=null; private Color pixcolor[][]=null; private Point pstart=null; private Point pend=null; public BackgroundRemover(){ super("Background Remover"); setSize(800,600); setLayout(new BorderLayout()); bopen=new JButton("Open"); bopen.addActionListener(new ButtonHandler()); pmain=new WallImage(); pmain.addMouseListener(new BackgroundRemover.MouseHandler()); pmain.addMouseMotionListener(new BackgroundRemover.MouseHandler()); bstart=new JButton("Remove"); bstart.addActionListener(new ButtonHandler()); add("North",bopen); add("Center",pmain); add("South",bstart); 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); } if(pstart!=null&&pend!=null){ g2.setColor(Color.YELLOW); g2.drawRect(pstart.x,pstart.y,pend.x-pstart.x,pend.y-pstart.y); } } } class ButtonHandler implements ActionListener{ public void actionPerformed(ActionEvent e){ if(e.getActionCommand()=="Open"){ FileDialog fd=new FileDialog(BackgroundRemover.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){ } pstart=null; pend=null; pmain.repaint(); } if(e.getActionCommand()=="Remove"){ int widthRect=pend.x-pstart.x; int heightRect=pend.y-pstart.y; pixcolor=new Color[widthRect][heightRect]; //Graphics2D g2=(Graphics2D)bufimage.getGraphics(); for(int i=0;i<widthRect;i++){ for(int j=0;j<heightRect;j++){ pixcolor[i][j]=new Color(bufimage.getRGB(pstart.x+i,pstart.y+j)); } } int widthImg=bufimage.getWidth(); int heightImg=bufimage.getHeight(); Graphics2D g2=(Graphics2D)bufimage.getGraphics(); for(int i=0;i<heightImg;i++){ for(int j=0;j<widthImg;j++){ Color c=new Color(bufimage.getRGB(j,i)); for(int a=0;a<widthRect;a++){ for(int b=0;b<heightRect;b++){ if(c.equals(pixcolor[a][b])){ bufimage.setRGB(j,i,new Color(255,255,255).getRGB()); } } } } } pixcolor=null; repaint(); } } } class MouseHandler implements MouseMotionListener, MouseListener{ public void mouseMoved(MouseEvent e){ } public void mouseDragged(MouseEvent e){ pend = e.getPoint(); repaint(); } public void mouseEntered(MouseEvent e){ } public void mouseExited(MouseEvent e){ } public void mouseReleased(MouseEvent e){ repaint(); } public void mousePressed(MouseEvent e){ pstart=e.getPoint(); } public void mouseClicked(MouseEvent e){ } } public static void main(String[] args){ new BackgroundRemover(); } } |
Thursday, March 31, 2016
Image Background Remover
As the name implies, this program removes the background from any image or photo. Unwanted colors are first selected by drawing rectangle on the main panel using mouse drag event. The removal process is then performed through the use of the getRGB() and setRGB() methods.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment