scale BufferedImage Image Pixels Value - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage Scale

Description

scale BufferedImage Image Pixels Value

Demo Code


//package com.java2s;
import java.awt.*;

import java.awt.image.BufferedImage;

import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;

public class Main {
    public static BufferedImage scaleImagePixelsValue(
            BufferedImage bufferedImage, double scaleRatio) {
        BufferedImage image = deepCopy(bufferedImage);
        for (int iw = 0; iw < image.getWidth(); ++iw) {
            for (int ih = 0; ih < image.getHeight(); ++ih) {
                Color imagePixelColor = new Color(bufferedImage.getRGB(iw,
                        ih));// w ww.  j a  va2 s  .  com

                Color newPixelColor = new Color(
                        (int) Math.min(Math.max(scaleRatio
                                * imagePixelColor.getRed(), 0.), 255.),
                        (int) Math.min(
                                Math.max(
                                        scaleRatio
                                                * imagePixelColor
                                                        .getGreen(), 0.),
                                255.), (int) Math.min(Math.max(scaleRatio
                                * imagePixelColor.getBlue(), 0.), 255.),
                        imagePixelColor.getAlpha());
                image.setRGB(iw, ih, newPixelColor.getRGB());
            }
        }
        return image;
    }

    public static BufferedImage deepCopy(BufferedImage bufferedImage) {
        ColorModel colorModel = bufferedImage.getColorModel();
        boolean isAlphaPremultiplied = colorModel.isAlphaPremultiplied();
        WritableRaster raster = bufferedImage.copyData(null);
        return new BufferedImage(colorModel, raster, isAlphaPremultiplied,
                null);
    }
}

Related Tutorials