123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
-
- Capture cam;
- Movie movie;
- boolean moviePlaying;
- PImage fallbackImage;
-
- void movieEvent(Movie m) {
- if (moviePlaying) {
- m.read();
- if (mode == 1) {
- sourceManager.setImage(movie.get());
- movie.jump(random(movie.duration()));
- }
- }
- }
- void newMovie(String path) {
- movie = new Movie(this, path);
- }
-
- boolean isVideo(String path) {
- String fpl = path.toLowerCase();
- if (fpl.endsWith(".mp4") || fpl.endsWith(".avi") || fpl.endsWith(".webm") || fpl.endsWith(".mkv") || fpl.endsWith(".mov")) return true;
- else return false;
- }
- boolean isImage(String path) {
- String fpl = path.toLowerCase();
- if (fpl.endsWith(".bmp") || fpl.endsWith(".png") || fpl.endsWith(".jpg") || fpl.endsWith(".jpeg") || fpl.endsWith(".gif")) return true; //gif = special case!
- else return false;
- }
-
- class SourceManager { //no constructor
-
- // This function returns all the files in a directory as an array of Strings
- String[] listFileNames(String dir) {
- File file = new File(dir);
- if (file.isDirectory()) {
- String names[] = file.list();
- return names;
- } else {
- return null;
- }
- }
-
-
- String dir;
- String[] selection;
- int selId;
- void importURI(String path) {
- File file = new File(path);
- if (file.isDirectory()) {
- dir = path;
- String names[] = file.list();
- ArrayList<String> usableFiles = new ArrayList<String>();
- for (int i = 0; i < names.length; i++) {
- if (isImage(names[i]) || isVideo(names[i])) {
- usableFiles.add(names[i]);
- }
- }
- selection = new String[usableFiles.size()];
- for (int i = 0; i < usableFiles.size(); i++) selection[i] = usableFiles.get(i);
- println("Got new selection from folder. " + selection.length + " usable files.");
- path = dir + "/" + selection[(int)random(selection.length)];
- }
-
-
- if (isImage(path) || isVideo(path)) {
- println("Importing file " + path);
- if (moviePlaying) movie.stop();
- if (isImage(path)) {
- moviePlaying = false;
- setImage(loadImage(path));
- } else if (isVideo(path)) {
- moviePlaying = true;
- newMovie(path);
- movie.loop();
- }
- }
- }
-
-
- void fallback() {
- println("Fallback");
- mode = 1;
- setImage(fallbackImage); //fallbackImage should always be a copy of the last image that worked
- }
-
- void update() {
- //check if source image is present
- if (mode == 0) {
- try {
- if (cam.available() == true) {
- cam.read();
- if (cam != null) {
- source = cam.get();
- } else {
- fallback();
- }
- }
- }
- catch (Exception e) {
- println(e);
- println("No cam available, falling back to image..");
- fallback();
- }
- }
- if (source == null) {
- fallback();
- }
-
- int toW, toH;
- if (source.width > source.height) {
- toW = renderSize;
- toH = int(map(source.height, 0, source.width, 0, renderSize));
- } else {
- toW = int(map(source.width, 0, source.height, 0, renderSize));
- toH = renderSize;
- }
- toW = (toW % 2 == 0) ? toW : toW+1;
- toH = (toH % 2 == 0) ? toH : toH+1;
-
- renderer.setSize(toW, toH);
- }
-
-
- void setImage(PImage img) { //set image to source
- source = img.get();
- fallbackImage = source;
- }
-
- void setSource() { //set source to renderer
-
-
- renderer.beginDraw();
- if (mode == 0) {
- try { //final check for source image being present & valid
- renderer.beginDraw(); //pre-fx
- renderer.tint(255, 220, 200); //beautify me (color correct webcam image)
- renderer.image(source, 0, 0, renderer.width, renderer.height);
- renderer.noTint();
- }
- catch(Exception e) {
- println("error here as well");
- }
- }
- if (mode == 1) {
- //else if (mode == 1){
-
- renderer.image(source, 0, 0, renderer.width, renderer.height);
- }
-
- renderer.endDraw();
- }
- }
|