Processing/JS

Vídeo Ejemplos 1

Detectar cámaras disponibles

Con este ejemplo detectamos las resoluciones que admite nuestra cámara y que la cámara es detectada prefectamente por processing.

import processing.video.*;
Capture cam;

void setup() {
size(640, 480);
String[] cameras = Capture.list();
if (cameras.length == 0) {
println("no hay cámara disponible.");
exit();
} else {
println("Cámaras disponibles:");
for (int i = 0; i < cameras.length; i++) {
println(cameras[i]);
}
// La cámara se inicializa con un elemento de la lista de disponibles
cam = new Capture(this, cameras[0]);
cam.start();
}
}
void draw() {
if (cam.available() == true) {
cam.read();
}
image(cam, 0, 0);
}

ASCII Vídeo

Para este ejercicio es necesario generar la fuente que vamos a usar, es importante que sea del tamalo de la celda, horizSpace y vertSpace

import processing.video.*;
// variables del tamaño de la captura
int w = 640;
int h = 480;
Capture cm;
String cam;

// font variables;
PFont font;
// text grid
int horizSpace = 12;
int vertSpace = 12;

// letras ordenadas por densidad
String [] letters = {"Q", "k", "h", "n", "j", "i", ";", "."};

void setup() {
size(w, h);
noStroke();
fill(0);
background(255);
String[] cameras = Capture.list();
for (int i = 0; i < cameras.length; i++) {
println(cameras[i]);
}
cm = new Capture(this, cameras[0]);
cm.start();

// load font
font = loadFont("ArialMT-12.vlw");
textFont(font);
}
void captureEvent(Capture cm) {
cm.read();
}
void draw() {
// dibuja la imagen capturada
//image(cm, 0, 0);
background(255);

for (int i = 0; i < w; i+= horizSpace) {
for (int j = 0; j < w; j+= vertSpace) {

float thisBrightness = brightness(cm.get(i, j));

String thisLetter = chooseLetter(thisBrightness);
text(thisLetter, i, j, horizSpace, vertSpace);
}
}
//saveFrame();
}
String chooseLetter(float br) {
String letter = "";
int difInValue = 40;
int thisValue = 0;
int oldValue = thisValue;
int counter = 0;
for (int b = 0; b < 255; b += difInValue) {
thisValue += difInValue;
if (br > oldValue && br < thisValue) {
letter = letters[counter];
}
counter++;
oldValue = thisValue;
}
return letter;
}