Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Taking a screenshot of screen using Java and save it in image format

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.08k
    Comment on it

    Hello there,

    In this blog I will provide a solution to how you can get screenshot of your screen and then save it at any location of your choice for this, we will use java.awt.Robot for creating an image which will generated by reading pixels from the screen. You can then write that image to a file on disk.

     

    Java code for taking screenshot

    import java.awt.AWTException;
    import java.awt.Rectangle;
    import java.awt.Robot;
    import java.awt.Toolkit;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.util.Objects;
    
    import javax.imageio.ImageIO;
    
    public class ScreenshotDemo {
    	public static void main(String[] args) {
    		Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());	
    		BufferedImage capture;
    		
    		String str = Objects.toString(System.currentTimeMillis());
    		
    		try {
    			capture = new Robot().createScreenCapture(screenRect);
    			try {
    				ImageIO.write(capture, "png", new File(str));
    				System.out.println("Image sucessfully created");
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		} catch (AWTException e) {
    			e.printStackTrace();
    		}
    		
    	}
    }
    

     

    Image generated by the above program

     

    Thanks for reading :)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: