⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 movieencoder.java

📁 MASON代表多主体邻里或网络仿真(Multi-Agent Simulator of Neighborhoods or Networks)。它是乔治梅森大学用Java开发的离散事件多主体仿真核心库
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        source = new MovieEncoderDataSource(format, frameRate);            processor = Manager.createProcessor(source);        processor.addControllerListener(this);        processor.configure();        if (!waitForState(processor, Processor.Configured))            throw new RuntimeException("Failed to configure processor");                    processor.setContentDescriptor(new ContentDescriptor(FileTypeDescriptor.QUICKTIME));            TrackControl tcs[] = processor.getTrackControls();                // old -- just take first one        /*          Format f[] = tcs[0].getSupportedFormats();          if (f == null || f.length <= 0)          throw new RuntimeException("The mux does not support the input format: " + tcs[0].getFormat());          tcs[0].setFormat(f[0]);          System.out.println(tcs[0]);          System.out.println(f[0]);*/                // new - set by requested format        tcs[0].setFormat(encodeFormat);        //        System.err.println("Realizing");        // realize the processor        processor.realize();        if (!waitForState(processor, Processor.Realized))            throw new RuntimeException("Failed to Realize processor");                //        System.err.println("Sinking");                sink = Manager.createDataSink(processor.getDataOutput(), new MediaLocator(file.toURL()));        sink.addDataSinkListener(this);        sink.open();        processor.start();        sink.start();        //        System.err.println("Done");        started = true;        }    BufferedImage preprocess(BufferedImage i)        {        // guarantee that subsequent images are exactly the same format and size        // as the first image        if (i.getWidth() != width || i.getHeight() != height || i.getType() != type)            {            BufferedImage temp = new BufferedImage(width,height,type);            Graphics2D g = temp.createGraphics();            g.drawImage(i,0,0,null);            i = temp;            }        return i;        }    /** Adds an image to the movie.  The very first image added will specify the size        of the movie's frames and the image type for later images.  You should strive to        keep the images the same size, else MovieEncoder must will conver and crop them        to the original image's format and size.  Returns true if the image was successfully        added.  Returns false if we've been stopped (either intentionally or due to an error).    */    public synchronized boolean add(BufferedImage i)        {        if (!stopped)            {            i = preprocess(i); // give source a chance to be created, then...            source.add(i);            }        return (!stopped);        }        /** Stops the writer and finishes uprocessor.  After this, this object cannot be used.        It is possible that the system may take a second to write out and finish up */    public synchronized boolean stop()        {        if (!started) return false;        if (stopped) return false;        stopped = true;        source.finish();        // Wait for EndOfStream event.        boolean success = waitForFileDone();        try { sink.close(); } catch (Exception e) {}        processor.removeControllerListener(this);        stopped = true;  // just in case the dataSinkUpdate didn't do it -- interruptedException maybe        return success;        }    }/* * MovieEncoderDataSource * provides the MovieEncoderDataStream, and that's all really * */// a very simple data sourceclass MovieEncoderDataSource extends PullBufferDataSource     {    MovieEncoderDataStream[] streams;        public MovieEncoderDataSource(Format format, float frameRate)         {        streams = new MovieEncoderDataStream[1];        streams[0] = new MovieEncoderDataStream(format, frameRate);        }        public void setLocator(MediaLocator source) { }        public MediaLocator getLocator() { return null; }        public String getContentType() { return ContentDescriptor.RAW; }        public void connect() {}        public void disconnect() {}        public void start() {}        public void stop() {}        public PullBufferStream[] getStreams() { return streams; }        public Time getDuration() { return DURATION_UNKNOWN; }        public Object[] getControls() { return new Object[0]; }        public Object getControl(String type) { return null; }        public void add(Image i)        {        streams[0].write(i);        }        public void finish()        {        streams[0].finish();        }        }/* * * MovieEncoderDataStream * Provides the underlying JMF Processor with images (converted to Buffers) for it to * encode and write out to disk as it sees fit. * */// Our stream from which the processor requests images.  Here's how it works.// You put an image in the stream with write().  This is blocking -- we have to// wait until any existing image in there has been flushed out.// Additionally, the underlying processor is reading images [as buffers] with read(),// and it's blocking waiting for us to provide stuff.  The blocks are handled with// spin-waits (25ms sleeps) because I'm being lazy.class MovieEncoderDataStream implements PullBufferStream    {    // we won't have a buffered mechanism here -- instead we'll assume    // that there is one, and exactly one, image waiting    Buffer buffer = null;    Format format;    boolean ended = false;    boolean endAcknowledged = false;    float frameRate;        MovieEncoderDataStream(Format format, float frameRate) { frameRate = this.frameRate ;this.format = format; }        void finish()        {        synchronized(this)            {            ended = true;            }        }        // blocks on write    void write(Image i)        {        Buffer b = ImageToBuffer.createBuffer(i, frameRate);        while(checkWriteBlock())             try { Thread.sleep(25); } catch (InterruptedException e) { return; }  // spin-wait, ugh        synchronized(this)            {            buffer = b;            }        }    synchronized boolean checkWriteBlock()  { return (buffer!=null); }                synchronized boolean checkReadBlock() { return (buffer==null && !ended); }    public boolean willReadBlock() { return false; } // lie    // could block on read    public void read(Buffer buf) throws IOException        {        while(checkReadBlock()) try { Thread.sleep(25); } catch (InterruptedException e) { }  // spin-wait, ugh        // Check if we need to close up shop        synchronized(this)            {            if (ended)                 {                // We are done.  Set EndOfMedia.                buf.setEOM(true);                buf.setOffset(0);                buf.setLength(0);                endAcknowledged = true;                }            else                {                // load the data                buf.setData(buffer.getData());                buf.setLength(buffer.getLength());                buf.setOffset(0);                buf.setFormat(format);                buf.setFlags(buf.getFlags() | Buffer.FLAG_KEY_FRAME | Buffer.FLAG_NO_DROP);  // must write the frame                }            buffer = null;            }        }    // returns the buffered image format    public Format getFormat() {  return format; }    public ContentDescriptor getContentDescriptor()  { return new ContentDescriptor(ContentDescriptor.RAW); }    public long getContentLength() { return 0; }    public boolean endOfStream() { return ended; } // or should it be endAcknowledged?    public Object[] getControls() { return new Object[0]; }    public Object getControl(String type) { return null; }    }///// LICENSES/// Some of this code was snarfed from /// http://java.sun.com/products/java-media/jmf/2.1.1/solutions/JpegImagesToMovie.java/// Here's the license to that code./* * @(#)JpegImagesToMovie.java   1.3 01/03/13 * * Copyright (c) 1999-2001 Sun Microsystems, Inc. All Rights Reserved. * * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use, * modify and redistribute this software in source and binary code form, * provided that i) this copyright notice and license appear on all copies of * the software; and ii) Licensee does not utilize the software in a manner * which is disparaging to Sun. * * This software is provided "AS IS," without a warranty of any kind. ALL * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE * POSSIBILITY OF SUCH DAMAGES. * * This software is not designed or intended for use in on-line control of * aircraft, air traffic, aircraft navigation or aircraft communications; or in * the design, construction, operation or maintenance of any nuclear * facility. Licensee represents and warrants that it will not use or * redistribute the Software for such purposes. */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -