Holger's Java API

## com.antelmann.image
Class ImageUtils

    java.lang.Object
      com.antelmann.image.ImageUtils

    public class ImageUtils

extends Object

implements several useful image-related functions

Author:
  Holger Antelmann

Nested Class Summary
`static class` `ImageUtils.Axis`

Method Summary
`static String` `asHexString(Color c)`
`static BufferedImage` `captureComponent(Component c)`
`static BufferedImage` `centerImageOnImage(Image foreground, Image background)` the returned Image is as large as needed to fully show both, foreground and background
`static BufferedImage` `centerTextOnImage(Image background, String text, Font font, Paint paint, boolean antiAlias)` the image is exactly as big as the background; if the text is larger, it is cut off
`static BufferedImage` `convertCMYK2RGB(Image img)` very useful when writing jpeg images
`static void` `copyToClipboard(Image image)`
`static Image` `createGreyscale(Image image)`
`static ImageIcon` `createIcon(String chars)` uses the default label font and a standard icon dimension of 16x16; useful for a single character
`static ImageIcon` `createIcon(String chars, Font font, Dimension dim)`
`static BufferedImage` `createImage(Component c)`
`static BufferedImage` `createImage(Component c, int width, int height)`
`static Image` `createImage(Icon icon)`
`static ImageIcon` `createImageIcon(Icon icon)`
`static Image` `createNegative(Image image)`
`static byte[]` `createThumbnail(Image image, int maxSize, float quality)` convenience method to create thumbnail
`static Image` `createTransparentImage(Image image, Color transparentColor)`
`static Image` `crop(Image image, RectangularShape rect)`
`static BufferedImage` `deserialize(byte[] imageData)`
`static Iterator<BufferedImage>` `extractFrames(InputStream animatedGifStream)`
`static BufferedImage` `extractImage(URL url)` makes a thorough attempt to extract an image from the given location.
`static Image` `filterImage(Image image, ImageFilter filter)`
`static AffineTransform` `findUpperLeftTranslation(AffineTransform at, Image image)` checks the four corners of the image and returns an AffineTransform that makes sure that the upper-left corner of the image is painted at the coordintates (0/0).
`static BufferedImage` `flip(Image image, ImageUtils.Axis axis)`
`static BufferedImage` `generateBarcodeImage(String code, String type, boolean showLabel)`
`static BufferedImage` `generateBarcodeImage(String code, String type, int dpi, boolean showLabel)` if type is null, `Code128` is assumed.
`static BufferedImage` `generateTextImage(String text)` generates an image based on the given text.
`static BufferedImage` `generateTextImage(String text, Font font, Paint textColor, Paint backgroundColor, boolean antiAlias)` generates an image based on the given text.
`static BufferedImage` `generateTransparentImage(int width, int height)`
`static Color` `getColorByName(String color)` first, this method looks for a field name (such as 'green') to derive the color, then it calls `Color.decode(String)`.
`static String` `getImageFormat(InputStream stream)`
`static Dimension` `getImageSize(InputStream stream)`
`static RGBImageFilter` `getNegativeFilter()`
`static RGBImageFilter` `getTransparentColorFilter(Color color)` makes the given Color transparent; already existing transparency is preserved
`static boolean` `hasAlpha(Image image)`
`static Image` `makeColorTransparent(Image image, Color c)`
`static Image` `makeTransparent(Image image, float scaleFactor)` returns a transparent version of the given image
`static BufferedImage` `paintImage(Paint paint, Dimension d)`
`static Image` `pasteFromClipboard()` returns null if the clipboard content cannot be read into an image
`static BufferedImage` `renderImage(Image image)` allows to convert an Image to a BufferedImage
`static BufferedImage` `rewriteImage(Image image, int bufferedImageType)`
`static Image` `rotate(Image image, double degrees)`
`static Image` `scale(Image image, double factor)` scales the image based on one given factor while preserving its ratio
`static Image` `scaleToFit(Image image, int size)` scales the image so that it fits into a square of the given size; if the image is smaller than the given boundary, it's returned with a scale of 1
`static Image` `scaleToFit(Image image, int size, int renderHints)`
`static byte[]` `serialize(Image image, String format)`
`static Image` `transformImage(Image image, AffineTransform xform)`
`static Image` `transformImage(Image image, BufferedImageOp bio)`
`static void` `writeJpeg(Image image, File file, float quality)`
`static void` `writeJpeg(Image image, OutputStream out, float quality)`

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Method Detail

### getColorByName

    public static Color getColorByName(String color)

  first, this method looks for a field name (such as 'green') to derive the color, then it calls `Color.decode(String)`. If both fails, null is returned.

### asHexString

    public static String asHexString(Color c)

  See Also:
    `Color.decode(String)`

### deserialize

    public static BufferedImage deserialize(byte[] imageData)
                                     throws IOException

  Throws:
    `IOException`

### serialize

    public static byte[] serialize(Image image,
                                   String format)
                            throws IOException

  Throws:
    `IOException`

### createThumbnail

    public static byte[] createThumbnail(Image image,
                                         int maxSize,
                                         float quality)
                                  throws IOException

  convenience method to create thumbnail

  Throws:
    `IOException`

### writeJpeg

    public static void writeJpeg(Image image,
                                 File file,
                                 float quality)
                          throws IOException

  Parameters:
    `quality` - a number between 0 and 1 where 1 is the highest quality
  Throws:
    `IOException`

### writeJpeg

    public static void writeJpeg(Image image,
                                 OutputStream out,
                                 float quality)
                          throws IOException

  Parameters:
    `quality` - a number between 0 and 1 where 1 is the highest quality
  Throws:
    `IOException`

### convertCMYK2RGB

    public static BufferedImage convertCMYK2RGB(Image img)
                                         throws IOException

  very useful when writing jpeg images

  Throws:
    `IOException`

### generateBarcodeImage

    public static BufferedImage generateBarcodeImage(String code,
                                                     String type,
                                                     boolean showLabel)

### generateBarcodeImage

    @ThirdParty(value="net.sourceforge.barbecue.BarcodeFactory")
    public static BufferedImage generateBarcodeImage(String code,
                                                                     String type,
                                                                     int dpi,
                                                                     boolean showLabel)
                                              throws IllegalArgumentException

  if type is null, `Code128` is assumed.

  Throws:
    `IllegalArgumentException`
  See Also:
    `BarcodeFactory`

### createIcon

    public static ImageIcon createIcon(String chars)

  uses the default label font and a standard icon dimension of 16x16; useful for a single character

### createIcon

    public static ImageIcon createIcon(String chars,
                                       Font font,
                                       Dimension dim)

### captureComponent

    public static BufferedImage captureComponent(Component c)

### createImageIcon

    public static ImageIcon createImageIcon(Icon icon)

### createImage

    public static Image createImage(Icon icon)

### createImage

    public static BufferedImage createImage(Component c)

### createImage

    public static BufferedImage createImage(Component c,
                                            int width,
                                            int height)

### getImageFormat

    public static String getImageFormat(InputStream stream)
                                 throws IOException

  Throws:
    `IOException`

### getImageSize

    public static Dimension getImageSize(InputStream stream)
                                  throws IOException

  Throws:
    `IOException`

### pasteFromClipboard

    public static Image pasteFromClipboard()

  returns null if the clipboard content cannot be read into an image

### copyToClipboard

    public static void copyToClipboard(Image image)
                                throws IOException

  Throws:
    `IOException`

### generateTransparentImage

    public static BufferedImage generateTransparentImage(int width,
                                                         int height)

### generateTextImage

    public static BufferedImage generateTextImage(String text)

  generates an image based on the given text. Uses black on white w/ the default font and no antiAlias.

### generateTextImage

    public static BufferedImage generateTextImage(String text,
                                                  Font font,
                                                  Paint textColor,
                                                  Paint backgroundColor,
                                                  boolean antiAlias)

  generates an image based on the given text. Particularly useful for web apps.

  Parameters:
    `font` - may be null (in which case the default font is used)
    `backgroundColor` - may be null, in which case the picture would be transparant

### centerTextOnImage

    public static BufferedImage centerTextOnImage(Image background,
                                                  String text,
                                                  Font font,
                                                  Paint paint,
                                                  boolean antiAlias)

  the image is exactly as big as the background; if the text is larger, it is cut off

### centerImageOnImage

    public static BufferedImage centerImageOnImage(Image foreground,
                                                   Image background)

  the returned Image is as large as needed to fully show both, foreground and background

### paintImage

    public static BufferedImage paintImage(Paint paint,
                                           Dimension d)

### scale

    public static Image scale(Image image,
                              double factor)

  scales the image based on one given factor while preserving its ratio

  Parameters:
    `factor` - a factor of 1 returns an image of the same size

### scaleToFit

    public static Image scaleToFit(Image image,
                                   int size)

  scales the image so that it fits into a square of the given size; if the image is smaller than the given boundary, it's returned with a scale of 1

### scaleToFit

    public static Image scaleToFit(Image image,
                                   int size,
                                   int renderHints)

  Parameters:
    `renderHints` - example: `Image.SCALE_DEFAULT`

### crop

    public static Image crop(Image image,
                             RectangularShape rect)

### rotate

    public static Image rotate(Image image,
                               double degrees)

### flip

    public static BufferedImage flip(Image image,
                                     ImageUtils.Axis axis)

### makeColorTransparent

    public static Image makeColorTransparent(Image image,
                                             Color c)

### makeTransparent

    public static Image makeTransparent(Image image,
                                        float scaleFactor)

  returns a transparent version of the given image

  Parameters:
    `scaleFactor` - a number between 0 and 1; 0 means complete transparaency and 1 means complete opacity.

### findUpperLeftTranslation

    public static AffineTransform findUpperLeftTranslation(AffineTransform at,
                                                           Image image)

  checks the four corners of the image and returns an AffineTransform that makes sure that the upper-left corner of the image is painted at the coordintates (0/0).

### transformImage

    public static Image transformImage(Image image,
                                       AffineTransform xform)

### transformImage

    public static Image transformImage(Image image,
                                       BufferedImageOp bio)

### rewriteImage

    public static BufferedImage rewriteImage(Image image,
                                             int bufferedImageType)

### renderImage

    public static BufferedImage renderImage(Image image)

  allows to convert an Image to a BufferedImage

### hasAlpha

    public static boolean hasAlpha(Image image)

### createGreyscale

    public static Image createGreyscale(Image image)

### filterImage

    public static Image filterImage(Image image,
                                    ImageFilter filter)

### createNegative

    public static Image createNegative(Image image)

### createTransparentImage

    public static Image createTransparentImage(Image image,
                                               Color transparentColor)

### getTransparentColorFilter

    public static RGBImageFilter getTransparentColorFilter(Color color)

  makes the given Color transparent; already existing transparency is preserved

### getNegativeFilter

    public static RGBImageFilter getNegativeFilter()

### extractFrames

    public static Iterator<BufferedImage> extractFrames(InputStream animatedGifStream)
                                                 throws IOException

  Throws:
    `IOException`

### extractImage

    @Todo(value="may not fully load images that are embedded into the html due to the stupid HTMLEditorKit")
    public static BufferedImage extractImage(URL url)

  makes a thorough attempt to extract an image from the given location. First, it tries to directly read an image from the url; if that fails, a `JEditorPane` is employed to create a component for it.

  Returns:
    an image representing the given content or null if no known useful representation exists

(c) Holger Antelmann since 2001- all rights reserved (contact: info@antelmann.com)
see www.antelmann.com/developer for further details and available downloads