Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

Flash, movie

Thread view: 
Boris Gorjan - 16 Feb 2006 14:09 GMT
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[i]));
                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());
            }
        }
    }
}
Timo Stamm - 16 Feb 2006 14:29 GMT
Boris Gorjan schrieb:
> 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.

This is just a guess (I dont know the API), but I think you are writing
all images on the first frame. You should advance 1 frame after adding
an image.

Timo
Boris Gorjan - 16 Feb 2006 15:44 GMT
> Boris Gorjan schrieb:
>
[quoted text clipped - 9 lines]
>
> Timo

Hm. Might be. But how do I advance one frame?

Meanwhile I "discovered" the actions. Just before adding an image, I say

movie.add(new FSFrameLabel(Integer.toString(i)));

and after the for loop I say

ArrayList actions = new ArrayList();
actions.add(new FSPush("0"));
actions.add(new FSGotoFrame2(false));
FSDoAction frameAction = new FSDoAction(actions);
movie.add(frameAction);

like in the api docs comments/descriptions. This supposedly rewinds the movie to
a first (0th) frame and starts it. But... not in my case.

Any ideas?
Timo Stamm - 16 Feb 2006 18:14 GMT
Boris Gorjan schrieb:
> Any ideas?

Sorry, no idea. Theres a forum on the site, I would try posting the
question there,

Timo
Boris Gorjan - 17 Feb 2006 12:33 GMT
> Boris Gorjan schrieb:
>
[quoted text clipped - 4 lines]
>
> Timo

I nailed it down. ;-)

Forget the "actions", the trick is this:

if(i == 0)
    movie.add(new FSPlaceObject2(shapeId, 1, 0, 0));
else
    movie.add(new FSPlaceObject2(shapeId, 1));

It means something like this: initialy you place the first object, for all other
objects you have to _re_place the previous one. And that's what the second
constructor does.

I guess I'll have to get used to Flash quirks. It would certainly help if I knew
anything about ActionScript. <lazymode>Which I don't really want to
do.</lazymode> So, a Flagstone tutorial would come in handy. Anyone  from
Flagstone listening?

On to the next issue: sound. Is there an internal Flash format, or does one
simply "link in" a midi or an mp3 (or ...)? How?
Timo Stamm - 17 Feb 2006 13:11 GMT
Boris Gorjan schrieb:
> I nailed it down. ;-)
>
[quoted text clipped - 8 lines]
> all other objects you have to _re_place the previous one. And that's
> what the second constructor does.

Thanks for posting the solution.

> I guess I'll have to get used to Flash quirks. It would certainly help
> if I knew anything about ActionScript.

I do know ActionScript (it's just ECMAScript + some host objects), but
it didn't help at all. The whole API is very specific to the SWF file
format. I guess it might help a lot to read the file format
specification. It is freely available somewhere on macromedia.com.

Timo


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.