2013-02-26 3 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

+0

질문 : 위의 코드를 수정하여 메모리가 아닌 디스크에서 작업하는 방법 ... – Tekmanoid

+0

그리고 무엇을 시도 했습니까? – Nick

답변

0

사용하면 자바를 제공하는 메모리 MappedByteBuffer .. 은 -> 메모리 -> 디스크 및 배열 모두 API

+0

고마워요. – Tekmanoid

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