making video clip fit within applet window without stretching it

edited December 2014 in How To...

hey guys,
This might be a silly question, but havnt found anything regarding this in the forums so here i go -
im doing a project which involves using the video library. as of now i have positioned the videos within the applet window as such -

void draw() { 
          background(0);
          set(0,0,movies[idx] ); 
        }       

now some of these videos are of high resolution ( so they are getting cut out of my applet window ) and some are really small, so they are aligning to the top left corner of the applet ) I want the videos to fit within the width of the applet and align itself such that it stays in the middle of the window. ( just the way a quicktime or a vlc player would do it )
so thats the issue. Please help!
thanks!

Tagged:

Answers

  • edited December 2014
    PImage img;
    float s;
    
    void setup(){
      size(400,400);
      imageMode(CENTER);
      img = loadImage("http://www.freshorigins.com/wp-content/uploads/2013/05/good21-copy.jpg");
      float r_i = img.width/float(img.height);
      float r_s = width/float(height);
      if(r_i>r_s){
        s = width/float(img.width);
      } else {
        s = height/float(img.height);
      }
      println("Rato image: " + r_i);
      println("Rato screen: " + r_s);
      println("Scale: " + s);
    }
    
    void draw(){
      background(0);
      translate(width/2,height/2);
      scale(s);
      image(img,0,0);
    }
    
  • haha okay. do let me know

  • thanks TfGuy44! but the code you have given is for centre aligning images right? how do i do that for video. im attaching my code. i tried to integrate what you have given my exisitng code and it is not working ( because of my complete in ability to code :( ) please could you have a look and suggest how i could do this same centre align thing for videos. Thanks

    import processing.serial.*;
    import processing.video.Movie;
    import ddf.minim.spi.*;
    import ddf.minim.signals.*;
    import ddf.minim.*;
    import ddf.minim.analysis.*;
    import ddf.minim.ugens.*;
    import ddf.minim.effects.*;
    
    Minim minim;
    
    static final int QTY = 3;
    static final int QTY2 = 3;
    
    final Movie[] movies = new Movie[QTY];
    final AudioPlayer[] players = new AudioPlayer[QTY2];
    
    int[][] myArray = new int[QTY][QTY2];
    
    int idx;
    int index;
    
    String typing = "";
    String saved = "";
    
    Serial myPort;
    
    float s;
    
    
    void setup() { 
      size(1440, 900, JAVA2D);
      frameRate(30);
      noSmooth();
    
      String portName = Serial.list()[2]; //change the 0 to a 1 or 2 etc. to match your port
      myPort = new Serial(this, portName, 9600);
    
      movies[0] = new Movie(this, "sin_city.mp4");
      movies[1] = new Movie(this, "blue_velvet.mp4");
      movies[2] = new Movie(this, "finale.mp4");
    
      minim = new Minim(this); 
    
      players[0] = minim.loadFile("one.mp3");
      players[1] = minim.loadFile("two.mp3");
      players[2] = minim.loadFile("three.mp3");
    
      imageMode(CENTER);
      float r_i = movies[idx].width/float(movies[idx].height);
      float r_s = width/float(height);
      if (r_i>r_s){
        s = width/float(movies[idx].width);
      } else {
        s = height/float(movies[idx].height);  
      }
    
    }
    
    
    
    void draw() { 
      background(0);
      translate(width/2, height/2);
      scale(s);
      set(0,0,movies[idx] ); 
    }
    
    
    
    void movieEvent(Movie m) { 
      m.read(); 
    } 
    
    
    
    void keyPressed() {
    
      if (key == '\n' ) {
        saved = typing;
        typing = ""; // A String can be cleared by setting it equal to ""
    
        int k = keyCode, n = getMovieIndex(k) ;  
    
        if ( n == 0){
          myPort.write('0');
        } 
    
        else if (n == 1){
          myPort.write('1');
        }
    
        else if (n == 2){
          myPort.write('2');
        }
    
        if ( n>=0 & n != idx){// if string typed is NOT the same as previous string then...
          index = int(random(2));
        } 
    
        else if( n>=0 & n == idx){// if string typed is the same as previous string...
    
          if(index == 0){
            index = 1;
          } 
    
          else if (index == 1){
            index = 0;
          }  
        }  
        //println(index);
    
      } 
    
      else {
        // Otherwise, concatenate the String
        // Each character typed by the user is added to the end of the String variable.
        typing = typing + key; 
      }
    
      int k = keyCode, n = getMovieIndex(k) ;  
    
      if (n >= 0 & n!= idx){// if string typed is NOT the same as previous string then... 
        println(n);
    
        if (index == 0){
          movies[idx].pause();
          players[idx].pause();  
          movies[idx = n].play();
        } 
    
        else if (index == 1){
          movies[idx].pause();  
          players[idx].pause();  
          players[idx = n].play();
        }
    
      } 
    
      else if ( n >= 0 & n == idx ){// if string typed is the same as previous string...
        println(n); 
    
        if (index == 0){
          players[idx].pause();
          movies[idx = n].play();
        } 
    
        else if (index == 1){
          movies[idx].pause();
          players[idx = n].play();
        } 
    
      }
    
      else{
        println(n);  
      }
    
    }
    
    int getMovieIndex(int k){
    
      if ( saved.equals("blue")){
      return 0;
      }
    
      else if(saved.equals("red")){
      return 1;
      }
    
      else if(saved.equals("green")){
      return 2;
      }
    
      else {
      return -1;
      }
    
    }
    
  • Function set() ignores any effects and simply stamps raw! For resizing and other fxs, we need image(). :-B
    image(movies[idx], width>>1, height>>1, width, height); // for imageMode(CENTER)

    Be aware that smooth() determines resizing quality too! For JAVA2D, smooth(4); is highest. (*)

  • hey GoToLoop! the problem is when is use image(); the video doesnt show up at all. all i can hear is the audio of the video :( im attaching my code till the void draw();..
    import processing.serial.*; import processing.video.Movie; import ddf.minim.spi.*; import ddf.minim.signals.*; import ddf.minim.*; import ddf.minim.analysis.*; import ddf.minim.ugens.*; import ddf.minim.effects.*;

    Minim minim;
    
    static final int QTY = 3;
    static final int QTY2 = 3;
    
    final Movie[] movies = new Movie[QTY];
    final AudioPlayer[] players = new AudioPlayer[QTY2];
    
    int[][] myArray = new int[QTY][QTY2];
    
    int idx;
    int index;
    
    String typing = "";
    String saved = "";
    
    Serial myPort;
    
    PImage img;
    float s;
    
    
    void setup() { 
      size(1440, 900, JAVA2D);
      frameRate(30);
      smooth(4);
    
      String portName = Serial.list()[2]; //change the 0 to a 1 or 2 etc. to match your port
      myPort = new Serial(this, portName, 9600);
    
      movies[0] = new Movie(this, "sin_city.mp4");
      movies[1] = new Movie(this, "blue_velvet.mp4");
      movies[2] = new Movie(this, "finale.mp4");
    
      minim = new Minim(this); 
    
      players[0] = minim.loadFile("one.mp3");
      players[1] = minim.loadFile("two.mp3");
      players[2] = minim.loadFile("three.mp3");
    
      imageMode(CENTER);
      float r_i = movies[idx].width/float(movies[idx].height);
      float r_s = width/float(height);
      if (r_i>r_s){
        s = width/float(movies[idx].width);
      } else {
        s = height/float(movies[idx].height);  
      }
    
    }
    
    
    
    
    
    
    void movieEvent(Movie m) { 
      m.read(); 
    } 
    
    
    void draw() { 
      background(0);
      translate(width/2, height/2);
      scale(s);
      //set(0,0,movies[idx] ); 
      //image(movies[idx], 0, 0, width, height );
      image(movies[idx], width>>1, height>>1, width, height);
    }
    
  • edited December 2014 Answer ✓

    I see you're mixing my advise w/ @TfGuy44's! #-o
    He's advised on translate() + scale(). While I tipped about image() w/ 5 parameters!
    You gotta experiment w/ both and check which 1 is better for yourself! ;)

  • o ok :( looks like il dig in on that tomorrow!

  • okayy @GoToLoop! once again you were right! only issue is the videos are getting stretched to cover my entire screen. So what i did was, since all my final videos are going to be 1920 X 1080, their equivalent size on my screen ( which is 1440 X 900 ) would be 1440 X 810. So i wrote the following - image( movies[idx] , width >> 1, height >> 1, 1440, 810 );
    this seems to be doing the job..thanks again!

    @TfGuy44 i tried your code as well, but just not able to get it to work, im sure im doing something wrong! thanks a bunch anyway.

Sign In or Register to comment.