The following Java method takes a BufferedImage object and returns its shrinked version. The parameter n specifies the shrinking factor.

To shrink an image by an integer factor n, we will sample every nth pixel in the horizontal and vertical dimensions and ignore the others.

  public static BufferedImage enlarge(BufferedImage image, int n) {
        int w = image.getWidth() / n;
        int h = image.getHeight() / n;
        BufferedImage shrunkImage =
                new BufferedImage(w, h, image.getType());
        for (int y=0; y < h; ++y)
            for (int x=0; x < w; ++x)
                shrunkImage.setRGB(x, y, image.getRGB(x*n, y*n));
        return shrunkImage;

Parent Category: Java SE Tips