Java BufferedImage Compress compression(BufferedImage src, int scale)

Here you can find the source of compression(BufferedImage src, int scale)

Description

compression

License

Open Source License

Declaration

public static BufferedImage compression(BufferedImage src, int scale) 

Method Source Code


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

import java.awt.image.BufferedImage;

public class Main {

    public static BufferedImage compression(BufferedImage src, int scale) {
        return compression(src, scale, scale);
    }/*from www .  j  a  va 2s  .  c  om*/

    public static BufferedImage compression(BufferedImage src, int widthScale, int heightScale) {
        int oldWidth = src.getWidth();
        int oldHeight = src.getHeight();
        int width = oldWidth / widthScale + ((oldWidth % widthScale) > 0 ? 1 : 0);
        int height = oldHeight / heightScale + ((oldHeight % heightScale) > 0 ? 1 : 0);

        BufferedImage dest = new BufferedImage(width, height, src.getType());

        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                int rgb = src.getRGB(x * widthScale, y * heightScale);
                dest.setRGB(x, y, rgb);
            }
        }

        return dest;
    }
}

Related

  1. compress(BufferedImage image, float quality)
  2. compress(BufferedImage image, String path)
  3. compressImage(BufferedImage originalImage, int type, int width, int height)
  4. compressPhoto(Image originImg, int newWidth, int newHeight)