This program loads an image, highlights a rectangular section with a certain
color and transparency, adds some text to its bottom, then saves the
image back to a file.
The image file I/O is done with javax.imageio.ImageIO.
That's why this program needs Java 1.4 or higher to run.
To use this program do the following:
Highlight.java (regard case).
Highlight.java is and compile it:
javac Highlight.javaYou should now have a new class file
Highlight.class.Here's an example call.
$ java Highlight Usage: java Highlight INFILE OUTFILE X1 Y1 X2 Y2 RED GREEN BLUE TRANSPARENCY $ java Highlight in.jpg out.jpg 155 150 205 185 255 255 0 50
Below you can see input and output images of the above call to Highlight.
If you add some nifty optical character recognition, you can extract text
and would get the coordinates for big boy part (155, 150) / (205, 185) programmatically.
That part is left as an exercise to the reader... ;-)
Seriously, this page is more about showing how to do the highlighting.
I got the idea from Google catalogs which
does OCR on scanned catalog pages and shows search results highlighted in yellow.
Very impressive.
Adding text (e.g. a copyright notice) to the bottom of an image is a typical
processing step on a server.
This program always adds Highlight plus the current date.
Check out the various methods that Graphics offers.
In addition to rectangles and text you can draw all kinds of
geometric primitives like lines or circles.
Photograph courtesy Philip Greenspun, see http://www.photo.net/photo/pcd0222/bobs-big-boy-29.tcl for the original.
Now a short tour of the program. First it reads most parameters into variables. Error checking is absent, all errors will lead to the current stack trace printed to the command line.
The image is loaded using a one-liner: ImageIO does all the work.
Then we get a Graphics object that allows us drawing on the image.
Note that we draw on the BufferedImage object in memory —
the drawing does not change the file from which the image was loaded.
Graphics objects are always provided in some context.
You can get one for drawing on a GUI component like a Canvas,
or you can get one to print on a page,
this one is for drawing into an image object.
We first create a Color image from the three RGB color components and
the transparency (alpha) value.
In the example, yellow (red = 255, green = 255, blue = 0) and fifty percent have been chosen.
The call to fillRect draws the rectangle.
The coordinates in the call are adjusted because fillRect expects width
and height instead of a second point.
The call to drawString prints Highlight plus the current date
to the bottom of the image.
Last, another one-liner is used to write the image back to a file. Some checking is done to save as PNG if the original image was a PNG as well. Usually, PNG and JPEG get used for different types of images, so you may want to keep the original format. JPEGs are for photos, PNGs for graphics.
/*
* Highlight.java (requires Java 1.4+)
*/
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import java.util.*;
/**
* This command line program loads an image,
* highlights a rectangular section and
* saves the modified image to a new file.
*/
public class Highlight {
public static void main(String[] args) throws Exception {
// make sure we have exactly ten arguments
if (args.length != 10) {
System.err.println("Usage: java Highlight INFILE " +
"OUTFILE X1 Y1 X2 Y2 RED GREEN BLUE TRANSPARENCY");
System.exit(1);
}
// load input image
BufferedImage image = ImageIO.read(new File(args[0]));
// parse coordinates of rectangle
int x1 = Integer.parseInt(args[2]);
int y1 = Integer.parseInt(args[3]);
int x2 = Integer.parseInt(args[4]);
int y2 = Integer.parseInt(args[5]);
// parse color and transparency information
int red = Integer.parseInt(args[6]);
int green = Integer.parseInt(args[7]);
int blue = Integer.parseInt(args[8]);
int transparency = Integer.parseInt(args[9]);
// do the highlighting
Graphics graphics = image.getGraphics();
Color color = new Color(red, green, blue,
255 * transparency / 100);
graphics.setColor(color);
graphics.fillRect(x1, y1, x2 - x1 + 1, y2 - y1 + 1);
graphics.drawString("Highlight " + new Date(),
10, image.getHeight() - 10);
// save modified image
String format = "JPG";
if (args[0].toLowerCase().endsWith(".png")) {
format = "PNG";
}
ImageIO.write(image, format, new File(args[1]));
// System.exit(0)
}
}