Not so long ago, almost recently, I’ve started a new project called JavaPixelazier. This is the stub of sample applications with primary goal on Java Desktop, Swing, Java Graphics, static and dynamic effects, animations and maybe much more…
The first sample application that has been pushed into the repository are ImageFilter Demo application. What is the primary goal or what is the capabilities that has provided?
It is simple, however, for someone it maybe interesting. The primary goal and capabilities is image filtering with one of the filters mentioned below:
- Blur filter
- Brighten filter
- Sharpen filter
- Edge detection filter
- Negative filter
- and Rotation filter
You can open your own image and while playing with them, apply a filter(s) to the image. Any new filter, what applies, will put modified image onto the layer. Preceded layer maybe come back with very obvious opportunity of the ctrl+Z shortcut or from the Edit → Undo Layer menu item, this capability could be reached.
For now, let just slightly touch the source code of the filters. Blur filter implementation:
public void blurFilter() { float weight = 1.0f / 9.0f; float[] elements = new float[9]; for (int i = 0; i < 9; i++) elements[i] = weight; convolve(elements); }
Brighten filter:
public void brightenFilter() { float a = 1.1f; // float b = 20.0f; float b = 0; RescaleOp op = new RescaleOp(a, b, null); filter(op); }
Sharpen filter:
public void sharpenFilter() { float[] elements = {0.0f, -1.0f, 0.0f, -1.0f, 5.f, -1.0f, 0.0f, -1.0f, 0.0f}; convolve(elements); }
Edge Detection filter:
public void edgeDetectFilter() { float[] elements = {0.0f, -1.0f, 0.0f, -1.0f, 4.f, -1.0f, 0.0f, -1.0f, 0.0f}; convolve(elements); }
Negative filter:
public void negativeFilter() { short[] negative = new short[256]; for (int i = 0; i < 256; i++) negative[i] = (short) (255 - i); ShortLookupTable table = new ShortLookupTable(0, negative); LookupOp op = new LookupOp(table, null); filter(op); }
Rotation filter:
public void rotationFilter() { BufferedImage image = imgFilter.getImage(); if (image == null) return; AffineTransform transform = AffineTransform.getRotateInstance(Math.toRadians(5), image.getWidth() / 2, image.getHeight() / 2); AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BICUBIC); filter(op); }
and private supported methods:
private void filter(BufferedImageOp imageOp) { BufferedImage image = imgFilter.getImage(); if (image == null) return; image = imageOp.filter(image, null); imgFilter.addLayer(image); window.repaint(); } private void convolve(float[] elements) { Kernel kernel = new Kernel(3, 3, elements); ConvolveOp op = new ConvolveOp(kernel); filter(op); }
To run the demo you need the JRE version 6 update 10 or greater.
The source code organized like IntelliJ IDEA project structure and with jar file is available in zip archive -> 
Also, the source code is available from my GitHub repository, if you wish.
Seriously, I absolutely liked reading your blogpost. You have convinced me to subscribe to your blog, but where can I find the RSS feed?
Here