Monday, February 16, 2009

Simple Player Applet using JMF


import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.lang.String;
import java.net.URL;
import java.net.MalformedURLException;
import java.io.IOException;
import java.util.Properties;
import javax.media.*;

public class SimplePlayerApplet extends Applet implements ControllerListener {
Player player = null; // media Player
Component visualComponent = null; // component in which video is playing
Component controlComponent = null; // controls gain, position, start, stop
Component progressBar = null; // displays progress during download
boolean firstTime = true;
long CachingSize = 0L;
Panel panel = null;
int controlPanelHeight = 0;
int videoWidth = 0;
int videoHeight = 0;

public void init() {
setLayout(null);
panel = new Panel();
panel.setLayout( null );
add(panel);
panel.setBounds(0, 0, 320, 240);
String mediaFile = null; // input file name from html param
MediaLocator mrl = null; // URL for our media file
URL url = null;
// Get the media filename info.The applet tag should contain the path to the
// source media file, relative to the html page.
if ((mediaFile = getParameter("FILE")) == null)
Fatal("Invalid media file parameter");
try {
url = new URL(getDocumentBase(), mediaFile);
mediaFile = url.toExternalForm();
}
catch (MalformedURLException mue) {}
try {
// Create a media locator from the file name
if ((mrl = new MediaLocator(mediaFile)) == null)
Fatal("Can't build URL for " + mediaFile);
// Create an instance of a player for this media
try {
player = Manager.createPlayer(mrl);
}
catch (NoPlayerException e) {
System.out.println(e);
Fatal("Could not create player for " + mrl);
}
// Add ourselves as a listener for a player's events
player.addControllerListener(this);
}
catch (MalformedURLException e) {
Fatal("Invalid media file URL!");
}
catch (IOException e) {
Fatal("IO exception creating player for " + mrl);
}
// This applet assumes that its start() calls player.start(). This causes the player to become
// realized. Once realized, the applet will get the visual and control panel components and add
// them to the Applet. These components are not added during init() because they are long
// operations that would make us appear unresposive to the user.
}
public void start() {
if (player != null)
player.start();
}
public void stop() {
if (player != null) {
player.stop();
player.deallocate();
}
}
public void destroy() {
player.close();
}
/**
* This controllerUpdate function must be defined in order to implement a ControllerListener
* interface. This function will be called whenever there is a media event
*/
public synchronized void controllerUpdate(ControllerEvent event) {
if (player == null)
return;
if (event instanceof RealizeCompleteEvent) {
if (progressBar != null) {
panel.remove(progressBar);
progressBar = null;
}
int width = 320;
int height = 0;
if (controlComponent == null)
if (( controlComponent = player.getControlPanelComponent()) != null) {
controlPanelHeight = controlComponent.getPreferredSize().height;
panel.add(controlComponent);
height += controlPanelHeight;
}
if (visualComponent == null)
if (( visualComponent =player.getVisualComponent())!= null) {
panel.add(visualComponent);
Dimension videoSize = visualComponent.getPreferredSize();
videoWidth = videoSize.width;
videoHeight = videoSize.height;
width = videoWidth;
height += videoHeight;
visualComponent.setBounds(0, 0, videoWidth, videoHeight);
}
panel.setBounds(0, 0, width, height);
if (controlComponent != null) {
controlComponent.setBounds(0, videoHeight,width, controlPanelHeight);
controlComponent.invalidate();
}
} else if (event instanceof CachingControlEvent) {
if (player.getState() > Controller.Realizing)
return;
// Put a progress bar up when downloading starts,take it down when downloading ends.
CachingControlEvent e = (CachingControlEvent) event;
CachingControl cc = e.getCachingControl();
// Add the bar if not already there ...
if (progressBar == null) {
if ((progressBar = cc.getControlComponent()) != null) {
panel.add(progressBar);
panel.setSize(progressBar.getPreferredSize());
validate();
}
}
} else if (event instanceof EndOfMediaEvent) {
player.setMediaTime(new Time(0));
player.start();
} else if (event instanceof ControllerErrorEvent) {
player = null;
Fatal(((ControllerErrorEvent)event).getMessage());
} else if (event instanceof ControllerClosedEvent) {
panel.removeAll();
}
}
void Fatal (String s) {
System.err.println("FATAL ERROR: " + s);
throw new Error(s);
}
}

JMF Based Camera Capture

Hi Friends,
this blog i submit for easy interface of JMF Camera Interface & also do the snapshot facility.
the codes for that are as follows

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import javax.media.Buffer;
import javax.media.CaptureDeviceInfo;
import javax.media.CaptureDeviceManager;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.Player;
import javax.media.control.FrameGrabbingControl;
import javax.media.format.VideoFormat;
import javax.media.util.BufferToImage;
import javax.swing.JButton;
import javax.swing.JComponent;
//import com.sun.image.codec.jpeg.JPEGCodec;
//import com.sun.image.codec.jpeg.JPEGEncodeParam;
//import com.sun.image.codec.jpeg.JPEGImageEncoder;
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); //Getting Camera Device Information
ml = di.getLocator(); //MediaLocator for Camera
try
{
player = Manager.createRealizedPlayer(ml);//Creating Player
player.start(); //Start Camera Player
Component comp; //for Getting Visual Player Component of Camera
if ((comp = player.getVisualComponent()) != null)
{
add(comp,BorderLayout.NORTH); //Add Camera to North
}
add(capture,BorderLayout.CENTER); //Add Capture Button in Between
add(imgpanel,BorderLayout.SOUTH); //Add Captured image
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
Frame f = new Frame("SwingCapture"); //Creating New Frame
SwingCapture cf = new SwingCapture(); //Main Object Create
f.addWindowListener(new WindowAdapter() //Window Closing
{
public void windowClosing(WindowEvent e)
{
playerclose(); //Closing Player
System.exit(0);
}
});
f.add("Center",cf); //Adding Main Object to Frame
f.pack(); //Packing Frame
f.setSize(new Dimension(320,550));
f.setVisible(true);
}
public static void playerclose()//Function While Closing Player
{
player.close();
player.deallocate();
}
public void actionPerformed(ActionEvent e)
{
JComponent c = (JComponent) e.getSource();//Getting Source of Event
if (c == capture)//Capture Button Event
{
FrameGrabbingControl fgc = (FrameGrabbingControl)
player.getControl("javax.media.control.FrameGrabbingControl");
buf = fgc.grabFrame(); // Grabbing a frame
btoi = new BufferToImage((VideoFormat)buf.getFormat());// Convert it to an image Buffer
img = btoi.createImage(buf); //Creating Image from Buffer
imgpanel.setImage(img); // show the image
saveJPG(img,"c:\\test.jpg"); // save image Function
}
}
class ImagePanel extends Panel //Panel for Displaying Cptured Image
{
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");
}
}
}



For more information visit Dwij IT Solutions.