Misfiring Neurons Just another geek with a blog

26Dec/08Off

Quick and easy image resizing with Java ImageIO

There are quite a lot of examples of image resizing floating around the web but they all seem to access all sort of hidden features and classes deep inside the JDK to achieve their goal. Here is the most straightforward method that I could come up with - all in all just 5 lines of code. It requires ImageIO and requires Java 1.4 or later to run. If the ImageIO.write() method accepted plain ol' AWT Image arguments it could have been reduced further still to three lines, but you have to give it a BufferedImage instance as input.

The snippet below assumes that there exist the following variables (coloured green in the code):

  • an input stream to read from
  • a desired width variable (specifying the height as -1 tells the toolkit to preserve the original aspect ratio)
  • and an output stream to write the encoded image to.

Sample code:

import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
// ...

BufferedImage sourceImage = ImageIO.read(inputStream);
Image thumbnail = sourceImage.getScaledInstance(width, -1, Image.SCALE_SMOOTH);
BufferedImage bufferedThumbnail = new BufferedImage(thumbnail.getWidth(null),
                                                    thumbnail.getHeight(null),
                                                    BufferedImage.TYPE_INT_RGB);
bufferedThumbnail.getGraphics().drawImage(thumbnail, 0, 0, null);
ImageIO.write(bufferedThumbnail, "jpeg", outputStream);
20Aug/08Off

Random rants

  • Is the SOAP Stack an Embarrassing Failure? (Or should that be "Java's SOAP Stack" rather?) Axis 2, um, "leads the pack" in that regard. WS-* not complicated enough for you? Let's reinvent the app server wheel while we're at it too. If you have to do Web Services in Java, I recommend you check out Spring Web Services.
  • Enabling Media Sharing in Vista doesn't unblock the Windows Firewall ports it uses. A while ago I got this to work with my Xbox 360 (though it was much slower and never worked with video compared to the free, 3rd party app TVersity). Then one day it decided to stop working. Turns out that Windows Firewall decided to block it for no apparent reason. This despite me re-enabling Media Sharing several times afterwards. @#$%! And this, kids, is why Apple is winning this round.
  • Badly implemented Ajax is worse than no Ajax. We survived for many years without dynamic web pages but now everyone feels the need to Web 2.0-ify their online presence. Looking at you, Good Reads. Rendering badly in Firefox is not hip in this day and age ;-)

P.S. Don't you just hate it when they put footnotes at the end of a book?

21Jun/08Off

Effective Java Updated

Brush up on your Java 5 bag of tricks with this talk by Joshua Bloch based on his updated Effective Java book (can't recommend it highly enough).

A colleague of mine commented that this book has about the same information density as maths textbooks, so don't be mislead by the low number of pages - it is excellent value for money ;-)

10Jun/08Off

QCon Enterprise Service Bus Presentation

Jim Webber and Martin Fowler discuss ESBs, agile methods, and... ASCII man-boob p0rn. Must see!

Video at InfoQ.

3Jun/08Off

JetBrains’ Dmitry Jemerov on Scala

I can rave about JetBrains' IntelliJ IDEA until the cows come home - it is simply a superbly executed and very well focused product. What I found especially interesting  is the following quote on Scala becoming a dominant language for the JVM:

I don't believe that, however: Scala is very complicated, it's tricky, and has a lot of surprises and edge-cases. I would say that Scala is at least as complicated as C++, and with C++ you need a hundred-page style guide before you even start writing C++ code, otherwise you'll end up writing C++ code that nobody will understand.

Scala gives an impression of great elegance and simplicity at first glance but the same goes for Perl as well. The one feature of Scala I really like is the language-level support for traits which allow reusing multiple concrete implementations similarly to multiple inheritance, but with fewer gotchas.

Via Artima: JetBrains' Dmitry Jemerov on IntelliJ 8, Flex, and Scala.