2017-10-10 124 views
0

我應該超越這個,但我不能深究這個錯誤的底部。我試圖寫一個草圖,記錄我的Mac相機的飼料,並將每個記錄「會話」存儲到PImage數組中,然後將其添加到會話列表(PImage [] ArrayList)中。我使用'Replay'類來訪問存儲在內存中的圖像,並在隨機位置重播它們。下面的代碼應該可以直接複製到IDE中。處理:網絡攝像頭保存和播放錯誤

任何幫助將不勝感激。我不知道爲什麼重播對象總是顯示實時圖像。謝謝!

import processing.video.*; 
Capture cam; 

ArrayList<PImage[]> allImages; 
ArrayList<PImage> currentImages; 
ArrayList<Replay> replays; 

boolean recording = false; 
boolean finishedSaving = true; 
int currentIndex = 0; 

void setup() { 
    size(1056, 704, P2D); 
    frameRate(30); 

    allImages = new ArrayList<PImage[]>(); 
    currentImages = new ArrayList<PImage>(); 
    replays = new ArrayList<Replay>(); 

    String[] cams = Capture.list(); 
    if (cams.length == 0) { 
    println("No cams!"); 
    exit(); 
    } else { 
    cam = new Capture(this, 1056, 704, cams[0], 30); 
    cam.start(); 
    } 

} 

void draw() { 
    background(0); 

    if (cam.available() == true) { 
    cam.read(); 
    } 


    for (Replay r : replays) { 
    r.display(); 
    } 

    if (recording) { 
    currentImages.add(cam); 
    noFill(); 
    stroke(255, 0, 0); 
    strokeWeight(5); 
    rect(0, 0, cam.width/3, cam.height/3); 
    } else { 
    saveToArray(); 
    } 

    image(cam, 0, 0, cam.width/3, cam.height/3); 

} 

void saveToArray() { 
    if (!finishedSaving) { 
    PImage[] tempImages = currentImages.toArray(new PImage[currentImages.size()]); 
    allImages.add(tempImages); 
    currentImages.clear(); 
    println("Finished saving to allImages array"); 
    println("allImages array size now = " + allImages.size()); 

    replays.add(new Replay(currentIndex)); 
    println("Added new Replay (index: " + currentIndex + ")"); 
    currentIndex++; 
    finishedSaving = true; 
    println(); 
    } 

} 

void keyPressed() { 
    if (key == 'r' || key == 'R') { 
    recording = !recording; 
    println("Recording: " + recording); 
    finishedSaving = false; 
    } 
} 

class Replay { 

    PVector position; 
    float w, h; 
    PImage[] images; 
    int count; 

Replay(int allImagesIndex) { 
    w = cam.width/3; 
    h = cam.height/3; 
    position = new PVector(random(width-w), random(height-h)); 
    count = 1; 
    images = allImages.get(allImagesIndex); 
} 

void display() { 
    image(images[count], position.x, position.y, w, h); 
    count++; 
    if (count > images.length-1) count = 1; 
} 

} 

回答

1

看起來像這是由於相機饋源總是分配給每個單獨的圖像。爲了獲得「當前」視頻流幀的「副本」,我只是將.get()添加到提要中,以便提取像素並將它們存儲在PImage變量中。

currentImages.add(cam.get());