2010-02-08 138 views
0

因此,在收到只有幾個建議用於創建視頻文件的Java包之後,我決定嘗試修改JPEGImagesToMovie文件作爲JMF的樣本。我設法讓所有的東西編譯,看起來應該運行。新類應該使用BufferedImages的Vector,然後不像之前那樣從文件讀取並轉換爲字節數組,而是直接從BufferedImage轉換爲字節數組。問題在於處理器不會配置(它鎖定或類似的東西)。任何人都可以看到任何明顯的缺陷,會造成這種情況?修改JPEGImagesToMovie文件

此外,我仍然完全接受有關更好/更簡單/更簡單的框架的建議。

編輯:這裏有一些代碼,我其實做了些什麼。爲了便於閱讀,在這裏稍微清理一下。

class ImageDataSource extends PullBufferDataSource { 

ImageSourceStream streams[]; 

ImageDataSource(int width, int height, int frameRate, Vector images) { 
    streams = new ImageSourceStream[1]; 
    streams[0] = new ImageSourceStream(width, height, frameRate, images); 
} 

/** 
* The source stream to go along with ImageDataSource. 
*/ 
class ImageSourceStream implements PullBufferStream { 

Vector images; 
int width, height; 
VideoFormat format; 

int nextImage = 0; // index of the next image to be read. 
boolean ended = false; 

public ImageSourceStream(int width, int height, int frameRate, Vector images) { 
    this.width = width; 
    this.height = height; 
    this.images = images; 

    format = new VideoFormat(null, 
      new Dimension(width, height), 
      Format.NOT_SPECIFIED, 
      Format.byteArray, 
      (float)frameRate); 
} 

/** 
* 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 >= images.size()) { 
    // We are done. Set EndOfMedia. 
    System.err.println("Done reading all images."); 
    buf.setEOM(true); 
    buf.setOffset(0); 
    buf.setLength(0); 
    ended = true; 
    return; 
    } 

    BufferedImage imageFile = (BufferedImage)images.elementAt(nextImage); 
    nextImage++; 


    byte data[] = ImageToByteArray.convertToBytes(imageFile); 
    // Check the input buffer type & size. 

    if (buf.getData() instanceof byte[]) 
    data = (byte[])buf.getData(); 

    // Check to see the given buffer is big enough for the frame. 

    buf.setData(data); 

    buf.setOffset(0); 
    buf.setLength((int)data.length); 
    buf.setFormat(format); 
    buf.setFlags(buf.getFlags() | buf.FLAG_KEY_FRAME); 
} 

我不確定的一件事是用作VideoFormat的編碼。

+0

http://java.sun.com/javase/technologies/desktop/media/jmf/2.1.1/solutions/JpegImagesToMovie.java與原始JMF相比,您沒有更改該特定代碼段中的任何內容示例代碼。你應該顯示你實際**已經改變的那段代碼!關於更好/更簡單/更簡單的框架的建議:實際上它不能做得更好,但Xuggler不那麼臃腫。 http://wiki.xuggle.com/MediaTool_Introduction – BalusC 2010-02-08 12:02:45

+0

糟糕。我在發現錯誤的地方發佈了代碼,沒有考慮到我沒有改變任何東西的事實。要避免這些通宵編碼狂歡。 – WLPhoenix 2010-02-08 14:32:18

+0

對於純Java實現,請使用Werner Randelshofer的[Monte Media Library](http://www.randelshofer.ch/monte/)。 – 2012-09-29 02:34:01

回答

0

說真的,你應該考慮用Xuggler來做這件事。