Willkommen auf unserem Seminar-Blog

Immer auf dem aktuellen Stand bleiben

Dieser Seminar-Blog befindet sich noch im Aufbau und wird in den kommenden Tagen entsprechend verfeinert.

Member Login

Lost your password?

Registration is closed

Sorry, you are not allowed to register by yourself on this site!

You must either be invited by one of our team member or request an invitation by email at viad.info {at} zhdk {dot} ch.

ASCII

Februar 27, 2012

Als Nächstes konnten wir den Umgang mit Schriftarten und Buchstaben üben. Die Bildpixel werden in Buchstaben umgewandelt, die Buchstaben nehmen die Farbe der Pixel auf dem Bild an. Mit den Up- und Downtasten kann man die Grösse der Buchstaben definieren.
String inputText = "ComputerVision,InteractionDesign,ZürcherHochschuleDerKünste,FS2012";
float fontSizeMax = 20;
float fontSizeMin = 10;
float spacing = 12; // line height
float kerning = 0.5; // between letters

boolean fontSizeStatic = false;
boolean blackAndWhite = false;

PFont font;
PImage img;

void setup()
{
 size(850,850);
 smooth();

 font = createFont("Times",10);

 img = loadImage("bild.jpg");
 println(img.width+" x "+img.height);
}

void draw()
{
 // if (savePDF) beginRecord(PDF, timestamp()+".pdf");

background(255);
 textAlign(LEFT);

float x = 0, y = 10;
 int counter = 0;

while (y < height)
 {
 // translate position (display) to position (image)
 int imgX = (int) map(x, 0,width, 0,img.width);
 int imgY = (int) map(y, 0,height, 0,img.height);
 // get current color
 color c = img.pixels[imgY*img.width+imgX];
 int greyscale = round(red(c)*0.222 + green(c)*0.707 + blue(c)*0.071);

pushMatrix();
 translate(x, y);

if (fontSizeStatic) {
 textFont(font, fontSizeMax);
 if (blackAndWhite) fill(greyscale);
 else fill(c);
 }
 else
 {
 // greyscale to fontsize
 float fontSize = map(greyscale, 0,255, fontSizeMax,fontSizeMin);
 fontSize = max(fontSize, 1);
 textFont(font, fontSize);
 if (blackAndWhite) fill(0);
 else fill(c);
 }

char letter = inputText.charAt(counter);
 text(letter, 0, 0);
 float letterWidth = textWidth(letter) + kerning;
 // for the next letter ... x + letter width
 x = x + letterWidth; // update x-coordinate
 popMatrix();

// linebreaks
 if (x+letterWidth >= width)
 {
 x = 0;
 y = y + spacing; // add line height
 }

counter++;
 if (counter > inputText.length()-1) counter = 0;
 }
}
/* void keyReleased()
{
 if (key == 's' || key == 'S') saveFrame(timestamp()+"_##.png");
 if (key == 'p' || key == 'P') savePDF = true;
 // change render mode
 if (key == '1') fontSizeStatic = !fontSizeStatic;
 // change color stlye
 if (key == '2') blackAndWhite = !blackAndWhite;
 println("fontSizeMin: "+fontSizeMin+" fontSizeMax: "+fontSizeMax+" fontSizeStatic: "+fontSizeStatic+" blackAndWhite: "+blackAndWhite);
}
*/

void keyPressed()
{
 // change fontSizeMax with arrowkeys up/down
 if (keyCode == UP) fontSizeMax += 2;
 if (keyCode == DOWN) fontSizeMax -= 2;
 // change fontSizeMin with arrowkeys left/right
 if (keyCode == RIGHT) fontSizeMin += 2;
 if (keyCode == LEFT) fontSizeMin -= 2;

//fontSizeMin = max(fontSizeMin, 2);
 //fontSizeMax = max(fontSizeMax, 2);
}

// timestamp
String timestamp()
{
 Calendar now = Calendar.getInstance();
 return String.format("%1$ty%1$tm%1$td_%1$tH%1$tM%1$tS", now);
}

&nbsp;