example
Example
// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com

// 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;

// The source text used in the mosaic pattern. A longer String might produce more interesting results.
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
  // Using a fixed-width font. In most fonts, individual characters have different widths. 
  // In a fixed-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 nonfixed width 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;
      
      // Looking up the appropriate color in the pixel array
      color c = video.pixels[i + j*video.width];
      // Displaying an individual character from the String instead of a rectangle
      textFont(f);
      fill(c);
      
      // 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.
      text(chars.charAt(charcount),x,y);
      
      // Go on to the next character
      charcount = (charcount + 1) % chars.length();
    }
  }
}
  • Ken Sherwood

    I wonder if anyone has tried to hack this for the purpose of reading an array of still images? Learning this without access to webcam or quicktime (as using Linux).

    I’ve initialized and loaded a png image. The hitch thus far for this newbie is that the read() function seems to be part of the video class.

  • http://www.learningprocessing.com Daniel Shiffman

    Hi Ken,

    With a still image you can eliminate the read() function. The read() function is necessary for a camera b/c you need to continuously “read” new images, but with loading a PImage it’s unnecessary.