Java ByteBuffer Size cutAndScaleToSquare(BufferedImage src, int size)

Here you can find the source of cutAndScaleToSquare(BufferedImage src, int size)

Description

cut And Scale To Square

License

Open Source License

Declaration

public static BufferedImage cutAndScaleToSquare(BufferedImage src, int size) 

Method Source Code

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

import java.awt.geom.AffineTransform;

import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;

public class Main {
    public static BufferedImage cutAndScaleToSquare(BufferedImage src, int size) {
        BufferedImage square = cutToSquare(src);
        int width = square.getWidth();
        double scale = size * 1.0 / width;
        BufferedImage result = scale(square, scale);
        return result;
    }/*from w  w  w .  j  a va 2s. c om*/

    public static BufferedImage cutToSquare(BufferedImage src) {
        int width = src.getWidth();
        int height = src.getHeight();
        if (width == height)
            return src;
        BufferedImage result = null;
        if (width > height) {
            int cut = (width - height) / 2;
            result = src.getSubimage(cut, 0, height, height);
        } else {
            int cut = (height - width) / 2;
            result = src.getSubimage(0, cut, width, width);
        }
        return result;
    }

    /**
     * Reference to http://stackoverflow.com/questions/4216123/how-to-scale-a-bufferedimage
     */
    public static BufferedImage scale(BufferedImage src, double scale) {
        int width = src.getWidth();
        int height = src.getHeight();
        AffineTransform at = new AffineTransform();
        at.setToScale(scale, scale);
        AffineTransformOp ato = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
        BufferedImage dest = new BufferedImage((int) Math.round(width * scale), (int) Math.round(height * scale),
                BufferedImage.TYPE_INT_ARGB);
        dest = ato.filter(src, dest);
        return dest;
    }
}

Related

  1. chooseAppropriateTileSize(BufferedImage image)
  2. collidesWithImage(BufferedImage image, int x, int y, int width, int height, boolean pixelPerfect)
  3. convolve(final BufferedImage image, float[] elements, int theWidth, int theHeight)
  4. cover(BufferedImage source, File to, Image cover, int width, int height, int sourceTop, int sourceLeft, int sourceWidth, int sourceHeight)
  5. cutAndScaleToRoundSquare(BufferedImage src, int size, int radius)
  6. decreaseBufferCapatity(final ByteBuffer byteBuffer, final int size, final int minSize)
  7. decryptData(final ByteBuffer buffer, final int size, final int key)
  8. depthToGrayBufferedImage(int[] depth, int width, int height)
  9. doThumb(BufferedImage from, int width, int height)