Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

54
LINES

< > BotCompany Repo | #626 // Screenshot maker (JavaX)

JavaX source code - run with: x30.jar

1  
!596 // auto-import
2  
3  
import javax.imageio.*;
4  
import java.awt.image.*;
5  
import java.awt.*;
6  
7  
public class main {
8  
	public static void main(String[] args) throws Exception {
9  
	  BufferedImage image = makeScreenshot();
10  
	  save("output/screenshot.png", image);
11  
	}
12  
13  
	static void save(String path, BufferedImage image) throws IOException {
14  
	  saveBinaryFile(path, convertToPNG(image));
15  
	}
16  
	
17  
  public static BufferedImage makeScreenshot() {
18  
    try {
19  
      Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
20  
      Rectangle screenRectangle = new Rectangle(screenSize);
21  
      Robot robot = new Robot();
22  
      return robot.createScreenCapture(screenRectangle);
23  
    } catch (Exception e) {
24  
      throw new RuntimeException(e);
25  
    }
26  
  }
27  
28  
  public static byte[] convertToPNG(BufferedImage image) {
29  
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
30  
    try {
31  
      ImageIO.write(image, "png", baos);
32  
    } catch (IOException e) {
33  
      throw new RuntimeException(e);
34  
    }
35  
    return baos.toByteArray();
36  
  }
37  
38  
  /** writes safely (to temp file, then rename) */
39  
  public static void saveBinaryFile(String fileName, byte[] contents) throws IOException {
40  
    File file = new File(fileName);
41  
    File parentFile = file.getParentFile();
42  
    if (parentFile != null)
43  
      parentFile.mkdirs();
44  
    String tempFileName = fileName + "_temp";
45  
    FileOutputStream fileOutputStream = new FileOutputStream(tempFileName);
46  
    fileOutputStream.write(contents);
47  
    fileOutputStream.close();
48  
    if (file.exists() && !file.delete())
49  
      throw new IOException("Can't delete " + fileName);
50  
51  
    if (!new File(tempFileName).renameTo(file))
52  
      throw new IOException("Can't rename " + tempFileName + " to " + fileName);
53  
  }
54  
}

Author comment

Seems to work. (java x12 626 yields an output/screenshot.png)

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, teubizvjbppd, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #626
Snippet name: Screenshot maker (JavaX)
Eternal ID of this version: #626/1
Text MD5: 6c33b128982e13c41054003b8f7b8b8e
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-06-19 20:41:23
Source code size: 1775 bytes / 54 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 686 / 622
Referenced in: [show references]