JMF - Webcam settings - Resolution, Gain, Brightness, Contrast

D

Desk-of-David

Hi,

I am fairly new to Java, and am working on a project in which I need
to grab a still-frame from a webcam for further processing.

The code below works just fine for getting the shot taken and saved.
However, I want to change from camera setting "0" to camera setting
"1":

0 RGB, 320x240, Length=230400, 24-bit, Masks=3:2:1, PixelStride=3,
LineStride=960, Flipped
1 RGB, 160x120, Length=57600, 24-bit, Masks=3:2:1, PixelStride=3,
LineStride=480, Flipped


Question 1 - How does one change the camera setting to the 160 x 120
setting

Question 2 - Is there a way to change or control the camera settings
like Gain, Brightness and Contrast inside of my program

It is somewhat strange, but I need everything cranked up to the
maximum since I am taking a still shot in total darkness. The key
data for me to get is any very slight variation in the brightness and
contrast between pixels.

I`m hoping there is an easy way to do this. I have searched high and
low and haven`t come up with anything.

Any help would be appreciated.

Thanks,

David

-------------------------------------------------

import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import javax.media.*;
import javax.media.control.*;
import javax.media.format.*;
import javax.media.util.*;


class FrameGrab
{
public static void main(String[] args) throws Exception
{
// Create capture device
CaptureDeviceInfo deviceInfo =
CaptureDeviceManager.getDevice("vfw:Microsoft WDM Image Capture
(Win32):0");
Player player =
Manager.createRealizedPlayer(deviceInfo.getLocator());
player.start();

// Wait a few seconds for camera to initialise (otherwise
img==null)
Thread.sleep(5000);

// Grab a frame from the capture device
FrameGrabbingControl frameGrabber =
(FrameGrabbingControl)player.getControl("javax.media.control.FrameGrabbingControl");
Buffer buf = frameGrabber.grabFrame();

// Convert frame to an buffered image so it can be processed
and saved
Image img = (new
BufferToImage((VideoFormat)buf.getFormat()).createImage(buf));
BufferedImage buffImg = new BufferedImage(img.getWidth(null),
img.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics2D g = buffImg.createGraphics();
g.drawImage(img, null, null);


ImageIO.write(buffImg, "jpg", new File("C:/Users//Pictures/
Test.jpg"));

// Stop using webcam
player.close();
player.deallocate();
System.exit(0);
}
}
 
K

Knute Johnson

Desk-of-David said:
Hi,

I am fairly new to Java, and am working on a project in which I need
to grab a still-frame from a webcam for further processing.

The code below works just fine for getting the shot taken and saved.
However, I want to change from camera setting "0" to camera setting
"1":

0 RGB, 320x240, Length=230400, 24-bit, Masks=3:2:1, PixelStride=3,
LineStride=960, Flipped
1 RGB, 160x120, Length=57600, 24-bit, Masks=3:2:1, PixelStride=3,
LineStride=480, Flipped


Question 1 - How does one change the camera setting to the 160 x 120
setting

Question 2 - Is there a way to change or control the camera settings
like Gain, Brightness and Contrast inside of my program

It is somewhat strange, but I need everything cranked up to the
maximum since I am taking a still shot in total darkness. The key
data for me to get is any very slight variation in the brightness and
contrast between pixels.

I`m hoping there is an easy way to do this. I have searched high and
low and haven`t come up with anything.

Any help would be appreciated.

Thanks,

David

-------------------------------------------------

import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import javax.media.*;
import javax.media.control.*;
import javax.media.format.*;
import javax.media.util.*;


class FrameGrab
{
public static void main(String[] args) throws Exception
{
// Create capture device
CaptureDeviceInfo deviceInfo =
CaptureDeviceManager.getDevice("vfw:Microsoft WDM Image Capture
(Win32):0");
Player player =
Manager.createRealizedPlayer(deviceInfo.getLocator());
player.start();

// Wait a few seconds for camera to initialise (otherwise
img==null)
Thread.sleep(5000);

// Grab a frame from the capture device
FrameGrabbingControl frameGrabber =
(FrameGrabbingControl)player.getControl("javax.media.control.FrameGrabbingControl");
Buffer buf = frameGrabber.grabFrame();

// Convert frame to an buffered image so it can be processed
and saved
Image img = (new
BufferToImage((VideoFormat)buf.getFormat()).createImage(buf));
BufferedImage buffImg = new BufferedImage(img.getWidth(null),
img.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics2D g = buffImg.createGraphics();
g.drawImage(img, null, null);


ImageIO.write(buffImg, "jpg", new File("C:/Users//Pictures/
Test.jpg"));

// Stop using webcam
player.close();
player.deallocate();
System.exit(0);
}
}

You need to get the FormatControl. When you display the format control
it will have a 'Video Source' button. Adjust your brightness etc. from
there. If you have multiple identical video sources then there should
be an option to select the other.
 
D

Desk-of-David

I don't suppose anyone has some working code for this? I've tried a
number of variations (after tons of Googling) and had assumed that the
issue might be found in the FormatControl area, but could not seem to
get it working. I can get the program to display the options that
should be available, such as:

0 RGB, 320x240, Length=230400, 24-bit, Masks=3:2:1,
PixelStride=3, LineStride=960, Flipped
1 RGB, 160x120, Length=57600, 24-bit, Masks=3:2:1,
PixelStride=3, LineStride=480, Flipped

However, I'm pretty clued out as to how to get the thing to accept
format #1. Since this program just needs to run in the console, I can
preset the exact format that I want and don't need to make it 'user
friendly' and let the user select.


Were you saying that my program can *also* control the brightness,
contrast, gain etc, inside of Java? I had hoped so, but had wondered
if it was only going to be inside of the webcam driver software
instead. Certainly my preference would be to control it directly
inside of Java so I can ensure that everything is cranked way up. If
it isn't all maxed out, then it will only give me 1 or 2 unique
colors. If I use the webcam software to take a still image, I will
get about 300 unique colors inside of a 160x120 - that size seems to
give me the best results. This process is key for me as I'm taking
the picture in total darkness and need to isolate any 'noise' that
comes across the line. Goofy project, but that's what makes these
things "fun".

Thanks again for any help anyone can give.

David
 
K

Knute Johnson

Desk-of-David said:
I don't suppose anyone has some working code for this? I've tried a
number of variations (after tons of Googling) and had assumed that the
issue might be found in the FormatControl area, but could not seem to
get it working. I can get the program to display the options that
should be available, such as:

0 RGB, 320x240, Length=230400, 24-bit, Masks=3:2:1,
PixelStride=3, LineStride=960, Flipped
1 RGB, 160x120, Length=57600, 24-bit, Masks=3:2:1,
PixelStride=3, LineStride=480, Flipped

However, I'm pretty clued out as to how to get the thing to accept
format #1. Since this program just needs to run in the console, I can
preset the exact format that I want and don't need to make it 'user
friendly' and let the user select.


Were you saying that my program can *also* control the brightness,
contrast, gain etc, inside of Java? I had hoped so, but had wondered
if it was only going to be inside of the webcam driver software
instead. Certainly my preference would be to control it directly
inside of Java so I can ensure that everything is cranked way up. If
it isn't all maxed out, then it will only give me 1 or 2 unique
colors. If I use the webcam software to take a still image, I will
get about 300 unique colors inside of a 160x120 - that size seems to
give me the best results. This process is key for me as I'm taking
the picture in total darkness and need to isolate any 'noise' that
comes across the line. Goofy project, but that's what makes these
things "fun".

Thanks again for any help anyone can give.

David

David:

You will have to fix some of the line wrap but below is my web cam
software. It hasn't been updated in some time and does some funky
things but it should give you a good idea how to get your code to do
what you want. The brightness and camera select are in fact features of
the driver but when you display the format control there will be a
button to open it. JMF has some problems and it hasn't been worked on
by Sun in years. Send me your email if you want the three button images.

k...

//
//
//
// WebCam - Knute's Web Cam Program
//
//
// Version Date Modification
// ------- ---------
---------------------------------------------------
// 0.40 05 feb 04 housekeeping, minor changes for sdk 1.5, start
// version record keeping
// 0.41 18 mar 04 stop and close player when program closed
from X on
// frame
// 0.42 18 mar 04 add info dialog when executing command after
grab
// 0.50 20 mar 04 add FormatControl dialog to File menu
// 0.51 28 mar 04 disable format control when no camera selected
// 0.52 28 mar 04 add code to detect controller error
// 0.53 30 mar 04 changes to run() for title display
// 0.60 31 mar 04 add ImageIcons to buttons, add menu item to
enable
// showing if execute dialog
// 0.61 07 jul 06 add Manager hint
// 0.62 23 jul 06 housekeeping
// 0.63 24 jul 06 improved initial format selection, more
housekeeping
// 0.64 30 jul 06 clean up execute after grab code
// 0.65 01 aug 06 add code to runnable to put gui creation on EDT
//
//
//

package com.knutejohnson.camera;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.util.*;
import java.util.regex.*;
import javax.swing.*;
import javax.media.*;
import javax.media.format.*;
import javax.media.protocol.*;
import javax.media.control.*;
import javax.media.util.*;
import javax.media.renderer.*;
import javax.imageio.*;

public class WebCam extends JFrame implements ActionListener, Runnable {
final static String VERSION = "0.65";
final static String ABOUT_MESSAGE =
"Knute's Web Cam - Version " + VERSION + "\n" +
"Copyright (c) 2003-2006 Knute Johnson. All rights reserved.";
final static String TITLE = "Knute's Web Cam";
final static String DATA_FILE = ".wc";
final static String[] extension = { ".jpg",".png",".bmp" };
final static String[] fileFormat = { "JPEG","PNG","BMP" };
String execute = "";
String fname = "photo";
int currentCamera = -1;
int fileType = 0;
int interval = 60;
volatile boolean automatic = false;
boolean showExecuteDialog;
String startTimeStr = "";
String stopTimeStr = "";
CaptureDeviceInfo[] deviceInfo;
ControllerListener cl;
Player player;
MediaLocator ml;
FrameGrabbingControl grabber;
FormatControl formatControl;
JRadioButtonMenuItem[] cameraMenuItem;
JRadioButtonMenuItem stopCaptureMenuItem;
JRadioButtonMenuItem[] fileTypeMenuItem;
JMenuItem formatMenuItem;
JMenuItem setIntervalMenuItem;
JRadioButtonMenuItem showExecuteDialogMenuItem;
IPanel ip;
JTextField startField;
JTextField stopField;
JButton grabButton;
JButton autoButton;
JLabel lastPhotoLabel;
ImageIcon grabIcon;
ImageIcon startIcon;
ImageIcon stopIcon;
Thread thread;
Container cp;
GridBagConstraints c;

public WebCam() {
super(TITLE);

getSettings();

Class thisClass = this.getClass();
grabIcon = new
ImageIcon(thisClass.getResource("wcimages/grab.png"));
startIcon = new
ImageIcon(thisClass.getResource("wcimages/start.png"));
stopIcon = new
ImageIcon(thisClass.getResource("wcimages/stop.png"));

addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
if (player != null) {
player.stop();
player.close();
}
System.exit(0);
}
});

cp = getContentPane();
cp.setLayout(new GridBagLayout());

c = new GridBagConstraints();
c.gridx = c.gridy = 0; c.gridwidth = 1;
c.insets = new Insets(2,4,4,4);
c.fill = GridBagConstraints.HORIZONTAL;

JMenuBar mb = new JMenuBar();
setJMenuBar(mb);

JMenu fileMenu = new JMenu("File");
mb.add(fileMenu);
JMenu optionsMenu = new JMenu("Options");
mb.add(optionsMenu);
JMenu helpMenu = new JMenu("Help");
mb.add(helpMenu);

JMenuItem saveMenuItem = new JMenuItem("Save Settings");
saveMenuItem.addActionListener(this);
fileMenu.add(saveMenuItem);

JMenuItem execMenuItem = new JMenuItem("Execute After Grab");
execMenuItem.addActionListener(this);
fileMenu.add(execMenuItem);

formatMenuItem = new JMenuItem("Format Control");
formatMenuItem.setEnabled(false);
formatMenuItem.addActionListener(this);
fileMenu.add(formatMenuItem);

fileMenu.addSeparator();
JMenuItem quitMenuItem = new JMenuItem("Quit");
quitMenuItem.addActionListener(this);
fileMenu.add(quitMenuItem);

JMenu cameraMenu = new JMenu("Camera");
optionsMenu.add(cameraMenu);

JMenuItem filenameMenuItem = new JMenuItem("File Name");
filenameMenuItem.addActionListener(this);
optionsMenu.add(filenameMenuItem);

JMenu filetypeMenu = new JMenu("File Type");
optionsMenu.add(filetypeMenu);

fileTypeMenuItem = new JRadioButtonMenuItem[fileFormat.length];
ButtonGroup bg = new ButtonGroup();
for (int i=0; i<fileFormat.length; i++) {
fileTypeMenuItem = new JRadioButtonMenuItem(fileFormat);
bg.add(fileTypeMenuItem);
fileTypeMenuItem.addActionListener(this);
filetypeMenu.add(fileTypeMenuItem);
}
fileTypeMenuItem[fileType].setSelected(true);

setIntervalMenuItem = new JMenuItem("Set Interval");
setIntervalMenuItem.setEnabled(!automatic);
setIntervalMenuItem.addActionListener(this);
optionsMenu.add(setIntervalMenuItem);

showExecuteDialogMenuItem =
new JRadioButtonMenuItem("Show Execute Dialog",showExecuteDialog);
showExecuteDialogMenuItem.addActionListener(this);
optionsMenu.add(showExecuteDialogMenuItem);

JMenuItem aboutMenuItem = new JMenuItem("About");
aboutMenuItem.addActionListener(this);
helpMenu.add(aboutMenuItem);

Vector deviceVector =
CaptureDeviceManager.getDeviceList(new RGBFormat());
deviceInfo =
new CaptureDeviceInfo[deviceVector.size()];
cameraMenuItem = new JRadioButtonMenuItem[deviceVector.size()];
bg = new ButtonGroup();
for (int i=0; i<deviceInfo.length; i++) {
deviceInfo = (CaptureDeviceInfo)deviceVector.get(i);
if (i == currentCamera)
cameraMenuItem =
new JRadioButtonMenuItem(deviceInfo.getName(),true);
else
cameraMenuItem =
new JRadioButtonMenuItem(deviceInfo.getName());
bg.add(cameraMenuItem);
cameraMenuItem.addActionListener(this);
cameraMenu.add(cameraMenuItem);
System.out.println(deviceInfo.getName());
}
stopCaptureMenuItem = new JRadioButtonMenuItem(
"Stop Capture",currentCamera == -1);
stopCaptureMenuItem.addActionListener(this);
bg.add(stopCaptureMenuItem);
cameraMenu.add(stopCaptureMenuItem);

c.gridheight = 6;
ip = new IPanel();
cp.add(ip,c);

c.gridx = 2; c.gridheight = 1;
c.anchor = GridBagConstraints.NORTH;
JLabel l = new JLabel("Start Time");
cp.add(l,c);

++c.gridx;
startField = new JTextField(startTimeStr);
cp.add(startField,c);

c.gridx = 2; ++c.gridy;
l = new JLabel("Stop Time");
cp.add(l,c);

++c.gridx;
stopField = new JTextField(stopTimeStr);
cp.add(stopField,c);

c.weighty = 0.3;
c.anchor = GridBagConstraints.SOUTH;
c.gridx = 2; ++c.gridy; c.gridwidth = 2;
grabButton = new JButton(grabIcon);
grabButton.setActionCommand("Grab");
grabButton.setFocusPainted(false);
grabButton.setEnabled(false);
grabButton.addActionListener(this);
cp.add(grabButton,c);

c.weighty = 0.7;
c.anchor = GridBagConstraints.NORTH;
++c.gridy;
autoButton = new JButton(automatic ? stopIcon : startIcon);
autoButton.setActionCommand(automatic ? "Stop Automatic" :
"Start Automatic");
autoButton.setFocusPainted(false);
autoButton.setEnabled(false);
autoButton.addActionListener(this);
cp.add(autoButton,c);

c.weighty = 0.0;
++c.gridy;
c.anchor = GridBagConstraints.SOUTH;
l = new JLabel("Last Photo Taken",JLabel.CENTER);
cp.add(l,c);

++c.gridy;
lastPhotoLabel = new JLabel(" ",JLabel.CENTER);
cp.add(lastPhotoLabel,c);

cl = new ControllerAdapter() {
public void realizeComplete(RealizeCompleteEvent rce) {
grabber = (FrameGrabbingControl)player.getControl(
"javax.media.control.FrameGrabbingControl");

RGBFormat rgbFormat = new RGBFormat(new Dimension(640,480),

921600,Format.byteArray,30f,24,3,2,1,3,1920,RGBFormat.TRUE,
RGBFormat.LITTLE_ENDIAN);

formatControl = (FormatControl)player.getControl(
"javax.media.control.FormatControl");

Format[] formats = formatControl.getSupportedFormats();
// for (int i=0; i<formats.length; i++)
// System.out.println(formats.toString());

if (formatControl != null) {
player.stop();
formatControl.setFormat(rgbFormat);
player.start();
formatMenuItem.setEnabled(true);
}

/*
for (int i=0; i<formats.length; i++) {
if (formats.matches(rgbFormat.relax())) {
formatControl.setFormat(formats);
break;
}
}
*/

System.out.println(formatControl.getFormat().toString());
}

public void formatChange(FormatChangeEvent fce) {
System.out.println(formatControl.getFormat());
}

public void start(StartEvent se) {
grabButton.setEnabled(true);
autoButton.setEnabled(true);
if (currentCamera != -1)
setTitle(TITLE + " - " +
deviceInfo[currentCamera].getName());
}

public void stop(StopEvent se) {
grabButton.setEnabled(false);
autoButton.setEnabled(false);
setTitle(TITLE);
}

public void controllerError(ControllerErrorEvent cee) {
stopCaptureMenuItem.setSelected(true);
formatMenuItem.setEnabled(false);
}

public void controllerClosed(ControllerClosedEvent cce) {
}
};

if (currentCamera >= 0 && currentCamera < deviceInfo.length) {
ml = deviceInfo[currentCamera].getLocator();
try {
Manager.setHint(Manager.PLUGIN_PLAYER,Boolean.TRUE);
player = Manager.createPlayer(ml);
player.addControllerListener(cl);
player.start();
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(WebCam.this,
"Could Not Create
Player","ERROR",JOptionPane.ERROR_MESSAGE);
}
}

pack();
setVisible(true);

if (automatic) {
thread = new Thread(this);
thread.start();
}
}

public void run() {
// delay 2 secs when first started to not miss first photo from
stream
int countDown = 2;
try {
while (automatic) {
if (checkTimes()) {
setTitle(TITLE + " - Next Photo In " +
Integer.toString(countDown) + " Seconds");
if (countDown == 0) {
setTitle(TITLE + " - Taking Photo");
grab();
countDown = interval;
}
--countDown;
} else
setTitle(TITLE);
Thread.sleep(1000);
}
} catch (InterruptedException ie) {
}
if (currentCamera != -1)
setTitle(TITLE + " - " + deviceInfo[currentCamera].getName());
else
setTitle(TITLE);
}

boolean checkTimes() {
boolean retcod = true;
int startTime;
int stopTime;
int currentTime;

java.text.SimpleDateFormat sdf =
new java.text.SimpleDateFormat("HH:mm");
String currentTimeStr = sdf.format(new Date());
currentTime = Integer.parseInt(currentTimeStr.substring(0,2)) *
60 * 60;
currentTime += (Integer.parseInt(currentTimeStr.substring(3,5))
* 60);

Pattern p = Pattern.compile("(\\d{1,2})[:.]?(\\d{2})");

Matcher m = p.matcher(startField.getText());
if (m.matches()) {
startTime = Integer.parseInt(m.group(1)) * 60 * 60;
startTime += (Integer.parseInt(m.group(2)) * 60);
m = p.matcher(stopField.getText());
if (m.matches()) {
stopTime = Integer.parseInt(m.group(1)) * 60 * 60;
stopTime += (Integer.parseInt(m.group(2)) * 60);
if (stopTime > startTime) {
if (!(startTime <= currentTime && currentTime <
stopTime))
retcod = false;
} else {
if (!(currentTime >= startTime || currentTime <
stopTime))
retcod = false;
}
}
}
return retcod;
}

public void actionPerformed(ActionEvent ae) {
Object src = ae.getSource();
if (src instanceof JRadioButtonMenuItem) {
if (src == stopCaptureMenuItem) {
if (player != null) {
player.stop();
player.close();
}
currentCamera = -1;
if (automatic) {
setIntervalMenuItem.setEnabled(true);
autoButton.setIcon(startIcon);
autoButton.setActionCommand("Start Automatic");
thread.interrupt();
automatic = false;
}
formatMenuItem.setEnabled(false);
return;
}
for (int i=0; i<cameraMenuItem.length; i++) {
if (src == cameraMenuItem) {
currentCamera = i;
grabButton.setEnabled(false);
autoButton.setEnabled(false);
Runnable r = new Runnable() {
public void run() {
if (player != null) {
player.stop();
player.close();
}
ml = deviceInfo[currentCamera].getLocator();
try {
Manager.setHint(Manager.PLUGIN_PLAYER,Boolean.TRUE);
player = Manager.createPlayer(ml);
player.addControllerListener(cl);
player.start();
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(WebCam.this,
"Could Not Create Player","ERROR",
JOptionPane.ERROR_MESSAGE);
}
}
};
new Thread(r).start();
return;
}
}
if (src == showExecuteDialogMenuItem) {
if (showExecuteDialogMenuItem.isSelected())
showExecuteDialog = true;
else
showExecuteDialog = false;
}
}
String ac = ae.getActionCommand();
for (int i=0; i<fileFormat.length; i++)
if (ac.equals(fileFormat)) {
fileType = i;
break;
}
if (ac.equals("Save Settings")) {
saveSettings();
} else if (ac.equals("Execute After Grab")) {
String newExecute = JOptionPane.showInputDialog(WebCam.this,
"Enter Command To Execute After Grab",execute);
if (newExecute != null)
execute = newExecute;
} else if (ac.equals("Format Control")) {
Component co = formatControl.getControlComponent();
if (co != null) {
player.stop();
JDialog d = new JDialog(WebCam.this,"Format Control",true);
d.add(co);
d.pack();
d.setLocationRelativeTo(WebCam.this);
d.setVisible(true);
d.dispose();
player.start();
}
} else if (ac.equals("File Name")) {
String newFname = JOptionPane.showInputDialog(WebCam.this,
"Enter File Name",fname);
if (newFname != null)
fname = newFname;
} else if (ac.equals("Set Interval")) {
String newInterval = JOptionPane.showInputDialog(WebCam.this,
"Enter Interval In Seconds",Integer.toString(interval));
if (newInterval != null) {
try {
interval = Integer.parseInt(newInterval);
} catch (NumberFormatException nfe) { }
}
} else if (ac.equals("Grab")) {
grab();
} else if (ac.equals("Start Automatic")) {
setIntervalMenuItem.setEnabled(false);
autoButton.setIcon(stopIcon);
autoButton.setActionCommand("Stop Automatic");
automatic = true;
thread = new Thread(WebCam.this);
thread.start();
} else if (ac.equals("Stop Automatic")) {
setIntervalMenuItem.setEnabled(true);
autoButton.setIcon(startIcon);
autoButton.setActionCommand("Start Automatic");
thread.interrupt();
automatic = false;
} else if (ac.equals("About")) {
JOptionPane.showMessageDialog(WebCam.this,ABOUT_MESSAGE);
} else if (ac.equals("Quit")) {
if (player != null) {
player.stop();
player.close();
}
System.exit(0);
}
}

public void grab() {
Buffer buf = grabber.grabFrame();
BufferToImage b2i = new
BufferToImage((VideoFormat)buf.getFormat());
BufferedImage bi = (BufferedImage)b2i.createImage(buf);
if (bi != null) {
ip.setImage(bi);
java.text.SimpleDateFormat sdf =
new java.text.SimpleDateFormat("HH:mm:ss");
lastPhotoLabel.setText(sdf.format(new Date()));
try {
ImageIO.write(bi,fileFormat[fileType],
new File(fname+extension[fileType]));
bi.flush();
} catch (IllegalArgumentException iae) {
iae.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
if (!execute.equals("")) {
Runnable r = new Runnable() {
public void run() {
JDialog d = null;
try {
if (showExecuteDialog) {
d = new JDialog(WebCam.this,
"Excecute After Grab");
d.setLocationRelativeTo(WebCam.this);
final JTextArea ta = new JTextArea("",10,30);
JScrollPane s = new JScrollPane(ta,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
d.getContentPane().add(s);
d.pack();
d.setVisible(true);
ProcessBuilder pb = new
ProcessBuilder(execute);
pb.redirectErrorStream(true);
final Process p = pb.start();
Runnable r = new Runnable() {
public void run() {
BufferedReader br = new BufferedReader(
new
InputStreamReader(p.getInputStream()));
String str;
try {
while ((str = br.readLine()) !=
null) {
ta.append(str + "\n");
ta.setCaretPosition(
ta.getText().length());
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
};
new Thread(r).start();
p.waitFor();
Thread.sleep(4000);
d.dispose();
} else {
ProcessBuilder pb = new
ProcessBuilder(execute);
pb.redirectErrorStream(true);
final Process p = pb.start();
Runnable r = new Runnable() {
public void run() {
BufferedReader br = new BufferedReader(
new
InputStreamReader(p.getInputStream()));
String str;
try {
while ((str = br.readLine()) !=
null)
System.out.println(str);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
};
new Thread(r).start();
p.waitFor();
}
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (InterruptedException ie) {
ie.printStackTrace();
} finally {
if (d != null && d.isVisible()) {
d.setVisible(false);
d.dispose();
}
}
}
};
new Thread(r).start();
}
}

public void saveSettings() {
try {
BufferedWriter bw =
new BufferedWriter(new FileWriter(new File(DATA_FILE)));
bw.write(Integer.toString(currentCamera));
bw.newLine();
bw.write(fname);
bw.newLine();
bw.write(Integer.toString(fileType));
bw.newLine();
bw.write(Integer.toString(interval));
bw.newLine();
bw.write(Boolean.toString(automatic));
bw.newLine();
bw.write(execute);
bw.newLine();
bw.write(Boolean.toString(showExecuteDialog));
bw.newLine();
bw.write(startField.getText());
bw.newLine();
bw.write(stopField.getText());
bw.newLine();
bw.close();
} catch (IOException ioe) {
System.out.println(ioe);
}
}

public void getSettings() {
try {
File f = new File(DATA_FILE);
if (f.exists()) {
BufferedReader br =
new BufferedReader(new FileReader(f));
currentCamera = Integer.parseInt(br.readLine());
fname = br.readLine();
fileType = Integer.parseInt(br.readLine());
interval = Integer.parseInt(br.readLine());
automatic = Boolean.parseBoolean(br.readLine());
execute = br.readLine();
showExecuteDialog = Boolean.parseBoolean(br.readLine());
startTimeStr = br.readLine();
stopTimeStr = br.readLine();
br.close();
}
} catch (IOException ioe) {
System.out.println(ioe);
}
}

public static void main(String[] args) {
Runnable r = new Runnable() {
public void run() {
new WebCam();
}
};
EventQueue.invokeLater(r);
}

public class IPanel extends JPanel {
Image image;

public IPanel() {
}

public void setImage(Image image) {
this.image = image;
repaint();
}

public void paintComponent(Graphics g) {
super.paintComponent(g);

if (image != null)
g.drawImage(image,0,0,320,240,this);
else
g.drawString("No Image Available",40,120);
}

public Dimension getPreferredSize() {
return new Dimension(320,240);
}
}
}
 
D

Desk-of-David

Thanks for the tips and the code. It didn't fire up, as is, so I'll
play with it a bit and see if i can get it to work.

Thanks for your help.

David
 
K

Knute Johnson

Desk-of-David said:
Thanks for the tips and the code. It didn't fire up, as is, so I'll
play with it a bit and see if i can get it to work.

Thanks for your help.

David

David:

It should work, probably just the messed up formatting. Send me an
email and I'll send you the source code.
 
D

Desk-of-David

Thanks again for your suggestion - after much playing with the code, I
finally managed to get it to do what I wanted. I have posted it here
in case it is of use to anyone else.

The key part was labeled below - although it is fancier to give the
user the option of the settings for the camera, I decided to hard-code
it in for my specific use.

import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import javax.media.*;
import javax.media.control.*;
import javax.media.format.*;
import javax.media.util.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.media.datasink.*;
import javax.media.protocol.*;
import java.awt.event.*;
import com.sun.image.codec.jpeg.*;

class FrameGrab
{
public static void main(String[] args) throws Exception
{
boolean saved;
CaptureDeviceInfo webCamDeviceInfo = null;
MediaLocator ml = null;
Dimension imageSize = null;
FormatControl formatControl = null;
VideoFormat currentFormat = null;
Format[] videoFormats = null;

// Create capture device
CaptureDeviceInfo deviceInfo =
CaptureDeviceManager.getDevice("vfw:Microsoft WDM Image Capture
(Win32):0");

Player player =
Manager.createRealizedPlayer(deviceInfo.getLocator());
player.start();
Format[] formats = deviceInfo.getFormats();
currentFormat = (RGBFormat)formats[3]; // This was the key
***

formatControl = (FormatControl)player.getControl
( "javax.media.control.FormatControl" );
formatControl.setFormat ( currentFormat );
Format currFormat = formatControl.getFormat();
player.start();

// Wait a few seconds for camera to initialise (otherwise
img==null)
Thread.sleep(5000);

// Grab a frame from the capture device
FrameGrabbingControl frameGrabber =
(FrameGrabbingControl)player.getControl("javax.media.control.FrameGrabbingControl");
Buffer buf = frameGrabber.grabFrame();

// Convert frame to an buffered image so it can be processed
and saved
Image img = (new
BufferToImage((VideoFormat)buf.getFormat()).createImage(buf));

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

Graphics2D g = buffImg.createGraphics();
g.drawImage(img, null, null);

FileOutputStream out = null;

try
{
out = new FileOutputStream ( "C:/Users/Pictures/
Test1.jpg" );
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder
( out );
JPEGEncodeParam param =
encoder.getDefaultJPEGEncodeParam ( buffImg );
param.setQuality ( 1.0f, false ); // 100% high
quality setting, no compression
encoder.setJPEGEncodeParam ( param );
encoder.encode ( buffImg );
out.close();
saved = true;
}
catch ( Exception ex )
{
System.out.println ("Error saving JPEG : " +
ex.getMessage() );
}

// Stop using webcam
player.stop();
player.close();
player.deallocate();
player = null;
System.exit(0);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top