Skip to content, Skip to search


Clojure Scripting

868 bytes added, 09:07, 28 August 2010
Added "load file as a byte array"
user=> sa
[0, 0, 0, 7, 0, 0, 0, 0, 0, 0]
<h3>Loading an image file into a byte array</h3>
<source lang="lisp">
(import [ File FileInputStream]
(defn ^bytes load-file
"Load a file into a byte array."
(let [^File f (File. filepath)
len (int (.length f))
^bytes b (byte-array len)]
(with-open [^FileInputStream fis (FileInputStream. f)]
(loop [offset (int 0)]
(if (< offset len)
(recur (unchecked-add offset (.read fis b offset (unchecked-subtract len offset)))))))
... which then may be parsed as a java.awt.Image:
<source lang="lisp">
(def img (javax.imageioImageIO/read
(load-file "/home/acardona/Desktop/t2/NileBend.jpg"))))
... which then may be shown as an ImagePlus:
<source lang="lisp">
(.show (ij.ImagePlus. "nile bend" img))
Emailconfirmed, uploaders