Skip to content

Instantly share code, notes, and snippets.

@thebusby
Created February 19, 2010 02:20
Show Gist options
  • Save thebusby/308335 to your computer and use it in GitHub Desktop.
Save thebusby/308335 to your computer and use it in GitHub Desktop.
(defn capture-image
"Capture an image of specified size from the primary screen at specified point"
[from-x from-y x-size y-size]
(let [rob (java.awt.Robot.)
rect (java.awt.Rectangle. from-x from-y x-size y-size)]
(. rob createScreenCapture rect)))
(defn grab-screen
"Capture the entire primary screen and return BufferedImage"
[]
(let [my-scr (.. java.awt.Toolkit (getDefaultToolkit) (getScreenSize))]
(capture-image 0 0 (. my-scr width) (. my-scr height))))
(defn save-image
"Save an image to disk as jpg"
[image filename]
(javax.imageio.ImageIO/write image "jpg" (java.io.File. filename)))
(defn load-image
"Read a jpg from disk"
[filename]
(javax.imageio.ImageIO/read (java.io.File. filename)))
(defn display-image
"Display an image to the screen"
[image]
(let [frame (doto (javax.swing.JFrame. "Display Image")
(.setBounds 0 0 (. image getWidth) (. image getHeight))
(.setVisible true))
cv (proxy [java.awt.Canvas] []
(paint [g] (. g drawImage image nil nil))
(update [g] (. g drawImage image nil nil)))]
(.add (.getContentPane frame) cv)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment