Skip to content

Posts from the ‘Java’ Category

17
Mar

Create thumbnails and avatars in Java

Avatars–icon-sized images used to represent people online–are usually square. Consider, Facebook avatars are 50 by 50 pixel images. Most source images are not square, however. Cameras typically take pictures with a 4:3 aspect ratio. So to create an avatar from an image, you must do two things: crop it to a square and resize it to your desired icon size.

Thumbnails present a similar challenge.  When displaying thumbnails in an image gallery, you typically want to enforce at least one of width|height to be the same for all images to avoid a very sloppy look.  The cleanest look is achieved with square thumbnails of equal size, though the necessary cropping may not be desireable in every case.
Read moreRead more

17
Mar

Handy RowMapper base class for Spring JDBC

RowMappers are needed all over the place in your Spring JDBC DAO classes. One challenge that I kept running into was that when I wanted to reuse a particular RowMapper class for numerous queries, there was an ever-present threat of an underlying SQLException if certain columns were not present in the ResultSet. Obviously, a reusable RowMapper will set every field on the object it maps for, however not every ResultSet will include every field. Calling rs.getString("column_name"); will result in an exception being thrown if column_name is not present in the particular ResultSet.

So to solve this problem, I wrote this base RowMapper class:
Read moreRead more

9
Mar

Define global lists, sets and maps in Spring configuration

Sometimes you may need to define stand-alone collections–lists, maps and sets–in your Spring XML configuration files, so that your beans can reference them as properties.  This is easy to do using the util schema.  For example, let’s say you want to define a set of credit scores in your applicationContext.xml file:

<util:set id="creditScores">
    <value>600</value>
    <value>710</value>
    <value>760</value>
</util:set>

Now you can reference the set in your beans, like so:

<bean id="creditBean" class="com.this.is.my.CreditBean">
    <property name="myScores">
        <ref local="creditScores"/>
    </property>
</bean>

Read moreRead more

4
Mar

How to load an image from a URL in java

Assuming you want to load the image from the URL into memory in order to display or manipulate it:

try {
    URL imageUrl = new URL("http//example/image.jpg");
    InputStream in = imageUrl.openStream();
    BufferedImage image = ImageIO.read(in);
    in.close();
}
catch (IOException ioe) {
    //log the error
}

Loading an image from a URL is as simple as that.  Now you’ve got a BufferedImage.

It can be even simpler, and if all you are doing is cropping or resizing the image, you may be interested in a little java image utility I wrote.  Using my ImageLoader and Image classes, the code would look like:

try {
    Image image = ImageLoader.fromUrl("http//example/image.jpg");
}
catch (IOException ioe) {
    //log the error
}

The Image class is a wrapper on a BufferedImage and provides some useful methods for cropping, resizing, and writing (saving) the image.  Let me know in the comments below if you’ve found this helpful.

3
Mar

Java member access levels – public, protected, private

Java access level cheat sheet:

Chart shows java access levels

 *no modifier is usually refered to as package-private.

  • Classes can be declared:
    • public
    • package-private
  • Methods and member variables can be declared: 
    • public
    • protected
    • package-private
    • private

It’s easy to remember the effect of public and private.  One grants total access, while the other restricts access to the class itself.  Both protected and package-private grant access to the package, but only protected grants access to subclasses.

Good practice dictates that you keep access as restricted as possible.  This enforces encapsulation and helps forestall bugs in the code.  Keep in mind, both public and protected members become part of your API forever (unless you want to break clients, which is not nice).  Preserving encapsulation ensures you are not tied to a particular implementation and keeps your API clean. 

More Here.

If you repost the chart above, please cite this blog.  (icons from famfamfam)

2
Mar

Resize images in Java, preserving image quality

It shouldn’t be so difficult to do simple image manipulation in java.  Resizing images is a frequently-encountered need, often to create thumbnails or to shrink pictures taken from digital cameras to a reasonable display size.  But how to create thumbnails in java without sacrificing image quality?  Standard library image manipulation is severely lacking in this area.

Luckily, talented java programmers have worked to create better solutions.  I’ve thrown together an image utility, building off of the work of others, to expose a few basic image manipulation functions, namely: open (from a file, URL, InputStream or byte array), save to file, soften, resize, and resize to square.  This may be useful to your project.  Just read the important caveat toward the bottom of this post.

I make no warrantees about this utility.  If you like it, a link back to this blog would be more than welcome.

Read moreRead more