Wednesday, 3 January 2018

How to take screen shot of a system and save it to database in Java ?

To view any user's desktop in network or any organisation, there are many paid or may be free software is available. But if you want to develop your own software, you can develop into Java.

Here is the example code in which I have used MYSQL database to save screen shot. This example will save new image to database of given system and if image already exists of given system then it will update the table.

If you are familiar with database connectivity in JAVA, you know that we require database driver- i.e. also known as mysql_connector.jar file to make database connectivity.

You can download this jar file from here.


import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.*;
import java.sql.*;
import javax.imageio.ImageIO;

public class TakeShot {
    
    public void shot()
    {
      try {

//To capture image
            Robot robot = new Robot();
// put the desired filename and file extension
            String format = "png";
            String fileName = "fscshot." + format;
  // get the screen size and image of screen           
            Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
            BufferedImage screenFullImage = robot.createScreenCapture(screenRect);
            ImageIO.write(screenFullImage, format, new File(fileName));
 //Save file to disk          
            File file=new File("fscshot.png");
            FileInputStream fis=new FileInputStream(file);
  //         
            System.out.println("A full screenshot saved!");
 //get ip address  of system
            InetAddress IP=InetAddress.getLocalHost();
            String ipadd=IP.getHostAddress();
System.out.println("IP of my system is := "+ipadd);
//Database connectivity
            PreparedStatement ps=null;
            Class.forName("com.mysql.jdbc.Driver");
            Connection con=DriverManager.getConnection("jdbc:mysql://localhost/lenser","root","");
            Statement stm=con.createStatement();
// check image is already saved in database then update image

            String sql="select ip from scshot where ip='"+ipadd+"'";
            ResultSet rs=stm.executeQuery(sql);
            if(rs.next())
            {
String ipp=rs.getString("ip");
System.out.println(ipp);
if(ipp.equals(ipadd)==true)
{
System.out.println("ipfound");
                                         ps=con.prepareStatement("update scshot set shot=? where ip='"+ipadd+"'"); 
                                       // ps.setString(1,ipadd);
                                        ps.setBinaryStream(1,fis,(int)file.length());
                                        ps.executeUpdate();
}
}
//if there is no image exist of IP address the insert new image
else
{
System.out.println("ipnotfound");
        ps=con.prepareStatement("insert into scshot (ip,shot) values(?,?)"); 
ps.setString(1,ipadd);
ps.setBinaryStream(2,fis,(int)file.length());
ps.executeUpdate();
}

            rs.close();
            ps.close();
            fis.close();
            con.close();
        } catch (Exception ex) {
            System.out.println(ex);
        }
    }
    
}

No comments:

Post a Comment

Change image source dynamically on hyperlink

 Changing image source dynamically using JQuery. Here in this example I have created there hyperlink and stored all images in the same folde...