multi Step Bilinear Resize for BufferedImage - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage Resize

Description

multi Step Bilinear Resize for BufferedImage

Demo Code


import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Main{

    /**/*from   www  .  ja va  2 s. c o  m*/
     * 
     */
    private static int[] multiStepBilinearResize(int[] buffer,
            int deviceXSize, int deviceYSize, int currentXSize,
            int currentYSize) {
        BufferedImage bi = new BufferedImage(currentXSize, currentYSize,
                BufferedImage.TYPE_INT_RGB);
        bi.setRGB(0, 0, currentXSize, currentYSize, buffer, 0, currentXSize);

        if (deviceXSize > currentXSize) {
            //upscale - used for debug view
            bi = Scalr.resize(bi, Scalr.Method.SPEED, deviceXSize,
                    deviceYSize);
        } else {
            //downscale - used to send to device
            bi = Scalr.resize(bi, Scalr.Method.QUALITY, deviceXSize,
                    deviceYSize);
        }
        return getPixelsFromImage(bi, deviceXSize, deviceYSize);
    }
    /**
     * internal use - get buffer from image
     * @param scaledImage
     * @param deviceXSize
     * @param deviceYSize
     * @return
     */
    private static int[] getPixelsFromImage(BufferedImage scaledImage,
            int deviceXSize, int deviceYSize) {
        //painfull slow!
        //return scaledImage.getRGB(0, 0, deviceXSize, deviceYSize, null, 0, deviceXSize);
        DataBufferInt buf = (DataBufferInt) scaledImage.getRaster()
                .getDataBuffer();
        return buf.getData();
    }
}

Related Tutorials