1 Reply Latest reply: Jan 13, 2014 6:20 AM by mahendra.yadav RSS

Storing images to a remote system connected through lan

bharath512 Newbie
Currently Being Moderated

how to store the images captured from a system using web cam(from where the web application accessed) to a folder of system(where the web application is deployed) connected through lan......?

 

i have used applets to connect to web cam.

 

 

 

 

 

 

 

import java.applet.Applet;

import java.awt.BorderLayout;

import java.awt.Button;

import java.awt.Component;

import java.awt.Dimension;

import java.awt.FlowLayout;

import java.awt.Frame;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Image;

import java.awt.Panel;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.awt.image.BufferedImage;

import java.awt.image.RenderedImage;

import java.io.BufferedReader;

import java.io.DataOutputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.net.HttpURLConnection;

import java.net.MalformedURLException;

import java.net.URL;

import java.io.File;

 

 

import javax.imageio.ImageIO;

import javax.media.Buffer;

import javax.media.CaptureDeviceInfo;

import javax.media.CaptureDeviceManager;

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;

import javax.swing.JApplet;

import javax.swing.JButton;

import javax.swing.JComponent;

import javax.swing.JOptionPane;

 

 

 

 

import com.sun.image.codec.jpeg.JPEGCodec;

import com.sun.image.codec.jpeg.JPEGEncodeParam;

import com.sun.image.codec.jpeg.JPEGImageEncoder;

 

 

 

 

public class Camagain extends Applet implements ActionListener {

 

 

    /**

     *

     */

   

    private static final long serialVersionUID = -196359544994677518L;

    public static Player player;

    public CaptureDeviceInfo DI;

    public MediaLocator ML;

    public JButton Capture;

    public JButton Close;

    public Buffer BUF;

    public Image img;

    public VideoFormat VF;

    public BufferToImage BtoI;

    public ImagePanel imgpanel;

  

 

    public void init(){

       

        setLayout(new FlowLayout());

        imgpanel = new ImagePanel();

        Capture = new JButton("Capture");

        add(Capture);

       

 

 

        Capture.addActionListener(this);

 

 

        Close = new JButton("Close");

        add(Close);

        Close.addActionListener(this);

       

       

 

 

    

        ML = new MediaLocator("vfw:Microsoft WDM Image Capture (Win32):0");

       

    

        try {

            player = Manager.createRealizedPlayer(ML);

            player.start();

            Component comp;

            if ((comp = player.getVisualComponent()) != null) {

                add(comp, BorderLayout.NORTH);

            }

      

          

         

                  

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

   

    public void executeCapture(Camagain cf) {

        Frame f = new Frame("SwingCapture");

       

 

 

        f.addWindowListener(new WindowAdapter() {

 

 

            public void windowClosing(WindowEvent e) {

                playerclose();

                //System.exit(0);

            }

        });

 

 

        f.add("Center", cf);

        f.pack();

        f.setSize(new Dimension(320, 600));

        f.setVisible(true);

    }

    public static void playerclose() {

        player.close();

        JOptionPane.showMessageDialog(null,"close block");

        player.deallocate();

    }

 

 

    public void actionPerformed(ActionEvent e) {

  

        if (e.getSource() instanceof JComponent) {

            JComponent c = (JComponent) e.getSource();

            if (c == Capture) {

                try {

 

  actionCapture();

  } catch (IOException e1) {

  // TODO Auto-generated catch block

  e1.printStackTrace();

  }  // maoved every thing to new method action()

            }else if(c == Close){

               

            playerclose();

 

        try {

            getAppletContext().showDocument(new URL("javascript:appletclose()"));

            }

          catch (MalformedURLException me) { }

 

 

            }

        }

    }

 

 

  

 

 

    public void actionCapture() throws IOException {    // your action handler code.....

        // Grab a frame

        FrameGrabbingControl fgc = (FrameGrabbingControl) player.getControl("javax.media.control.FrameGrabbingControl");

        BUF = fgc.grabFrame();

 

 

        // Convert it to an image

        BtoI = new BufferToImage((VideoFormat) BUF.getFormat());

        img = BtoI.createImage(BUF);

        

 

 

        // show the image

        imgpanel.setImage(img);

 

    

        // saving image

       // String directory=getcodeBase();

       

        String fileName = System.currentTimeMillis()+".jpg";

  String directory=getParameter("contextpath");

      

 

       

        String path =directory +fileName;

 

 

 

  saveJPG(img, path);

      

       

        path = path.replace("\\", "/");

   

        try {

                  playerclose();

           

  

        getAppletContext().showDocument(new URL("javascript:webImageShow(\""+fileName+"\")"));

       

            }

          catch (MalformedURLException me) { }

         

         

    }

    class ImagePanel extends Panel {

 

 

        /**

  *

  */

  private static final long serialVersionUID = 1L;

  public Image myimg = null;

 

 

        public ImagePanel() {

            setLayout(null);

            setSize(320, 240);

        }

 

 

        public void setImage(Image img) {

            this.myimg = img;

            repaint();

        }

 

 

        public void paint(Graphics g) {

            super.paint(g);

            g.drawImage(myimg, 0, 0, this);

        }

    }

 

 

    public static void saveJPG(Image img, String s) {

   BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);

      Graphics2D g2 = bi.createGraphics();

  g2.drawImage(img, null, null);

  FileOutputStream out=null;

        JOptionPane.showMessageDialog(null,s);

      

        try {

     out = new FileOutputStream(s);

  ImageIO.write((RenderedImage) img,"jpg",out);

         } catch (Exception io) {

       

 

            io.printStackTrace();

        }

 

 

 

       /* JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);

        JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);

        param.setQuality(0.5f, false);

        encoder.setJPEGEncodeParam(param);*/

 

 

        try {

            //encoder.encode(bi);

           out.close();

          } catch (java.io.IOException io) {

            System.out.println("IOException");

         }

    }

through this applet i am capturing the image and saving in the application deployed directory and accessing this image through a servlet.every thing works fine in my system but when i try to capture from other systems connected through lan the image not saving in my system.

 

requried solution.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points