Skip to content, Skip to search

Changes

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]
</source>
 
<h3>Loading an image file into a byte array</h3>
 
<source lang="lisp">
(import [java.io File FileInputStream]
(defn ^bytes load-file
"Load a file into a byte array."
[filepath]
(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)))))))
b))
</source>
 
... which then may be parsed as a java.awt.Image:
 
<source lang="lisp">
(def img (javax.imageioImageIO/read
(java.io.ByteArrayInputStream.
(load-file "/home/acardona/Desktop/t2/NileBend.jpg"))))
</source>
 
 
... which then may be shown as an ImagePlus:
 
<source lang="lisp">
(.show (ij.ImagePlus. "nile bend" img))
</source>
[[Category:Scripting]]
Emailconfirmed, uploaders
653
edits