java.net: Wiki

The Source for Java Technology Collaboration


 <<O>>  Difference Topic ChangeResolutionSampleProgram (1 - 27 Aug 2009 - Main.captfoss)
Line: 1 to 1
Added:
>
>
META TOPICPARENT name="JMFQuickstartGuide"
Home | Help | Changes | Index | Search | Go
<-- This creates the navigation links to :  Home | Help | Index | etc.  -->

Change Resolution Sample Program
<-- this automatically adds a header showing the name of this page -->

@author captfoss

The following JMF utility program from the JMF Quickstart Guide will allow you to change the resolution of your web cam capture via a combo box to any format supported by your web cam.


import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import javax.media.*;
import javax.media.control.FormatControl;
import javax.media.protocol.*;
import javax.swing.*;
/**
 *
 * @author captfoss & TBM
 */
public class ChangeResolution extends JFrame implements ControllerListener {
 
    Player p;
    DataSource ds;
    JComboBox combo;
    JPanel vidPanel;
 
    public ChangeResolution() {
        super("Resolution Change, by captfoss and TBM");
        Manager.setHint(Manager.LIGHTWEIGHT_RENDERER, true);
        try {
            ds = Manager.createDataSource(new MediaLocator("vfw://0"));
            p = Manager.createPlayer(ds);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        p.addControllerListener(this);
        p.realize();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        vidPanel = new JPanel(new BorderLayout());
        vidPanel.setBackground(new Color(250,230,250));
    }
 
    private void populateCombo() {
        Vector<Format> allFormats = new Vector<Format>();
        if (ds instanceof CaptureDevice) {
            FormatControl[] fcs = ((CaptureDevice) ds).getFormatControls();
            for (FormatControl fc : fcs) {
                Format[] formats = ((FormatControl) fc).getSupportedFormats();
                for (Format format : formats) {
                    allFormats.add(format);
                }
            }
        }
        combo = new JComboBox(allFormats);
        combo.addActionListener(new ActionListener() {
 
            public void actionPerformed(ActionEvent e) {
                new Thread(new Runnable() {
 
                    public void run() {
                        vidPanel.removeAll();
                        vidPanel.revalidate();
                        JLabel lab=new JLabel("<html><center><font color=\"red\">Wait for a while...</font><br/>" +
                                "<b><font color=\"green\">Format Changing...</font></b><br/>" +
                                "<i>You are using a free open source 'Webcam Resolution Change utility', <u>meant for " +
                                "demonstration only</u>, by:</br>" +
                                "<font color=\"blue\"> captfoss and T.B.M</i></font><br/>" +
                                "<font size=+1 color=\"green\"><b>ENJOY!!</b></font></center></html>");
                        lab.setHorizontalAlignment(JLabel.CENTER);
                        vidPanel.add(lab,BorderLayout.CENTER);
                        vidPanel.validate();
                        p.stop();
                        p.close();
                        try {
                            ds = Manager.createDataSource(new MediaLocator("vfw://0"));
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                        requestCaptureFormat((Format) combo.getSelectedItem(), ds);
                        p = null;
                        try {
                            p = Manager.createPlayer(ds);
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                        p.addControllerListener(ChangeResolution.this);
                        p.realize();
                    }
                }).start();
 
            }
        });
    }
// the method which actually changes the format.
    public boolean requestCaptureFormat(Format requested_format, DataSource ds) {
 
        if (ds instanceof CaptureDevice) {
            FormatControl[] fcs = ((CaptureDevice) ds).getFormatControls();
            for (FormatControl fc : fcs) {
                Format[] formats = ((FormatControl) fc).getSupportedFormats();
                for (Format format : formats) {
                    if (requested_format.matches(format)) {
                        ((FormatControl) fc).setFormat(format);
                        return true;
                    }
                }
            }
        }
        return false;
    }
 
    public static void main(String args[]) {
        try {
            SwingUtilities.invokeLater(new Runnable() {
 
                public void run() {
                    new ChangeResolution();
                }
            });
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
 
    public void controllerUpdate(ControllerEvent e) {
        if (e instanceof RealizeCompleteEvent) {
            p.prefetch();
        }
        if(e instanceof PrefetchCompleteEvent){
            vidPanel.removeAll();
            if (p.getVisualComponent() != null) {
                vidPanel.add(p.getVisualComponent(), BorderLayout.CENTER);
            }
            if (p.getControlPanelComponent() != null) {
                vidPanel.add(p.getControlPanelComponent(), BorderLayout.SOUTH);
            }
            vidPanel.validate();
            if (combo == null) {
                populateCombo();
                getContentPane().add(vidPanel, BorderLayout.CENTER);
                getContentPane().add(combo, BorderLayout.SOUTH);
            }
            combo.setPreferredSize(new Dimension(p.getVisualComponent().getPreferredSize().width, combo.getPreferredSize().height));
            pack();
            setLocationRelativeTo(null);
            setVisible(true);
            p.start();
        }
    }
}



Topic ChangeResolutionSampleProgram . { View | Diffs r1 | More }
 XML java.net RSS