Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 42 lines (36 sloc) 1.36 kB
44962b2 @cemerick initial import from O'Reilly svn repo
cemerick authored
1 (ns com.clojurebook.imaging
2 (:use [clojure.java.io :only (file)])
3 (:import (java.awt Image Graphics2D)
4 javax.imageio.ImageIO
5 java.awt.image.BufferedImage
6 java.awt.geom.AffineTransform))
7
8 (defn load-image
9 [file-or-path]
10 (-> file-or-path file ImageIO/read))
11
12 (defn resize-image
13 ^BufferedImage [^Image original factor]
14 (let [scaled (BufferedImage. (* factor (.getWidth original))
15 (* factor (.getHeight original))
16 (.getType original))]
17 (.drawImage ^Graphics2D (.getGraphics scaled)
18 original
19 (AffineTransform/getScaleInstance factor factor)
20 nil)
21 scaled))
22
23 (gen-class
24 :name ResizeImage
25 :main true
26 :methods [^:static [resizeFile [String String double] void]
27 ^:static [resize [java.awt.Image double] java.awt.image.BufferedImage]])
28
29 (def ^:private -resize resize-image)
30
31 (defn- -resizeFile
32 [path outpath factor]
33 (ImageIO/write (-> path load-image (resize-image factor))
34 "png"
35 (file outpath)))
36
37 (defn -main
38 [& [path outpath factor]]
39 (when-not (and path outpath factor)
40 (println "Usage: java -jar example-uberjar.jar ResizeImage [INFILE] [OUTFILE] [SCALE FACTOR]")
41 (System/exit 1))
42 (-resizeFile path outpath (Double/parseDouble factor)))
Something went wrong with that request. Please try again.