Copyright (c) 2002 - 2003, Junzo Sato. All rights reserved.

QuickTime for Java

QuickTime for Java is one of the powerful standards to handle multimedia resources.
Combination of the QuickTime and the Mathematica mades some interesting things easy, such as composing fractal music and movie image capturing.

Junzo SATO

Jul 18th 2003
Jun 10th 2002

•Installation

QuickTime for Java is available from the Apple computer's web site: http://www.apple.com/.
In this notebook, java2 (ver 1.4.1_01) and QuickTime for Java 6.0 is used.

•Loading J/Link

<< JLink`

If[$MachineType === "PC", SetOptions[InstallJava, CommandLine -> "C:\j2sdk1.4.1_01\bin\java.exe"]]

{ClassPath -> Automatic, CommandLine -> C:\\j2sdk1.4.1_01\\bin\\java.exe, MicrosoftJava -> False, Java1 -> False}

InstallJava[]

LinkObject[C:\\j2sdk1.4.1_01\\bin\\java.exe, 2, 2]

Off[General :: spell, General :: spell1]

•Fractal Music with General MIDI

•Preparation

Open QTSession

LoadClass["quicktime.QTSession"]

JavaClass[quicktime.QTSession, 0]

If[quicktime`QTSession`isInitialized[] === False, quicktime`QTSession`open[]]

•Play note with General MIDI

Create a ToneDescription of an instrument in General MIDI. The piano (#1) is chosen here.

tonedesc = JavaNew["quicktime.std.music.ToneDescription", 1]

<<JavaObject[quicktime.std.music.ToneDescription] >>

NoteRequest and NoteChannel are prepared.

noterequest = JavaNew["quicktime.std.music.NoteRequest", tonedesc] notechannel = JavaNew["quicktime.std.music.NoteChannel", noterequest]

<<JavaObject[quicktime.std.music.NoteRequest] >>

<<JavaObject[quicktime.std.music.NoteChannel] >>

The function PlayNote[ ] is defined.

LoadClass["java.lang.Thread"] ;  PlayNote[n_, msec_] := ( notechannel @ playNote[n, 127] ;  Thread`sleep[msec] ;  notechannel @ playNote[n, 0])

Do[PlayNote[i, 100], {i, 0, 127}]

•Compose Fractal Music

<< Statistics`ContinuousDistributions`

ndist := NormalDistribution(0, 1)

gauss = Table[Random[ndist], {f, 1, 300}] ;

walk(β_) := Re(InverseFourier[Table[1/f^β^(1/2) gauss [[ f ]], {f, 1, 300}]])

list = walk(1.1) ; nlist = Round((127 (list - Min(list)))/(Max(list) - Min(list))) ;

ListPlot[nlist, PlotJoined -> True]

[Graphics:HTMLFiles/index_23.gif]

-Graphics -

•Play

Do[PlayNote[nlist[[i]], 100], {i, 1, Length[nlist]}]

•Dispose

Objects are released.

ReleaseObject[tonedesc] ReleaseObject[notechannel] ReleaseObject[noterequest]

QTSession‚ð•Â‚¶‚é

If[quicktime`QTSession`isInitialized[] === True, quicktime`QTSession`close[]]

•Control QuickTime Movie

A movie specified by the URL is played using QTPlayer.
MovieController controls movie frames.

[Graphics:HTMLFiles/index_28.gif]

[Graphics:HTMLFiles/index_29.gif]

•Open QTSession

LoadClass["quicktime.QTSession"]

JavaClass[quicktime.QTSession, 0]

If[quicktime`QTSession`isInitialized[] === False, quicktime`QTSession`open[]]

•Get the reference to the movie by specifying the URL

[Windows] c:\quicktime-for-java-sdk\media\jumps.mov
[MacOS X] /quicktime-for-java-sdk/media/jumps.mov

LoadClass["quicktime.std.movies.media.DataRef"]

JavaClass[quicktime.std.movies.media.DataRef, 2]

urlMovie = JavaNew["quicktime.std.movies.media.DataRef",  "file:///Documents and Settings/Administrator/My Documents/QTJava_6.0_SDK/Demos/media/jumps.mov"]

<<JavaObject[quicktime.std.movies.media.DataRef] >>

•Get Movie, MovieController, and QTPlayer.

LoadClass["quicktime.std.movies.Movie"]

JavaClass[quicktime.std.movies.Movie, 6]

LoadClass["quicktime.std.StdQTConstants"] ;

m = Movie`fromDataRef[urlMovie, StdQTConstants`newMovieActive]

<<JavaObject[quicktime.std.movies.Movie] >>

LoadClass["quicktime.std.movies.MovieController"]

JavaClass[quicktime.std.movies.MovieController, 8]

mc = JavaNew["quicktime.std.movies.MovieController", m]

<<JavaObject[quicktime.std.movies.MovieController] >>

mc @ setKeysEnabled[True]

LoadClass["quicktime.app.players.QTPlayer"]

JavaClass[quicktime.app.players.QTPlayer, 11]

myPlayer = JavaNew["quicktime.app.players.QTPlayer", mc]

<<JavaObject[quicktime.app.players.QTPlayer] >>

•Show movie on QTCanvas

LoadClass["quicktime.app.display.QTCanvas"] ; <br /> qtcanvas = JavaNew["quicktime.app.display.QTCanvas"]

<<JavaObject[quicktime.app.display.QTCanvas] >>

JavaBlock[qtcanvas @ setClient[myPlayer, True]]

frame = JavaNew["com.wolfram.jlink.MathFrame", "QT in Java"]

<<JavaObject[com.wolfram.jlink.MathFrame] >>

frame @ add[qtcanvas]

<<JavaObject[quicktime.app.display.QTCanvas] >>

frame @ pack[] frame @ setLocation[0, 48]

JavaShow[frame]

•Control movie frames

[Graphics:HTMLFiles/index_60.gif]

[Graphics:HTMLFiles/index_61.gif]

Rewind to the beginning

m @ goToBeginning[]

Get time scale

timescale = mc @ getTimeScale[]

600

Get total seconds

total = N[m @ getDuration[]/timescale]

4.6433333333333335`

Calculate current second of the frame. If the beginning, the time is 0.

sec = N[mc @ getCurrentTime[]/timescale]

0.`

Forward the frames.

[Graphics:HTMLFiles/index_69.gif]

[Graphics:HTMLFiles/index_70.gif]

mc @ step[10]

Now the current second is increased.

sec = N[mc @ getCurrentTime[]/timescale]

1.`

Play movie with the rate 1.0.

mc @ play[1.0]

If the rate is set to 0.0 in the middle of the play, the movie is stopped.

[Graphics:HTMLFiles/index_75.gif]

[Graphics:HTMLFiles/index_76.gif]

mc @ play[0.0]

Go to the end of the movie.

[Graphics:HTMLFiles/index_78.gif]

[Graphics:HTMLFiles/index_79.gif]

m @ goToEnd[]

Get the current time information of a frame.
This information is set to the captureThis for later use.

•Windows

[Graphics:HTMLFiles/index_81.gif]

captureThis = mc @ getCurrentTime[]

1261

N[mc @ getCurrentTime[]/timescale]

2.1016666666666666`

•MacOS X

[Graphics:HTMLFiles/index_86.gif]

captureThis = mc @ getCurrentTime[]

1423

N[mc @ getCurrentTime[]/timescale]

2.3716666666666666`

•Dispose

Objects are released.

ReleaseObject[qtcanvas]

frame @ dispose[]

ReleaseObject[frame] ReleaseObject[mc] ReleaseObject[m] ReleaseObject[myPlayer] ReleaseObject[urlMovie]

Close QTSession

If[quicktime`QTSession`isInitialized[] === True, quicktime`QTSession`close[]]

•Get JavaImage from QuickTime Movie (Frame Caputuring)

Using the QTImageProducer, a frame of the movis is derived as a JavaImage.

•Open QTSession

LoadClass["quicktime.QTSession"]

JavaClass[quicktime.QTSession, 0]

If[quicktime`QTSession`isInitialized[] === False, quicktime`QTSession`open[]]

•Get QTFile

LoadClass["quicktime.io.QTFile"]

JavaClass[quicktime.io.QTFile, 20]

Switch[$MachineType,  "PC",  qtf = JavaNew["quicktime.io.QTFile",  "C ... ac",  qtf = JavaNew["quicktime.io.QTFile", "/QTJavaSDK/media/jumps.mov"]]

<<JavaObject[quicktime.io.QTFile] >>

•Get movie by OpenMovieFile

Get Movie and MoviePlayer by opening the file.

LoadClass["quicktime.io.OpenMovieFile"] ; openMovieFile = OpenMovieFile`asRead[qtf]

<<JavaObject[quicktime.io.OpenMovieFile] >>

LoadClass["quicktime.std.movies.Movie"] ; movie = Movie`fromFile[openMovieFile]

<<JavaObject[quicktime.std.movies.Movie] >>

LoadClass["quicktime.std.StdQTConstants"] ; JavaBlock[movie @ getTimeBase[] @ setFlags[StdQTConstants`loopTimeBase]]

LoadClass["quicktime.app.players.MoviePlayer"] ; moviePlayer = JavaNew["quicktime.app.players.MoviePlayer", movie]

<<JavaObject[quicktime.app.players.MoviePlayer] >>

Get image size of the movie.

r = moviePlayer @ getDisplayBounds[] r @ toString[]

<<JavaObject[quicktime.qd.QDRect] >>

quicktime.qd.QDRect[x=0.0,y=0.0,width=160.0,height=120.0]

LoadClass["java.awt.Dimension"] ; d = JavaNew["java.awt.Dimension", r @ getWidth[], r @ getHeight[]] d @ toString[]

<<JavaObject[java.awt.Dimension] >>

java.awt.Dimension[width=160,height=120]

Get time information

timescale = moviePlayer @ getScale[]

600

total = N[moviePlayer @ getDuration[]/timescale]

4.6433333333333335`

Specify the frame to be captured as a JavaImage. The captureThis was set by the previous example in this notebook.

captureThis

1261

•Windows

moviePlayer @ setTime[captureThis]

sec = N[moviePlayer @ getTime[]/timescale]

2.1016666666666666`

•MacOS X

moviePlayer @ setTime[captureThis]

sec = N[moviePlayer @ getTime[]/timescale]

2.3716666666666666`

•Create QTImageProducer

LoadClass["quicktime.app.image.QTImageProducer"] ; imgProducer = JavaNew["quicktime.app.image.QTImageProducer", moviePlayer, d]

<<JavaObject[quicktime.app.image.QTImageProducer] >>

•Create the JavaImage and show it on the MathCanvas

Get the frame as the JavaImage

LoadClass["java.awt.Toolkit"]

JavaClass[java.awt.Toolkit, 31]

tk = Toolkit`getDefaultToolkit[] ; javaImage = tk @ createImage[imgProducer]

<<JavaObject[sun.awt.windows.WImage] >>

Show JavaImage on the MathCanvas.

[Graphics:HTMLFiles/index_133.gif]

[Graphics:HTMLFiles/index_134.gif]

frame = JavaNew["com.wolfram.jlink.MathFrame", qtf @ getName[]] ; JavaBlock[frame @  ... jlink.MathCanvas"] ; mathCanvas @ setSize[d] ; frame @ add["Center", mathCanvas] ;

mathCanvas @ setImage[javaImage] frame @ pack[]

frame @ layout[] ; frame @ setLocation[0, 48] ; JavaShow[frame]

•Java Photo Editor and Digital Image Processing (Option)

If the user is using Java Photo Editor which is developed by the author, and Digital Image Processing package,
the captured JavaImage is ready to use for digital image processing operations.

<< ImageProcessing`

<< JLinkProgramming`JavaPhotoEditor`

Welcome to the Java Photo Editor!

Loading JavaPhotoEditor...

Finished loading JavaPhotoEditor

<< JLinkProgramming`JavaPhotoEditor`DIPSupportJ`

img = JavaImageToImageData[javaImage]

-ImageData-

Show[Graphics[img]]

[Graphics:HTMLFiles/index_147.gif]

-Graphics -

TryFilters[data_] := Show[GraphicsArray[Graphics /@ { data,  DiscreteConvolve[data, BoxFilter[ ...  15, 7], Centered -> True],  DiscreteConvolve[data, LoGFilter[15, 15], Centered -> True] }]]

TryFilters[img]

[Graphics:HTMLFiles/index_151.gif]

-GraphicsArray -

•Dispose

Objects are released.

ReleaseObject[mathCanvas] frame @ dispose[] ReleaseObject[frame]

ReleaseObject[javaImage] ReleaseObject[tk] ReleaseObject[moviePlayer] ReleaseObject[imgProduce ... seObject[movie] ReleaseObject[openMovieFile] ReleaseObject[qtf] ReleaseObject[r] ReleaseObject[d]

Close QTSession

If[quicktime`QTSession`isInitialized[] === True, quicktime`QTSession`close[]]

•Uninstall

UninstallJava[]

C:\\j2sdk1.4.1_01\\bin\\java.exe

Quit[]


Converted by Mathematica  (July 18, 2003)