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

BufferedImagerepairImage(final BufferedImage bfi, final List order)
repair Image
final List<BufferedImage> list = new ArrayList<>();
final int width = bfi.getWidth();
for (int i = 0, k = width / 12; i < k; i++) {
    list.add(bfi.getSubimage(12 * i + 1, 0, 11, 58));
for (int i = 0, k = width / 12; i < k; i++) {
    list.add(bfi.getSubimage(12 * i + 1, 58, 11, 58));
final List<BufferedImage> orderList = new ArrayList<>();
for (final int i : order) {
    orderList.add(list.get(i));
final BufferedImage oriBfi = getOriPicture(orderList);
return oriBfi;
BufferedImageresampleImage(BufferedImage image, int height)
resample Image
double mult = height / ((double) image.getHeight());
Image unbufScaledImage = image.getScaledInstance((int) (mult * image.getWidth()), height,
        Image.SCALE_DEFAULT);
BufferedImage scaledImage = new BufferedImage(unbufScaledImage.getWidth(null),
        unbufScaledImage.getHeight(null), BufferedImage.TYPE_BYTE_GRAY);
Graphics g = scaledImage.createGraphics();
g.drawImage(unbufScaledImage, 0, 0, null);
g.dispose();
...
BufferedImageresampleWithAffineTransformOp(BufferedImage srcImage, double sx, double sy)
resample With Affine Transform Op
AffineTransform transform = AffineTransform.getScaleInstance(sx, sy);
AffineTransformOp transformOp = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);
BufferedImage dstImage = transformOp.createCompatibleDestImage(srcImage, srcImage.getColorModel()); 
dstImage = transformOp.filter(srcImage, dstImage);
return dstImage;
BufferedImagerevertBlackAndWhite(BufferedImage img)
revert Black And White
int width = img.getWidth();
int height = img.getHeight();
for (int x = 0; x < width; ++x) {
    for (int y = 0; y < height; ++y) {
        if (isWhite(img.getRGB(x, y)) == 1) {
            img.setRGB(x, y, Color.BLACK.getRGB());
        } else {
            img.setRGB(x, y, Color.WHITE.getRGB());
...
BufferedImageroate90(BufferedImage src)
roate
int width = src.getWidth();
int height = src.getHeight();
BufferedImage bRotate = new BufferedImage(height, width, src.getType());
for (int x = 0; x < width; x++)
    for (int y = 0; y < height; y++)
        bRotate.setRGB(height - 1 - y, x, src.getRGB(x, y));
return bRotate;
Colorsample9Points(BufferedImage src, int x, int y)
Samples 9 pixels at and around the given pixel coordinates
int averageRed = 0;
int averageGreen = 0;
int averageBlue = 0;
int width = src.getWidth();
int height = src.getHeight();
for (int i = x - 1; i < x + 2; i++) {
    for (int j = y - 1; j < y + 2; j++) {
        int limitedX = limitSamplingIndex(i, width - 1);
...
voidscanFill(BufferedImage sourceImage)
scan Fill
if (null == sourceImage || sourceImage.getWidth() <= 3 || sourceImage.getHeight() <= 3)
    return;
ArrayList<Integer> voids = new ArrayList<Integer>();
voids.add(0); 
voids.add(0xFF << 24); 
voids.add(0xFFFFFFFF); 
voids.add(0x00FFFFFF); 
int NODATA_TRANSPARENT = 0x00000000;
...
byte[][]scrapeColors(BufferedImage image)
Get's all of the unique colors in an image.
int i;
Raster raster = image.getRaster();
byte[][] clrs = new byte[raster.getNumBands()][256];
int[][] pixels = new int[raster.getNumBands()][];
int nextClr = 0;
for (int band = 0; band < raster.getNumBands(); band++) {
    pixels[band] = raster.getSamples(0, 0, raster.getWidth(), raster.getHeight(), band, (int[]) null);
for (int pixel = 0; pixel < pixels[0].length; pixel++) {
    byte red = 0;
    byte green = 0;
    byte blue = 0;
    for (i = 0; i < clrs[0].length; i++) {
        red = (byte) pixels[0][pixel];
        green = (byte) pixels[1][pixel];
        blue = (byte) pixels[2][pixel];
        if ((red == clrs[0][i]) && (green == clrs[1][i]) && (blue == clrs[2][i])) {
            break;
    if ((i == clrs[0].length) && (nextClr < 256)) {
        clrs[0][nextClr] = red;
        clrs[1][nextClr] = green;
        clrs[2][nextClr++] = blue;
return clrs;
voidsetArrayAlpha(BufferedImage image, int[] array)
set Array Alpha
int width = image.getWidth();
int height = image.getHeight();
image.getAlphaRaster().setPixels(0, 0, width, height, array);
voidsetArrayColor(BufferedImage image, int[][] array)
set Array Color
int width = image.getWidth();
int height = image.getHeight();
int[] pixels = new int[width * height];
int j = 0;
for (int y = 0; y < height; y++) {
    for (int x = 0; x < width; x++) {
        pixels[j] = array[x][y];
        j++;
...