Monday, October 30, 2006

Here's some code for blurring an image:

float[] kernelData = new float[9];
for (int i = 0; i < kernelData.length; i++)
{
kernelData[i] = 0.15f;
}
ConvolveOp blur = new ConvolveOp(new Kernel(3, 3, kernelData));
BufferedImage blurryImage = new BufferedImage(w, h, w, h,
BufferedImage.TYPE_INT_ARGB);
blurryImage.createGraphics().drawImage(imageToBlur, blur, 0, 2);

Add time stamp to picture using Java

public static void addDate(File inputFile) throws Exception {
String inputFilename = inputFile.getPath();
System.out.println("Starting with '" + inputFilename + "'");
BufferedImage input = ImageIO.read(inputFile);
Graphics2D graphics = (Graphics2D) input.createGraphics();
Date lastModified = new Date(inputFile.lastModified());
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
String dateStamp = sdf.format(lastModified);
int fontSize = (int) (0.4 * input.getHeight() / 10.0);
Font font = new Font("Arial", Font.BOLD, fontSize);
graphics.setFont(font);
int height = graphics.getFontMetrics().getHeight();
int width = graphics.getFontMetrics().stringWidth(dateStamp);

BufferedImage dateImage = new BufferedImage(2 * fontSize + width, 2
* fontSize + height, BufferedImage.TYPE_INT_ARGB);
Graphics2D dateGraphics = (Graphics2D) dateImage.getGraphics().create();
dateGraphics.setColor(new Color(0, 0, 0, 0));
dateGraphics.setComposite(AlphaComposite.Src);
dateGraphics.fillRect(0, 0, width, height);

dateGraphics.setComposite(AlphaComposite.SrcOver);
dateGraphics.setFont(font);
dateGraphics.setColor(Color.black);
dateGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
GlyphVector gv = font.createGlyphVector(dateGraphics
.getFontRenderContext(), dateStamp);
// dateGraphics.drawString(dateStamp, fontSize, fontSize);
dateGraphics.translate(fontSize, 2 * fontSize);
for (int i = 0; i < gv.getNumGlyphs(); i++) {
Shape glyph = gv.getGlyphOutline(i);
dateGraphics.setStroke(new BasicStroke(fontSize / 10));
dateGraphics.draw(glyph);
}
float[] kernel = new float[25];
for (int i = 0; i < kernel.length; i++)
kernel[i] = 1.0f / kernel.length;
ConvolveOp cOp = new ConvolveOp(new Kernel(5, 5, kernel));
BufferedImage blurred = cOp.filter(dateImage, null);
dateGraphics.dispose();

int iWidth = input.getWidth();
int iHeight = input.getHeight();
int cHeight = iWidth * 2 / 3;
int dHeight = (iHeight - cHeight) / 2;

int y = (iHeight - dHeight) - blurred.getHeight();
int x = iWidth - blurred.getWidth();

graphics.drawImage(blurred, x - fontSize, y - 2 * fontSize, null);
graphics.setFont(font);
graphics.setColor(Color.white);
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
graphics.drawString(dateStamp, x, y);
graphics.dispose();

int lastDotIndex = inputFilename.lastIndexOf('.');
String outputFilename = inputFilename.substring(0, lastDotIndex)
+ ".new" + inputFilename.substring(lastDotIndex);
FileOutputStream out = new FileOutputStream(outputFilename);
/* encodes image as a JPEG data stream */
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(input);
param.setQuality(0.9f, true);
encoder.setJPEGEncodeParam(param);
encoder.encode(input);
System.out.println("Ending with '" + inputFilename + "'");
}

Of course, it's not a pinnacle of Java2D programming, nor was it intended to. Since i don't have any imaging-related java.net project, i'm not intending to polish it to perfection. It even uses classes in the com.sun.image.codec.jpeg package. Hereby it is released to the public domain - feel free to use, reuse and abuse it in any way - but don't come back if your nuclear reactor stops functioning because of this :)

The result is quite OK:

Tuesday, October 24, 2006

Add new Features to Java

Fun :) Very funny :)

A much fair comparsion should be Rudy vs Groovy vs Python vs Perl.

Java should and it should be much complex than Ruby becuz of its strong typing system.

However, some of the Ease of Development(EoD) of Ruby can be absorded by Java, to richer Java's expression prower without damaging the strong typing system.

For example:
'yield return' can save your works from creating a new array to store the results in list.

Direct "regular expression' support like what Javascript does can save works from createing/managing Pattern, Matcher Objects.

Lambda provide better DSL support in Java.

...... etc.

You may(are going to) say: 'Hey !!! we the community don't need them all. Seriously, I dun need them.'

Okay, who asked for GC, Object-Oriented programming support, RTTI, Bytecode and Cross platform support ???

Back to just 3 years back now, I think sun is the one person who support GC. If you read the javalobby and even the Sun's Java REF DB. NOBODY think GC is good, everybody just try to reinforce the 'delete' key into the language.

Now, I think 99% of us agreed that GC is a must.
(hey, u disagree !!! Why?? I'm Sorry, I forget that C programmers do read javalobby :)

We're lucky that we get some smart folks to do the R&D for us. Sometime even all of us think that something go wrong, maybe its bucz (we're)||(the things) is wrong.

I'm not telling you that the community is making the wrong decisions to any change in java language( I agree that the current generic implementation sucks). Spring, iBat, Tomcat...etc from the java community are the masterpiece. But sometime people object to the new language changes just bcuz:

I dun understand it
I dun like it BUCZ I dun learned it b4
I dun use it I dunno the advantage of it
I just simple dislike changes
I have a good reason blah blah...

I know many people (of course just surrounding me, U is not the one) hate something just bcuz they dun understand it. This is a psychology topic I learned in uni:
If someone put a unopenable, sealed, opaque large box in your house with no good reason and order you to not open it. At the beginning u should be curious about it. But later on experts found out two very funny results:

1) treat it as normal, the box is here just like it should be.

2) lock/hidden/throw/remove/return the box. Someone even called the police. Why? bcuz people afraid the stuffs inside the box. if the box is similar to a human size 50% people think a dead body inside. if the size is similar to the 21" TV 30% of people afraid that it is dirty $$ ....so on.

Now, the reaction of the java community is similar. Why add closure????? it is damager??? why ??? I dunno why.....

Well, dun start a flame war that Java shouldn't add XXXXX. This is not related to this topic.

What I want to say is the main purpose of comparing Java and Rudy is to show that java is bad or show that xxx features from ruby should be added to java bucz java is weak in this use case.

I PERDICT:

I THINK ALL RUBY FEATURES WILL BE ADDED TO JAVA SOON OR LATER.

u: omg/suck/shit, I want clear and simple java only, dun change java into ruby.

Hey, hold one. U misunderstand me. I means JRuby == power of ruby + power of java

What !?!? It doesn't look like Java.

Then look for Groovy == ruby power + Java syntax

What ?!?!?! You just want part of the ruby in java.
Please move to other post that talk about adding new features like closure .... etc.

It is meaningless to compare the color and size of an apple and orange.
Go and make something like Rudy vs Perl --- Why Perl sucks? Or C# vs Java why people always think C# is easier? Or Java vs VB.Net why Java can’t replace VB for the past 10 ten is much educational and meaningful that Java vs Ruby – Why Strong typed language always look sucks in front of dynamic typed language.