Java ByteBuffer Size cutAndScaleToRoundSquare(BufferedImage src, int size, int radius)

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

Description

cut And Scale To Round Square

License

Open Source License

Declaration

public static BufferedImage cutAndScaleToRoundSquare(BufferedImage src, int size, int radius) 

Method Source Code


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

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics2D;

import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;

public class Main {
    public static BufferedImage cutAndScaleToRoundSquare(BufferedImage src, int size, int radius) {
        BufferedImage bi = cutAndScaleToSquare(src, size);
        BufferedImage result = makeRoundedCorner(bi, radius);
        return result;
    }//from   w  w w .  j a v a  2  s .  co m

    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;
    }

    /**
     * Copy from http://stackoverflow.com/questions/7603400/how-to-make-a-rounded-corner-image-in-java
     */
    public static BufferedImage makeRoundedCorner(BufferedImage image, int cornerRadius) {
        int w = image.getWidth();
        int h = image.getHeight();
        BufferedImage output = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);

        Graphics2D g2 = output.createGraphics();

        // This is what we want, but it only does hard-clipping, i.e. aliasing
        // g2.setClip(new RoundRectangle2D ...)

        // so instead fake soft-clipping by first drawing the desired clip shape
        // in fully opaque white with antialiasing enabled...
        g2.setComposite(AlphaComposite.Src);
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setColor(Color.WHITE);
        g2.fill(new RoundRectangle2D.Float(0, 0, w, h, cornerRadius, cornerRadius));

        // ... then compositing the image on top,
        // using the white shape from above as alpha source
        g2.setComposite(AlphaComposite.SrcAtop);
        g2.drawImage(image, 0, 0, null);

        g2.dispose();

        return output;
    }

    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. checkBufferSize(ByteBuffer buf, int elementSize)
  2. chooseAppropriateTileSize(BufferedImage image)
  3. collidesWithImage(BufferedImage image, int x, int y, int width, int height, boolean pixelPerfect)
  4. convolve(final BufferedImage image, float[] elements, int theWidth, int theHeight)
  5. cover(BufferedImage source, File to, Image cover, int width, int height, int sourceTop, int sourceLeft, int sourceWidth, int sourceHeight)
  6. cutAndScaleToSquare(BufferedImage src, int size)
  7. decreaseBufferCapatity(final ByteBuffer byteBuffer, final int size, final int minSize)
  8. decryptData(final ByteBuffer buffer, final int size, final int key)
  9. depthToGrayBufferedImage(int[] depth, int width, int height)