B
Boris Gorjan
Does anyone use Flash utilities of Flagstone Software?
http://www.flagstonesoftware.com/
On their page there is an example of how to pack an image inside a flash (and
show it):
http://www.flagstonesoftware.com/code/Example.java
http://www.flagstonesoftware.com/code/ShowImage.java
I wanted to extend that by making a movie out of a series of images. I thought
that all I had to do is create a list of Files and use ShowImage's code inside a
loop. The result is not as expected. A .swf file is generated but when I want to
play it, only the first image is shown. Can anyone tell me what am I doing
wrong? Or what am I not doing that I should?
Here's the code:
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import com.flagstone.transform.FSColorTable;
import com.flagstone.transform.FSDefineObject;
import com.flagstone.transform.FSDefineShape3;
import com.flagstone.transform.FSPlaceObject2;
import com.flagstone.transform.FSSetBackgroundColor;
import com.flagstone.transform.FSShowFrame;
import com.flagstone.transform.FSSolidLine;
import com.flagstone.transform.util.FSImageConstructor;
public class MovieAssembler extends Example {
public static void main(String[] args)
{
try {
new MovieAssembler(args);
}
catch(Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public MovieAssembler(String[] args)
throws Exception
{
super(args);
createMovie();
writeFile("MovieAssembler.swf");
}
/**
*
*/
public static byte[] read(InputStream is) throws IOException {
if(is == null)
return null;
ByteArrayOutputStream baos = null;
try {
baos = new ByteArrayOutputStream();
byte[] block = new byte[1024];
int bytesRead = -1;
while( (bytesRead = is.read(block, 0, block.length)) > 0)
baos.write(block, 0, bytesRead);
return baos.toByteArray();
}
finally {
try {
baos.reset();
}
catch(Exception x) {}
try {
baos.close();
}
catch(Exception x) {}
try {
is.close();
}
catch(Exception x) {}
}
}
public void createMovie()
throws Exception
{
movie.setFrameRate(25.0f);
movie.add(new FSSetBackgroundColor(FSColorTable.lightblue()));
//A series of jpg files is in this dir
String sourceDirname = "E:/temp/video/frames/";
File sourceDir = new File(sourceDirname);
FileFilter filter = new FileFilter() {
public boolean accept(File file) {
return file.getName().endsWith(".jpg");
}
};
File[] imageFiles = sourceDir.listFiles(filter);
if(imageFiles != null) {
for(int i = 0; i < imageFiles.length; i++) {
byte[] content = read(new FileInputStream(imageFiles));
if(content == null)
continue;
FSImageConstructor imageGenerator =
new FSImageConstructor(content);
int imageId = movie.newIdentifier();
int shapeId = movie.newIdentifier();
int xOrigin = imageGenerator.getWidth()/2;
int yOrigin = imageGenerator.getHeight()/2;
// Generate the image defintion
FSDefineObject image = imageGenerator.defineImage(imageId);
/*
* All images must be displayed as a bitmap fill inside a
shape. The
* FSImageConstructor class generates the shape enclosing the
image.
* If no border is required then the line style may be set to null.
*/
FSDefineShape3 shape =
imageGenerator.defineEnclosingShape(shapeId, imageId,
-xOrigin, -yOrigin, new FSSolidLine(20, FSColorTable.black()));
/*
* Add all the objects together to create the movie.
*/
//movie.setFrameRate(25.0f);
movie.setFrameSize(shape.getBounds());
//movie.add(new FSSetBackgroundColor(FSColorTable.lightblue()));
movie.add(image);
movie.add(shape);
movie.add(new FSPlaceObject2(shapeId, 1, 0, 0));
movie.add(new FSShowFrame());
}
}
}
}
http://www.flagstonesoftware.com/
On their page there is an example of how to pack an image inside a flash (and
show it):
http://www.flagstonesoftware.com/code/Example.java
http://www.flagstonesoftware.com/code/ShowImage.java
I wanted to extend that by making a movie out of a series of images. I thought
that all I had to do is create a list of Files and use ShowImage's code inside a
loop. The result is not as expected. A .swf file is generated but when I want to
play it, only the first image is shown. Can anyone tell me what am I doing
wrong? Or what am I not doing that I should?
Here's the code:
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import com.flagstone.transform.FSColorTable;
import com.flagstone.transform.FSDefineObject;
import com.flagstone.transform.FSDefineShape3;
import com.flagstone.transform.FSPlaceObject2;
import com.flagstone.transform.FSSetBackgroundColor;
import com.flagstone.transform.FSShowFrame;
import com.flagstone.transform.FSSolidLine;
import com.flagstone.transform.util.FSImageConstructor;
public class MovieAssembler extends Example {
public static void main(String[] args)
{
try {
new MovieAssembler(args);
}
catch(Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public MovieAssembler(String[] args)
throws Exception
{
super(args);
createMovie();
writeFile("MovieAssembler.swf");
}
/**
*
*/
public static byte[] read(InputStream is) throws IOException {
if(is == null)
return null;
ByteArrayOutputStream baos = null;
try {
baos = new ByteArrayOutputStream();
byte[] block = new byte[1024];
int bytesRead = -1;
while( (bytesRead = is.read(block, 0, block.length)) > 0)
baos.write(block, 0, bytesRead);
return baos.toByteArray();
}
finally {
try {
baos.reset();
}
catch(Exception x) {}
try {
baos.close();
}
catch(Exception x) {}
try {
is.close();
}
catch(Exception x) {}
}
}
public void createMovie()
throws Exception
{
movie.setFrameRate(25.0f);
movie.add(new FSSetBackgroundColor(FSColorTable.lightblue()));
//A series of jpg files is in this dir
String sourceDirname = "E:/temp/video/frames/";
File sourceDir = new File(sourceDirname);
FileFilter filter = new FileFilter() {
public boolean accept(File file) {
return file.getName().endsWith(".jpg");
}
};
File[] imageFiles = sourceDir.listFiles(filter);
if(imageFiles != null) {
for(int i = 0; i < imageFiles.length; i++) {
byte[] content = read(new FileInputStream(imageFiles));
if(content == null)
continue;
FSImageConstructor imageGenerator =
new FSImageConstructor(content);
int imageId = movie.newIdentifier();
int shapeId = movie.newIdentifier();
int xOrigin = imageGenerator.getWidth()/2;
int yOrigin = imageGenerator.getHeight()/2;
// Generate the image defintion
FSDefineObject image = imageGenerator.defineImage(imageId);
/*
* All images must be displayed as a bitmap fill inside a
shape. The
* FSImageConstructor class generates the shape enclosing the
image.
* If no border is required then the line style may be set to null.
*/
FSDefineShape3 shape =
imageGenerator.defineEnclosingShape(shapeId, imageId,
-xOrigin, -yOrigin, new FSSolidLine(20, FSColorTable.black()));
/*
* Add all the objects together to create the movie.
*/
//movie.setFrameRate(25.0f);
movie.setFrameSize(shape.getBounds());
//movie.add(new FSSetBackgroundColor(FSColorTable.lightblue()));
movie.add(image);
movie.add(shape);
movie.add(new FSPlaceObject2(shapeId, 1, 0, 0));
movie.add(new FSShowFrame());
}
}
}
}