Results 1 to 7 of 7

Thread: Make it work !

  1. #1
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Help !

    I have found this:
    Java Code:
    import java.awt.Dimension;
    import java.io.File;
    import java.io.IOException;
    import javax.media.Buffer;
    import javax.media.ConfigureCompleteEvent;
    import javax.media.ControllerEvent;
    import javax.media.ControllerListener;
    import javax.media.DataSink;
    import javax.media.EndOfMediaEvent;
    import javax.media.Format;
    import javax.media.Manager;
    import javax.media.MediaLocator;
    import javax.media.PrefetchCompleteEvent;
    import javax.media.Processor;
    import javax.media.RealizeCompleteEvent;
    import javax.media.ResourceUnavailableEvent;
    import javax.media.Time;
    import javax.media.control.TrackControl;
    import javax.media.datasink.DataSinkErrorEvent;
    import javax.media.datasink.DataSinkEvent;
    import javax.media.datasink.DataSinkListener;
    import javax.media.datasink.EndOfStreamEvent;
    import javax.media.format.VideoFormat;
    import javax.media.protocol.ContentDescriptor;
    import javax.media.protocol.DataSource;
    import javax.media.protocol.FileTypeDescriptor;
    import javax.media.protocol.PullBufferDataSource;
    import javax.media.protocol.PullBufferStream;
    
    
    
    public class AviCreator implements ControllerListener, DataSinkListener {
        public boolean doIt(
            int width,
            int height,
            int frameRate,
            MediaLocator outML)
        {
            ImageDataSource ids = new ImageDataSource(width, height, frameRate);
    
            Processor p;
    
            try
            {
                System.err.println(
                    "- create processor for the image datasource ...");
                p = Manager.createProcessor(ids);
            }
            catch (Exception e)
            {
                System.err.println(
                    "Yikes! Cannot create a processor from the data source.");
                return false;
            }
    
            p.addControllerListener(this);
    
            // Put the Processor into configured state so we can set
            // some processing options on the processor.
            p.configure();
            if (!waitForState(p, Processor.Configured))
            {
                System.err.println("Failed to configure the processor.");
                return false;
            }
    
            // Set the output content descriptor to QuickTime.
            p.setContentDescriptor(
                new ContentDescriptor(FileTypeDescriptor.MSVIDEO));
    
            // Query for the processor for supported formats.
            // Then set it on the processor.
            TrackControl tcs[] = p.getTrackControls();
            Format f[] = tcs[0].getSupportedFormats();
            if (f == null || f.length <= 0)
            {
                System.err.println(
                    "The mux does not support the input format: "
                        + tcs[0].getFormat());
                return false;
            }
    
            tcs[0].setFormat(f[0]);
    
            System.err.println("Setting the track format to: " + f[0]);
    
            // We are done with programming the processor. Let's just
            // realize it.
            p.realize();
            if (!waitForState(p, Processor.Realized))
            {
                System.err.println("Failed to realize the processor.");
                return false;
            }
    
            // Now, we'll need to create a DataSink.
            DataSink dsink;
            if ((dsink = createDataSink(p, outML)) == null)
            {
                System.err.println(
                    "Failed to create a DataSink for the given output MediaLocator: "
                        + outML);
                return false;
            }
    
            dsink.addDataSinkListener(this);
            fileDone = false;
    
            System.err.println("start processing...");
    
            // OK, we can now start the actual transcoding.
            try
            {
                p.start();
                dsink.start();
            }
            catch (IOException e)
            {
                System.err.println("IO error during processing");
                return false;
            }
    
            // Wait for EndOfStream event.
            waitForFileDone();
    
            // Cleanup.
            try
            {
                dsink.close();
            }
            catch (Exception e)
            {
            }
            p.removeControllerListener(this);
    
            System.err.println("...done processing.");
    
            return true;
        }
    
        /**
        * Create the DataSink.
        */
        private DataSink createDataSink(Processor p, MediaLocator outML)
        {
    
            DataSource ds;
    
            if ((ds = p.getDataOutput()) == null)
            {
                System.err.println(
                    "Something is really wrong: the processor does not have an output DataSource");
                return null;
            }
    
            DataSink dsink;
    
            try
            {
                System.err.println("- create DataSink for: " + outML);
                dsink = Manager.createDataSink(ds, outML);
                dsink.open();
            }
            catch (Exception e)
            {
                System.err.println("Cannot create the DataSink: " + e);
                return null;
            }
    
            return dsink;
        }
    
        private Object waitSync = new Object();
        private boolean stateTransitionOK = true;
    
        /**
        * Block until the processor has transitioned to the given state.
        * Return false if the transition failed.
        */
        private boolean waitForState(Processor p, int state)
        {
            synchronized (waitSync)
            {
                try
                {
                    while (p.getState() < state && stateTransitionOK)
                        waitSync.wait();
                }
                catch (Exception e)
                {
                }
            }
            return stateTransitionOK;
        }
    
        /**
        * Controller Listener.
        */
        public void controllerUpdate(ControllerEvent evt)
        {
    
            if (evt instanceof ConfigureCompleteEvent
                || evt instanceof RealizeCompleteEvent
                || evt instanceof PrefetchCompleteEvent)
            {
                synchronized (waitSync)
                {
                    stateTransitionOK = true;
                    waitSync.notifyAll();
                }
            }
            else if (evt instanceof ResourceUnavailableEvent)
            {
                synchronized (waitSync)
                {
                    stateTransitionOK = false;
                    waitSync.notifyAll();
                }
            }
            else if (evt instanceof EndOfMediaEvent)
            {
                evt.getSourceController().stop();
                evt.getSourceController().close();
            }
        }
    
        private Object waitFileSync = new Object();
        private boolean fileDone = false;
        private boolean fileSuccess = true;
    
        /**
        * Block until file writing is done.
        */
        private boolean waitForFileDone()
        {
            synchronized (waitFileSync)
            {
                try
                {
                    while (!fileDone)
                        waitFileSync.wait();
                }
                catch (Exception e)
                {
                }
            }
            return fileSuccess;
        }
    
        /**
        * Event handler for the file writer.
        */
        public void dataSinkUpdate(DataSinkEvent evt)
        {
    
            if (evt instanceof EndOfStreamEvent)
            {
                synchronized (waitFileSync)
                {
                    fileDone = true;
                    waitFileSync.notifyAll();
                }
            }
            else if (evt instanceof DataSinkErrorEvent)
            {
                synchronized (waitFileSync)
                {
                    fileDone = true;
                    fileSuccess = false;
                    waitFileSync.notifyAll();
                }
            }
        }
    
        public static void main(String args[]) throws Exception
        {
    
            // Parse the arguments.
            int i = 0;
            int width = 500, height = 500, frameRate = 90;
            String outputURL = null;
    
            width = 128;
            height = 128;
            outputURL = "test.avi";
    
            // Generate the output media locators.
            MediaLocator oml;
    
            if ((oml = createMediaLocator(outputURL)) == null)
            {
                System.err.println("Cannot build media locator from: " + outputURL);
                System.exit(0);
            }
    
            AviCreator imageToMovie = new AviCreator();
            imageToMovie.doIt(width, height, frameRate, oml);
    
            System.exit(0);
        }
    
        /**
        * Create a media locator from the given string.
        */
        private static MediaLocator createMediaLocator(String url)
        {
    
            MediaLocator ml;
    
            if (url.indexOf(":") > 0 && (ml = new MediaLocator(url)) != null)
                return ml;
    
            if (url.startsWith(File.separator))
            {
                if ((ml = new MediaLocator("file:" + url)) != null)
                    return ml;
            }
            else
            {
                String file =
                    "file:" + System.getProperty("user.dir") + File.separator + url;
                if ((ml = new MediaLocator(file)) != null)
                    return ml;
            }
    
            return null;
        }
    
        ///////////////////////////////////////////////
        //
        // Inner classes.
        ///////////////////////////////////////////////
    
        /**
        * A DataSource to read from a list of JPEG image files and
        * turn that into a stream of JMF buffers.
        * The DataSource is not seekable or positionable.
        */
        private class ImageDataSource extends PullBufferDataSource
        {
    
            private ImageSourceStream streams[];
    
            ImageDataSource(int width, int height, int frameRate)
            {
                streams = new ImageSourceStream[1];
                streams[0] = new ImageSourceStream(width, height, frameRate);
            }
    
            public void setLocator(MediaLocator source)
            {
            }
    
            public MediaLocator getLocator()
            {
                return null;
            }
    
            /**
            * Content type is of RAW since we are sending buffers of video
            * frames without a container format.
            */
            public String getContentType()
            {
                return ContentDescriptor.RAW;
            }
    
            public void connect()
            {
            }
    
            public void disconnect()
            {
            }
    
            public void start()
            {
            }
    
            public void stop()
            {
            }
    
            /**
            * Return the ImageSourceStreams.
            */
            public PullBufferStream[] getStreams()
            {
                return streams;
            }
    
            /**
            * We could have derived the duration from the number of
            * frames and frame rate. But for the purpose of this program,
            * it's not necessary.
            */
            public Time getDuration()
            {
                System.out.println("dur is " + streams[0].nextImage);
                //return new Time(1000000000);
                return DURATION_UNKNOWN;
            }
    
            public Object[] getControls()
            {
                return new Object[0];
            }
    
            public Object getControl(String type)
            {
                return null;
            }
        }
    
        /**
        * The source stream to go along with ImageDataSource.
        */
        class ImageSourceStream implements PullBufferStream
        {
    
            final int width, height;
            final VideoFormat format;
    
            // Bug fix from Forums - next two lines
            float frameRate;
            long seqNo = 0;
    
            int nextImage = 0; // index of the next image to be read.
            boolean ended = false;
    
            public ImageSourceStream(int width, int height, int frameRate)
            {
                this.width = width;
                this.height = height;
    
                // Bug fix from Forums (next line)
                this.frameRate = (float) frameRate;
    
                final int rMask = 0x00ff0000;
                final int gMask = 0x0000FF00;
                final int bMask = 0x000000ff;
    
                format =
                    new javax.media.format.RGBFormat(
                        new Dimension(width, height),
                        Format.NOT_SPECIFIED,
                        Format.intArray,
                        frameRate,
                        32,
                        rMask,
                        gMask,
                        bMask);
            }
    
            /**
            * We should never need to block assuming data are read from files.
            */
            public boolean willReadBlock()
            {
                return false;
            }
    
            /**
            * This is called from the Processor to read a frame worth
            * of video data.
            */
            public void read(Buffer buf) throws IOException
            {
                // Check if we've finished all the frames.
                if (nextImage >= 100)
                {
                    // We are done. Set EndOfMedia.
                    System.err.println("Done reading all images.");
                    buf.setEOM(true);
                    buf.setOffset(0);
                    buf.setLength(0);
                    ended = true;
                    return;
                }
    
                nextImage++;
    
                int data[] = null;
    
                // Check the input buffer type & size.
    
                if (buf.getData() instanceof int[])
                    data = (int[]) buf.getData();
    
                // Check to see the given buffer is big enough for the frame.
                if (data == null || data.length < width * height)
                {
                    data = new int[width * height];
                    buf.setData(data);
                }
    
                // Bug fix from Forums ( next 3 lines).
                long time = (long) (seqNo * (1000 / frameRate) * 1000000);
                buf.setTimeStamp(time);
                buf.setSequenceNumber(seqNo++);
    
                java.awt.Color clr = java.awt.Color.red;
                if (nextImage > 30)
                    clr = java.awt.Color.GREEN;
                if (nextImage > 60)
                    clr = java.awt.Color.BLUE;
                for (int i = 0; i < width * height; i++)
                {
                    // TODO - figure what the guy was trying to do here.
                    data[i] = clr.getRGB();
                }
    
                buf.setOffset(0);
                buf.setLength(width * height);
                buf.setFormat(format);
                buf.setFlags(buf.getFlags() | Buffer.FLAG_KEY_FRAME);
            }
    
            /**
            * Return the format of each video frame. That will be JPEG.
            */
            public Format getFormat()
            {
                return format;
            }
    
            public ContentDescriptor getContentDescriptor()
            {
                return new ContentDescriptor(ContentDescriptor.RAW);
            }
    
            public long getContentLength()
            {
                return 0;
            }
    
            public boolean endOfStream()
            {
                return ended;
            }
    
            public Object[] getControls()
            {
                return new Object[0];
            }
    
            public Object getControl(String type)
            {
                return null;
            }
        }
    }
    but I have no idea how to make it work (it works but I don't know how to add my own images).

    Could anyone help me out?
    Last edited by PhQ; 09-19-2010 at 08:43 PM.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,017
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by PhQ View Post
    I have found this: [snip]
    Cute; what does it do, or what is it supposed to do? Don't throw just a bunch of code at us and ask us to make it work.

    kind regards,

    Jos

  3. #3
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    Quote Originally Posted by JosAH View Post
    Cute; what does it do, or what is it supposed to do? Don't throw just a bunch of code at us and ask us to make it work.

    kind regards,

    Jos
    It should convert jpeg images into a .avi video file.
    It also uses JMF.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,017
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by PhQ View Post
    It should convert jpeg images into a .avi video file.
    It also uses JMF.
    So it doesn't do what you want it to do. What does it do then? Doesn't it compile? Does it throw an exception at you? Class not found exceptions? What? We are not going to 'make it work!' for you without you showing some efforts first; better go to rentacoder.com first then.

    kind regards,

    Jos

  5. #5
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    Quote Originally Posted by JosAH View Post
    So it doesn't do what you want it to do. What does it do then? Doesn't it compile? Does it throw an exception at you? Class not found exceptions? What? We are not going to 'make it work!' for you without you showing some efforts first; better go to rentacoder.com first then.

    kind regards,

    Jos
    I does what I want to do. Everything is perfect. I don't know how to use my own pictures, because when I run it - it shows a red square for a few seconds then a blue square square and then a blue square.

  6. #6
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    Meh, I give up.

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,017
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by PhQ View Post
    Meh, I give up.
    That's the spirit!

    kind regards,

    Jos

Similar Threads

  1. Unnable to make validations work.
    By fierof2 in forum Web Frameworks
    Replies: 1
    Last Post: 07-28-2010, 11:31 AM
  2. How do i make this work What am i doing Wrong.
    By Ramaan in forum New To Java
    Replies: 2
    Last Post: 03-01-2010, 11:36 PM
  3. Make the Button Work
    By ŖΫ ỏ Ңόρę in forum New To Java
    Replies: 1
    Last Post: 02-27-2010, 10:52 AM
  4. Can't make JTable work -- please help!!
    By cagalli83 in forum Advanced Java
    Replies: 0
    Last Post: 02-13-2008, 09:31 AM
  5. To make work DLL from webservices
    By Felissa in forum Advanced Java
    Replies: 1
    Last Post: 07-05-2007, 07:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •