Posts Tagged ‘Java’

Musings of a SpringOne 2009 Attendee – Day 2

October 21st, 2009

Running a day late on my posts. Here’s day two (yesterday)

Grails Quick Start – David Klien

David walked through the creation of a Grails web application to track a JUG’s meeting schedule. I liked his presentation style or maybe because the room wasn’t very crowded things just registered better. Picked up a few tips such as the Bootstrap class. Grails still has a ways to go in the eclipse tooling. It would’ve been nice to have been able to File –> New Project and follow along. Too bad IntelliJIDEA CE doesn’t support grails though there has been plenty of buzz on the latest STS. Downloading this right now. Only 3 more hours for the download to complete!

I think I’m beginning to dig duck typing. All in all the presentation encouraged me to put my head down and hammer out a sample app to start building some grails knowledge. More homework! » Read more: Musings of a SpringOne 2009 Attendee – Day 2

Hands-on OSGi and Modular Web Applications – Part I – Toes First

October 19th, 2009

A Brief Introduction

This is the first in a series of blog posts that will attempt to demystify OSGi and demonstrate how it enables the creation of modular web applications. We will explore various aspects of the technology along with the challenges of using this technology. I encourage you to join in the discussion by posting any comments about your own experiences or challenges you have faced developing OSGi applications. We start with the assumption that we understand what OSGi is and the specific modularity problem it tries to solve. Here are some resources you can visit to read up on this.

  1. http://neilbartlett.name/blog/2008/06/06/what-is-osgi-for/ – this one talks about the problem space
  2. http://www.infoq.com/interviews/osgi-adrian-colyer – this one brings Spring and OSGi together

Turn on the ignition

Lets get started. This first post will show you how to launch an OSGi framework and how you can interact with it. You will first need to have a JDK installed. I recommend the Sun JDK. You then need an OSGi implementation. » Read more: Hands-on OSGi and Modular Web Applications – Part I – Toes First

Hibernate Criteria trick

October 8th, 2009

So here’s the situation.

Let’s say I have this query here:

SELECT * FROM employees
WHERE employee_id NOT IN ( 1234 , 3456 , 5678 );

How do we do that with the Hibernate Criteria object with a Restriction?  You would think that the Restrictions API would have a “not in” method, since it does have a not equals method(ne), but alas, there is nothing…

Well, here’s the solution:

//Create the criteria
Criteria crit = factory.getCurrentSession().createCriteria(Employee.class);
 
//add my restriction where idList is a list of emp ids that need to be excluded
crit.add(Restrictions.not(Restrictions.in("employeeId", idList)));
 
//get some results
List employees = crit.list();

There you go!  Now you know this neat little trick and you can use it in your own app… Be forewarned though, it can be slow…

Image Processing Using ImageMagick and JMagick

October 4th, 2009

Introduction to ImageMagick

ImageMagick® is a software suite to create, edit, and compose bitmap images. It can read, convert and write images in a variety of formats (over 100) including DPX, EXR, GIF, JPEG, JPEG-2000, PDF, PhotoCD, PNG, Postscript, SVG, and TIFF. Use ImageMagick to translate, flip, mirror, rotate, scale, shear and transform images, adjust image colors, apply various special effects, or draw text, lines, polygons, ellipses and Bézier curves.

The functionality of ImageMagick is typically utilized from the command line. In this blog I am focussing on how to use Java with ImageMagick. There are two options available to use ImageMagick

1)      JMagick provides an object-oriented Java interface to ImageMagick which I am going to show in this blog.

2)      Calling the ImagicMagick directly as the Command line using Runtime.getRuntime().exec(command);

Jmagick

JMagick is an open source Java interface of ImageMagick. It is implemented in the form of Java Native Interface (JNI) into the ImageMagick API.

JMagick does not attempt to make the ImageMagick API object-oriented. It is merely a thin interface layer into the ImageMagick API.

Image Conversion using Jmagick

This function shows how to convert one file format to other format mainly I am focusing on PDF to TIFF conversion. Conversion of PDF into multiple page TIFF  or single page TIFF and also the function is also extensible for accepting Compression Format such as GROUP4, FAX or JPEG.

public void convert(File inputFile, File outputDirectory, ImageType outputType, boolean multiple) {
 
if (inputFile != null && inputFile.exists() && ImageUtil.isValidMime(inputFile)) {
 
try {
 
ImageInfo info = getImageInfo(inputFile);
 
String fileName = inputFile.getName();
 
fileName = inputFile.getName().split("\\.")[0];
 
if (multiple) {
 
MagickImage image = new MagickImage(info);
 
MagickImage[] imArray = image.breakFrames();
 
for (int i = 0; i < imArray.length; i++) {
 
StringBuilder outputFile = new StringBuilder(outputDirectory.getAbsolutePath());
 
outputFile.append(File.separatorChar);
 
File file = new File(outputFile.toString());
 
imArray[i].setFileName(file.getAbsolutePath());
 
imArray[i].writeImage(info);
 
}
 
} else {
 
StringBuilder outputFile = new StringBuilder(outputDirectory.getAbsolutePath());
 
outputFile.append(File.separatorChar);
 
outputFile.append(fileName);
 
outputFile.append(".");
 
outputFile.append(outputType.name().toLowerCase());
 
File file = new File(outputFile.toString());
 
report.addOutputFile(file);
 
info.setAdjoin(1);
 
MagickImage image = new MagickImage(info);
 
image.setFileName(file.getAbsolutePath());
 
image.writeImage(info);
 
}
 
} catch (Exception e) {
 
e.printStackTrace();
 
}
 
}
 
}
 
private ImageInfo getImageInfo(File inputName) throws MagickException {
 
String density = this.getProperties().getProperty(IMAGEMAGIC_DENSITY, "300");
 
ImageInfo info = new ImageInfo(inputName.getAbsolutePath());
 
info.setDensity(density);
 
info.setCompression(CompressionType.Group4Compression);
 
info.setColorspace(ColorspaceType.RGBColorspace);
 
return info;
 
}