1.00/1.001 - Intro To Computation and Engineering Problem Solving Fall 2002
1.00/1.001 - Intro To Computation and Engineering Problem Solving Fall 2002
001 Intro To
Problem Solving
Fall 2002
Media in Java
Topics
The AWT, Java 2D API and the newer Java
Media Framework provide tools for working
with diverse media.
structure of digital media
the AWT Image class and its related methods
the MediaTracker class
the Java Media Framework structure
use of the JMF for video and audio playing
media synchronization
1
Digital Media
Includes text, video, audio, animation, images
and vector graphics
video and audio have temporal dimension
files are often large and compressed
diverse file formats
latency and network throughput are often
issues
2
Images in the AWT
Image class supports the "push model". Images are
"pushed" to an ImageConsumer. Works well for
applets and other Web applications.
A digital image (once loaded) is a rectangular
collection of pixels, where each pixel represents a
color
Java AWT has an Image class for loading and
displaying images
AWT can load image from file or URL over the Web
The Java 2D API has BufferedImage class that has
much wider range of capabilities
Image in an Applet
Simplest case is loading an image into an
application
construct an Image object with code such as:
Image im = new Image(myImageFile.gif);
display image in the paintComponent()
method of Component object using
drawImage() method:
boolean b drawImage(Image im, int x, int y,
ImageObserver iob)
returns true if Image is fully loaded, false otherwise
3
ImageObserver interface
Images take a long time to load, and
may load incrementally over time
ImageObserver is an interface that has
methods for handling notification of
state of image loading. It can use this
for redisplay as needed.
JFrame and Applet both implement
ImageObserver interface
Drawing an Image
import java.awt.*;
import javax.swing.*;
public ImagePanel(Image i) {
im = i; }
super.paintComponent(g);
if(im == null)
4
Other Image loading methods
public Image getImage(URL u)
loads image from Web from URL u. URL is a
class in java.net package. Its simplest
constructor is
URL u = new URL(String s)
public Image getImage(URL u, String name)-u
is the base location on a Web server; name is
the file name
MediaTracker class
Sometimes you want to (or have to wait) for
image to load fully before proceeding
MediaTracker class allows you to register
images and wait until they are fully loaded
supports waiting for groups of images
you register images with MediaTracker object
MediaTracker can establish priorities for
image loading
5
MediaTracker Constructor and Methods
MediaTracker Use
MediaTracker iTrack = new MediaTracker(this);
iTrack.addImage(tImage, 1);
try {
iTrack.waitForID(1);
catch(InterruptedException e) {
6
import java.awt.*;
import javax.swing.*;
new ImageDisplay(args[0]); }
testImage = Toolkit.getDefaultToolkit().getImage(imageFile);
iTrack.addImage(testImage, 1);
try {
iTrack.waitForID(1); }
catch(InterruptedException e) {
System.out.println("Exception in loading image(s).");
System.exit(0);
}
7
Java 2D API
Java 2D API provide "immediate mode"
model for images. the BufferedImage class is
central to this.
BufferedImage class allows much greater
programmatic control than AWT's Image
class
BufferedImage supports affine
transformations, image filtering, multiple color
models, compositing, multiple compression
methods
import java.awt.*;
import javax.swing.*;
import java.io.*;
8
public BufferedImageDisplay(String imageFile) {
setTitle("Simple Image Displayer");
try {
InputStream inStream = new FileInputStream(imageFile);
JPEGImageDecoder de =
JPEGCodec.createJPEGDecoder(inStream);
testImage = de.decodeAsBufferedImage();
}
catch(IOException e) {
System.out.println("Exception in loading image "+imageFile);
System.exit(0); }
iPanel = new ImagePanel(testImage);
getContentPane().add(iPanel);
setSize(testImage.getWidth(this)+50, testImage.getHeight(this)+50);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
} // end of ImageDisplay
9
Common Color Spaces
RGB-Red, Green Blue, often with an
Alpha component for opacity
CMYK Cyan, Magenta, Yellow and
Black
GrayScale
Buffered Operations
There are standard filters for operating on
BufferedImages that implement the
BufferedImageOp interface
Basic coding pattern:
create source BufferedImage object
Use a "factory method" to create an appropriate
converter that implements BufferedImageOp
interface
run converter object's filter method, as in
BufferedImage result = converter.filter(source, null); // null
can be the destination image
10
Image Sharpening Example
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import java.io.*;
11
float kern[] = {-1f, -1f, -1f, -1f, 9f, -1f,
-1f, -1f, -1f};
Kernel sKernel = new Kernel(3,3,kern);
ConvolveOp sharp = new ConvolveOp(sKernel);
}
} // end of SharpenOp
12
JMF Concepts
DataSource - source of media content--file
server, etc.
Two types are PushDataSource and
PullDataSource
Example
URL mURL = new URL(http://www-
ceci.mit.edu/seg6.mpg);
DataSource mySource = new DataSource(mURL)
Player Interface
Player-an interface for a player of a media
type. Classes meeting this interface will
typically involve native code. Implementations
of Player can be plug ins.
Concept is to allow many different groups
build Player implementations.
13
Manager class
A factory that can create a player
appropriate to particular DataSource.
Manager can locate Player classes if
available and provide them to applications
public static Player createPlayer (DataSource source)
throws IOException, NoPlayerException
public static Player createPlayer(URL sourceURL)
throws IOException, NoPlayerException
14
Player States
unrealized - Player has been created but doesn't
know anything about its media except the URL
realizing - Player is in process of acquiring whatever
resouces it needs
realized - Player finished realizing and knows about
its resources and media. It now knows how to display
itself.
prefetching - Player preparing to present media
prefetched - Player done preparing to present media
started - clock of Player is running
15
Player Components
public Component getControlPanelComponent() -
returns a panel with controls for media
(start,stop,etc.)
public Component getVisualComponent()-returns a
Component in which media can be displayed
public Component getGainComponent() - returns a
Component with audio volume controls
16
Player Events
17
Sample JMF Usage
Create Frame for video player to display in
create URL for video file
Use Manager class to create appropriate
Player for media
handle event where Player is fully realized,
and add visual and control Component of
Player to Frame
start the Player by calling player.start()
method
Container m_container;
fr.setSize(428, 398);
fr.setVisible(true);
18
// constructor method for VideoPlayer class
public VideoPlayer(String mediaFile, Container container) {
URL mediaURL = null;
m_container = container;
m_container.setLayout(new BorderLayout());
try {
mediaURL = new URL(mediaFile);
player = Manager.createPlayer(mediaURL);
player.addControllerListener(this);
player.realize();
catch (MalformedURLException e) {
return; }
catch(NoPlayerException e) {
return; }
catch(IOException e) {
return; }
// When player is realized, get its visual and control Components and
// put them into the container and start() the complete player.
public void controllerUpdate(ControllerEvent event)
{
if(event instanceof RealizeCompleteEvent){
if((visualComponent = player.getVisualComponent()) != null)
m_container.add("Center", visualComponent);
if ((controlComponent = player.getControlPanelComponent())
!= null)
m_container.add("South",controlComponent);
m_container.validate(); // ensure layout is up to date
m_container.repaint(); // ensure that anything drawn painted
player.start(); // start the player
}
else if (event instanceof ControllerErrorEvent){
System.err.println("*** ControllerErrorEvent *** " +
((ControllerErrorEvent)event).getMessage());
}
} // end of controllerUpdate method
} // end of VideoPlayer class
19