Java BufferedImage Operation generate(int w, int h, BufferedImage img)

Here you can find the source of generate(int w, int h, BufferedImage img)

Description

generate

License

Open Source License

Declaration

public static Color[][] generate(int w, int h, BufferedImage img) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

public class Main {
    public static Color[][] generate(int w, int h, BufferedImage img) {
        Color[][] ret = new Color[h][w];

        int reqW = 5 * w;
        int reqH = 5 * h;

        float ratio = Math.max((reqW + 0.0f) / img.getWidth(), (reqH + 0.0f) / img.getHeight());

        img = resizeImage(img, (int) (img.getWidth() * ratio), (int) (img.getHeight() * ratio));

        int xFill = (int) Math.max(0, (img.getWidth() - reqW) / 2.0f);
        int yFill = (int) Math.max(0, (img.getHeight() - reqH) / 2.0f);

        for (int y = 0; y < h; y++) {
            for (int x = 0; x < w; x++) {
                int r = 0;
                int g = 0;
                int b = 0;

                for (int i = 0; i < 5; i++) {
                    for (int j = 0; j < 5; j++) {
                        Color c = new Color(img.getRGB(xFill + x * 5 + j, yFill + y * 5 + i));
                        r += c.getRed();
                        g += c.getGreen();
                        b += c.getBlue();
                    }/*from  ww w.ja v a 2s.c o m*/
                }

                r /= 25;
                g /= 25;
                b /= 25;

                if (r < 20 && g < 20 && b < 20) {
                    r = 20;
                    g = 20;
                    b = 20;
                }

                ret[y][x] = new Color(r, g, b);

            }
        }

        return ret;
    }

    private static BufferedImage resizeImage(BufferedImage originalImage, int imgWidth, int imgHeigth) {
        int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();

        BufferedImage resizedImage = new BufferedImage(imgWidth, imgHeigth, type);
        Graphics2D g = resizedImage.createGraphics();
        g.drawImage(originalImage, 0, 0, imgWidth, imgHeigth, null);
        g.dispose();

        return resizedImage;
    }
}

Related

  1. floatBufferToGrayBufferedImage(FloatBuffer floatBuffer, BufferedImage bi)
  2. floodFill(BufferedImage img, int startX, int startY, Color targetColor, Color replacementColor)
  3. fuzzyCompare(final BufferedImage img1, final BufferedImage img2, final double colorTolerance, final double pixelTolerance, final int fuzzyBlockDimension)
  4. fuzzyEquals(BufferedImage a, BufferedImage b, int threshold)
  5. genBlackAndWhiteImage(BufferedImage image)
  6. generateBufferedImageFromComponent(Component component)
  7. generateImageHash(BufferedImage image)
  8. generateOutline(BufferedImage source, Color color, boolean alpha)
  9. gradientMask(BufferedImage img, float start, float stop)