2

I'm a Java beginner. I'm building a screenshot application, wherein you press PrtScn and it captures the screen. I want this screen image to be saved with the help of a Filedialog or JFileChooser. I am not sure how to pass the BufferedImage to the Filedialog box. In my code I have just hard coded the saving location.

import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class SeeEyeEye {
public static void main(String[] args) {
JFrame aWindow = new JFrame("SeeEyeEye");
aWindow.setBounds(100, 310, 210, 110);
aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 JTextField typingArea = new JTextField(20);
 typingArea.addKeyListener(new KeyListener() {


        public void keyTyped(KeyEvent e) {
           displayInfo(e, "KEY TYPED: "); }

    public void keyPressed(KeyEvent e) {
           displayInfo(e, "KEY PRESSED: "); }

    public void keyReleased(KeyEvent e) {
           displayInfo(e, "KEY RELEASED: "); }


  protected void displayInfo(KeyEvent e, String s) {
    String keyString;

      int keyCode = e.getKeyCode();
      if(keyCode==154) {  
                 try {

                         Robot robot = new Robot();
                          // Capture the screen shot of the area of the screen defined by the rectangle
                           BufferedImage bi=robot.createScreenCapture(new Rectangle(0,25,1366,744));
                           ImageIO.write(bi, "png", new File("C://Users//Rao//Desktop//s.png"));
                }

                     catch (AWTException e1) {
                         e1.printStackTrace(); }
                     catch (IOException e1) {
                         e1.printStackTrace(); }

               }
      else { System.out.println("try again");}
      keyString = "key code = " + keyCode + " (" + KeyEvent.getKeyText(keyCode) + ")";
  System.out.println(keyString);

    }

});
aWindow.add(typingArea);
aWindow.setVisible(true);
 }
}
Scitech
  • 513
  • 1
  • 5
  • 16
  • Use the FileDialog or JFileChooser to get the path/name of the file, and use that File to save the Image. – copeg May 07 '15 at 15:50

2 Answers2

3

Use a FileDialog or JFileChooser to get the File (name and path) the user selects. Then use this File to save the image using ImageIO:

JFileChooser fileChooser = new JFileChooser();
int returnVal = fileChooser.showOpenDialog(null);
if ( returnVal == JFileChooser.APPROVE_OPTION ){
    File file = fileChooser.getSelectedFile();
    ImageIO.write(image, 'jpg', file);//assuming you want a jpg, and BufferedImage variable name is image
}
copeg
  • 8,290
  • 19
  • 28
0

You don't pass the 'Buffered Image' to the file.

FileDialog is to ask the user where to store the file, and once the user selects the location and file name, you write the 'Buffered Image' to that file and save it.

K139
  • 3,654
  • 13
  • 17