Results 1 to 4 of 4
  1. #1
    baobao816 is offline Member
    Join Date
    Feb 2012
    Posts
    6
    Rep Power
    0

    Default Webcam Images and Transparency

    Is that anyone know how to remove the images background to transparency??
    The ideas I use is after click capture the images had been taken will just focus on the people faces and all the background will be transparent.

    I had been success create a webcam code and display the images but I failed to remove the images background~~is that anyone can help me and gave me some hint???

    How to turn the background to transparent???

    Java Code:
    import javax.swing.*;
    import javax.swing.JFrame;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.media.Buffer;
    import javax.media.Manager;
    import javax.media.MediaLocator;
    import javax.media.Player;
    import javax.media.control.FrameGrabbingControl;
    import javax.media.format.VideoFormat;
    import javax.media.util.BufferToImage;
    
    public class Webcaming extends JFrame {
    	private JPanel display = null;
    	private JButton capturing = new JButton("Images Capture");
    	private Player player = null;
    	private Buffer buf = null;
    	private Image img = null;
    	private BufferToImage btoi = null;
    		
    	public Webcaming(){
    		webCamComponent();
    	}//end WebCamTest()
    
    private void webCamComponent(){
    			setLayout(new GridLayout(0,3,5,5));
                JFrame jfmDisplay = new JFrame("Display");
                JPanel displaying = new JPanel();
                Container details = jfmDisplay.getContentPane();
                jfmDisplay.setSize(1000,1000);
                details.add(displaying);
                jfmDisplay.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                
                JPanel show = new JPanel();
                show.setSize(400, 400);
                display = new JPanel();
                show.add(display, BorderLayout.CENTER);
                add(show, BorderLayout.CENTER);
    
                getContentPane().add(display, BorderLayout.CENTER);
    		
                JPanel buttonCap = new JPanel();
                //buttonCap.setLayout(new GridLayout(0,3,5,5));
                buttonCap.add(capturing, BorderLayout.NORTH);
                add(buttonCap, BorderLayout.CENTER);
                
                capturing.addActionListener(new ActionListener(){
                    public void actionPerformed(ActionEvent capBtn){
                        jButtonCapActionPerformed(capBtn);    
                    }
                });
    
                pack();
                setVisible(true);
    	}//end webCamComponent
    	
    	private void jButtonCapActionPerformed(ActionEvent capBtn){
    		if(capBtn.getActionCommand().equals("Images Capture")){
    			JFrame jfmWebcam = new JFrame("Webcam");
            	jfmWebcam.setSize(320, 320);
            	JButton capture = new JButton("Capture");
                jfmWebcam.add(capture, BorderLayout.NORTH);
    
                //Button Event
                capture.addActionListener(new ActionListener(){
    				public void actionPerformed(ActionEvent captureBtn){
                        jButtonCaptureActionPerformed(captureBtn);		
                    }//end actionPerformed
                   });
    
    			try{
    				MediaLocator webcamera = new MediaLocator("vfw://0");
                    player = Manager.createRealizedPlayer(webcamera);
    
                    if(player.getVisualComponent()!=null){
                            jfmWebcam.getContentPane().add(player.getVisualComponent());
                    }//end if
    
                    player.start();
                    
                    jfmWebcam.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
                    jfmWebcam.pack();
                    jfmWebcam.setVisible(true);
                }//end try
                catch (Exception ex){
                        JOptionPane.showMessageDialog(null, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                }//end catch			
    		}//end if	
    	}//end jButtonCapActionPerformed
    	
    	private void jButtonCaptureActionPerformed(ActionEvent captureBtn){
    		if(captureBtn.getActionCommand().equals("Capture")){
    			if(player != null){
    				FrameGrabbingControl fgcFrame = (FrameGrabbingControl) player.getControl("javax.media.control.FrameGrabbingControl");
    				buf = fgcFrame.grabFrame();
    				
    				//convert it to an image
    				btoi = new BufferToImage((VideoFormat)buf.getFormat());
    				img = btoi.createImage(buf);
    				
    				img = new ImageIcon(img).getImage();
    				//display.setIconImage(img);
    			}//end if
    			
    		}//end if		
    	}//end jButtonCaptureActionPerformed	
    	
    	public void paint(Graphics g){
    		g.drawImage(img, 0, 0, null);
    		g.dispose();
    		
    	}
    	public static void main(String[] args) {
    		Webcaming wct = new Webcaming();
    	}//end main
    	
    }//end class

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,842
    Rep Power
    19

    Default Re: Webcam Images and Transparency

    So you've captured the image, and have you identified which parts of the image you want to keep?
    If so, and the image type allows for Alpha then set the alpha for all pixels that aren't your face.

  3. #3
    baobao816 is offline Member
    Join Date
    Feb 2012
    Posts
    6
    Rep Power
    0

    Default Re: Webcam Images and Transparency

    emmm~~any example??

    I have captured the image but how shd I identified the part of the image???

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,842
    Rep Power
    19

    Default Re: Webcam Images and Transparency

    Oh.
    Read up on facial recognition?
    Google might throw up some useable packages for Java.

Similar Threads

  1. Transparency
    By zirbinator in forum Java 2D
    Replies: 7
    Last Post: 03-28-2012, 05:51 AM
  2. DrawImage with transparency ?
    By mrhid6 in forum AWT / Swing
    Replies: 2
    Last Post: 10-01-2011, 11:58 PM
  3. Filling with transparency
    By zirbinator in forum Java 2D
    Replies: 3
    Last Post: 02-09-2011, 02:36 AM
  4. ImageIcon Transparency
    By Lingerz in forum New To Java
    Replies: 2
    Last Post: 06-04-2010, 02:53 PM
  5. capture webcam images with java
    By MuslimCoder in forum Advanced Java
    Replies: 3
    Last Post: 12-21-2009, 03:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •