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

voidoutput(BufferedImage image, OutputStream out)
output
ImageIO.write(image, "JPEG", out);
voidoutPutImage(BufferedImage bufferedimage, String targetPath)
out Put Image
ImageIO.write(bufferedimage, "JPEG", new File(targetPath));
BufferedImagepaletteSwapARGB8(Color[] colorSet, Color clearToColorRequested, BufferedImage argbMappedBufferedImage)
This method transforms the inputed BufferedImage by the supplied Color[].
if (argbMappedBufferedImage == null) {
    return null; 
final Color BLACK_NO_ALPHA = new Color(0x00000000);
final Color WHITE_NO_ALPHA = new Color(0x00FFFFFF);
final int ALPHA = 3; 
final int RED = 0; 
final int GREEN = 1; 
...
BufferedImagepalettize(BufferedImage img, int maxEntries)
Convert an image to a palletized one.
int addedCount = 0;
Map<Integer, Integer> added = Maps.newHashMap();
for (int y = 0; y < img.getHeight(); y++) {
    for (int x = 0; x < img.getWidth(); x++) {
        if (!added.containsKey(img.getRGB(x, y))) {
            added.put(img.getRGB(x, y), addedCount++);
        if (added.size() > maxEntries) {
...
BufferedImageperformRescaleOperation(BufferedImage image, float scale, float offset)
Perform Rescale Operation.
RescaleOp brightenOp = new RescaleOp(scale, offset, null);
image = brightenOp.filter(image, null);
return image;
BufferedImageprepareModel(BufferedImage model)
Converts the supplied BufferedImage to a format that is appropriate to use as a model for an output image.
if (model.getWidth() > 2 || model.getHeight() > 2) {
    return model.getSubimage(0, 0, 2, 2); 
return model;
BufferedImagerecolor(BufferedImage img, int newColor)
Recolor a given default tile image.
int[] pixels = new int[img.getWidth() * img.getHeight()];
img.getRGB(0, 0, img.getWidth(), img.getHeight(), pixels, 0, img.getWidth());
for (int i = 0; i < pixels.length; i++) {
    int c = pixels[i];
    if (c == 0xFF000000) {
        pixels[i] = newColor;
BufferedImage result = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
result.setRGB(0, 0, img.getWidth(), img.getHeight(), pixels, 0, img.getWidth());
return result;
BufferedImagerecolor(BufferedImage src, Color sc)
recolor
float[][] colorMatrix = { { ((float) sc.getRed()) / 255f, 0, 0, 0 },
        { ((float) sc.getGreen()) / 255f, 0, 0, 0 }, { ((float) sc.getBlue()) / 255f, 0, 0, 0 },
        { 0f, 0f, 0f, 1f } };
BandCombineOp changeColors = new BandCombineOp(colorMatrix, null);
Raster sourceRaster = src.getRaster();
WritableRaster displayRaster = sourceRaster.createCompatibleWritableRaster();
changeColors.filter(sourceRaster, displayRaster);
return new BufferedImage(src.getColorModel(), displayRaster, true, null);
...
BufferedImagerecombine(BufferedImage[][] blocks)
Handles recombining a two-dimensional array of image blocks back into a single image.
int type = 0;
int totalWidth = 0;
int totalHeight = 0;
int blockWidth = 0;
int blockHeight = 0;
for (int row = 0; row < blocks.length; row++) {
    for (int col = 0; col < blocks[row].length; col++) {
        BufferedImage block = blocks[row][col];
...
BufferedImagereorientImage(BufferedImage image, boolean yAxisFlipNeeded, int cwRotationNeeded)
Flipping (Mirroring) is performed BEFORE Rotating the image.
cwRotationNeeded = cwRotationNeeded % 360;
BufferedImage orientedImage = image;
if (!yAxisFlipNeeded && cwRotationNeeded == 0) {
    return image;
} else if (yAxisFlipNeeded && cwRotationNeeded == 0) {
    orientedImage = flipImageHorizontally(image);
} else if (!yAxisFlipNeeded && cwRotationNeeded == 180) {
    orientedImage = rotateImage(image, 180);
...