Archive for October, 2007

Adding a date/time stamp to a jpeg image in Java

Monday, October 22nd, 2007

I’ve been looking for a simple tool to add a date/time stamp to some jpeg images. There are quite a lot of tools to do this but they all tend to cost money. I decided to make my own in Java. The actual process of loading a jpeg, adding a stamp and then saving it again is very simple. The below snippet will do the job.

BufferedImage bi = ImageIO.read(sourceFile);
Graphics2D graphics = bi.createGraphics();
Font font = new Font("ARIAL", Font.PLAIN, 20);
graphics.setFont(font);
graphics.drawString(stamp, 50, 50);
bi.flush();
ImageIO.write(bi, "jpg", targetFile);

I added some options (you can change font size, position, colour) to meet my needs. You can download it for free from http://www.neiljohan.com/software/photostamper/. It is very basic – command line only – but does the job well enough. The Java source code and Eclipse project files are also available for download.

Adding source code into WordPress blog posting

Sunday, October 21st, 2007

As you can see from the below post I finally figured out how to easily add source code into a WordPress blog posting. Searching on Google for this issue reveals large numbers of people facing the same problem. It makes you think that including source code in a WordPress 2.2.1 blog posting is the most difficult problem known to man.

Many of the problems seem to be caused by the visual editor in WordPress. It reformats code in ways that you are unlikely to want (such as trimming any whitespace), leaving your carefully constructed source code looking like a real mess.

The way to turn it off is to go to ‘Login -> Users -> Edit’ and then untick the ‘Use the visual editor when writing’. There seems to be no way to edit and then re-edit in-line source code when the visual editor is turned on, because when you re-edit your posting the visual editor reformats your code automatically – even if you don’t make any changes and switch straight to the code editor.

This problem is in part because if you have the visual editor enabled it becomes the default editor and re-formats any code that it loads. WordPress could easily fix this be allowing you to make the code editor the default editor so the visual editor only gets its hands on your posting if you give it permission. There is a discussion on the worpress.org site about this here.

If you still want to use the visual editor then the workaround that I’ve seen mentioned is to create a second user account with the visual editor turned on. You then need to use the correct account depending on whether you will post source code or not and remember never to load your source code posts with the visual editor.

The next problem that I came across was that adding code within <pre></pre> tags causes the lines to be double spaced when using the default WordPress theme. I solved this by installing the CodeMarkup plugin which allows code samples to be displayed in a way which looks sensible to me. It automatically escapes all your code so that you can paste it directly in between <pre><code></code></pre> tags without having to use a separate tool to escape the special characters.

ProGuard Eclipse Ant build.xml

Saturday, October 20th, 2007

If you are building J2SE apps with Eclipse and want to use ProGuard to compress, optimise or obfuscate your code you’ll need to create an Ant build.xml file to do this. I found various bits of help on the internet explaining parts of the build.xml file, but couldn’t find anywhere that gave the complete build.xml to do the full compile, jar and proguard steps.

Here is the full build.xml I cobbled together to do all three:


<?xml version="1.0" ?>

<project default="main">

<taskdef resource="proguard/ant/task.properties"
classpath="D:\apps\proguard4.0.1\lib\proguard.jar" />

<target name="main" depends="compile, jar, obfuscate"
description="Create project">
<echo>Creating project.</echo>
</target>

<target name="compile" description="Compile target">
<javac srcdir="src" destdir="bin"/>
</target>

<target name="jar" description="Jar target">
<jar jarfile="PhotoStamper_debug.jar"
basedir="bin" includes="*.class">
<manifest>
<attribute name="Main-Class" value="PhotoStamper" />
</manifest>
</jar>
</target>

<target name="obfuscate" depends="jar"
description="Obfuscate compiled classes">
<proguard>
-libraryjars "${java.home}\lib\rt.jar"
-injars PhotoStamper_debug.jar
-outjars PhotoStamper.jar
-keep public class PhotoStamper {
public static void main(java.lang.String[]);
}
</proguard>
</target>

</project>