2013-02-26 55 views
0

我需要一些帮助/提示,​​以将基于内存的循环缓冲区转换为基于磁盘的...通过对Java文件进行读/写操作来存储一系列图像。磁盘上的循环缓冲区实现

研究在线我发现我需要:

* 不需要固定大小的帧,但与包括实际尺寸,以及时间戳和标记的报头前缀的每个帧。然后你可以简单地记住第一个有效帧头部的偏移量和最后一个有效帧尾部的偏移量,你就可以实现一个FIFO循环缓冲区。 * 对标题结构使用8的包装并将缓冲区填充为8的倍数,以便您可以读取/写入文件而不会出现对齐错误。

下面是我现在使用的代码:

class CircularBuffer { 

private ImageIcon buffer[]; 
private int lastIdx; 
private int firstIdx; 
private int size; 
private boolean empty; 

/** default size */ 
static final int DEFAULT_SIZE = 50; 

public CircularBuffer(int size) { 
    this.size = size; 
    this.clear(); 
} 

public CircularBuffer() { 
    this(CircularBuffer.DEFAULT_SIZE); 
} 

public synchronized void push(ImageIcon data) { 
    buffer[this.lastIdx] = data; 
    if(this.lastIdx == this.firstIdx && !this.empty) { 
     this.firstIdx++; 
     this.firstIdx %= this.size; 
      } 
    if (this.empty){ 
     this.empty = false; 
      } 
    this.lastIdx++; 
    this.lastIdx %= this.size; 
} 

public synchronized int getLength() { 
    if (this.empty) 
     return 0; 
    int len = this.lastIdx - this.firstIdx; 
    if (len < 0) 
     len = this.size + len; 
    return len == 0 ? this.size-1 : len; 
} 

public synchronized ImageIcon pop() { 
    if (isEmpty()) { 
     throw new IndexOutOfBoundsException("Empty buffer"); 
    } 
    ImageIcon res = buffer[this.firstIdx]; 
    buffer[this.firstIdx] = null; 
    this.firstIdx++; 
    this.firstIdx %= this.size; 
    if (this.firstIdx == this.lastIdx) 
     this.empty = true; 
    return res; 
} 

public synchronized boolean isEmpty() { 
    return this.empty; 
} 

public void clear() { 
    this.firstIdx = 0; 
    this.lastIdx = 0; 
    this.empty = true; 
    this.buffer = new ImageIcon[size]; 
} 

public int getSize() { 
    return this.size; 
} 

}

感谢您的帮助!

+0

你的问题是什么? – Nick 2013-02-26 13:44:36

+0

问题:如何修改上面的代码在磁盘而不是内存上执行操作... – Tekmanoid 2013-02-26 13:48:35

+0

你试过了什么? – Nick 2013-02-26 13:50:29

回答

0

使用,让你的java内存MappedByteBuffer .. - >存储 - >磁盘和阵列都API

+0

谢谢,看起来很有趣! – Tekmanoid 2013-02-26 13:57:37

1

从您的其他问题,我建议你不需要具有任何打扰像基于磁盘的循环缓冲区一样先进。那么下面的伪代码如何:

Start: 
    lastFrame = 0 
    Loop Until STOP_REQUESTED: 
     get_new_image_frame 
     lastFrame++ 
     if (lastFrame > maxFrames) 
     lastFrame = 1 
     EndIf 
     write_image_to_disk_named("image" + lastFrame + ".jpg") 
    EndLoop 

    initialise_video_ready_to_add_frames() 
    Loop: 
     add_image_to_video("image" + lastFrame + ".jpg") 
     lastFrame++ 
     if (lastFrame > maxFrames) 
     lastFrame = 1 
     EndIf 
    EndLoop 
+0

我可能不得不采取更简单的方式来解决这个问题。 – Tekmanoid 2013-02-26 15:16:27