06.01.2013 Views

Learning Processing: A Beginner's Guide to Programming Images ...

Learning Processing: A Beginner's Guide to Programming Images ...

Learning Processing: A Beginner's Guide to Programming Images ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Example 17-4: Text mirror<br />

import processing.video.*;<br />

// Size of each cell in the grid, ratio of window size <strong>to</strong> video size<br />

int videoScale = 14;<br />

// Number of columns and rows in our system<br />

int cols, rows;<br />

// Variable <strong>to</strong> hold on<strong>to</strong> capture object<br />

capture video;<br />

// A String and Font<br />

String chars = " helloworld" ;<br />

PFont f;<br />

void setup() {<br />

size(640,480);<br />

//set up columns and rows<br />

cols = width/videoScale;<br />

rows = height/videoScale;<br />

video = new Capture(this,cols,rows,15);<br />

// Load the font<br />

f = loadFont (" Courier-Bold-20.vlw ");<br />

}<br />

void draw() {<br />

background(0);<br />

// Read image from the camera<br />

if (video.available()) {<br />

video.read();<br />

}<br />

video.loadPixels();<br />

// Use a variable <strong>to</strong> count through chars in String<br />

int charcount = 0;<br />

// Begin loop for rows<br />

for (int j = 0; j < rows; j + + ) {<br />

// Begin loop for columns<br />

for (int i = 0; i < cols; i + + ) {<br />

// Where are we, pixel-wise?<br />

int x = i*videoScale;<br />

int y = j*videoScale;<br />

The source text used in the mosaic<br />

pattern. A longer String might<br />

produce more interesting results.<br />

// Looking up the appropriate color in the pixel array<br />

color c = video.pixels[i + j*video.width];<br />

// Displaying an individual character from the String<br />

// Instead of a rectangle<br />

textFont(f);<br />

fill(c);<br />

text(chars.charAt(charcount),x,y);<br />

// Go on <strong>to</strong> the next character<br />

charcount = (charcount + 1) % chars.length();<br />

}<br />

}<br />

}<br />

Using a “fi xed-width” font. In most fonts,<br />

individual characters have different<br />

widths. In a fi xed-width font, all characters<br />

have the same width. This is useful here<br />

since we intend <strong>to</strong> display the letters one<br />

at a time spaced out evenly. See Section<br />

17.7 for how <strong>to</strong> display text character by<br />

character with a nonfi xed width font.<br />

Text 315<br />

One character from the<br />

source text is displayed<br />

colored accordingly <strong>to</strong> the<br />

pixel location. A counter<br />

variable—“charcount”—is<br />

used <strong>to</strong> walk through<br />

the source String one<br />

character at a time.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!