scale BufferedImage Image - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage Scale

Description

scale BufferedImage Image

Demo Code


//package com.java2s;

import java.awt.image.BufferedImage;

public class Main {
    public static BufferedImage scaleImage(BufferedImage originalImage,
            int multiplier) {
        int imageWidth = originalImage.getWidth();
        int imageHeight = originalImage.getHeight();
        BufferedImage scaledImage = new BufferedImage(imageWidth
                * multiplier, imageHeight * multiplier,
                BufferedImage.TYPE_INT_ARGB);
        for (int x = 0; x < scaledImage.getWidth(); x++) {
            for (int y = 0; y < scaledImage.getHeight(); y++) {
                int scaledX = new Double(x
                        / (double) scaledImage.getWidth()
                        * originalImage.getWidth()).intValue();
                int scaledY = new Double(y
                        / (double) scaledImage.getHeight()
                        * originalImage.getHeight()).intValue();
                scaledImage.setRGB(x, y,
                        originalImage.getRGB(scaledX, scaledY));
            }// ww  w  .ja  va  2s .c om
        }

        return scaledImage;
    }
}

Related Tutorials