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

BufferedImagechangeContrast(BufferedImage img, float amount)
Changes the contrast of an image
if (amount == 1) {
    return img;
RescaleOp rescaleOp = new RescaleOp(amount, 0, null);
return rescaleOp.filter(img, null);
int[][]changeImageToArray(BufferedImage bufferedImage)
change Image To Array
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
int[][] rgbPixels = new int[width][height];
for (int y = 0; y < height; y++) {
    for (int x = 0; x < width; x++) {
        rgbPixels[x][y] = bufferedImage.getRGB(x, y); 
return rgbPixels;
booleancheckIfManyColors(BufferedImage image)
check If Many Colors
int w = image.getWidth();
int h = image.getHeight();
if (w == 0 && h == 0)
    return false;
int unicolor = image.getRGB(0, 0);
for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
        int pixel = image.getRGB(x, y);
...
voidcheckImageMatch(BufferedImage img1, String imgName1, BufferedImage img2, String imgName2)
Check image dimensions are matching.
if ((img1.getWidth() != img2.getWidth()) || (img1.getHeight() != img2.getHeight())) {
    throw new IllegalArgumentException(
            String.format("Size mismatch (%s.{w,h}={%d,%d} and %s.{w,h}={%d,%d})", imgName1,
                    img1.getWidth(), img1.getHeight(), imgName2, img2.getWidth(), img2.getHeight()));
voidcheckImageType(BufferedImage img, String name)
Check image type.
if (CHECK_TYPE && img.getType() != IMAGE_TYPE) {
    throw new IllegalArgumentException(
            String.format("Invalid %s.type=%s, expected %s", name, img.getType(), IMAGE_TYPE));
booleancheckRadius(final BufferedImage src, final int x, final int y, final int opaqueLimit, final int radius)
check Radius
int minX = Math.max(x - radius, 0);
int maxX = Math.min(src.getWidth(), x + radius);
int minY = Math.max(y - radius, 0);
int maxY = Math.min(src.getHeight(), y + radius);
for (int curY = minY; curY < maxY; curY++) {
    for (int curX = minX; curX < maxX; curX++) {
        if (getDistance(curX, curY, x, y, radius) <= 1
                && ((src.getRGB(curX, curY) >> 24) & 0xff) <= opaqueLimit) {
...
voidchunk(BufferedImage image)
chunk
int chunks = rows * cols;
int chunkWidth = image.getWidth() / cols; 
int chunkHeight = image.getHeight() / rows;
int count = 0;
BufferedImage imgs[] = new BufferedImage[chunks]; 
for (int x = 0; x < rows; x++) {
    for (int y = 0; y < cols; y++) {
        imgs[count] = new BufferedImage(chunkWidth, chunkHeight, image.getType());
...
BufferedImagecircularize(BufferedImage image)
Makes an image circular
BufferedImage out = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = out.createGraphics();
try {
    graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    graphics.setColor(Color.BLACK); 
    graphics.fillOval(0, 0, image.getWidth(), image.getHeight());
    graphics.setComposite(AlphaComposite.SrcIn); 
    graphics.drawImage(image, 0, 0, null);
...
voidcleanBinaryImage(BufferedImage image)
clean Binary Image
boolean needsMore = true;
while (needsMore) {
    needsMore = false;
    for (int i = 0; i < image.getWidth(); i++)
        for (int j = 0; j < image.getHeight(); j++)
            if (image.getRGB(i, j) == Color.black.getRGB() && nNeighbors(image, i, j) < 3) {
                image.setRGB(i, j, Color.white.getRGB());
                needsMore = true;
...
BufferedImagecompose(BufferedImage image, BufferedImage mask)
Add the alpha channel of mask to image, and return the composed image.
Graphics2D g2d = mask.createGraphics();
g2d.setComposite(AlphaComposite.SrcIn);
g2d.drawImage(image, 0, 0, null);
g2d.dispose();
return mask;