Not written by me and forgot what website i found this (probably the sun site) but still wanted to share the code for those that want to use java in combination with a webcam + saving the image to jpeg. I haven't tested the code myself though because i don't have the JMF package installed but if you have any trouble running the code let me know and i'll try to help you out.
GeSHi (java):
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import javax.media.*;
import javax.media.format.*;
import javax.media.util.*;
import javax.media.control.*;
import javax.media.protocol.*;
import java.util.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import com.sun.image.codec.jpeg.*;
{
public static Player player = null;
public CaptureDeviceInfo di = null;
public MediaLocator ml = null;
public Buffer buf = null;
public VideoFormat vf = null;
public BufferToImage btoi = null;
public ImagePanel imgpanel = null;
public SwingCapture()
{
setSize(320,550);
imgpanel = new ImagePanel();
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();
if ((comp = player.getVisualComponent()) != null)
{
}
}
{
e.printStackTrace();
}
}
public static void main
(String[] args
) {
SwingCapture cf = new SwingCapture();
playerclose();
f.add("Center",cf);
f.pack();
f.setVisible(true);
}
public static void playerclose()
{
player.close();
player.deallocate();
}
{
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();
}
{
if (myimg != null)
{
g.drawImage(myimg, 0, 0, this);
}
}
}
{
g2.drawImage(img, null, null);
try
{
}
{
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();
}
{
System.
out.
println("IOException");
}
}
}
Created by GeSHI 1.0.7.20
Logitech QuickCam.
GeSHi (java):
public static boolean PassportPhotoOpen = false;
...
// program sample menu item creation
String empID = nText.
employeeID;
// derived from tree node String empName = nText.
employeeName;
createPhotoFrame(empID,empName);
}
});
popup.add(mItem);
...
// routine that is executed above that creates the photo capture internal frame
protected void createPhotoFrame
(String empID,
String empName
) { // kill photo capture frame if it exists
if (PassportPhotoOpen){
PassportPhoto.playerclose(); // rather use the reference?
photoFrameReference.dispose();
PassportPhotoOpen = false;
}
// create and display frame
PassportPhoto frame = new PassportPhoto(empID, empName);
frame.setVisible(true);
ImageIcon frameIcon =
new ImageIcon(Passport.
class.
getClassLoader().
getResource("images/SmallWIM.gif"));
frame.setFrameIcon(frameIcon);
PassportPhotoOpen = true;
photoFrameReference = frame;
frame.setRequestFocusEnabled(true);
try {frame.setSelected(true);}
frame.toFront();
}
...
// modified photo capture code
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import javax.media.*;
import javax.media.format.*;
import javax.media.util.*;
import javax.media.control.*;
import javax.media.protocol.*;
import java.util.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import com.sun.image.codec.jpeg.*;
public static Player player = null;
public CaptureDeviceInfo di = null;
public MediaLocator ml = null;
public Buffer buf = null;
public VideoFormat vf = null;
public BufferToImage btoi = null;
public ImagePanel imgpanel = null;
super("Photo Capture " +empName, false, true, false, true);
setDefaultCloseOperation
(WindowConstants.
DO_NOTHING_ON_CLOSE);
// exit control EmployeeTree.PassportPhotoOpen = false;
playerclose();
dispose();
}
});
capture.
setMinimumSize(new Dimension(79,
26));
capture.
setMaximumSize(new Dimension(79,
26));
capture.addActionListener(this);
imgpanel = new ImagePanel();
//String str1 = "vfw:Logitech USB Video Camera:0";
//String str2 = "vfw:Microsoft WDM Image Capture (Win32):0";
//di = CaptureDeviceManager.getDevice(str1);
//ml = di.getLocator();
// remove above and substitute below
ml = new MediaLocator("vfw://0");
try {
Manager.
setHint(Manager.
LIGHTWEIGHT_RENDERER,
new Boolean(true));
player = Manager.createRealizedPlayer(ml);
player.start();
if ((comp = player.getVisualComponent()) != null) {
videoPanel.
setMinimumSize(new Dimension(320,
240));
videoPanel.
setMaximumSize(new Dimension(320,
240));
}
else {
// actually will warn user, log error and exit here
}
}
// actually will warn user, log error and exit here
e.printStackTrace();
}
/******************************/
/* assemble the final screen */
/******************************/
setSize(730, 275); // window size
setLocation(30, 30); // window location
}
public static void playerclose() {
player.close();
player.deallocate();
}
if (c == capture) {
// Grab the frame
FrameGrabbingControl fgc = (FrameGrabbingControl) player.getControl("javax.media.control.FrameGrabbingControl");
buf = fgc.grabFrame();
btoi = new BufferToImage((VideoFormat)buf.getFormat()); // Convert to image
img = btoi.createImage(buf);
imgpanel.setImage(img); // display the image
// actually will have a separate save routine
saveJPG(img,"c:\\test.jpg"); // save image
}
}
class ImagePanel
extends JPanel { public Image myimg =
null;
public ImagePanel() {
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);
}} }
g2.drawImage(img, null, null);
// again actually will warn user, log error and exit here
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();
}
// again actually will warn user, log error and exit here
System.
out.
println("IOException");
}
}
}
Created by GeSHI 1.0.7.20