Read and Write Images to files in Java

While working with Images we often think images I/O is similar to any other File I/O, I recently faced a situation when I had to work with Images in Java.

There might be several other ways to do it, I liked it through ImageIO utility.

Read Image

   1: //read it from a file.
   2: BufferedImage fromFile = ImageIO.read(new File("path to the file"));
   3: //read it from an InputStream
   4: Buffered fromIs = ImageIO.read(new FileInputStream("path to the file"));
   5: //read it from a URL
   6: Buffered fromUrl = ImageIO.read(new URL("a URL for the image"));

Write Image

   1: //Read the Image from a byte Array - I am assuming the Image coming from a stream

   2: BufferedImage image = ImageIO.read(new ByteArrayOutputStream(bytearray));

   3: //Write the image to a file
   4: ImageIO.write(image,"GIF",new FileOutputStream("path to the file"));

Advertisements

Integrate Java and R – Just in a minute

Hi,
Creating a rich statistical application can become very handy and useful once you integrate Java with R.
Integration needs following to be installed already:

  1. JDK 1.5 or above ( I tried with 1.6 )
  2. R – can be downloaded from CRAN
  3. R_HOME environment variable is set.

Here is how you can integrate and configure your java to work with R:

  1. From R install the rJava package ( can be easily be done with install command in R ). The latest rJava contains JRI implicit so no need to download it manually.
  2. Go to the download directory of R  and you can see the rJava directory, and inside that there will be JRI directory include that in your java.library.path

And you are done 🙂
Enjoy !