Processing 2.0 中的视频延迟/缓冲 [英] Video Delay/Buffer in Processing 2.0

查看:128
本文介绍了Processing 2.0 中的视频延迟/缓冲的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在处理简单的视频延迟时遇到了很多麻烦.我在互联网上环顾四周,我一直在寻找相同的代码,但我根本无法让它工作.当我第一次尝试时,它什么也没做(根本没有).这是我的修改版本(至少似乎将帧加载到缓冲区中),我真的不知道为什么它不起作用,而且我真的厌倦了拔头发.拜托...拜托,看在上帝的份上,请有人指出我在这里犯的愚蠢错误.现在,没有进一步的延迟(哈哈,明白了吗?),代码:

I'm having a ton of trouble making a simple video delay in processing. I looked around on the internet and I keep finding the same bit of code and I can't get it to work at all. When I first tried it, it did nothing (at all). Here's my modified version (which at least seems to load frames into the buffer), I really have no idea why it doesn't work and I'm getting really tired of pulling out my hair. Please... please, for the love of god, please somebody point out the stupid mistake I'm making here. And now, without further delay (hah, get it?), the code:

import processing.video.*;

VideoBuffer vb;
Movie myMovie;
Capture cam;

float seconds = 1;

void setup() {
  size(320,240, P3D);
  frameRate(30);

  String[] cameras = Capture.list();

  if (cameras.length == 0) {
    println("There are no cameras available for capture.");
    exit();
  } else {
    println("Available cameras:");
    for (int i = 0; i < cameras.length; i++) {
      println(cameras[i]);
    }
  cam = new Capture(this, cameras[3]);
  cam.start(); 

  }

  vb = new VideoBuffer(90, width, height);

}

void draw() {

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

  image(cam, 0, 0);
  image( vb.getFrame(), 150, 0 );

}  



class VideoBuffer
{
  PImage[] buffer;

  int inputFrame = 0;
  int outputFrame = 0;
  int frameWidth = 0;
  int frameHeight = 0;

  VideoBuffer( int frames, int vWidth, int vHeight )
  {
    buffer = new PImage[frames];
    for(int i = 0; i < frames; i++)
    {
  this.buffer[i] = new PImage(vWidth, vHeight);
    }
    this.inputFrame = 0;
    this.outputFrame = 1;
    this.frameWidth = vWidth;
    this.frameHeight = vHeight;
  }

  // return the current "playback" frame.  
  PImage getFrame()
  {
    return this.buffer[this.outputFrame];
  } 

  // Add a new frame to the buffer.
  void addFrame( PImage frame )
  {
    // copy the new frame into the buffer.
    this.buffer[this.inputFrame] = frame;

    // advance the input and output indexes
    this.inputFrame++;
    this.outputFrame++;
    println(this.inputFrame + " " + this.outputFrame);

    // wrap the values..    
    if(this.inputFrame >= this.buffer.length)
    {
 this.inputFrame = 0;
    }
    if(this.outputFrame >= this.buffer.length)
    {
  this.outputFrame = 0;
    }
  }  
} 

推荐答案

这适用于 Processing 2.0.1.

This works in Processing 2.0.1.

import processing.video.*;

Capture cam;
PImage[] buffer;
int w = 640;
int h = 360;
int nFrames = 60;
int iWrite = 0, iRead = 1;

void setup(){
  size(w, h);
  cam = new Capture(this, w, h);
  cam.start();
  buffer = new PImage[nFrames];
}

void draw() {
  if(cam.available()) {
    cam.read();
    buffer[iWrite] = cam.get();
    if(buffer[iRead] != null){
      image(buffer[iRead], 0, 0);
    }
    iWrite++;
    iRead++;
    if(iRead >= nFrames-1){
      iRead = 0;
    }
    if(iWrite >= nFrames-1){
      iWrite = 0;
    }
  }       
}

您的 addFrame-Method 中存在问题.您只需存储对 PImage 对象的引用,其像素会一直被覆盖.您必须使用 buffer[inputFrame] = frame.get() 而不是 buffer[inputFrame] = frame.get() 方法返回图像的副本.

There is a problem inside your addFrame-Method. You just store a reference to the PImage object, whose pixels get overwritten all the time. You have to use buffer[inputFrame] = frame.get() instead of buffer[inputFrame] = frame. The get() method returns a copy of the image.

这篇关于Processing 2.0 中的视频延迟/缓冲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆