Top Description Inners Fields Constructors Methods
com.openhtmltopdf.util

public Class ImageUtil

extends Object
Class Inheritance
Imports
java.awt.*, java.awt.image.BufferedImage, java.util.Base64, .HashMap, .Map, java.util.logging.Level, java.util.regex.Pattern

Static utility methods for working with images. Meant to suggest "best practices" for the most straightforward cases of working with images.
Author
pwright

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv abstract static class
pack-priv static class
ImageUtil.AreaAverageScaler

AWT-style one-step scaling, using area averaging

pack-priv static class
ImageUtil.FastScaler

Fast but decent scaling

pack-priv static class
ImageUtil.HighQualityScaler

Step-wise downscaling

pack-priv static class
ImageUtil.OldScaler

Old AWT-style scaling, poor quality

pack-priv static interface

Field Summary

Modifier and TypeField and Description
private static final Map<DownscaleQuality, ImageUtil.Scaler>
private static final Pattern

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static BufferedImage

Returns:

BufferedImage with same content.
convertToBufferedImage
(Image
image to convert; if already a BufferedImage, returned unmodified
awtImg
,
int
the type of BufferedImage to create; see java.awt.image.BufferedImage#BufferedImage(int, int, int)
type
)

Utility method to convert an AWT Image to a BufferedImage.

public static BufferedImage

Returns:

A BufferedImage compatible with the screen (best fit).
createCompatibleBufferedImage
(int
Target width for the image
width
,
int
Target height for the image
height
,
int
Value from the java.awt.image.BufferedImage class; see docs for java.awt.image.BufferedImage#BufferedImage(int, int, int). The actual type used will be the type specified in this parameter, if in headless mode, or the type most compatible with the screen, if in non-headless more.
biType
)

Helper method to instantiate new BufferedImages; if the graphics environment is actually connected to real screen devices (e.g. not in headless mode), the image will be compatible with the screen device allowing for best performance.

public static BufferedImage
createTransparentImage(int width, int height)

public static byte[]
public static byte[]

Returns:

The binary content
getEmbeddedBase64Image
(String
URI of the embedded image
imageDataUri
)

Get the binary content of an embedded base 64 image.

private static GraphicsConfiguration
public static BufferedImage

Returns:

The scaled image instance.
getScaledInstance
(ScalingOptions opt, BufferedImage
The image to scale
orgImage
)

Scales an image to the requested width and height, assuming these are both >= 1; size given in pixels.

public static BufferedImage

Returns:

The scaled image instance.
getScaledInstance
(BufferedImage
The image to scale
orgImage
,
int
The target width in pixels
targetWidth
,
int
The target height in pixels
targetHeight
)

Scales an image to the requested width and height, assuming these are both >= 1; size given in pixels.

public static BufferedImage
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

qualback to summary
private static final Map<DownscaleQuality, ImageUtil.Scaler> qual
WHITE_SPACEback to summary
private static final Pattern WHITE_SPACE

Constructor Detail

ImageUtilback to summary
public ImageUtil()

Method Detail

convertToBufferedImageback to summary
public static BufferedImage convertToBufferedImage(Image awtImg, int type)

Utility method to convert an AWT Image to a BufferedImage. Size is preserved, BufferedImage is compatible with current display device.

Parameters
awtImg:Image

image to convert; if already a BufferedImage, returned unmodified

type:int

the type of BufferedImage to create; see java.awt.image.BufferedImage#BufferedImage(int, int, int)

Returns:BufferedImage

BufferedImage with same content.

createCompatibleBufferedImageback to summary
public static BufferedImage createCompatibleBufferedImage(int width, int height, int biType)

Helper method to instantiate new BufferedImages; if the graphics environment is actually connected to real screen devices (e.g. not in headless mode), the image will be compatible with the screen device allowing for best performance. In a headless environment, simply creates a new BufferedImage. For non-headless environments, this just sets up and calls java.awt.GraphicsConfiguration#createCompatibleImage(int, int, int). The image will not have anything drawn to it, not even a white background; you must do this yourself.

Parameters
width:int

Target width for the image

height:int

Target height for the image

biType:int

Value from the java.awt.image.BufferedImage class; see docs for java.awt.image.BufferedImage#BufferedImage(int, int, int). The actual type used will be the type specified in this parameter, if in headless mode, or the type most compatible with the screen, if in non-headless more.

Returns:BufferedImage

A BufferedImage compatible with the screen (best fit).

createTransparentImageback to summary
public static BufferedImage createTransparentImage(int width, int height)
fromBase64Encodedback to summary
public static byte[] fromBase64Encoded(String b64encoded)
getEmbeddedBase64Imageback to summary
public static byte[] getEmbeddedBase64Image(String imageDataUri)

Get the binary content of an embedded base 64 image.

Parameters
imageDataUri:String

URI of the embedded image

Returns:byte[]

The binary content

getGraphicsConfigurationback to summary
private static GraphicsConfiguration getGraphicsConfiguration()
getScaledInstanceback to summary
public static BufferedImage getScaledInstance(ScalingOptions opt, BufferedImage orgImage)

Scales an image to the requested width and height, assuming these are both >= 1; size given in pixels. If either width or height is <=0, the current image width or height will be used. This method assumes that, at the moment the method is called, the width and height of the image are available; it won't wait for them. Therefore, the method should be called once the image has completely loaded and not before.
Override this method in a subclass to optimize image scaling operations; note that the legacy java.awt.Image#getScaledInstance(int, int, int) is considered to perform poorly compared to more recent developed techniques.
For a discussion of the options from a member of the Java2D team, see http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html

Parameters
orgImage:BufferedImage

The image to scale

Returns:BufferedImage

The scaled image instance.

getScaledInstanceback to summary
public static BufferedImage getScaledInstance(BufferedImage orgImage, int targetWidth, int targetHeight)

Scales an image to the requested width and height, assuming these are both >= 1; size given in pixels. If either width or height is <=0, the current image width or height will be used. This method assumes y that, at the moment the method is called, the width and height of the image are available; it won't wait for them. Therefore, the method should be called once the image has completely loaded and not before.
Override this method in a subclass to optimize image scaling operations; note that the legacy java.awt.Image#getScaledInstance(int, int, int) is considered to perform poorly compared to more recent developed techniques.
For a discussion of the options from a member of the Java2D team, see http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html

Parameters
orgImage:BufferedImage

The image to scale

targetWidth:int

The target width in pixels

targetHeight:int

The target height in pixels

Returns:BufferedImage

The scaled image instance.

makeCompatibleback to summary
public static BufferedImage makeCompatible(BufferedImage bimg)
com.openhtmltopdf.util back to summary

pack-priv abstract Class ImageUtil.AbstractFastScaler

extends Object
implements Scaler
Class Inheritance
All Implemented Interfaces
com.openhtmltopdf.util.ImageUtil.Scaler
Known Direct Subclasses
com.openhtmltopdf.util.ImageUtil.OldScaler, com.openhtmltopdf.util.ImageUtil.AreaAverageScaler

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
protected abstract int
public BufferedImage
getScaledInstance(BufferedImage
the original image to be scaled
img
,
ScalingOptions
options
opt
)

Implements com.openhtmltopdf.util.ImageUtil.Scaler.getScaledInstance.

Convenience method that returns a scaled instance of the provided BufferedImage, taken from article on java.net by Chris Campbell http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

AbstractFastScalerback to summary
pack-priv AbstractFastScaler()

Method Detail

getImageScalingMethodback to summary
protected abstract int getImageScalingMethod()
getScaledInstanceback to summary
public BufferedImage getScaledInstance(BufferedImage img, ScalingOptions opt)

Implements com.openhtmltopdf.util.ImageUtil.Scaler.getScaledInstance.

Doc from com.openhtmltopdf.util.ImageUtil.Scaler.getScaledInstance.

Convenience method that returns a scaled instance of the provided BufferedImage, taken from article on java.net by Chris Campbell http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html. Expects the image to be fully loaded (e.g. no need to wait for loading on requesting height or width.

Parameters
img:BufferedImage

the original image to be scaled

opt:ScalingOptions

options

Returns:BufferedImage

a scaled version of the original BufferedImage

com.openhtmltopdf.util back to summary

pack-priv Class ImageUtil.AreaAverageScaler

extends AbstractFastScaler
Class Inheritance

AWT-style one-step scaling, using area averaging

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
protected int
Inherited from com.openhtmltopdf.util.ImageUtil.AbstractFastScaler:
getScaledInstance

Constructor Detail

AreaAverageScalerback to summary
pack-priv AreaAverageScaler()

Method Detail

getImageScalingMethodback to summary
protected int getImageScalingMethod()

Implements abstract com.openhtmltopdf.util.ImageUtil.AbstractFastScaler.getImageScalingMethod.

com.openhtmltopdf.util back to summary

pack-priv Class ImageUtil.FastScaler

extends Object
implements Scaler
Class Inheritance
All Implemented Interfaces
com.openhtmltopdf.util.ImageUtil.Scaler

Fast but decent scaling

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public BufferedImage
getScaledInstance(BufferedImage
the original image to be scaled
img
,
ScalingOptions
options
opt
)

Implements com.openhtmltopdf.util.ImageUtil.Scaler.getScaledInstance.

Convenience method that returns a scaled instance of the provided BufferedImage, taken from article on java.net by Chris Campbell http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

FastScalerback to summary
pack-priv FastScaler()

Method Detail

getScaledInstanceback to summary
public BufferedImage getScaledInstance(BufferedImage img, ScalingOptions opt)

Implements com.openhtmltopdf.util.ImageUtil.Scaler.getScaledInstance.

Doc from com.openhtmltopdf.util.ImageUtil.Scaler.getScaledInstance.

Convenience method that returns a scaled instance of the provided BufferedImage, taken from article on java.net by Chris Campbell http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html. Expects the image to be fully loaded (e.g. no need to wait for loading on requesting height or width.

Parameters
img:BufferedImage

the original image to be scaled

opt:ScalingOptions

options

Returns:BufferedImage

a scaled version of the original BufferedImage

com.openhtmltopdf.util back to summary

pack-priv Class ImageUtil.HighQualityScaler

extends Object
implements Scaler
Class Inheritance
All Implemented Interfaces
com.openhtmltopdf.util.ImageUtil.Scaler

Step-wise downscaling

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public BufferedImage
getScaledInstance(BufferedImage
the original image to be scaled
img
,
ScalingOptions
options
opt
)

Implements com.openhtmltopdf.util.ImageUtil.Scaler.getScaledInstance.

Convenience method that returns a scaled instance of the provided BufferedImage, taken from article on java.net by Chris Campbell http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

HighQualityScalerback to summary
pack-priv HighQualityScaler()

Method Detail

getScaledInstanceback to summary
public BufferedImage getScaledInstance(BufferedImage img, ScalingOptions opt)

Implements com.openhtmltopdf.util.ImageUtil.Scaler.getScaledInstance.

Doc from com.openhtmltopdf.util.ImageUtil.Scaler.getScaledInstance.

Convenience method that returns a scaled instance of the provided BufferedImage, taken from article on java.net by Chris Campbell http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html. Expects the image to be fully loaded (e.g. no need to wait for loading on requesting height or width.

Parameters
img:BufferedImage

the original image to be scaled

opt:ScalingOptions

options

Returns:BufferedImage

a scaled version of the original BufferedImage

com.openhtmltopdf.util back to summary

pack-priv Class ImageUtil.OldScaler

extends AbstractFastScaler
Class Inheritance

Old AWT-style scaling, poor quality

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
protected int
Inherited from com.openhtmltopdf.util.ImageUtil.AbstractFastScaler:
getScaledInstance

Constructor Detail

OldScalerback to summary
pack-priv OldScaler()

Method Detail

getImageScalingMethodback to summary
protected int getImageScalingMethod()

Implements abstract com.openhtmltopdf.util.ImageUtil.AbstractFastScaler.getImageScalingMethod.

com.openhtmltopdf.util back to summary

pack-priv Interface ImageUtil.Scaler

Known Direct Implementers
com.openhtmltopdf.util.ImageUtil.AbstractFastScaler, com.openhtmltopdf.util.ImageUtil.FastScaler, com.openhtmltopdf.util.ImageUtil.HighQualityScaler

Method Summary

Modifier and TypeMethod and Description
public BufferedImage

Returns:

a scaled version of the original BufferedImage
getScaledInstance
(BufferedImage
the original image to be scaled
img
,
ScalingOptions
options
opt
)

Convenience method that returns a scaled instance of the provided BufferedImage, taken from article on java.net by Chris Campbell http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html.

Method Detail

getScaledInstanceback to summary
public BufferedImage getScaledInstance(BufferedImage img, ScalingOptions opt)

Convenience method that returns a scaled instance of the provided BufferedImage, taken from article on java.net by Chris Campbell http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html. Expects the image to be fully loaded (e.g. no need to wait for loading on requesting height or width.

Parameters
img:BufferedImage

the original image to be scaled

opt:ScalingOptions

options

Returns:BufferedImage

a scaled version of the original BufferedImage