Cannot resize Monitor
Hello and Hi Java Community!
At work I took over some code from my predecessor which is supposed to take some live Input from a camera and feed it on screen, and send it to another applet for saving. So far, so good and I've run into some trouble.
My biggest problem is that the original code was intended for a video resolution of 320x240, but now a resolution of at least 640x480 is wanted. I've tried to resize the monitor in a lot of ways, but nothing has helped. How can I make this bigger?
The biggest problem here is that the captureFrame (the screenshot function) method throws an AWT-Exception when I call it and the original author made video-stills by taking a screenshot of the whole screen and cutting out the part in which the video is in.
Is there a way to circumvent this? I hope you can help me!
Thank you,
Florian S.
(I will post the code for the two classes in question. Video.java is the main class and CaptureUtil.java is where the video-device is called)
The Video Class:
package jmf3;
/*
videoscreen... with record and screenshot function
*/
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import javax.media.Processor;
import javax.media.DataSink;
import javax.media.protocol.DataSource;
import javax.media.MediaLocator;
import javax.media.Manager;
import javax.media.format.VideoFormat;
import javax.media.Format;
import javax.media.util.*;
import javax.media.protocol.FileTypeDescriptor;
import javax.media.control.FrameRateControl;
import javax.media.control.MonitorControl;
import javax.media.ProcessorModel;
import javax.media.format.AudioFormat;
import java.util.StringTokenizer;
import javax.media.*;
import javax.media.control.*;
import javax.media.protocol.*;
import java.io.*;
publicclass Videoextends JAppletimplements ActionListener, KeyListener{
boolean isStandalone =false;
ProcessorModel proc =null;
MonitorControl monitorControl =null;
static Control[] controls =null;
MyWindowListener mwl;
MyKeyListener mkl;
String recordSize ="640x480";
String previewSize ="640x480";
final String outputType ="video.x_msvideo";// Available:video.x_msvideo and video.quicktime
finalfloat FRAMERATE = 25;
Robot robot;
// Audio aufnehmem
boolean recordAudio =false;
AudioFormat af =null;
VideoFormat vf =null;
// GUI components
JFrame frame;
JButton buttonStart =new JButton();
JButton buttonEnd =new JButton();
JButton buttonSnapShot =new JButton();
JPanel panel1 =new JPanel();// Panel f黵 Hauptfenster
// JMF objects
Processor processor =null;
DataSink datasink =null;
Component monitor =null;
DataSource datasource =null;
public Dimension getDimension(String size)
{
StringTokenizer st =new StringTokenizer(size,"x");
int sizeX = Integer.parseInt(st.nextToken());
int sizeY = Integer.parseInt(st.nextToken());
Dimension dimension =new Dimension(sizeX, sizeY);
return dimension;
}
// Get a parameter value
public String getParameter(String key, String def){
return isStandalone ? System.getProperty(key, def)
: (getParameter(key) !=null ? getParameter(key) : def);
}
// Construct the applet
public Video(){
}
// Initialize the applet
publicvoid init(){
try{
jbInit();
}catch (Exception e){
e.printStackTrace();
}
}
// Component initialization
privatevoid jbInit()throws Exception{
createGUI();
System.out.println("Applet gestartet...");
fillGUI();
}
// Start the applet
publicvoid start(){
}
// Stop the applet
publicvoid stop(){
if (processor !=null){
processor.close();
processor.stop();
processor.deallocate();
datasource.disconnect();
destroy();
}
}
// Destroy the applet
publicvoid destroy(){
if (processor !=null)
{
processor.close();
processor.stop();
processor.deallocate();
datasource.disconnect();
}
System.out.println("Thank you and bye-bye.");
System.exit(0);
}
// Get Applet information
public String getAppletInfo(){
return"Visuelles Aufnahme System - Bestandteil des ARC Systems." +
"\n(c)2006";
}
// Get parameter info
public String[][] getParameterInfo(){
returnnull;
}
publicvoid fillGUI(){
//Alle Buttons mit Listenern belegen
buttonStart.addActionListener(this);
buttonEnd.addActionListener(this);
buttonSnapShot.addActionListener(this);
this.addKeyListener(this);
mwl =new MyWindowListener();
mkl =new MyKeyListener();
// Anfangen mit dem Monitoring:
this.startMonitoring(panel1);
}
publicvoid createGUI(){
Container contentPane = this.getContentPane();
contentPane.setLayout(null);
// intialisiere Screenshot
buttonSnapShot.setText("Standbild");
buttonSnapShot.setBounds(5, 5, 120, 20);
this.add(buttonSnapShot);
// intialisiere Start-Knopf
buttonStart.setText("Video aufnehmen");
buttonStart.setBounds(130, 5, 150, 20);
this.add(buttonStart);
// intialisiere Ende-Knopf
buttonEnd.setText("Ende");
buttonEnd.setBounds(285, 5, 120, 20);
buttonEnd.setEnabled(false);
this.add(buttonEnd);
panel1.setBounds(5, 30, 352, 288);//5, 30, 352, 288
this.add(panel1);
}
publicvoid startMonitoring(JPanel panel){
try{
System.out.println("Starting monitoring...");
// Close the previous processor, which in turn closes the capture device
if (processor !=null){
processor.close();
processor.deallocate();
}
// Remove the previous monitor
if (monitor !=null){
panel.remove(monitor);
monitor =null;
}
String encoding ="YUV";// "RGB";
Dimension recordSizeDimension = getDimension(recordSize);//recordSize
vf =new VideoFormat(encoding, recordSizeDimension, Format.NOT_SPECIFIED, null, FRAMERATE);
// Need audio
if (recordAudio){
int samplingRate = 44100;
int samplingSize = 16;//false ? 8 : 16;
int channels = 2;//false ? 1 : 2;
af =new AudioFormat(AudioFormat.LINEAR, samplingRate,
samplingSize, channels);
}
// Use CaptureUtil to create a monitored capture datasource
datasource = CaptureUtil.getCaptureDS(vf, af);
if (datasource !=null){
// Set the preferred content type for the Processor's output
FileTypeDescriptor ftd =new FileTypeDescriptor(outputType);
Format[] formats =null;
if (af !=null && vf !=null){
formats =new Format[]{new AudioFormat(null),
new VideoFormat(null)};
}
if (af ==null)
formats =new Format[]{new VideoFormat(null)};
ProcessorModel pm =new ProcessorModel(datasource, formats, ftd);
//Manager.setHint(Manager.LIGHTWEIGHT_RENDERER, true);
processor = Manager.createRealizedProcessor(pm);
FrameRateControl frc = (FrameRateControl) processor
.getControl("javax.media.control.FrameRateControl");
frc.setFrameRate(FRAMERATE);
// Get the monitor control:
// Since there are more than one MonitorControl objects
// exported by the DataSource, we get the specific one
// that is also the MonitorStream object.
monitorControl =null;
Object[] controls = datasource.getControls();
for (int i = 0; i < controls.length; i++){
System.out.println("control: "
+ controls[i].getClass().getName());
if (controls[i]instanceof jmf3.MonitorStream){
monitorControl = (MonitorControl) controls[i];
monitorControl.setPreviewFrameRate(FRAMERATE);
}
}
if (monitorControl !=null){
monitor = monitorControl.getControlComponent();
Dimension dim = getDimension(recordSize);
System.out.println("Size: " + monitor.getSize());
//panel1.setSize(dim);
monitor.setSize(dim);
//monitor.setMaximumSize(dim);
//monitor.setBounds(0, 0, 640, 480);
//panel1.add(monitor);
panel1.add(monitor);
System.out.println("Size after: " + monitor.getSize());
// Make sure the monitor is enabled
monitorControl.setEnabled(true);
}
}
else{
System.out.println("Kein Videoger鋞 gefunden.");
JLabel notFoundLabel =new JLabel();
notFoundLabel.setText("Kein Videoger鋞 gefunden.");
panel1.add(notFoundLabel);
}
}
catch (Exception e1)
{
System.err.println("Es ist ein Fehler aufgetreten! Schlie遝 Datenverbindungen. Fehler: ");
e1.printStackTrace();
if (datasource !=null)
datasource.disconnect();
if (processor !=null)
processor.stop();
return;
}
}
publicvoid startCapture(){
try{
System.out.println("Starting capture...");
buttonStart.setText("Pause");
buttonEnd.setEnabled(true);
buttonSnapShot.setEnabled(false);
// Get the processor's output, create a DataSink and connect the two.
DataSource outputDS = processor.getDataOutput();
MediaLocator ml =new MediaLocator(
"file:///C:\\\\livevideotemp\\capture."
+ (outputType.equals("video.x_msvideo") ?"avi"
:"mov"));
datasink = Manager.createDataSink(outputDS, ml);
processor.start();
datasink.open();
datasink.start();
System.out.println("Started saving...");
}
catch (NullPointerException e1)
{
System.err.println("Kein Videoger鋞 ansprechbar.");
}
catch (Exception e)
{
System.err.println("Fehler!");
e.printStackTrace();
}
}
publicvoid pauseCapture(){
try{
System.out.println("Pausing capture...");
processor.stop();
buttonStart.setText("Resume");
}
catch (NullPointerException e1)
{
System.err.println("Kein Videoger鋞 ansprechbar.");
}
}
publicvoid resumeCapture(){
System.out.println("Resuming capture...");
processor.start();
buttonStart.setText("Pause");
}
publicvoid stopCapture(){
buttonStart.setText("Video aufnehmen");
buttonEnd.setEnabled(false);
buttonSnapShot.setEnabled(true);
System.out.println("Stopping capture...");
try{
// Stop the capture and the file writer (DataSink)
processor.stop();
processor.close();
datasink.close();
processor =null;
Applet applet = (Applet) getAppletContext().getApplet("ImageBrowser");
if (applet !=null && appletinstanceof ImageBrowser){
((ImageBrowser) applet).machevideo(this.previewSize);
}
System.out.println("Done saving.");
// Restart monitoring
this.startMonitoring(panel1);
}
catch (NullPointerException e)
{
System.err.println("Kein Videoger鋞 ansprechbar.");
}
}
/*public void captureFrame() {
try {
robot = new Robot();
} catch (AWTException ex) {
}
Dimension dim = new Dimension(monitor.getWidth(), monitor.getHeight());
Rectangle rec = new Rectangle(monitor.getLocationOnScreen(), dim);
BufferedImage bui = robot.createScreenCapture(rec);
Image img = bui.getScaledInstance(monitor.getWidth(), monitor
.getHeight(), 1);
System.out.println(img.getHeight(this));
ImageIcon icon = new ImageIcon(img);
Applet applet = (Applet) getAppletContext().getApplet("ImageBrowser");
if (applet != null && applet instanceof ImageBrowser) {
((ImageBrowser) applet).macheicon(icon);
}
}
*/publicvoid captureFrame(){
try{
// Bild aufnehmen
FrameGrabbingControl fgc = (FrameGrabbingControl) processor
.getControl("javax.media.control.FrameGrabbingControl");
Buffer buffer = fgc.grabFrame();
// Als Bild speichern
BufferToImage bufferedImage =new BufferToImage((VideoFormat) buffer
.getFormat());
Image img = bufferedImage.createImage(buffer);
System.out.println("Bild: " + img.getWidth(this) +"x" + img.getHeight(this) +"aufgenommen");
ImageIcon icon =new ImageIcon(img);
Applet applet = (Applet) getAppletContext().getApplet("ImageBrowser");
if (applet !=null && appletinstanceof ImageBrowser){
((ImageBrowser) applet).macheicon(icon);
}
}
catch(NullPointerException e){
System.err.println("Konnte kein Standbild aufnehmen!");
e.printStackTrace();
}
}
publicvoid actionPerformed(ActionEvent ae){
String action = ae.getActionCommand();
if (action.equals("Video aufnehmen")){
startCapture();
}elseif (action.equals("Pause")){
pauseCapture();
}elseif (action.equals("Resume")){
resumeCapture();
}elseif (action.equals("Ende")){
stopCapture();
}elseif (action.equals("Standbild")){
captureFrame();
}elseif (action.equals("Minimize")){
startMonitoring(panel1);
panel1.setBounds(5, 30, 640, 480);
add(panel1);
buttonSnapShot.setVisible(true);
frame.setVisible(false);
}elseif (action.equals("meinTest")){
}
}
publicvoid keyReleased(KeyEvent ke){
System.out.println("me r");
}
// keyEvent: the focus must be set on the right applet
publicvoid keyPressed(KeyEvent kp){
Integer action = kp.getKeyCode();
System.out.println("bin beim keyevent");
System.out.println(action.toString());
if (action.equals(new Integer(83))){
startCapture();// S(tart)
}elseif (action.equals(new Integer(80))){
pauseCapture();// P(ause)
}elseif (action.equals(new Integer(87))){
resumeCapture();// W(eiter)
}elseif (action.equals(new Integer(69))){
stopCapture();// E(nde)
}elseif (action.equals(new Integer(66))){
captureFrame();// B(ild)
}
}
publicvoid keyTyped(KeyEvent kt){
System.out.println("me t");
}
publicclass MyWindowListenerimplements WindowListener{
publicvoid windowOpened(WindowEvent e){
}
publicvoid windowClosing(WindowEvent e){
System.out.println("Window is closing...");
startMonitoring(panel1);
panel1.setBounds(5, 30, 352, 288);
add(panel1);
buttonSnapShot.setVisible(true);
}
publicvoid windowClosed(WindowEvent e){
System.out.println("Das Fenster wird geschlossen.");
startMonitoring(panel1);
panel1.setBounds(5, 30, 352, 288);
add(panel1);
buttonSnapShot.setVisible(true);
}
publicvoid windowIconified(WindowEvent e){
}
publicvoid windowDeiconified(WindowEvent e){
}
publicvoid windowActivated(WindowEvent e){
}
publicvoid windowDeactivated(WindowEvent e){
}
}
publicclass MyKeyListenerimplements KeyListener{
publicvoid keyReleased(KeyEvent ke){
System.out.println("me r");
}
// keyEvent: the focus must be set on the right applet
publicvoid keyPressed(KeyEvent kp){
Integer action = kp.getKeyCode();
System.out.println("bin beim keyevent");
System.out.println(action.toString());
if (action.equals(new Integer(83))){
startCapture();// S(tart)
}elseif (action.equals(new Integer(80))){
pauseCapture();// P(ause)
}elseif (action.equals(new Integer(87))){
resumeCapture();// W(eiter)
}elseif (action.equals(new Integer(69))){
stopCapture();// E(nde)
}elseif (action.equals(new Integer(66))){
captureFrame();// B(ild)
}
}
publicvoid keyTyped(KeyEvent kt){
System.out.println("me t");
}
}
}
The CaptureUtil class:
/*
* Copyright (c) 1996-2001 Sun Microsystems, Inc. All Rights Reserved.
*
* Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
* modify and redistribute this software in source and binary code form,
* provided that i) this copyright notice and license appear on all copies of
* the software; and ii) Licensee does not utilize the software in a manner
* which is disparaging to Sun.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
* IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
* NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
* LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
* OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
* LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
* INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
* CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
* OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGES.
*
* This software is not designed or intended for use in on-line control of
* aircraft, air traffic, aircraft navigation or aircraft communications; or in
* the design, construction, operation or maintenance of any nuclear
* facility. Licensee represents and warrants that it will not use or
* redistribute the Software for such purposes.
*/
package jmf3;
import javax.media.*;
import javax.media.protocol.*;
import javax.media.format.*;
import javax.media.control.*;
import java.util.Vector;
import java.awt.*;
import java.io.IOException;
import com.sun.media.ui.PlayerWindow;
publicclass CaptureUtil{
publicstatic DataSource getCaptureDS(VideoFormat vf, AudioFormat af){
DataSource dsVideo =null;
DataSource dsAudio =null;
DataSource ds =null;
// Create a capture DataSource for the video
// If there is no video capture device, then exit with null
if (vf !=null){
dsVideo = createDataSource(vf);
if (dsVideo ==null)
{
System.out.println("No capture device detected");
returnnull;
}
}
if (af !=null){
System.out.println("Audio device detected");
dsAudio = createDataSource(af);
}
// Create the monitoring datasource wrapper
if (dsVideo !=null){
System.out.println("Video device detected");
dsVideo =new MonitorCDS(dsVideo);
if (dsAudio ==null)
return dsVideo;
ds = dsVideo;
}elseif (dsAudio !=null){
return dsAudio;
}else{
System.out.println("Standardfunktion...");
returnnull;
}
// Merge the data sources, if both audio and video are available
try{
ds = Manager.createMergingDataSource(new DataSource []{
dsAudio, dsVideo
});
}catch (IncompatibleSourceException ise){
System.err.println("Inkompatibles Ger鋞!");
returnnull;
}
return ds;
}
static DataSource createDataSource(Format format){
DataSource ds;
Vector devices;
CaptureDeviceInfo cdi;
MediaLocator ml;
// Find devices for format
devices = CaptureDeviceManager.getDeviceList(format);
if (devices.size() < 1){
System.err.println("! No Devices for " + format);
returnnull;
}
// Pick the first device
cdi = (CaptureDeviceInfo) devices.elementAt(0);
ml = cdi.getLocator();
try{
ds = Manager.createDataSource(ml);
ds.connect();
if (dsinstanceof CaptureDevice){
setCaptureFormat((CaptureDevice) ds, format);
}
return ds;
}
catch (NoDataSourceException e1)
{
System.err.println("Keine Datenquelle gefunden.");
returnnull;
}
catch (IOException e)
{
System.err.println("Konnte keine Verbindung mit dem Ger鋞 herstellen.");
returnnull;
}
catch (Exception e){
System.err.println("Konnte nicht auf die Videoquelle zugreifen.");
returnnull;
}
}
staticvoid setCaptureFormat(CaptureDevice cdev, Format format){
FormatControl [] fcs = cdev.getFormatControls();
if (fcs.length < 1)
return;
FormatControl fc = fcs[0];
Format [] formats = fc.getSupportedFormats();
for (int i = 0; i < formats.length; i++){
if (formats[i].matches(format)){
format = formats[i].intersects(format);
System.out.println("Setting format " + format);
fc.setFormat(format);
break;
}
}
}
}
( It is also very laggy blurry and any video sequence that is taken will lag every few seconds. Is there a quick fix for this? )

