Text 315
Example 17-4: Text mirror
import processing.video.*;
// Size of each cell in the grid, ratio of window size to video size
int videoScale = 14;
// Number of columns and rows in our system
int cols, rows;
// Variable to hold onto capture object
capture video;
// A String and Font
String chars = " helloworld" ;
PFont f;
void setup() {
size(640,480);
//set up columns and rows
cols = width/videoScale;
rows = height/videoScale;
video = new Capture(this,cols,rows,15);
// Load the font
f = loadFont (" Courier-Bold-20.vlw ");
}
void draw() {
background(0);
// Read image from the camera
if (video.available()) {
video.read();
}
video.loadPixels();
// Use a variable to count through chars in String
int charcount = 0;
// Begin loop for rows
for (int j = 0; j < rows; j + + ){
// Begin loop for columns
for (int i = 0; i < cols; i + + ){
// Where are we, pixel-wise?
int x = i*videoScale;
int y = j*videoScale;
color c = video.pixels[i + j*video.width];
// Instead of a rectangle
textFont(f);
fill(c);
text(chars.charAt(charcount),x,y);
// Go on to the next character
charcount = (charcount + 1) % chars.length();
}
}
}
The source text used in the mosaic
pattern. A longer String might
produce more interesting results.
Using a “fi xed-width” font. In most fonts,
individual characters have different
widths. In a fi xed-width font, all characters
have the same width. This is useful here
since we intend to display the letters one
at a time spaced out evenly. See Section
17.7 for how to display text character by
character with a nonfi xed width font.
One character from the
source text is displayed
colored accordingly to the
pixel location. A counter
variable—“charcount”—is
used to walk through
the source String one
character at a time.
// Looking up the appropriate color in the pixel array
// Displaying an individual character from the String