Thursday, 3 December 2015

Image processing using JaxaXT in a Notes / Domino Agent



Today I stumbled across JavaXT. JavaXT is described as
a collection of Java libraries and utilities that provide a number of functions not available in the standard JDK. It is an extension of the Java core, written to simplify Java development.

I needed a library to resize images - from a good old java agent :-/  So far I used the Java Advanced Imaging API  but I had issues resizing some JPEG images. I was not able to find a correlation what caused the issues: And to be honest, my code was not very clean and neat so this was a good opportunity for improvements anyway. 
Using JavaXT I refactored my whole code to very few lines to do some resizing of images and save them in a NotesDatabase. 

This sample processes all Files in a given folder and resizes them to a fixed width of 200 pixel. 

import java.io.File;
import lotus.domino.*;

public class JavaAgent extends AgentBase {

  public void NotesMain() {

    try {
      Session session = getSession();
      AgentContext agentContext = session.getAgentContext();

      File[] files = new File("D:/tmp/pix/").listFiles();
      for (File file : files) {
        if (!file.isDirectory()) {
          String srcFileName = "D:/tmp/pix/" + file.getName();

          javaxt.io.Image image = new javaxt.io.Image(srcFileName);

          String destFileName = "D:/tmp/pix/resize/" + file.getName();
          image.setWidth(200);
          image.saveAs(destFileName);
          
        }
      }

    } catch(Exception e) {
      e.printStackTrace();
    }
  }
}

Job done! In a few lines of code :-)
Besides image processing JavaXT provides a lot more, I can only recommend to check it out. 
If you have suggestions , e. g. for a better approach  I am more than happy about your feedback.