Package com.logicaldoc.core.imaging

# Class ImageUtil

java.lang.Object
com.logicaldoc.core.imaging.ImageUtil

public class ImageUtil extends Object
Utility methods for handling images

Since:
  8.5.3
Author:
  Marco Meschieri - LogicalDOC

## Method Summary

Modifier and Type
Method
Description
`static BufferedImage`
`clone(BufferedImage bi)`
Clones an image.
`static BufferedImage`
`convertToBinary(BufferedImage image)`
A simple method to convert an image to binary or B/W image.
`static BufferedImage`
`convertToGrayscale(BufferedImage image)`
A simple method to convert an image to gray scale.
`static BufferedImage`
`crop(BufferedImage originalImage, ImageZone zone)`
Crops an area from given image
`static BufferedImage`
`cropCenterSquare(BufferedImage originalImage, int squareSize)`
Crops a square at the center of the given image
`static void`
`cropToFile(String content, ImageZone zone, File file)`
Crops an area and stores it in a given file
`static int[]`
`cropVisibleContent(File src, File dst)`
Crops an image leaving the smallest square containing something visible
`static BufferedImage`
`decode(String text)`
Creates an image frfom the given Base64 encoded text
`static String`
`encode(File image)`
Encodes in Base64 the given image file
`static BufferedImage`
`generateTransparentSinglePixelPng()`
Generates a PNG with a single transparent pixel
`static byte[]`
`getBytes(BufferedImage image)`
Write an image into an array of bytes
`static BufferedImage`
`getSubImage(BufferedImage image, int x, int y, int width, int height)`
A replacement for the standard `BufferedImage.getSubimage` method.
`static BufferedImage`
`invertColors(BufferedImage image)`
Inverts image color.
`static boolean`
`isBlack(BufferedImage image, int x, int y)`
Whether the pixel is black.
`static boolean`
`isBlack(BufferedImage image, int x, int y, int luminanceCutOff)`
Whether the pixel is black.
`static void`
`paste(BufferedImage container, BufferedImage imageIn, float opaque, int x, int y)`
Prints the contents of imageIn on container the given opaque value
`static void`
`printFirstPage(File originalFile, String originalFileName, File out)`
Prints the first page of the given file into the output as .jpg image
`static BufferedImage`
`rotate(BufferedImage image, double angle)`
Rotates an image.
`static BufferedImage`
`rotate(BufferedImage image, double angle, int cx, int cy)`
Rotates image.
`static void`
`save(String content, File file)`
Saves an image into a given file in an image format
`static BufferedImage`
`scale(BufferedImage image, int targetWidth, int targetHeight)`
Convenience method that returns a scaled instance of the provided `BufferedImage`.

### Methods inherited from class java.lang.Object

`equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Method Details

### save

public static void save(String content, File file) throws IOException
Saves an image into a given file in an image format

Parameters:
  `content` - the content of the image Base64 encoded
  `file` - the output file
Throws:
  `IOException` - in case of I/O error

### cropToFile

public static void cropToFile(String content, ImageZone zone, File file) throws IOException
Crops an area and stores it in a given file

Parameters:
  `content` - the content of the source image Base64 encoded
  `zone` - the zone definition
  `file` - the output file
Throws:
  `IOException` - an I/O error

### crop

public static BufferedImage crop(BufferedImage originalImage, ImageZone zone)
Crops an area from given image

Parameters:
  `originalImage` - the original image
  `zone` - the zone definition
Returns:
  the cropped image

### cropCenterSquare

public static BufferedImage cropCenterSquare(BufferedImage originalImage, int squareSize)
Crops a square at the center of the given image

Parameters:
  `originalImage` - the original image
  `squareSize` - size of the square
Returns:
  the cropped square

### cropVisibleContent

public static int[] cropVisibleContent(File src, File dst) throws IOException
Crops an image leaving the smallest square containing something visible

Parameters:
  `src` - the source image file
  `dst` - the cropped image file (png format)
Returns:
  width and height of the cropped image
Throws:
  `IOException` - In case of elaboration error

### paste

public static void paste(BufferedImage container, BufferedImage imageIn, float opaque, int x, int y)
Prints the contents of imageIn on container the given opaque value

Parameters:
  `container` - the bigger image
  `imageIn` - the image to print in
  `opaque` - and indication of how opaque must be the print
  `x` - left coordinate
  `y` - top coordinate

### encode

public static String encode(File image) throws IOException
Encodes in Base64 the given image file

Parameters:
  `image` - the image to encode
Returns:
  the encoded image
Throws:
  `IOException` - error in the elaboration of the image

### printFirstPage

public static void printFirstPage(File originalFile, String originalFileName, File out) throws IOException
Prints the first page of the given file into the output as .jpg image

Parameters:
  `originalFile` - the original file
  `originalFileName` - the original filename
  `out` - the output file
Throws:
  `IOException` - a generic I/O error

### isBlack

public static boolean isBlack(BufferedImage image, int x, int y)
Whether the pixel is black.

Parameters:
  `image` - source image
  `x` - x coordinate
  `y` - y coordinate
Returns:
  true only if the image is black

### isBlack

public static boolean isBlack(BufferedImage image, int x, int y, int luminanceCutOff)
Whether the pixel is black.

Parameters:
  `image` - source image
  `x` - x coordinate
  `y` - y coordinate
  `luminanceCutOff` - cut off luminance
Returns:
  true only if the image is black

### rotate

public static BufferedImage rotate(BufferedImage image, double angle, int cx, int cy)
Rotates image.

Parameters:
  `image` - source image
  `angle` - expressed degrees
  `cx` - x-coordinate of pivot point
  `cy` - y-coordinate of pivot point
Returns:
  rotated image

### scale

public static BufferedImage scale(BufferedImage image, int targetWidth, int targetHeight)
Convenience method that returns a scaled instance of the provided `BufferedImage`.

Parameters:
  `image` - the original image to be scaled
  `targetWidth` - the desired width of the scaled instance, in pixels
  `targetHeight` - the desired height of the scaled instance, in pixels
Returns:
  a scaled version of the original `BufferedImage`

### getSubImage

public static BufferedImage getSubImage(BufferedImage image, int x, int y, int width, int height)
A replacement for the standard `BufferedImage.getSubimage` method.

Parameters:
  `image` - the image to take the sub area from
  `x` - the X coordinate of the upper-left corner of the specified rectangular region
  `y` - the Y coordinate of the upper-left corner of the specified rectangular region
  `width` - the width of the specified rectangular region
  `height` - the height of the specified rectangular region
Returns:
  a BufferedImage that is the subimage of `image`.

### convertToBinary

public static BufferedImage convertToBinary(BufferedImage image)
A simple method to convert an image to binary or B/W image.

Parameters:
  `image` - input image
Returns:
  a monochrome image

### convertToGrayscale

public static BufferedImage convertToGrayscale(BufferedImage image)
A simple method to convert an image to gray scale.

Parameters:
  `image` - input image
Returns:
  a monochrome image

### invertColors

public static BufferedImage invertColors(BufferedImage image)
Inverts image color.

Parameters:
  `image` - input image
Returns:
  an inverted-color image

### rotate

public static BufferedImage rotate(BufferedImage image, double angle)
Rotates an image.

Parameters:
  `image` - the original image
  `angle` - the degree of rotation
Returns:
  a rotated image

### clone

public static BufferedImage clone(BufferedImage bi)
Clones an image.

Parameters:
  `bi` - the image to clone
Returns:
  the cloned image

### decode

public static BufferedImage decode(String text)
Creates an image frfom the given Base64 encoded text

Parameters:
  `text` - the text to print in the image
Returns:
  The generated image

### generateTransparentSinglePixelPng

public static BufferedImage generateTransparentSinglePixelPng()
Generates a PNG with a single transparent pixel

Returns:
  the generated image

### getBytes

public static byte[] getBytes(BufferedImage image) throws IOException
Write an image into an array of bytes

Parameters:
  `image` - the image to read
Returns:
  the image's bytes
Throws:
  `IOException` - I/O error