Showing posts with label screen. Show all posts
Showing posts with label screen. Show all posts

Sunday, December 18, 2016

Create your own Free Screen Recorder using Java

Their are many commercial software which allows you to record your desktop screen and save the video.
In this post we will learn how to create a simple screen recorder using Java. This recorder will be capable of recording full screen or record the user selected region.

How it works:
1) When user starts the recording then program starts 2 threads.
2) The first thread keeps on taking screenshot of the screen and saves them into local disk mentioned in variable inputImgDir
3) The second thread continuously looks over the inputImageDir and adds all the new images into the video.
4) When user press stop recording then both threads stop and final video is retrieved.

Language Used:
Java

Git Repo:
https://github.com/csanuragjain/recorder/tree/master/ScreenRecorder

POM Dependency:
Please add the below pom dependency from https://mvnrepository.com/artifact/org.bytedeco/javacv:
<dependency>   
   <groupId>org.bytedeco</groupId>   
   <artifactId>javacv</artifactId>   
   <version>1.0</version>   
  </dependency>  

Reference:
https://cooltrickshome.blogspot.in/2016/11/convert-movie-to-images-using-java.html
https://cooltrickshome.blogspot.in/2016/11/create-desktop-screenshot-maker.html

Program:

Variables:
      public static boolean videoComplete=false;  
      public static String inputImageDir="inputImgFolder"+File.separator;  
      public static String inputImgExt="png";  
      public static String outputVideo="recording.mp4";   
      public static int counter=0;  
      public static int imgProcessed=0;  
      public static FFmpegFrameRecorder recorder=null;  
      public static int videoWidth=1920;  
      public static int videoHeight=1080;  
      public static int videoFrameRate=3;  
      public static int videoQuality=0; // 0 is the max quality  
      public static int videoBitRate=9000;  
      public static String videoFormat="mp4";  
      public static int videoCodec=avcodec.AV_CODEC_ID_MPEG4;  
      public static Thread t1=null;  
      public static Thread t2=null;  
      public static JFrame frame=null;  
      public static boolean isRegionSelected=false;  
      public static int c1=0;  
      public static int c2=0;  
      public static int c3=0;  
      public static int c4=0;  


Explanation:
1) videoComplete variables tells if user has stopped the recording or not.
2) inputImageDir defines the input directory where screenshots will be stored which would be utilized by the video thread
3) inputImgExt denotes the extension of the image taken for screenshot.
4) outputVideo is the name of the recorded video file
5) counter is used for numbering the screenshots when stored in input directory.
6) recorder is used for starting and stopping the video recording
7) videoWidth, videoFrameRate etc define output video param
8) If user wants to record only a selected region then c1,c2,c3,c4 denotes the coordinate

getRecorder method:
      public static FFmpegFrameRecorder getRecorder() throws Exception  
      {  
            if(recorder!=null)  
            {  
                 return recorder;  
            }  
            recorder = new FFmpegFrameRecorder(outputVideo,videoWidth,videoHeight);  
            try  
            {  
            recorder.setFrameRate(videoFrameRate);  
      recorder.setVideoCodec(videoCodec);  
      recorder.setVideoBitrate(videoBitRate);  
      recorder.setFormat(videoFormat);  
      recorder.setVideoQuality(videoQuality); // maximum quality  
      recorder.start();  
            }  
            catch(Exception e)  
            {  
                 JOptionPane.showMessageDialog(frame, "Exception while starting the recorder object "+e.getMessage());  
                 System.out.println("Exception while starting the recorder object "+e.getMessage());  
                 throw new Exception("Unable to start recorder");  
            }  
      return recorder;  
      }  

Explanation:
1) This method is used to get the Recorder object.
2) We create an object of FFmpegFrameRecorder named "Recorder" and then set all its video parameters.
3) Lastly we start the recorder and then return the object.

getRobot method:
      public static Robot getRobot() throws Exception  
      {  
           Robot r=null;  
           try {  
                r = new Robot();  
                return r;  
           } catch (AWTException e) {  
                JOptionPane.showMessageDialog(frame, "Issue while initiating Robot object "+e.getMessage());  
                System.out.println("Issue while initiating Robot object "+e.getMessage());  
                throw new Exception("Issue while initiating Robot object");  
           }  
      }  

Explanation:
1) This method retrieves an object of Robot class which could be further utilized by remaining methods.

main Method:
      public static void main(String[] args) {  
           try {  
                if(getRecorder()==null)  
                {  
                     System.out.println("Cannot make recorder object, Exiting program");  
                     System.exit(0);  
                }  
                if(getRobot()==null)  
                {  
                     System.out.println("Cannot make robot object, Exiting program");  
                     System.exit(0);  
                }  
                File scanFolder=new File(inputImageDir);  
                scanFolder.delete();  
                scanFolder.mkdirs();  
                createGUI();  
           } catch (Exception e) {  
                System.out.println("Exception in program "+e.getMessage());  
           }  
      }  

Explanation:
1) We initialize the recorder and Robot object.
2) We create the input image folder.
3) Now we open up the GUI which will allow user to start/stop the recording.

createGUI method:
      public static void createGUI()  
      {  
           frame=new JFrame("Screen Recorder");  
           JButton b1=new JButton("Select Region for Recording");  
           JButton b2=new JButton("Start Recording");  
           JButton b3=new JButton("Stop Recording");  
           JLabel l1=new JLabel("<html><br/>If you dont select a region then full screen recording <br/> will be made when you click on Start Recording</html>");  
           l1.setFont (l1.getFont ().deriveFont (20.0f));  
           b1.addActionListener(new ActionListener() {  
       @Override  
       public void actionPerformed(ActionEvent e) {  
            try {  
                 JOptionPane.showMessageDialog(frame, "A new window will open. Use your mouse to select the region you like to record");  
                          new CropRegion().getImage();  
                     } catch (Exception e1) {  
                          // TODO Auto-generated catch block  
                          System.out.println("Issue while trying to call the module to crop region");  
                          e1.printStackTrace();  
                     }   
       }  
     });  
           b2.addActionListener(new ActionListener() {  
       @Override  
       public void actionPerformed(ActionEvent e) {  
            counter=0;  
         startRecording();  
       }  
     });  
           b3.addActionListener(new ActionListener() {  
       @Override  
       public void actionPerformed(ActionEvent e) {  
         stopRecording();  
         System.out.print("Exiting...");  
         System.exit(0);  
       }  
     });  
           frame.add(b1);  
           frame.add(b2);  
           frame.add(b3);  
           frame.add(l1);  
           frame.setLayout(new FlowLayout(0));  
           frame.setVisible(true);  
           frame.setSize(1000, 170);  
           frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
      }  

Explanation:
1) We make a JFrame with the button for staring and stopping the recording. One more button is added for allowing user to record only a selected portion of screen
2) If user clicks to select only certain region then we call a class CropRegion method getImage which helps in retrieving the coordinate of the region selected by user and update the same in variable c1,c2,c3,c4
3) If user clicks on start recording then startRecording method is called
4) If user clicks on stoprecording then stopRecording method is called

startRecording method:
      public static void startRecording()  
      {  
           t1=new Thread()  
           {  
             public void run() {  
               try {  
                          takeScreenshot(getRobot());  
                     } catch (Exception e) {  
                          JOptionPane.showMessageDialog(frame, "Cannot make robot object, Exiting program "+e.getMessage());  
                          System.out.println("Cannot make robot object, Exiting program "+e.getMessage());  
                          System.exit(0);  
                     }  
             }  
           };  
           t2=new Thread()  
           {  
             public void run() {  
               prepareVideo();  
             }  
           };  
           t1.start();  
           t2.start();  
           System.out.println("Started recording at "+new Date());  
      }  


Explanation:
1) Two threads are started in this module when user starts the recording
2) First thread calls the takeScreenshot module which keeps on taking screenshot of user screen and saves them on local disk.
3) Second thread calls the prepareVideo which monitors the screenshot created in step 2 and add them continuously on the video.

takeScreenshot method:
      public static void takeScreenshot(Robot r)  
      {  
           Dimension size = Toolkit.getDefaultToolkit().getScreenSize();  
           Rectangle rec=new Rectangle(size);  
           if(isRegionSelected)  
           {  
                rec=new Rectangle(c1, c2, c3-c1, c4-c2);  
           }  
           while(!videoComplete)  
           {  
           counter++;  
           BufferedImage img = r.createScreenCapture(rec);  
           try {  
                ImageIO.write(img, inputImgExt, new File(inputImageDir+counter+"."+inputImgExt));  
           } catch (IOException e) {  
                JOptionPane.showMessageDialog(frame, "Got an issue while writing the screenshot to disk "+e.getMessage());  
                System.out.println("Got an issue while writing the screenshot to disk "+e.getMessage());  
                counter--;  
           }  
           }  
      }  

Explanation:
1) If user has selected a region for recording then we set the rectangle with the coordinate value of c1,c2,c3,c4. Otherwise we set the rectangle to be full screen
2) Now we run a loop until videoComplete is false (remains false until user press stop recording.
3) Now we capture the region and write the same to the input image directory.
4) So when user starts the recording this method keeps on taking screenshot and saves them into disk.

prepareVideo method:
      public static void prepareVideo()  
      {  
           File scanFolder=new File(inputImageDir);  
           while(!videoComplete)  
           {  
                File[] inputFiles=scanFolder.listFiles();  
                try {  
                     getRobot().delay(500);  
                } catch (Exception e) {  
                }  
                //for(int i=0;i<scanFolder.list().length;i++)  
                for(int i=0;i<inputFiles.length;i++)  
                {  
                     //imgProcessed++;  
                     addImageToVideo(inputFiles[i].getAbsolutePath());  
                     //String imgToAdd=scanFolder.getAbsolutePath()+File.separator+imgProcessed+"."+inputImgExt;  
                     //addImageToVideo(imgToAdd);  
                     //new File(imgToAdd).delete();  
                     inputFiles[i].delete();  
                }  
           }  
           File[] inputFiles=scanFolder.listFiles();  
           for(int i=0;i<inputFiles.length;i++)  
           {  
                addImageToVideo(inputFiles[i].getAbsolutePath());  
                inputFiles[i].delete();  
           }  
      }  

Explanation:
1) We start a loop which will run until video complete is set true (done only when user press stop recording)
2) We keep on monitoring the input  Image directory
3) We traverse each file found in the input image directory and add those images to video using the addImageToVideo method. After the image has been added we delete the image
4) Using the loop in step1 we keep on repeating step 2 and 3 so that each image gets added to video. We added a delay of 500ms so that this module does not picks a half created image from the takeScreenshot module
5) When user press stop recording the loop gets broken. Now we finally traverse the input image directory and add the remaining images to video.

addImageToVideo method:
      public static void addImageToVideo(String imgPath)  
      {  
           try {  
                getRecorder().record(getFrameConverter().convert(cvLoadImage(imgPath)));  
           } catch (Exception e) {  
                JOptionPane.showMessageDialog(frame, "Exception while adding image to video "+e.getMessage());  
                System.out.println("Exception while adding image to video "+e.getMessage());  
           }  
      }  

Explanation:
1) cvLoadImage is used to load the image passed as argument
2) We call the convert method to convert the image to frame which could be used by the recorder
3) We pass the frame obtained in step 2 and add the same in the recorder by calling the record method.

getFrameConverter method:
      public static OpenCVFrameConverter.ToIplImage getFrameConverter()  
      {  
           OpenCVFrameConverter.ToIplImage grabberConverter = new OpenCVFrameConverter.ToIplImage();  
           return grabberConverter;  
      }  

Explanation:
1) We make an object of OpenCVFrameConverter.ToIplImage named grabberConverter and returns this object for other modules.

stopRecording method:
      public static void stopRecording()  
      {  
           try {  
                videoComplete=true;  
                System.out.println("Stopping recording at "+new Date());  
                t1.join();  
                System.out.println("Screenshot thread complete");  
                t2.join();  
                System.out.println("Video maker thread complete");  
                getRecorder().stop();  
                System.out.println("Recording has been saved successfully at "+new File(outputVideo).getAbsolutePath());  
                JOptionPane.showMessageDialog(frame, "Recording has been saved successfully at "+new File(outputVideo).getAbsolutePath());  
           } catch (Exception e) {  
                System.out.println("Exception while stopping the recorder "+e.getMessage());  
           }  
      }  


Explanation:
1) After user press the stop recording, we wait for both the threads to complete.
2) After thread completes, we stop the recorder and show the video path to user.

Output:

 Started recording at Sun Dec 18 18:29:36 IST 2016  
 Stopping recording at Sun Dec 18 18:29:51 IST 2016  
 Screenshot thread complete  
 Video maker thread complete  
 Recording has been saved successfully at C:\Users\anjain\workspace\BrowserMobProxy\cooltrickshome\recording.mp4  
 Exiting...libpng error: PNG unsigned integer out of range  
 Output #0, mp4, to 'recording.mp4':  
   Stream #0:0: Video: mpeg4, yuv420p, 1920x1080, q=2-31, 9 kb/s, 3 tbn, 3 tbc  

Full Program:

ScreenRecorder.java
 package com.cooltrickshome;  
 import static org.bytedeco.javacpp.opencv_imgcodecs.cvLoadImage;  
 import java.awt.AWTException;  
 import java.awt.Dimension;  
 import java.awt.FlowLayout;  
 import java.awt.Rectangle;  
 import java.awt.Robot;  
 import java.awt.Toolkit;  
 import java.awt.event.ActionEvent;  
 import java.awt.event.ActionListener;  
 import java.awt.image.BufferedImage;  
 import java.io.File;  
 import java.io.IOException;  
 import java.util.Date;  
 import javax.imageio.ImageIO;  
 import javax.swing.JButton;  
 import javax.swing.JFrame;  
 import javax.swing.JLabel;  
 import javax.swing.JOptionPane;  
 import org.bytedeco.javacpp.avcodec;  
 import org.bytedeco.javacv.FFmpegFrameRecorder;  
 import org.bytedeco.javacv.OpenCVFrameConverter;  
 public class ScreenRecorder{  
      public static boolean videoComplete=false;  
      public static String inputImageDir="inputImgFolder"+File.separator;  
      public static String inputImgExt="png";  
      public static String outputVideo="recording.mp4";   
      public static int counter=0;  
      public static int imgProcessed=0;  
      public static FFmpegFrameRecorder recorder=null;  
      public static int videoWidth=1920;  
      public static int videoHeight=1080;  
      public static int videoFrameRate=3;  
      public static int videoQuality=0; // 0 is the max quality  
      public static int videoBitRate=9000;  
      public static String videoFormat="mp4";  
      public static int videoCodec=avcodec.AV_CODEC_ID_MPEG4;  
      public static Thread t1=null;  
      public static Thread t2=null;  
      public static JFrame frame=null;  
      public static boolean isRegionSelected=false;  
      public static int c1=0;  
      public static int c2=0;  
      public static int c3=0;  
      public static int c4=0;  
      public static void main(String[] args) {  
           try {  
                if(getRecorder()==null)  
                {  
                     System.out.println("Cannot make recorder object, Exiting program");  
                     System.exit(0);  
                }  
                if(getRobot()==null)  
                {  
                     System.out.println("Cannot make robot object, Exiting program");  
                     System.exit(0);  
                }  
                File scanFolder=new File(inputImageDir);  
                scanFolder.delete();  
                scanFolder.mkdirs();  
                createGUI();  
           } catch (Exception e) {  
                System.out.println("Exception in program "+e.getMessage());  
           }  
      }  
      public static void createGUI()  
      {  
           frame=new JFrame("Screen Recorder");  
           JButton b1=new JButton("Select Region for Recording");  
           JButton b2=new JButton("Start Recording");  
           JButton b3=new JButton("Stop Recording");  
           JLabel l1=new JLabel("<html><br/>If you dont select a region then full screen recording <br/> will be made when you click on Start Recording</html>");  
           l1.setFont (l1.getFont ().deriveFont (20.0f));  
           b1.addActionListener(new ActionListener() {  
       @Override  
       public void actionPerformed(ActionEvent e) {  
            try {  
                 JOptionPane.showMessageDialog(frame, "A new window will open. Use your mouse to select the region you like to record");  
                          new CropRegion().getImage();  
                     } catch (Exception e1) {  
                          // TODO Auto-generated catch block  
                          System.out.println("Issue while trying to call the module to crop region");  
                          e1.printStackTrace();  
                     }   
       }  
     });  
           b2.addActionListener(new ActionListener() {  
       @Override  
       public void actionPerformed(ActionEvent e) {  
            counter=0;  
         startRecording();  
       }  
     });  
           b3.addActionListener(new ActionListener() {  
       @Override  
       public void actionPerformed(ActionEvent e) {  
         stopRecording();  
         System.out.print("Exiting...");  
         System.exit(0);  
       }  
     });  
           frame.add(b1);  
           frame.add(b2);  
           frame.add(b3);  
           frame.add(l1);  
           frame.setLayout(new FlowLayout(0));  
           frame.setVisible(true);  
           frame.setSize(1000, 170);  
           frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
      }  
      public static void startRecording()  
      {  
           t1=new Thread()  
           {  
             public void run() {  
               try {  
                          takeScreenshot(getRobot());  
                     } catch (Exception e) {  
                          JOptionPane.showMessageDialog(frame, "Cannot make robot object, Exiting program "+e.getMessage());  
                          System.out.println("Cannot make robot object, Exiting program "+e.getMessage());  
                          System.exit(0);  
                     }  
             }  
           };  
           t2=new Thread()  
           {  
             public void run() {  
               prepareVideo();  
             }  
           };  
           t1.start();  
           t2.start();  
           System.out.println("Started recording at "+new Date());  
      }  
      public static Robot getRobot() throws Exception  
      {  
           Robot r=null;  
           try {  
                r = new Robot();  
                return r;  
           } catch (AWTException e) {  
                JOptionPane.showMessageDialog(frame, "Issue while initiating Robot object "+e.getMessage());  
                System.out.println("Issue while initiating Robot object "+e.getMessage());  
                throw new Exception("Issue while initiating Robot object");  
           }  
      }  
      public static void takeScreenshot(Robot r)  
      {  
           Dimension size = Toolkit.getDefaultToolkit().getScreenSize();  
           Rectangle rec=new Rectangle(size);  
           if(isRegionSelected)  
           {  
                rec=new Rectangle(c1, c2, c3-c1, c4-c2);  
           }  
           while(!videoComplete)  
           {  
           counter++;  
           BufferedImage img = r.createScreenCapture(rec);  
           try {  
                ImageIO.write(img, inputImgExt, new File(inputImageDir+counter+"."+inputImgExt));  
           } catch (IOException e) {  
                JOptionPane.showMessageDialog(frame, "Got an issue while writing the screenshot to disk "+e.getMessage());  
                System.out.println("Got an issue while writing the screenshot to disk "+e.getMessage());  
                counter--;  
           }  
           }  
      }  
      public static void prepareVideo()  
      {  
           File scanFolder=new File(inputImageDir);  
           while(!videoComplete)  
           {  
                File[] inputFiles=scanFolder.listFiles();  
                try {  
                     getRobot().delay(500);  
                } catch (Exception e) {  
                }  
                //for(int i=0;i<scanFolder.list().length;i++)  
                for(int i=0;i<inputFiles.length;i++)  
                {  
                     //imgProcessed++;  
                     addImageToVideo(inputFiles[i].getAbsolutePath());  
                     //String imgToAdd=scanFolder.getAbsolutePath()+File.separator+imgProcessed+"."+inputImgExt;  
                     //addImageToVideo(imgToAdd);  
                     //new File(imgToAdd).delete();  
                     inputFiles[i].delete();  
                }  
           }  
           File[] inputFiles=scanFolder.listFiles();  
           for(int i=0;i<inputFiles.length;i++)  
           {  
                addImageToVideo(inputFiles[i].getAbsolutePath());  
                inputFiles[i].delete();  
           }  
      }  
      public static FFmpegFrameRecorder getRecorder() throws Exception  
      {  
            if(recorder!=null)  
            {  
                 return recorder;  
            }  
            recorder = new FFmpegFrameRecorder(outputVideo,videoWidth,videoHeight);  
            try  
            {  
            recorder.setFrameRate(videoFrameRate);  
      recorder.setVideoCodec(videoCodec);  
      recorder.setVideoBitrate(videoBitRate);  
      recorder.setFormat(videoFormat);  
      recorder.setVideoQuality(videoQuality); // maximum quality  
      recorder.start();  
            }  
            catch(Exception e)  
            {  
                 JOptionPane.showMessageDialog(frame, "Exception while starting the recorder object "+e.getMessage());  
                 System.out.println("Exception while starting the recorder object "+e.getMessage());  
                 throw new Exception("Unable to start recorder");  
            }  
      return recorder;  
      }  
      public static OpenCVFrameConverter.ToIplImage getFrameConverter()  
      {  
           OpenCVFrameConverter.ToIplImage grabberConverter = new OpenCVFrameConverter.ToIplImage();  
           return grabberConverter;  
      }  
      public static void addImageToVideo(String imgPath)  
      {  
           try {  
                getRecorder().record(getFrameConverter().convert(cvLoadImage(imgPath)));  
           } catch (Exception e) {  
                JOptionPane.showMessageDialog(frame, "Exception while adding image to video "+e.getMessage());  
                System.out.println("Exception while adding image to video "+e.getMessage());  
           }  
      }  
      public static void stopRecording()  
      {  
           try {  
                videoComplete=true;  
                System.out.println("Stopping recording at "+new Date());  
                t1.join();  
                System.out.println("Screenshot thread complete");  
                t2.join();  
                System.out.println("Video maker thread complete");  
                getRecorder().stop();  
                System.out.println("Recording has been saved successfully at "+new File(outputVideo).getAbsolutePath());  
                JOptionPane.showMessageDialog(frame, "Recording has been saved successfully at "+new File(outputVideo).getAbsolutePath());  
           } catch (Exception e) {  
                System.out.println("Exception while stopping the recorder "+e.getMessage());  
           }  
      }  
 }  

CropRegion.java
 package com.cooltrickshome;  
 import java.awt.AWTException;  
 import java.awt.Dimension;  
 import java.awt.FlowLayout;  
 import java.awt.Graphics;  
 import java.awt.Rectangle;  
 import java.awt.Robot;  
 import java.awt.Toolkit;  
 import java.awt.event.MouseEvent;  
 import java.awt.event.MouseListener;  
 import java.awt.event.MouseMotionListener;  
 import java.awt.image.BufferedImage;  
 import java.io.IOException;  
 import javax.swing.JFrame;  
 import javax.swing.JLabel;  
 import javax.swing.JOptionPane;  
 public class CropRegion implements MouseListener,  
           MouseMotionListener {  
      int drag_status = 0;  
      int c1;  
      int c2;  
      int c3;  
      int c4;  
      JFrame frame=null;  
      static int counter=0;  
      JLabel background=null;  
      public void getImage() throws AWTException, IOException, InterruptedException {  
           Dimension size = Toolkit.getDefaultToolkit().getScreenSize();  
        Robot robot = new Robot();  
        BufferedImage img = robot.createScreenCapture(new Rectangle(size));  
        ImagePanel panel = new ImagePanel(img);  
        frame=new JFrame();  
        frame.add(panel);  
        frame.setLocation(0, 0);  
        frame.setSize(size);  
        frame.setLayout(new FlowLayout());  
        frame.setUndecorated(true);  
        frame.setVisible(true);  
        frame.addMouseListener(this);  
        frame.addMouseMotionListener(this);  
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);  
      }  
      public void draggedScreen() throws Exception {  
           ScreenRecorder.c1=c1;  
           ScreenRecorder.c2=c2;  
           ScreenRecorder.c3=c3;  
           ScreenRecorder.c4=c4;  
           ScreenRecorder.isRegionSelected=true;  
           JOptionPane.showMessageDialog(frame, "Region Selected.Please click on Start Recording button to record the selected region.");  
           frame.dispose();  
      }  
      public void mouseClicked(MouseEvent arg0) {  
      }  
      public void mouseEntered(MouseEvent arg0) {  
      }  
      public void mouseExited(MouseEvent arg0) {  
      }  
      public void mousePressed(MouseEvent arg0) {  
           paint();  
           this.c1 = arg0.getX();  
           this.c2 = arg0.getY();  
      }  
      public void mouseReleased(MouseEvent arg0) {  
           paint();  
           if (this.drag_status == 1) {  
                this.c3 = arg0.getX();  
                this.c4 = arg0.getY();  
                try {  
                     draggedScreen();  
                } catch (Exception e) {  
                     e.printStackTrace();  
                }  
           }  
      }  
      public void mouseDragged(MouseEvent arg0) {  
           paint();  
           this.drag_status = 1;  
           this.c3 = arg0.getX();  
           this.c4 = arg0.getY();  
      }  
      public void mouseMoved(MouseEvent arg0) {  
      }  
      public void paint() {  
           Graphics g = frame.getGraphics();  
           frame.repaint();  
           int w = this.c1 - this.c3;  
           int h = this.c2 - this.c4;  
           w *= -1;  
           h *= -1;  
           if (w < 0) {  
                w *= -1;  
           }  
           g.drawRect(this.c1, this.c2, w, h);  
      }  
 }  

ImagePanel.java
 package com.cooltrickshome;  
 import java.awt.Dimension;  
 import java.awt.Graphics;  
 import java.awt.Image;  
 import javax.swing.ImageIcon;  
 import javax.swing.JPanel;  
 class ImagePanel  
  extends JPanel  
 {  
  private Image img;  
  public ImagePanel(String img)  
  {  
   this(new ImageIcon(img).getImage());  
  }  
  public ImagePanel(Image img)  
  {  
   this.img = img;  
   Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));  
   setPreferredSize(size);  
   setMinimumSize(size);  
   setMaximumSize(size);  
   setSize(size);  
   setLayout(null);  
  }  
  public void paintComponent(Graphics g)  
  {  
   g.drawImage(this.img, 0, 0, null);  
  }  
 }  


Note:
I am not sure if there could be a way to reduce the resulting video size. If you have please let me know
Right now I am using an int variable named counter to name the screenshot. For a long recording this will cause an issue since int range will be exhausted. Need to think about an alternative for same.
I have recorded for few minutes but haven't tested for longer duration.

If you have any suggestions on this, please feel free to contact me via comments or directly at my email cs.anurag.jain@gmail.com :)

Hope it helps :)

Saturday, November 19, 2016

Play Movies & Videos on Desktop Wallpaper using Java

This blog post will help you play movie/video on your desktop wallpaper.

Langauge Used:
Java & C++

Git Location:
https://github.com/csanuragjain/extra/tree/master/DesktopMovieWallpaper

Working Software:

Part 1 https://github.com/csanuragjain/extra/blob/master/DesktopMovieWallpaper/Working%20Software/Working%20Software.z01?raw=true
Part 2
https://github.com/csanuragjain/extra/blob/master/DesktopMovieWallpaper/Working%20Software/Working%20Software.zip?raw=true

Download both part and use winzip or winrar to extract.

Concept:

1) We ask user the video which he would like to set as desktop wallpaper
2) Now we convert the video to images using tutorial http://cooltrickshome.blogspot.com/2016/11/convert-movie-to-images-using-java.html
3) Assume step 2 converted the video to 100 images.
4) We will start a loop and set desktop wallpaper all the 100 images found in step 3 one by one.
5) Overall effect is a movie playing in desktop wallpaper

Note:
1) This program converts the video to images. The overall converted images will be of size greater than the video. Make sure you have sufficient space if you are trying to convert a big video.

Pom Dependency:
Please add the below pom dependency from https://mvnrepository.com/artifact/org.bytedeco/javacv :
 <dependency>  
   <groupId>org.bytedeco</groupId>  
   <artifactId>javacv</artifactId>  
   <version>1.0</version>  
 </dependency>  

Java Program:
Main method:
      public static void main(String[] args) throws InterruptedException, Exception, IOException {  
           Scanner s=new Scanner(System.in);  
            System.out.println("Enter the path of mp4 (for eg c:\\test.mp4)");  
            String mp4Path=s.nextLine();  
            File imgPath=new File("convert");  
            imgPath.delete();  
            imgPath.mkdirs();  
            String imagePath=imgPath.getAbsolutePath();  
            convertMovietoJPG(mp4Path, imagePath,"jpg",3);  
            System.out.println("Conversion complete. Please find the images at "+imagePath);  
            changeDesktopWallpaper(imagePath,5000);  
      }  

How it works:
1) We make a scanner object to read user input
2) We ask user to enter path of mp4 file
3) We make a new folder named convert in current project directory. This will be the directory where images extracted from video would be kept
4) We call convertMovietoJPG method which converts the mp4 to images. I kept frametoJump as 3 but you may change it as per your requirement. More details on this http://cooltrickshome.blogspot.com/2016/11/convert-movie-to-images-using-java.html
5) Finally we call changeDesktopWallpaper which will set the images extracted from movie as wallpaper one by one giving a movie effect.
6) First argument defines the path containing the converted image and 5000 represents the millisecond after which video will start replay after it completes.

convertMovietoJPG method:
       public static void convertMovietoJPG(String mp4Path, String imagePath, String imgType, int frameJump) throws Exception, IOException  
       {  
                 Java2DFrameConverter converter = new Java2DFrameConverter();  
          FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber(mp4Path);  
          frameGrabber.start();  
          Frame frame;  
          double frameRate=frameGrabber.getFrameRate();  
          int imgNum=0;  
          System.out.println("Video has "+frameGrabber.getLengthInFrames()+" frames and has frame rate of "+frameRate);  
          try {           
            for(int ii=1;ii<=frameGrabber.getLengthInFrames();ii++){  
            imgNum++;       
            frameGrabber.setFrameNumber(ii);  
            frame = frameGrabber.grab();  
            BufferedImage bi = converter.convert(frame);  
            String path = imagePath+File.separator+imgNum+".jpg";  
            ImageIO.write(bi,imgType, new File(path));  
            ii+=frameJump;  
            }  
            frameGrabber.stop();  
          } catch (Exception e) {  
            e.printStackTrace();  
          }  
        }  


How it works:
1) Already covered the explanation at http://cooltrickshome.blogspot.com/2016/11/convert-movie-to-images-using-java.html

changeDesktopWallpaper method:
      public static void changeDesktopWallpaper(String path, int sleepTime) throws InterruptedException  
      {  
           System.out.println("Starting to replay video after every "+(sleepTime/1000)+"s");  
           File f=new File(path);  
           File[] filePath=f.listFiles();  
           while(true)  
           {  
           for(int i=1;i<=filePath.length;i++)  
           {       
           changeWallpaper(path+"\\"+i+".jpg");  
           }  
           Thread.sleep(sleepTime);  
           }  
      }  


How it works:
1) We point file array on all the images extracted from video
2) For each image we call changeWallpaper which changes the wallpaper to that image.
3) Once all images are complete it waits for sleepTime and then restarts from step2

C++ Program:

wallpaperchanger.h
 /* DO NOT EDIT THIS FILE - it is machine generated */  
 #include <jni.h>  
 /* Header for class com_cooltrickshome_DesktopMovieWallpaper */  
 #ifndef _Included_com_cooltrickshome_DesktopMovieWallpaper  
 #define _Included_com_cooltrickshome_DesktopMovieWallpaper  
 #ifdef __cplusplus  
 extern "C" {  
 #endif  
 /*  
  * Class:   com_cooltrickshome_DesktopMovieWallpaper  
  * Method:  changeWallpaper  
  * Signature: (Ljava/lang/String;)I  
  */  
 JNIEXPORT jint JNICALL Java_com_cooltrickshome_DesktopMovieWallpaper_changeWallpaper  
  (JNIEnv *, jclass, jstring);  
 #ifdef __cplusplus  
 }  
 #endif  
 #endif  

wallpaperchanger.cpp
  #include <iostream>  
  #include <windows.h>  
  #include <fstream>  
  #include <cstdlib>  
  #include <jni.h>  
  #include "wallpaperchanger.h"  
  JNIEXPORT jint JNICALL Java_com_cooltrickshome_DesktopMovieWallpaper_changeWallpaper  
  (JNIEnv *env, jclass, jstring wallpaper){  
  const char *wallpaper_file = env->GetStringUTFChars(wallpaper, JNI_FALSE);  
  int return_value = SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, (void *)wallpaper_file, SPIF_UPDATEINIFILE | SPIF_SENDCHANGE);  
  env->ReleaseStringUTFChars(wallpaper, wallpaper_file);  
  }  

How it works:
1) changeWallpaper method used by Java is defined in wallpaperchanger.dll which is created using wallpaperchanger.h & wallpaperchanger.cpp (Conversion from cpp & h to dll is explained at https://cooltrickshome.blogspot.in/2016/11/creating-your-personal-keylogger-from.html)
2) For cpp code, wallpaper variable contains the path of the wallpaper which is passed by Java
3) First we need to convert it to const char* which is done using env->GetStringUTFChars
4) SystemParametersInfo method is used to convert the wallpaper. We pass the desktop wallpaper path in it to change the wallpaper
5) Now we release the converted string.

Output:
 Enter the path of mp4 (for eg c:\test.mp4)  
 C:\Users\anjain\Desktop\images\rough\1.mp4  
 Video has 132 frames and has frame rate of 25.0  
 Conversion complete. Please find the images at C:\Users\anjain\workspace\BrowserMobProxy\cooltrickshome\convert  
 Starting to replay video after every 5s  

Full Program:

DesktopMovieWallpaper.java
 package com.cooltrickshome;  
 import java.awt.image.BufferedImage;  
 import java.io.File;  
 import java.io.IOException;  
 import java.util.Scanner;  
 import javax.imageio.ImageIO;  
 import org.bytedeco.javacv.FFmpegFrameGrabber;  
 import org.bytedeco.javacv.Frame;  
 import org.bytedeco.javacv.Java2DFrameConverter;  
 import org.bytedeco.javacv.FrameGrabber.Exception;  
 public class DesktopMovieWallpaper {  
      public static native int changeWallpaper(String path);  
      static  
   {  
     System.loadLibrary("wallpaperchanger");  
   }  
      public static void changeDesktopWallpaper(String path, int sleepTime) throws InterruptedException  
      {  
           System.out.println("Starting to replay video after every "+(sleepTime/1000)+"s");  
           File f=new File(path);  
           File[] filePath=f.listFiles();  
           while(true)  
           {  
           for(int i=1;i<=filePath.length;i++)  
           {       
           changeWallpaper(path+"\\"+i+".jpg");  
           }  
           Thread.sleep(sleepTime);  
           }  
      }  
       public static void convertMovietoJPG(String mp4Path, String imagePath, String imgType, int frameJump) throws Exception, IOException  
       {  
                 Java2DFrameConverter converter = new Java2DFrameConverter();  
          FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber(mp4Path);  
          frameGrabber.start();  
          Frame frame;  
          double frameRate=frameGrabber.getFrameRate();  
          int imgNum=0;  
          System.out.println("Video has "+frameGrabber.getLengthInFrames()+" frames and has frame rate of "+frameRate);  
          try {           
            for(int ii=1;ii<=frameGrabber.getLengthInFrames();ii++){  
            imgNum++;       
            frameGrabber.setFrameNumber(ii);  
            frame = frameGrabber.grab();  
            BufferedImage bi = converter.convert(frame);  
            String path = imagePath+File.separator+imgNum+".jpg";  
            ImageIO.write(bi,imgType, new File(path));  
            ii+=frameJump;  
            }  
            frameGrabber.stop();  
          } catch (Exception e) {  
            e.printStackTrace();  
          }  
        }  
      public static void main(String[] args) throws InterruptedException, Exception, IOException {  
           Scanner s=new Scanner(System.in);  
            System.out.println("Enter the path of mp4 (for eg c:\\test.mp4)");  
            String mp4Path=s.nextLine();  
            File imgPath=new File("convert");  
            imgPath.delete();  
            imgPath.mkdirs();  
            String imagePath=imgPath.getAbsolutePath();  
            convertMovietoJPG(mp4Path, imagePath,"jpg",3);  
            System.out.println("Conversion complete. Please find the images at "+imagePath);  
            changeDesktopWallpaper(imagePath,5000);  
      }  
 }  

wallpaperchanger.h
 /* DO NOT EDIT THIS FILE - it is machine generated */  
 #include <jni.h>  
 /* Header for class com_cooltrickshome_DesktopMovieWallpaper */  
 #ifndef _Included_com_cooltrickshome_DesktopMovieWallpaper  
 #define _Included_com_cooltrickshome_DesktopMovieWallpaper  
 #ifdef __cplusplus  
 extern "C" {  
 #endif  
 /*  
  * Class:   com_cooltrickshome_DesktopMovieWallpaper  
  * Method:  changeWallpaper  
  * Signature: (Ljava/lang/String;)I  
  */  
 JNIEXPORT jint JNICALL Java_com_cooltrickshome_DesktopMovieWallpaper_changeWallpaper  
  (JNIEnv *, jclass, jstring);  
 #ifdef __cplusplus  
 }  
 #endif  
 #endif  

wallpaperchanger.cpp
  #include <iostream>  
  #include <windows.h>  
  #include <fstream>  
  #include <cstdlib>  
  #include <jni.h>  
  #include "wallpaperchanger.h"  
  JNIEXPORT jint JNICALL Java_com_cooltrickshome_DesktopMovieWallpaper_changeWallpaper  
  (JNIEnv *env, jclass, jstring wallpaper){  
  const char *wallpaper_file = env->GetStringUTFChars(wallpaper, JNI_FALSE);  
  int return_value = SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, (void *)wallpaper_file, SPIF_UPDATEINIFILE | SPIF_SENDCHANGE);  
  env->ReleaseStringUTFChars(wallpaper, wallpaper_file);  
  }  

Hope it helps :)

Wednesday, November 16, 2016

Access Clipboard content using Java

You can access the text or images in clipboard using Java.

Software Features:

1) On copying any text, it gets placed in clipboard. This program can retrieve the text from clipboard
2) On using print screen, the image gets stored in clipboard. This program can retrieve the image from clipboard
3) When you copy file in windows, the file path get stored in clipboard. This program can retrieve the file path from clipboard and then copy the file to current directory.

Language:
Java

Git Location:
https://github.com/csanuragjain/extra/tree/master/Clipboard/AccessClipboard

Terms Used:
Current directory: Current directory in this post refer to the path from where you are running the program

Program:

Main method:
      public static void main(String[] args) throws Exception {  
           Toolkit toolkit = Toolkit.getDefaultToolkit();  
           Clipboard clipboard = toolkit.getSystemClipboard();  
           if (clipboard.isDataFlavorAvailable(DataFlavor.javaFileListFlavor))   
           {  
           getCopiedFile(clipboard);  
           }  
           if (clipboard.isDataFlavorAvailable(DataFlavor.stringFlavor))   
           {  
           getText(clipboard);  
           }  
           if (clipboard.isDataFlavorAvailable(DataFlavor.imageFlavor))   
           {  
           getImageFromClipboard(clipboard);  
           }  
      }  


How it works:
1) We access the clipboard object by using Toolkit.getDefaultToolkit().getSystemClipboard()
2) Now we check what data is present in clipboard by calling isDataFlavorAvailable(DataFlavor)
3) If copied image is present in clipboard then clipboard.isDataFlavorAvailable(DataFlavor.javaFileListFlavor) will return true else false. If true, we call getCopiedFile(clipboard) which shows the name of all files which user copied and saves the copied file at current directory.
4) If text is present in clipboard then clipboard.isDataFlavorAvailable(DataFlavor.stringFlavor) will return true else false. If true then we call getText(clipboard) which retrieves the text from clipboard and prints the same
5) If print screen image is present in clipboard then clipboard.isDataFlavorAvailable(DataFlavor.imageFlavor) return true else false. If true, we call getImageFromClipboard(clipboard) which retrieves the print screen image and saves it to your current directory

getCopiedFile method:
      public static Object getCopiedFile(Clipboard clipboard) throws UnsupportedFlavorException, IOException  
      {  
           Object result = clipboard.getData(DataFlavor.javaFileListFlavor);  
     List files = (List) result;  
     for (int i = 0; i < files.size(); i++) {  
       File file = (File) files.get(i);  
       File destFile=new File(file.getName());  
       copyFileUsingStream(file, destFile);  
       System.out.println("Copied file "+file.getName()+" to "+destFile.getAbsolutePath());  
     }  
           return result;  
      }  


How it works:
1) This method is called to retrieve the copied image and save those.
2) To explain we will take an example. Assume user copied c:\1.jpg & c:\2.jpg
3) Clipboard will now have 2 entries which is c:\1.jpg and c:\2.jpg
4) clipboard.getData(DataFlavor.javaFileListFlavor) will retrieve both the entries from clipboard in form of List<File>
5) We iterate through each entry of List<File>
6) For each File found we will copy it to the current directory. So in 1st iteration we will copy 1.jpg to the current directory & in next iteration it would copy 2.jpg to current directory.

getImageFromClipboard method:
      public static Image getImageFromClipboard(Clipboard clipboard)  
                throws Exception  
                {  
                 Transferable transferable = clipboard.getContents(null);  
                 Image img=(Image) transferable.getTransferData(DataFlavor.imageFlavor);  
                     if(img!=null)  
                     {  
                          counter++;  
                          BufferedImage buffered = (BufferedImage) img;  
                          ImageIO.write(buffered, "jpg", new File("copiedFile"+counter+".jpg"));  
                          System.out.println("Saved the clipboard image as copiedFile"+counter+".jpg");  
                     }  
                     return img;  
                }  


How it works:
1) Assume user press print screen on a page. Now the print screen image gets saved to clipboard.
2) clipboard.getContents(null).getTransferData(DataFlavor.imageFlavor) gets the image from the clipboard
3) We cast the Image to BufferedImage and then use ImageIO to write the image to a file which gets saved in current directory

getText method:
      public static String getText(Clipboard clipboard) throws UnsupportedFlavorException, IOException  
      {  
           String result = (String) clipboard.getData(DataFlavor.stringFlavor);  
           System.out.println("String from Clipboard:" + result);  
           return result;  
      }  


How it works:
1) Assume user copies a text and now the text is present in clipboard
2) clipboard.getData(DataFlavor.stringFlavor) gets the text from the clipboard and then we print it

copyFileUsingStream method:
      private static void copyFileUsingStream(File source, File dest) throws IOException {  
        InputStream is = null;  
        OutputStream os = null;  
        try {  
          is = new FileInputStream(source);  
          os = new FileOutputStream(dest);  
          byte[] buffer = new byte[1024];  
          int length;  
          while ((length = is.read(buffer)) > 0) {  
            os.write(buffer, 0, length);  
          }  
        } finally {  
          is.close();  
          os.close();  
        }  
      }  


How it works:
1) This method copies files from source to destination.

Sample Output:
Copied file 2.png to C:\Users\anurag\workspace\cooltrickshome\2.png
Copied file oranges-1117628_640.jpg to C:\Users\anurag\workspace\cooltrickshome\oranges-1117628_640.jpg

Full Program:
 package com.cooltrickshome;  
 import java.awt.Image;  
 import java.awt.Toolkit;  
 import java.awt.datatransfer.Clipboard;  
 import java.awt.datatransfer.DataFlavor;  
 import java.awt.datatransfer.Transferable;  
 import java.awt.datatransfer.UnsupportedFlavorException;  
 import java.awt.image.BufferedImage;  
 import java.io.File;  
 import java.io.FileInputStream;  
 import java.io.FileOutputStream;  
 import java.io.IOException;  
 import java.io.InputStream;  
 import java.io.OutputStream;  
 import java.util.ArrayList;  
 import java.util.List;  
 import javax.imageio.ImageIO;  
 public class AccessClipboard {  
      /**  
       * @param args  
       * @throws Exception   
       */  
      static int counter=0;  
      public static void main(String[] args) throws Exception {  
           Toolkit toolkit = Toolkit.getDefaultToolkit();  
           Clipboard clipboard = toolkit.getSystemClipboard();  
           if (clipboard.isDataFlavorAvailable(DataFlavor.javaFileListFlavor))   
           {  
           getCopiedFile(clipboard);  
           }  
           if (clipboard.isDataFlavorAvailable(DataFlavor.stringFlavor))   
           {  
           getText(clipboard);  
           }  
           if (clipboard.isDataFlavorAvailable(DataFlavor.imageFlavor))   
           {  
           getImageFromClipboard(clipboard);  
           }  
      }  
      public static Object getCopiedFile(Clipboard clipboard) throws UnsupportedFlavorException, IOException  
      {  
           Object result = clipboard.getData(DataFlavor.javaFileListFlavor);  
     List files = (List) result;  
     for (int i = 0; i < files.size(); i++) {  
       File file = (File) files.get(i);  
       File destFile=new File(file.getName());  
       copyFileUsingStream(file, destFile);  
       System.out.println("Copied file "+file.getName()+" to "+destFile.getAbsolutePath());  
     }  
           return result;  
      }  
      public static Image getImageFromClipboard(Clipboard clipboard)  
                throws Exception  
                {  
                 Transferable transferable = clipboard.getContents(null);  
                 Image img=(Image) transferable.getTransferData(DataFlavor.imageFlavor);  
                     if(img!=null)  
                     {  
                          counter++;  
                          BufferedImage buffered = (BufferedImage) img;  
                          ImageIO.write(buffered, "jpg", new File("copiedFile"+counter+".jpg"));  
                          System.out.println("Saved the clipboard image as copiedFile"+counter+".jpg");  
                     }  
                     return img;  
                }  
      public static String getText(Clipboard clipboard) throws UnsupportedFlavorException, IOException  
      {  
           String result = (String) clipboard.getData(DataFlavor.stringFlavor);  
           System.out.println("String from Clipboard:" + result);  
           return result;  
      }  
      private static void copyFileUsingStream(File source, File dest) throws IOException {  
        InputStream is = null;  
        OutputStream os = null;  
        try {  
          is = new FileInputStream(source);  
          os = new FileOutputStream(dest);  
          byte[] buffer = new byte[1024];  
          int length;  
          while ((length = is.read(buffer)) > 0) {  
            os.write(buffer, 0, length);  
          }  
        } finally {  
          is.close();  
          os.close();  
        }  
      }  
 }  

Hope it helps :)

Monday, November 14, 2016

Take Screenshot of any Website using Java

In this blog post, we will take screenshot of any given set of website using Java Selenium.

Language Used:
Java

Git Repository:
https://github.com/csanuragjain/recorder/tree/master/WebsiteScreenshot

Pre-requisite:

chromedriver.exe
1) Create a folder named driver.
2) Add the chromedriver.exe from https://sites.google.com/a/chromium.org/chromedriver/downloads and place the chromedriver.exe under driver folder

Pom dependency:
1) Add below 2 pom dependency:
   <dependency>  
    <groupId>org.apache.commons</groupId>  
    <artifactId>commons-io</artifactId>  
    <version>1.3.2</version>  
    <scope>compile</scope>  
   </dependency>  
   <dependency>  
    <groupId>org.seleniumhq.selenium</groupId>  
    <artifactId>selenium-java</artifactId>  
    <version>2.42.2</version>  
    <scope>compile</scope>  
   </dependency>  


Program:

initDriver method:
      public static void initDriver()  
      {  
           System.setProperty("webdriver.chrome.driver",  
                     "./driver/chromedriver.exe");  
           driver=new ChromeDriver();  
           driver.manage().window().setPosition(new Point(-2000, 0));  
      }  

How it works:
1) First we tell the location of the chromedriver.exe to our java program. Since we placed it under driver folder in current project directory so we set the same.
2) Now we initialize our webdriver using a chromedriver.
3) We set the position to (-2000,0) so that the browser gets minimized.

capture method:
      public void capture(String site) throws IOException  
      {  
           screenshotNum++;  
           driver.get(site);  
           File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);  
           FileUtils.copyFile(scrFile, new File("site"+screenshotNum+".png"));  
           System.out.println("Took Screenshot for "+site+" and saved as "+"site"+screenshotNum+".png");  
      }  

How it works:
1) screenshotNum shows the number of screenshots taken. This is used to name the screenshot saved.
2) we navigate to the site for which we need the screenshot.
3) Now we take the screenshot by calling getScreenshotAs function.
4) Now we copy the File pointing to screenshot into our local computer system by calling copyFile method. (copyFile is obtained using the maven dependency org.apache.commons)

destroy method:
      public static void destroy()  
      {  
           driver.quit();  
      }  

How it works:
1) we call the quit method to dispose the driver

main method:
      public static void main(String[] args) throws IOException {  
           initDriver();  
           WebPageScreenShotTaker ws=new WebPageScreenShotTaker();  
           ws.capture("https://www.google.com");  
           ws.capture("https://facebook.com");  
           destroy();  
      }  

How it works:
1) We call initDriver to intialize the webdriver
2) We call the capture method which takes screenshot of the passed website
3) We call the destroy method to close our webdriver


Sample Saved Screenshot:


Full Program:
 package com.cooltrickshome;  
 import java.io.File;  
 import java.io.IOException;  
 import org.apache.commons.io.FileUtils;  
 import org.openqa.selenium.OutputType;  
 import org.openqa.selenium.Point;  
 import org.openqa.selenium.TakesScreenshot;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.chrome.ChromeDriver;  
 public class WebPageScreenShotTaker {  
      static int screenshotNum=0;  
      static WebDriver driver=null;  
      public static void main(String[] args) throws IOException {  
           initDriver();  
           WebPageScreenShotTaker ws=new WebPageScreenShotTaker();  
           ws.capture("https://www.google.com");  
           ws.capture("https://facebook.com");  
           destroy();  
      }  
      public static void initDriver()  
      {  
           System.setProperty("webdriver.chrome.driver",  
                     "./driver/chromedriver.exe");  
           driver=new ChromeDriver();  
           driver.manage().window().setPosition(new Point(-2000, 0));  
      }  
      public void capture(String site) throws IOException  
      {  
           screenshotNum++;  
           driver.get(site);  
           File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);  
           FileUtils.copyFile(scrFile, new File("site"+screenshotNum+".png"));  
           System.out.println("Took Screenshot for "+site+" and saved as "+"site"+screenshotNum+".png");  
      }  
      public static void destroy()  
      {  
           driver.quit();  
      }  
 }  

Hope it helps :)

Sunday, November 13, 2016

Create Desktop Screenshot Maker Software using Java

In this blog post we will learn to create a Java program which can take screenshots from your desktop. It will also support taking screenshots of a selected region.

Features of the software:
1) Take Screenshots of a selected region or fullscreen
2) You can press F6 to start taking Screenshots and F8 to exit from the application
3) Once you press F6, just select the screenshot region by dragging your mouse and a screenshot would be created for selected region.
4) All screenshots are kept at same directory from where program is run

Logic Used:
1) Java can take screenshots using Robot class
2) It can act on custom key like F6 & F8 using JNI (https://cooltrickshome.blogspot.in/2016/11/creating-your-personal-keylogger-from.html)
3) Challenge is how we can make user select region of a screen for which he want to take screenshot.
 A) We take the full size screenshot of the user screen and show the same screenshot as background image of a full window JFrame.
 B) When user drags the region, he is actually dragging the region from the background image of a JFrame. Hence we can show a rectangle for the selection region.

Running Software:

Please find a working copy of the software at:
https://github.com/csanuragjain/recorder/blob/master/DesktopScreenshotMaker/Software/DesktopScreenshotMaker.zip?raw=true

Unzip it and then Use below command to run (make sure you use 64 bit java)
 java -jar DesktopScreenshotMaker.jar  

Source Code:
https://github.com/csanuragjain/recorder/tree/master/DesktopScreenshotMaker/Code

Language Used:
Java, C++

C++ Usage:

C++ captures all the user keys and passes them to Java using JNI (Already discussed in https://cooltrickshome.blogspot.in/2016/11/creating-your-personal-keylogger-from.html)
When user press F6 then Java program starts the screen capture module and likewise when it receives F8 then it exits.

Java Usage:

DesktopScreenshot.java

Variables:
 public class DesktopScreenshot implements MouseListener,  
           MouseMotionListener {  
      int drag_status = 0;  
      int c1;  
      int c2;  
      int c3;  
      int c4;  
      JFrame frame=null;  
      static int counter=0;  
      JLabel background=null;  
      static  
      {  
           System.loadLibrary("MyLogger");  
      }  
      public static native int GetKey();  

Explanation:
1) drag_status tells if mouse is currently dragging a region or not
2) c1,c2,c3,c4 defines the coordinate of the region for which screenshot would be taken
3)  Screenshot are saved as screenshot<counter>.jpg so counter will help naming screenshot files.
4) MyLogger is the dll which is tracking the user key input
5) GetKey gives the key input by user.

Main method:
      public static void main(String args[]) throws AWTException, IOException, InterruptedException  
      {  
           DesktopScreenshot ds=new DesktopScreenshot();  
           System.out.println("Press F6 and then drag your mouse to take screenshot. For exiting press F8");  
           while(true)  
           {  
           int d=GetKey();  
           if(d==117)  
           {  
                ds.getImage();  
           }  
           else  
                if(d==119)  
                {  
                     System.exit(0);  
                }  
           }  
      }  

Explanation:
1) We make an infinite loop and keep track of all user key pressed
2) If key input is 117 which is the code for F6 then we call the screenshot maker module getImage
3) If key input is 119 which is code for F8 then program exits using System.exit

getImage method:
      public void getImage() throws AWTException, IOException, InterruptedException {  
           Dimension size = Toolkit.getDefaultToolkit().getScreenSize();  
        Robot robot = new Robot();  
        BufferedImage img = robot.createScreenCapture(new Rectangle(size));  
        ImagePanel panel = new ImagePanel(img);  
        frame=new JFrame();  
        frame.add(panel);  
        frame.setLocation(0, 0);  
        frame.setSize(size);  
        frame.setLayout(new FlowLayout());  
        frame.setUndecorated(true);  
        frame.setVisible(true);  
        frame.addMouseListener(this);  
        frame.addMouseMotionListener(this);  
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);  
      }  

Explanation:
1) Toolkit.getDefaultToolkit().getScreenSize() is used to determine the size of screen
2) Now we use the createScreenCapture method of Robot class to capture the screen. Full screen would be captured since we pass the size to be screen size
3) We have made a custom ImagePanel class which would add the screenshot captured on a Frame
4) Now we create a frame and add the panel containing the screenshot of the screen.
5) We set the function setUndecorated to true so that the frame appears without any title bar
6) We add mouse listener so that user can crop the regions from the screenshot in the frame as discussed later.

Mouse events:
      public void mouseClicked(MouseEvent arg0) {  
      }  
      public void mouseEntered(MouseEvent arg0) {  
      }  
      public void mouseExited(MouseEvent arg0) {  
      }  
      public void mouseMoved(MouseEvent arg0) {  
      }  
      public void mousePressed(MouseEvent arg0) {  
           paint();  
           this.c1 = arg0.getX();  
           this.c2 = arg0.getY();  
      }  
      public void mouseDragged(MouseEvent arg0) {  
           paint();  
           this.drag_status = 1;  
           this.c3 = arg0.getX();  
           this.c4 = arg0.getY();  
      }  
      public void mouseReleased(MouseEvent arg0) {  
           paint();  
           if (this.drag_status == 1) {  
                this.c3 = arg0.getX();  
                this.c4 = arg0.getY();  
                try {  
                     draggedScreen();  
                } catch (Exception e) {  
                     e.printStackTrace();  
                }  
           }  
      }  
      public void paint() {  
           Graphics g = frame.getGraphics();  
           frame.repaint();  
           int w = this.c1 - this.c3;  
           int h = this.c2 - this.c4;  
           w *= -1;  
           h *= -1;  
           if (w < 0) {  
                w *= -1;  
           }  
           g.drawRect(this.c1, this.c2, w, h);  
      }  
      public void draggedScreen() throws Exception {  
           int w = this.c1 - this.c3;  
           int h = this.c2 - this.c4;  
           w *= -1;  
           h *= -1;  
           Robot robot = new Robot();  
           BufferedImage img = robot.createScreenCapture(new Rectangle(this.c1,  
                     this.c2, w, h));  
           counter++;  
           File save_path = new File("screenshot"+counter+".jpg");  
           ImageIO.write(img, "JPG", save_path);  
           c1=0;  
           c2=0;  
           c3=0;  
           c4=0;       
           frame.disable();  
           frame.dispose();  
           new File("desktop.jpg").delete();  
      }  

Explanation:
1) When mouse is pressed we record the starting coordinate in c1 & c2
2) When mouse is dragged then we record the ending coordinate in c3 & c4. We call the paint method which starts drawing the rectangle to show the selected region described by (c1,c2) & (c3,c4)
3) When mouse is released we get the final region for which screenshot need to taken and update c3 and c4.
4) We call draggedScreen method which simply takes the screenshot of the region selected by user and saves it using ImageIO write method.

ImagePanel.java
 package com.cooltrickshome;  
 import java.awt.Dimension;  
 import java.awt.Graphics;  
 import java.awt.Image;  
 import javax.swing.ImageIcon;  
 import javax.swing.JPanel;  
 class ImagePanel  
  extends JPanel  
 {  
  private Image img;  
  public ImagePanel(String img)  
  {  
   this(new ImageIcon(img).getImage());  
  }  
  public ImagePanel(Image img)  
  {  
   this.img = img;  
   Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));  
   setPreferredSize(size);  
   setMinimumSize(size);  
   setMaximumSize(size);  
   setSize(size);  
   setLayout(null);  
  }  
  public void paintComponent(Graphics g)  
  {  
   g.drawImage(this.img, 0, 0, null);  
  }  
 }  

Explanation:
1) We determine the height and width of the image passed
2) We make the JPanel with same size
3) We draw the image over JPanel using Graphics drawImage method.

Sample Screenshot:


Full Code:
DesktopScreenshot.java
 package com.cooltrickshome;  
 import java.awt.AWTException;  
 import java.awt.Dimension;  
 import java.awt.FlowLayout;  
 import java.awt.Graphics;  
 import java.awt.Rectangle;  
 import java.awt.Robot;  
 import java.awt.Toolkit;  
 import java.awt.event.MouseEvent;  
 import java.awt.event.MouseListener;  
 import java.awt.event.MouseMotionListener;  
 import java.awt.image.BufferedImage;  
 import java.io.File;  
 import java.io.IOException;  
 import javax.imageio.ImageIO;  
 import javax.swing.JFrame;  
 import javax.swing.JLabel;  
 public class DesktopScreenshot implements MouseListener,  
           MouseMotionListener {  
      int drag_status = 0;  
      int c1;  
      int c2;  
      int c3;  
      int c4;  
      JFrame frame=null;  
      static int counter=0;  
      JLabel background=null;  
      static  
      {  
           System.loadLibrary("MyLogger");  
      }  
      public static native int GetKey();  
      /**  
       * @param args  
       * @throws AWTException   
       * @throws IOException   
       * @throws InterruptedException   
       */  
      public static void main(String args[]) throws AWTException, IOException, InterruptedException  
      {  
           DesktopScreenshot ds=new DesktopScreenshot();  
           System.out.println("Press F6 and then drag your mouse to take screenshot. For exiting press F8");  
           while(true)  
           {  
           int d=GetKey();  
           if(d==117)  
           {  
                ds.getImage();  
           }  
           else  
                if(d==119)  
                {  
                     System.exit(0);  
                }  
           }  
      }  
      public void getImage() throws AWTException, IOException, InterruptedException {  
           Dimension size = Toolkit.getDefaultToolkit().getScreenSize();  
        Robot robot = new Robot();  
        BufferedImage img = robot.createScreenCapture(new Rectangle(size));  
        ImagePanel panel = new ImagePanel(img);  
        frame=new JFrame();  
        frame.add(panel);  
        frame.setLocation(0, 0);  
        frame.setSize(size);  
        frame.setLayout(new FlowLayout());  
        frame.setUndecorated(true);  
        frame.setVisible(true);  
        frame.addMouseListener(this);  
        frame.addMouseMotionListener(this);  
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);  
      }  
      public void draggedScreen() throws Exception {  
           int w = this.c1 - this.c3;  
           int h = this.c2 - this.c4;  
           w *= -1;  
           h *= -1;  
           Robot robot = new Robot();  
           BufferedImage img = robot.createScreenCapture(new Rectangle(this.c1,  
                     this.c2, w, h));  
           counter++;  
           File save_path = new File("screenshot"+counter+".jpg");  
           ImageIO.write(img, "JPG", save_path);  
           c1=0;  
           c2=0;  
           c3=0;  
           c4=0;       
           frame.disable();  
           frame.dispose();  
           new File("desktop.jpg").delete();  
      }  
      public void mouseClicked(MouseEvent arg0) {  
      }  
      public void mouseEntered(MouseEvent arg0) {  
      }  
      public void mouseExited(MouseEvent arg0) {  
      }  
      public void mousePressed(MouseEvent arg0) {  
           paint();  
           this.c1 = arg0.getX();  
           this.c2 = arg0.getY();  
      }  
      public void mouseReleased(MouseEvent arg0) {  
           paint();  
           if (this.drag_status == 1) {  
                this.c3 = arg0.getX();  
                this.c4 = arg0.getY();  
                try {  
                     draggedScreen();  
                } catch (Exception e) {  
                     e.printStackTrace();  
                }  
           }  
      }  
      public void mouseDragged(MouseEvent arg0) {  
           paint();  
           this.drag_status = 1;  
           this.c3 = arg0.getX();  
           this.c4 = arg0.getY();  
      }  
      public void mouseMoved(MouseEvent arg0) {  
      }  
      public void paint() {  
           Graphics g = frame.getGraphics();  
           frame.repaint();  
           int w = this.c1 - this.c3;  
           int h = this.c2 - this.c4;  
           w *= -1;  
           h *= -1;  
           if (w < 0) {  
                w *= -1;  
           }  
           g.drawRect(this.c1, this.c2, w, h);  
      }  
 }  

ImagePanel.java
 package com.cooltrickshome;  
 import java.awt.Dimension;  
 import java.awt.Graphics;  
 import java.awt.Image;  
 import javax.swing.ImageIcon;  
 import javax.swing.JPanel;  
 class ImagePanel  
  extends JPanel  
 {  
  private Image img;  
  public ImagePanel(String img)  
  {  
   this(new ImageIcon(img).getImage());  
  }  
  public ImagePanel(Image img)  
  {  
   this.img = img;  
   Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));  
   setPreferredSize(size);  
   setMinimumSize(size);  
   setMaximumSize(size);  
   setSize(size);  
   setLayout(null);  
  }  
  public void paintComponent(Graphics g)  
  {  
   g.drawImage(this.img, 0, 0, null);  
  }  
 }  

DLL:
https://github.com/csanuragjain/recorder/blob/master/DesktopScreenshotMaker/Code/dll/MyLogger.dll?raw=true

Hope it helps :)