Hack Forums
[Java] Take a photo from a webcam - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Java] Take a photo from a webcam (/showthread.php?tid=5966656)



Take a photo from a webcam - Nemo2478 - May 4, 2019

Hi all,

in case anyone wants to know how to take the photo I leave this code (which is not mine just in case) in order to contribute something :D

First you must download and install the JMF.

Then you can execute the code:
Code
import javax.swing.*;
import java.io.*;
import javax.media.*;
import javax.media.format.*;
import javax.media.util.*;
import javax.media.control.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import com.sun.image.codec.jpeg.*;

public class SwingCapture extends Panel implements ActionListener
{
 public static Player player = null;
 public CaptureDeviceInfo di = null;
 public MediaLocator ml = null;
 public JButton capture = null;
 public Buffer buf = null;
 public Image img = null;
 public VideoFormat vf = null;
 public BufferToImage btoi = null;
 public ImagePanel imgpanel = null;

 public SwingCapture()
 {
   setLayout(new BorderLayout());
   setSize(320,550);

   imgpanel = new ImagePanel();
   capture = new JButton("Capture");
   capture.addActionListener(this);

   String str1 = "vfw:Logitech USB Video Camera:0";
   String str2 = "vfw:Microsoft WDM Image Capture (Win32):0";
   di = CaptureDeviceManager.getDevice(str2);
   ml = di.getLocator();

   try
   {
     player = Manager.createRealizedPlayer(ml);
     player.start();
     Component comp;

     if ((comp = player.getVisualComponent()) != null)
     {
       add(comp,BorderLayout.NORTH);
     }
     add(capture,BorderLayout.CENTER);
     add(imgpanel,BorderLayout.SOUTH);
   }
   catch (Exception e)
   {
     e.printStackTrace();
   }
 }

 public static void main(String[] args)
 {
   Frame f = new Frame("SwingCapture");
   SwingCapture cf = new 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,550));
   f.setVisible(true);  

 }


 public static void playerclose()
 {
   player.close();
   player.deallocate();
 }

 public void actionPerformed(ActionEvent e)
 {
   JComponent c = (JComponent) e.getSource();

   if (c == capture)
   {
     // 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);

     // save image
     saveJPG(img,"c:\\test.jpg");
   }
 }

 class ImagePanel extends Panel
 {
   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)
   {
     if (myimg != null)
     {
       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;
   try
   {
     out = new FileOutputStream(s);
   }
   catch (java.io.FileNotFoundException io)
   {
     System.out.println("File Not Found");
   }

   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");
   }
 }

}

Oh and if you want to know the name of your device or webcam here I leave a method that shows them.
Code
public void devices(){
   Vector devices = new Vector (CaptureDeviceManager.getDeviceList(null));
   for(int i=0;i<devices.size();i++)
   System.out.println(devices.elementAt(i));
 }

Regards

PS.: I would like that you add my post to the post where the Java source codes are located.


RE: Take a photo from a webcam - .C1PH3R. - May 5, 2019

(May 4, 2019 12:33 PM)Nemo2478 Wrote:  Hi all,

in case anyone wants to know how to take the photo I leave this code (which is not mine just in case) in order to contribute something :D

First you must download and install the JMF.

Then you can execute the code:
Code
import javax.swing.*;
import java.io.*;
import javax.media.*;
import javax.media.format.*;
import javax.media.util.*;
import javax.media.control.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import com.sun.image.codec.jpeg.*;

public class SwingCapture extends Panel implements ActionListener
{
 public static Player player = null;
 public CaptureDeviceInfo di = null;
 public MediaLocator ml = null;
 public JButton capture = null;
 public Buffer buf = null;
 public Image img = null;
 public VideoFormat vf = null;
 public BufferToImage btoi = null;
 public ImagePanel imgpanel = null;

 public SwingCapture()
 {
   setLayout(new BorderLayout());
   setSize(320,550);

   imgpanel = new ImagePanel();
   capture = new JButton("Capture");
   capture.addActionListener(this);

   String str1 = "vfw:Logitech USB Video Camera:0";
   String str2 = "vfw:Microsoft WDM Image Capture (Win32):0";
   di = CaptureDeviceManager.getDevice(str2);
   ml = di.getLocator();

   try
   {
     player = Manager.createRealizedPlayer(ml);
     player.start();
     Component comp;

     if ((comp = player.getVisualComponent()) != null)
     {
       add(comp,BorderLayout.NORTH);
     }
     add(capture,BorderLayout.CENTER);
     add(imgpanel,BorderLayout.SOUTH);
   }
   catch (Exception e)
   {
     e.printStackTrace();
   }
 }

 public static void main(String[] args)
 {
   Frame f = new Frame("SwingCapture");
   SwingCapture cf = new 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,550));
   f.setVisible(true);  

 }


 public static void playerclose()
 {
   player.close();
   player.deallocate();
 }

 public void actionPerformed(ActionEvent e)
 {
   JComponent c = (JComponent) e.getSource();

   if (c == capture)
   {
     // 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);

     // save image
     saveJPG(img,"c:\\test.jpg");
   }
 }

 class ImagePanel extends Panel
 {
   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)
   {
     if (myimg != null)
     {
       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;
   try
   {
     out = new FileOutputStream(s);
   }
   catch (java.io.FileNotFoundException io)
   {
     System.out.println("File Not Found");
   }

   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");
   }
 }

}

Oh and if you want to know the name of your device or webcam here I leave a method that shows them.
Code
public void devices(){
   Vector devices = new Vector (CaptureDeviceManager.getDeviceList(null));
   for(int i=0;i<devices.size();i++)
   System.out.println(devices.elementAt(i));
 }

Regards

PS.: I would like that you add my post to the post where the Java source codes are located.

Nice share OP
But I think most people don't go for java when it comes to code malware. Java needs JVM/JRE for executing .jar and ofcourse there is a work around this like create a native executable from your code.


RE: Take a photo from a webcam - HFDone - May 5, 2019

JavaCV seems to be much simple https://stackoverflow.com/a/9046373
JMF vs JavaCV https://softwareengineering.stackexchange.com/a/176873