Example: PhotoScalingImageTransformer
PhotoScalingImageTransformer.java
This is an ImageContentTransformer which uses attributes short-edge-length and long-edge-length to scale an image. If the aspect ratio of all of your images is consistent (i.e. 4:3), the scaling factor should also be consistent.
This is useful where you want to, say, create thumbnails of all your photos. For example, when uploading a photo and setting the long-edge-width to 1024, both 1600x1200 images and 1200x1600 images will have a scaling factor of .64. If the existing width attribute were used instead, these images would have scaling factors of .64 and .85, respectively – probably not what you expected.
The example bean and corresponding meta data are also included in the javadoc for the class.
public class MyBean {
private int id = -1;
private byte[] image = null;
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public byte[] getImage() { return image; }
public void setImage(byte[] image) { this.image = image; }
public void setImageThumbnail(byte[] imageThumbnail) { } public byte[] getImageThumbnail() { return image; }
}
public class MyBeanMetaData extends MetaData {
private static final PhotoScalingImageTransformer IMG_TRANSFORMER = new PhotoScalingImageTransformer();
public void activateMetaData()
{
addConstraint(new ConstrainedProperty("image")
.notNull(true)
.file(true)
.mimeType(MimeType.IMAGE_JPEG));
addConstraint(new ConstrainedProperty("imageThumbnail")
.notNull(true)
.file(true)
.listed(true)
.position(1)
.editable(false)
.mimeType(MimeType.IMAGE_JPEG)
.contentAttribute("short-edge-length", 74)
.transformer(IMG_TRANSFORMER)
);
}
}
Requirements
Testing
This example was written on a system using:
- Jetty 5.1.4+
- rife-1.4-jdk15.jar
- JDK 1.5
References
See also:
[top]