Package com.openhtmltopdf.util
Class ImageUtil
- java.lang.Object
-
- com.openhtmltopdf.util.ImageUtil
-
public class ImageUtil extends Object
Static utility methods for working with images. Meant to suggest "best practices" for the most straightforward cases of working with images.- Author:
- pwright
-
-
Constructor Summary
Constructors Constructor Description ImageUtil()
-
Method Summary
Modifier and Type Method Description static voidclearImage(BufferedImage image)Sets the background of the image to white.static voidclearImage(BufferedImage image, Color bgColor)Sets the background of the image to the specified colorstatic BufferedImageconvertToBufferedImage(Image awtImg, int type)Utility method to convert an AWT Image to a BufferedImage.static BufferedImagecreateCompatibleBufferedImage(int width, int height)Creates a BufferedImage compatible with the local graphics environment; this is a helper method for a common process and just sets up and callsGraphicsConfiguration.createCompatibleImage(int,int,int).static BufferedImagecreateCompatibleBufferedImage(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.static BufferedImagecreateTransparentImage(int width, int height)static byte[]getEmbeddedBase64Image(String imageDataUri)Get the binary content of an embedded base 64 image.static byte[]getEmbeddedDataUri(String dataUri)static BufferedImagegetScaledInstance(ScalingOptions opt, BufferedImage orgImage)Scales an image to the requested width and height, assuming these are both >= 1; size given in pixels.static BufferedImagegetScaledInstance(BufferedImage orgImage, int targetWidth, int targetHeight)Scales an image to the requested width and height, assuming these are both >= 1; size given in pixels.static booleanisDataUri(String uri)static booleanisEmbeddedBase64Image(String uri)Detect if an URI represents an embedded base 64 image.static BufferedImageloadEmbeddedBase64Image(String imageDataUri)Get the BufferedImage of an embedded base 64 image.static BufferedImagemakeCompatible(BufferedImage bimg)static ListscaleMultiple(ScalingOptions opt, BufferedImage img, List dimensions)Scales one image to multiple dimensions, using the same ScalingOptions for each.
-
-
-
Method Detail
-
clearImage
public static void clearImage(BufferedImage image, Color bgColor)
Sets the background of the image to the specified color- Parameters:
image- the imagebgColor- the color
-
clearImage
public static void clearImage(BufferedImage image)
Sets the background of the image to white.- Parameters:
image- the image
-
makeCompatible
public static BufferedImage makeCompatible(BufferedImage bimg)
-
createCompatibleBufferedImage
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 callsGraphicsConfiguration.createCompatibleImage(int,int,int). The image will not have anything drawn to it, not even a white background; you must do this yourself. TheclearImage(BufferedImage)method will do this for you if you like.- Parameters:
width- Target width for the imageheight- Target height for the imagebiType- Value from theBufferedImageclass; see docs forBufferedImage(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:
- A BufferedImage compatible with the screen (best fit).
-
createCompatibleBufferedImage
public static BufferedImage createCompatibleBufferedImage(int width, int height)
Creates a BufferedImage compatible with the local graphics environment; this is a helper method for a common process and just sets up and callsGraphicsConfiguration.createCompatibleImage(int,int,int). The image will support transparent pixels.- Parameters:
width- Target width for the imageheight- Target height for the image- Returns:
- A BufferedImage compatible with the screen (best fit) supporting transparent pixels.
-
getScaledInstance
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 legacyImage.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- The image to scale- Returns:
- The scaled image instance.
-
getScaledInstance
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 legacyImage.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- The image to scaletargetWidth- The target width in pixelstargetHeight- The target height in pixels- Returns:
- The scaled image instance.
-
scaleMultiple
public static List scaleMultiple(ScalingOptions opt, BufferedImage img, List dimensions)
Scales one image to multiple dimensions, using the same ScalingOptions for each. The method follows the same process for scaling asgetScaledInstance(ScalingOptions,java.awt.image.BufferedImage).- Parameters:
opt- Options to apply to control scaling process.img- The original image to scaledimensions- List of dimensions to scale to; one output image will be produced for each dimension. Will not check for duplicate dimensions.- Returns:
- List of buffered images in the given dimensions.
-
convertToBufferedImage
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 to convert; if already a BufferedImage, returned unmodifiedtype- the type of BufferedImage to create; seeBufferedImage(int,int,int)- Returns:
- BufferedImage with same content.
-
createTransparentImage
public static BufferedImage createTransparentImage(int width, int height)
-
isEmbeddedBase64Image
public static boolean isEmbeddedBase64Image(String uri)
Detect if an URI represents an embedded base 64 image.- Parameters:
uri- URI of the image- Returns:
- A boolean
-
isDataUri
public static boolean isDataUri(String uri)
-
getEmbeddedBase64Image
public static byte[] getEmbeddedBase64Image(String imageDataUri)
Get the binary content of an embedded base 64 image.- Parameters:
imageDataUri- URI of the embedded image- Returns:
- The binary content
-
getEmbeddedDataUri
public static byte[] getEmbeddedDataUri(String dataUri)
-
loadEmbeddedBase64Image
public static BufferedImage loadEmbeddedBase64Image(String imageDataUri)
Get the BufferedImage of an embedded base 64 image.- Parameters:
imageDataUri- URI of the embedded image- Returns:
- The BufferedImage
-
-