Java Utililty Methods BufferedImage Operation

List of utility methods to do BufferedImage Operation

Description

The list of methods to do BufferedImage Operation are organized into topic(s).

Method

BufferedImagedeclareNewBufferedImageAndCopy(BufferedImage img)
declare New Buffered Image And Copy
BufferedImage imgOut = declareNewBufferedImage(img);
copyBufferedImage(img, imgOut);
return imgOut;
BufferedImagedepalettize(BufferedImage img, int maxBytes)
Convert an image to a direct color map
if (img.getWidth() * img.getHeight() * 4 > maxBytes) {
    return null;
ColorModel colorModel = img.getColorModel();
if (colorModel instanceof IndexColorModel) {
    IndexColorModel indexModel = (IndexColorModel) colorModel;
    return indexModel.convertToIntDiscrete(img.getData(), false);
return null;
ColordetermineBackgroundColor(BufferedImage bim)
determine Background Color
int inset = 5;
int pixelUpLeft = bim.getRGB(inset, inset);
int pixelUpRight = bim.getRGB(bim.getWidth() - inset, inset);
int pixelDownLeft = bim.getRGB(inset, bim.getHeight() - inset);
int pixelDownRight = bim.getRGB(bim.getWidth() - inset, bim.getHeight() - inset);
HashMap<Integer, Integer> stats = new HashMap<Integer, Integer>();
stats.put(pixelUpLeft, 0);
stats.put(pixelUpRight, 0);
...
voiddoInGraphics(final BufferedImage image, final Consumer consumer)
do In Graphics
final Graphics2D graphics = image.createGraphics();
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
graphics.setColor(Color.BLACK);
graphics.fillRect(0, 0, image.getWidth(), image.getHeight());
consumer.accept(graphics);
graphics.dispose();
BufferedImageduplicate(BufferedImage image)
Create a copy of this BufferedImage.
BufferedImage copy = new BufferedImage(image.getWidth(), image.getHeight(), image.getType());
copy.getGraphics().drawImage(image, 0, 0, null);
return copy;
BufferedImageDuplicateBufferedImage(BufferedImage bi)
Duplicate Buffered Image
ColorModel cm = bi.getColorModel();
boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
WritableRaster raster = bi.copyData(null);
return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
BufferedImageduplicateImage(BufferedImage image)
duplicate Image
BufferedImage dup = new BufferedImage(image.getWidth(), image.getHeight(), image.getType());
image.copyData(dup.getWritableTile(0, 0));
dup.releaseWritableTile(0, 0);
return dup;
BufferedImagedye(BufferedImage image, Color color)
code taken from: http://stackoverflow.com/questions/21382966/colorize-a-picture-in-java
int w = image.getWidth();
int h = image.getHeight();
BufferedImage dyed = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = dyed.createGraphics();
g.drawImage(image, 0, 0, null);
g.setComposite(AlphaComposite.SrcAtop);
g.setColor(color);
g.fillRect(0, 0, w, h);
...
BufferedImageensureIntRGB(final BufferedImage img)
Ensure that an RGB image is the same kind of RGB, so that the int arrays can be treated as storing the pixels as packed RGB values.
if (img == null)
    return null;
switch (img.getType()) {
case BufferedImage.TYPE_3BYTE_BGR:
case BufferedImage.TYPE_4BYTE_ABGR:
case BufferedImage.TYPE_4BYTE_ABGR_PRE:
case BufferedImage.TYPE_INT_BGR:
    BufferedImage img2 = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB);
...
BufferedImageensureRGBAImage(BufferedImage img)
Checks if the type of img is BufferedImage.TYPE_INT_ARGB and if is not, create a new one, just like img but with transparency
if (img == null) {
    throw new NullPointerException("Image cannot be null!");
if (img.getType() != BufferedImage.TYPE_INT_ARGB) {
    BufferedImage tmp = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics g = tmp.getGraphics();
    g.drawImage(img, 0, 0, null);
    g.dispose();
...