|
Check: If you are using ANT 1.6 or above,the Image task included in it may satisfy your requirement.
I had offered to contribute these Image tasks to ANT 1.7 but have not received any reply yet.
I am planning to stop developing and maintaining these tasks since I see no point in maintaining separate internal and external Image tasks.
Moreoever I can concentrate on developing PMIW. If you need some specialized imaging operations (not available in the ANT distribution) let me know.
License: Apache License ( http://www.apache.org)
Download: Source and Binary.
You will also need the supporting PMIW library and pjatools.jar from the
PJA Toolkit for GIF encoding support.
Technologies: Java2D, ImageIO, Java Advanced Imaging
Description
The structure is very similar to the standard "copy" ANT task.
The funcationality is almost same as the jakarta image taglib (currently in the jakarta-taglibs-sandbox),
because the underlying library used (PMIW: see below) is the same.
It does image operations on a file or FileSet to a new file or directory.
The operations are specified as a set of nested elements.
By default, files are only copied if the source file is newer than the destination file,
or when the destination file does not exist.
However, you can explicitly overwrite files with the overwrite attribute.
FileSets are used to select a set of files to copy. To use a <fileset>,
the todir attribute must be set.
This Image task generates and transforms images dynamically.
It exposes the imaging capability available in Java2D, Java Advanced
Imaging, ImageIO, etc., as set of nested elements.
One of the design objectives was to keep it as simple as possible.
It does not require the user to have an in-depth knowledge of the underlying
imaging APIs. Currently it includes the basic Java2D and Java
Advanced Imaging based implementations. The Java2D implementation uses
ImageIO if it is available or else defaults to AWT.
Requirements info
This Image Task can use optional software such as Java Advanced Imaging
(JAI), ImageIO, etc., other than Java2D.
- Jakarta ANT 1.4 or above.
- Java 1.2 or above.
- Currently the tags use Java2D (with ImageIO or just AWT) OR Java
Advanced Imaging API (JAI). JAI or ImageIO support is loaded if the classes
are available. Absence of ImageIO and JAI classes will force the usage of
the AWT for loading and encoding/ saving. AWT currently supports only the
reading of GIF and JPEG files and encoding of JPEG only. Writing GIF is NOT
supported by any of the above mentioned libraries. ImageIO may be added
separately for Java versions prior to 1.4.
- On a server without a graphics environment (X-Windows or MS Windows), the
"
java.awt.headless=true" option should be set or a work-around
such as loading XVFB or so must be done.
- Since Java2D loads only GIF, JPEG and BMP images, the new ImageIO is used.
Hence now it needs: - J2SDK-1.4 and above OR J2SDK-1.2 and above with ImageIO
jar OR J2SDK-1.2 and above with JAI jars.
- This Task uses
"Poor Man's Imaging Wrapper" (PMIW), a simple imaging wrapper library.
This wrapper exposes a very simple interface for imaging operation using
Java2D, JAI, etc. It is distributed under the Apache software
license and the binary is included in this taglib distribution. The source
of PMIW can be obtained from PMIW project home.
It was not made part of this library because it is used for other projects
such as jakarta image taglibs (in the sandbox), etc.
Currently supported transformations/ operations
The nested elements of the <image> task can be a set of
transformations such as: -
-
resize -- scale the image by a percentage or
to a specified width and height. Thus it supports
differential scaling (i.e., scaleX != scaleY)
-
grayscale -- convert a color image to grayscale.
-
rotate -- rotate the image by some degrees and enlarges it
to still accommodate the full image.
-
crop -- get a rectangular region specified as a percentage
or absolute pixels.
-
border -- add a border to the image in the color
specified. Color is specified as a number such as 0xff00ff (red)
or 0x0000ff(blue).
-
overlay -- overlays/ paints a new image specified by a nested
<image> tag, on the parent image at a specified point.
Transparency effect can be added by specifying a color The pixels
with the color will be considered as transparent.
-
text -- writes a string/ text on the image in the color
and font at a given point.
These elements can be combined and used in any order to get the desired effect.
The overlay is useful to add tags like "SOLD", "NEW", etc., to a product image.
Text can be used to add copyright information to images. Grayscale and Resize
can be used to generate thumbnail images. There are numerous ways to use them
in combination. Take a look at the example.
It is possible to refine the set of files that are being operated on. This can be
done with the includes, includesfile, excludes, excludesfile and defaultexcludes
attributes. With the includes or includesfile attribute you specify the files you want to
have included by using patterns. The exclude or excludesfile attribute is used to specify
the files you want to have excluded. This is also done with patterns. And
finally with the defaultexcludes attribute, you can specify whether you
want to use default exclusions or not. See the section on directory based tasks, on how the
inclusion/exclusion of files works, and how to write patterns.
This task forms an implicit FileSet and
supports all attributes of <fileset>
(dir becomes basedir) as well as the nested
<include>, <exclude> and
<patternset> elements.
You can also use nested file sets for more flexibility, and specify
multiple ones to merge together different trees of files.
Parameters
| Attribute |
Description |
Required |
| file |
The file to copy. |
Yes, unless a nested
<fileset> element is used. |
| preservelastmodified |
Give the copied files the same last modified
time as the original source files.
(Note: Ignored on Java 1.1) |
No; defaults to false. |
| tofile |
The file to copy to. |
With the file
attribute, either tofile or todir can be used.
With nested <fileset> elements, if the set of files
is greater than 1, or if only the dir attribute is
specified in the <fileset>, or if the
file attribute is also specified, then only
todir is allowed. |
| todir |
The directory to copy to. |
| overwrite |
Overwrite existing files even if the destination
files are newer. |
No; defaults to false. |
| filtering |
Indicates whether token filtering using the global
build-file filters should take place during the copy.
Note: Nested <filterset> elements will
always be used, even if this attribute is not specified, or its value is
false (no, or off). |
No; defaults to false. |
| flatten |
Ignore the directory structure of the source files,
and copy all files into the directory specified by the todir
attribute. Note that you can achieve the same effect by using a
flatten mapper. |
No; defaults to false. |
| includeEmptyDirs |
Copy any empty directories included in the FileSet(s).
|
No; defaults to true. |
| failonerror |
Log a warning message, but do not stop the build,
when the file to copy does not exist.
Only meaningful when copying a single file.
|
No; defaults to true. |
| verbose |
Log the files that are being copied. |
No; defaults to false. |
Parameters specified as nested elements
fileset
FileSets are used to select
sets of files to copy.
To use a fileset, the todir attribute must be set.
| resize |
|
| This nested/ child element of the <image> task
can be used to scale an image to a percentage (both width
and height) or can be scaled to a specific width and height.
If either width or height is present while the other is missing,
the other value is calculated keeping the aspect-ratio. Either
scale OR width OR height
OR both width and height can be
specified. |
| Element Body |
empty |
|
|
|
|
| Restrictions |
None |
| Attributes |
Name |
Required |
|
|
| |
scale |
No |
|
|
| |
The scale is specified as a percentage of the image's
dimensions, for eg., "50%" means that the width and height of the image
will be reduced to half its current values. This also means that the aspect-ratio
of the image is maintained in this case. The scale percentage cannot be
zero or less. When this attribute is specified, the width and height attributes,
if specified, are ignored. |
| |
width |
No |
|
|
| |
The width of the output image can be specified as number
of pixels wide OR as a percentage of the current width. In either case,
if the height attribute is not specified, it is calculated keeping the
aspect ratio. However if the scale attribute is specified, this attribute
is ignored. |
| |
height |
No |
|
|
| |
The height of the output image can be specified as number
of pixels high OR as a percentage of the current height. In either case,
if the width attribute is not specified, it is calculated keeping the
aspect ratio. However if the scale attribute is specified, this attribute
is ignored. |
| overlay |
|
| This nested/ child element of the <image>
task can paint another image on the parent image. The new image is specified
by the <src> attribute. Since this
image is not encoded but only used to overlay, even GIF images can be
used. |
| Element Body |
empty |
|
|
|
|
| Restrictions |
None |
| Attributes |
Name |
Required |
|
|
| |
src |
Yes |
|
|
| |
The source of the image to be overlaid. |
| |
x |
No |
|
|
| |
The horizontal distance from the top-left corner of the
current image to the point where the new image is painted (the top-left
corner of the new image). The value can be in the number of pixels OR
as a percentage of the current width. |
| |
y |
No |
|
|
| |
The vertical distance from the top-left corner of the
current image to the point where the new image is painted (the top-left
corner of the new image). The value can be in the number of pixels OR
as a percentage of the current height. |
| |
color |
No |
|
|
| |
A color can be specified to be made transparent. This
is particularly useful to remove/ignore the black/ white background of
images that does not support transparency. The color can be specified
as an integer, for e.g., "0xff0000" for red. This will be decoded to obtain
the color. There is no default color. |
| |
tolerance |
No |
|
|
| |
The tolerance value is used to specify the amount of
variation in the color components allowed of the color specified as transparent,
i.e., if the difference of the "red", "green" and "blue" components of
the specified "color" and a pixel on the overlaid image is within this
tolerance value, that pixel is considered transparent. |
| rotate |
|
| This nested/ child element of the <image>
task can rotate an image through a specified degrees. The background of
the new image after rotation may depend on the type of image such as transparent
or otherwise. The new image is made large enough to accommodate the entire
image without any cropping. |
| Element Body |
empty |
|
|
|
|
| Restrictions |
None |
| Attributes |
Name |
Required |
|
|
| |
degrees |
Yes |
|
|
| |
The degrees through which the image is rotated. A negative
value rotates the image in the anti-clockwise direction. |
| border |
|
| This nested/ child element of the <image>
task adds a border to the image. The color of the border can also be specified.
The thickness of the border at the sides and at the top and bottom of
the image is specified separately. |
| Element Body |
empty |
|
|
|
|
| Restrictions |
None |
| Attributes |
Name |
Required |
|
|
| |
width |
Yes |
|
|
| |
The width of the border on the left and right sides of
the image can be specified as number of pixels wide. If no value s specified,
no border on the left and right side is added. |
| |
height |
Yes |
|
|
| |
The height of the border on the top and bottom sides
of the image can be specified as number of pixels wide. If no value s
specified, no border on the top and bottom is added. |
| |
color |
No |
|
|
| |
The color of the border can be specified as an integer,
for e.g., "0xff0000" for red. This will be decoded to obtain the color.
The default color is black. |
| grayscale |
|
| This nested/ child element of the <image>
task converts a color image to a grayscale image. |
| Element Body |
empty |
|
|
|
|
| Restrictions |
None |
| Attributes |
Name |
Required |
|
|
| |
brightness |
No |
|
|
| |
This value between "0.00" and "9.99" specifies the brightness
factor of the grayscale image generated. |
| crop |
|
| This nested/ child element of the <image>
task can crop or cut-out a subImage of a bigger image. The sub-image area
is always specified as a rectangular area whose top-left point and the
width and height can be specified. |
| Element Body |
empty |
|
|
|
|
| Restrictions |
None |
| Attributes |
Name |
Required |
|
|
| |
x |
No |
|
|
| |
The horizontal distance from the top-left corner of the
current image to the point where the cropping begins. The value can be
in the number of pixels OR as a percentage of the current width. |
| |
y |
No |
|
|
| |
The vertical distance from the top-left corner of the
current image to the point where the cropping begins. The value can be
in the number of pixels OR as a percentage of the current height. |
| |
width |
Yes |
|
|
| |
The width of the output sub-image is specified relative
to the "x" attribute. The value can be in the number of pixels OR as a
percentage of the current width. |
| |
height |
Yes |
|
|
| |
The height of the output sub-image is specified relative
to the "y" attribute. The value can be in the number of pixels OR as a
percentage of the current height. |
| text |
|
| This nested/ child element of the <image>
task can add a line of text on the image. It can be written from a point
specified. The color and font can also be specified. |
| Element Body |
empty |
|
|
|
|
| Restrictions |
None |
| Attributes |
Name |
Required |
|
|
| |
x |
No |
|
|
| |
The horizontal distance from the top-left corner of the
current image to the point where the text begins. The value can be in
the number of pixels OR as a percentage of the current width. The default
value is "2" |
| |
y |
No |
|
|
| |
The vertical distance from the top-left corner of the
current image to the point of the where the text begins. This point is
the BASE of the font/text and hence the text is painted above this point.
If this value if zero, the text is painted outside the bounds and hence
is not visible. The value can be in the number of pixels OR as a percentage
of the current height. The default value is "98%" |
| |
text |
Yes |
|
|
| |
The String value that should be written/painted on the
image. |
| |
font |
No |
|
|
| |
The name of the font in which the text has to appear.
Make sure that this font is available. The default font is "SanSerif"
|
| |
size |
No |
|
|
| |
The size of the font in which the text has to appear.The
default size is "10" |
| |
color |
No |
|
|
| |
The color of the font in which the text has to appear.
The default color value is "0x000000" (black) |
| |
bold |
No |
|
|
| |
Specifies if the font needs to be bold. The default
is "false" |
| |
italic |
No |
|
|
| |
Specifies if the font needs to be italic. The
default is "false" |
Examples
<image todir="processed" overwrite="true" failOnError="true">
<fileset dir="images" includes="**/a*.jpg"/>
<text text="Created by Image tasks" color="0x0000ff"/>
<rotate degrees="30" />
<overlay src="images/new.gif" x="10%" y="10%"/>
<resize width="80%"/>
</image>
This above task operates on all the "jpg" files starting with "a".
All the images in this list will be operated with the following operations in the given order: -
- Paint/ write a string "Created by Image tasks" on the image at the bottom.
The color is specified as RGB values in hexadecimal "0x0000ff" (blue)
- Rotate the image by "30" degrees in the clockwise direction
- Overlay/paint the "new.gif" on the image at the specified location
- Resize the whole image to "80%" of its current size
- The final image is encoded as "JPEG" to a file witht he same file name in the directory
"processed"
Planned features
- Specifying pattern based filenames for output files.
- Loading Scalable Vector Graphics (SVG) files using Batik.
- Shape overlay and transformation operations such as "ShapeOverlay", "Circle",
"Rectangle", etc.
- Image kernel operations such as "Brightness", "Contrast", "Threshold",
"Blur", "Posterize", etc.
- Special operations such as "Tile", "Bevel border", etc.
- Implementations based on Standard Windowing Toolkit (SWT) or similar library
that might not have "headless" issues.
- Support for GIF file writing using ACME or similar libraries.
I have just started development on this. But do send me your feedback.
|