hi there
i found a possibility to make a maske for a livecam which scales from small to big and back. now i tryed to write it in a function. unfortunately it doesn’t work propper and i have no idea why. it must be something with the startTime and duration. within a function it says «The local variable startTime may not have been initialized» but if i do so, it doesn’t work at all. so i took the lastReadTime which is already used by the code anyway. but with this the scaling doesn’t work propper. it flickers and at the end it scales
only for a short moment. has anybody an idea why? here ist my code - thanks for help - cheers
import processing.video.*;
import processing.sound.*;
Capture cam;
PImage img;
PImage imgMask;
/*
float startSize = 1; // Anfangsgröße der Ellipse (200x200)
float targetSize = 300; // Zielgröße der Ellipse (10x10)
float currentSize = startSize; // Aktuelle Größe der Ellipse
float duration = 9000; // Dauer der Animation in Millisekunden (2 Sekunden)
float startTime; // Zeitpunkt, an dem die Animation gestartet wird
*/
Amplitude amp;
AudioIn in;
// two movie objects
Movie myMovie;
String fileMovie1 = "auge_drehen_25fps_conv.mp4";
Movie yourMovie;
String fileMovie2 = "auge_1080_verzerren_3_conv.mp4";
// movie that is currently playing
Movie currentMovie;
// last time that a frame was read
int lastReadTime = 0;
// timeout for play-once movie; 1 second
int readTimeout = 1000;
void setup()
{
size(900, 900);
fullScreen();
background(0,11,51);
// frameRate(30);
//--------------------------------
String[] cameras = Capture.list();
if (cameras.length == 0) {
println("There are no cameras available for capture.");
exit();
} else {
println("Available cameras:");
printArray(cameras);//gibt die kameranummer aus
for (int i = 0; i < cameras.length; i++) {
//println(cameras[i]);
}
// The camera can be initialized directly using an
// element from the array returned by list():
cam = new Capture(this, cameras[1]);
cam.start();
}
//--------------------------------
amp = new Amplitude(this);
in = new AudioIn(this, 0);
//start the microphone
in.start();
amp.input(in);
// first movie, will play continously
myMovie = new Movie(this, fileMovie1);
// second movie, plays once after clap
yourMovie = new Movie(this, fileMovie2);
// set the current movie to myMovie
currentMovie = myMovie;
// and loop it
currentMovie.loop();
img = loadImage("auge4-kl-maske.png"); // Lade das Hintergrundbild (ersetze "background_image.jpg" mit dem Dateinamen deines Bildes)
img.resize(width, height); // Skaliere das Hintergrundbild auf die Größe der Leinwand
//startTime = millis();
noStroke(); // Entferne die Konturlinie
}
void draw()
{
if (cam.available() == true) {
cam.read();
}
imageMode(CENTER);
image(currentMovie, width/2,height/2,900,900);
// switch to
soundDetector();
// if the current movie is yourMovie which plays only once
if (currentMovie == yourMovie)
{
// check if we did not have data for a specified time
if (millis() - lastReadTime >= readTimeout)
{
println("Switching back to 'myMovie'");
// stop the current movie (yourMovie) so a new play will start it from the beginning
currentMovie.stop();
// switch to the looping movie (myMovie)
currentMovie = myMovie;
// and (continue) playing
currentMovie.play();
}
maske();
}
}
void maske(){
//mmmmmmmmmmmmmmmmmmm grafikmaske einschalten
float startSize = 1; // Anfangsgröße der Ellipse (200x200)
float targetSize = 300; // Zielgröße der Ellipse (10x10)
float currentSize = startSize; // Aktuelle Größe der Ellipse
float duration = 2000; // Dauer der Animation in Millisekunden (2 Sekunden)
float startTime; // Zeitpunkt, an dem die Animation gestartet wird
startTime = lastReadTime;
// Berechne den Fortschritt der Animation (0.0 bis 1.0)
float t = min(1.0, (millis() - startTime) / duration);
// Berechne die aktuelle Größe der Ellipse mithilfe von Interpolation
if (t < 0.5) {
// Vergrößern (von startSize zu targetSize)
t = t * 2; // Verdopple den Fortschritt, um eine reibungslose Vergrößerung zu erreichen
currentSize = lerp(startSize, targetSize, t);
} else {
// Verkleinern (von targetSize zu startSize)
t = (t - 0.5) *2; // Verschiebe den Fortschritt, um eine reibungslose Verkleinerung zu erreichen
currentSize = lerp(targetSize, startSize, t);
}
println(min(1.0, (millis() - startTime) / duration)," - ", t);
PGraphics maskPG = createGraphics(cam.width, cam.height);
maskPG.beginDraw();
maskPG.background(0);
maskPG.fill(255);
maskPG.noStroke();
maskPG.ellipse(320,240, 640, 480 );//halbe cameragrösse für x und y und ganze kameragrösse für form = rund
maskPG.endDraw();
cam.mask(maskPG);
// image(cam,0,0);
// Überlagerung der Maske (transparentes Rechteck)
translate(width/2,height/2);
fill(255, 150); // Setze die Füllfarbe des transparenten Rechtecks (Alpha = 150)
imageMode(CENTER);
image(cam,0,0,currentSize,currentSize);
//mmmmmmmmmmmmmmmmmmm
}
//Sound dection; start other movie if a sounc is detected
void soundDetector()
{
// if a clap was detected
if (amp.analyze() > 0.015){
// if we were playing the (looping) myMovie
if (currentMovie == myMovie) {
println("Switching to 'yourMovie'");
// pause the looping movie (myMovie) so the next time that we play it, it starts where it paused
currentMovie.pause();
// change the movie to yourMovie
currentMovie = yourMovie;
// and play it
currentMovie.play();
}
}
}
// Called every time a new frame is available to read
void movieEvent(Movie m)
{
// keep track of the last time that we read a frame
lastReadTime = millis();
// read frame depending on movie that provided the frame
if (m == myMovie)
{
myMovie.read();
} //
else if (m == yourMovie)
{
yourMovie.read();
}
}