Java BufferedImage Operation matte(BufferedImage image, int top, int bottom, int left, int right, Color bg)

Here you can find the source of matte(BufferedImage image, int top, int bottom, int left, int right, Color bg)

Description

Add a matte border around the image

License

Open Source License

Parameter

Parameter Description
image The image
top top space
bottom bottom space
left left space
right right space
bg Background color

Return

The matted image

Declaration

public static BufferedImage matte(BufferedImage image, int top, int bottom, int left, int right, Color bg) 

Method Source Code


//package com.java2s;
/*/*from w w  w  .  j  a  v  a  2  s . c o  m*/
 * Copyright 1997-2016 Unidata Program Center/University Corporation for
 * Atmospheric Research, P.O. Box 3000, Boulder, CO 80307,
 * support@unidata.ucar.edu.
 * 
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at
 * your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Image;

import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;

import java.awt.image.PixelGrabber;

public class Main {
    /**
     * Add a matte border around the image
     *
     * @param image The image
     * @param  top top space
     * @param  bottom bottom space
     * @param  left left space
     * @param  right right space
     * @param bg Background color
     *
     * @return The matted image
     */
    public static BufferedImage matte(BufferedImage image, int top, int bottom, int left, int right, Color bg) {
        int imageWidth = image.getWidth(null);
        int imageHeight = image.getHeight(null);
        BufferedImage newImage = new BufferedImage(imageWidth + left + right, imageHeight + top + bottom,
                getImageType(image));
        Graphics newG = newImage.getGraphics();

        newG.setColor(bg);
        newG.fillRect(0, 0, newImage.getWidth(null), newImage.getHeight(null));
        newG.drawImage(image, left, top, null);

        return newImage;
    }

    /**
     * Get the image type
     *
     * @param image  the image to check
     *
     * @return  the type (ARGB or RGB)
     */
    private static int getImageType(Image image) {
        if (hasAlpha(image)) {
            return BufferedImage.TYPE_INT_ARGB;
        } else {
            return BufferedImage.TYPE_INT_RGB;
        }
    }

    /**
     * Check to see if the image has alpha
     *
     * @param image  the image
     *
     * @return true if has alpha
     */
    public static boolean hasAlpha(Image image) {

        // If buffered image, the color model is readily available
        if (image instanceof BufferedImage) {
            BufferedImage bimage = (BufferedImage) image;

            return bimage.getColorModel().hasAlpha();
        }

        // Use a pixel grabber to retrieve the image's color model;
        // grabbing a single pixel is usually sufficient
        PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);

        try {
            pg.grabPixels();
        } catch (InterruptedException e) {
        }

        // Get the image's color model
        ColorModel cm = pg.getColorModel();

        return cm.hasAlpha();
    }
}

Related

  1. makeOpaque(BufferedImage img, Color col)
  2. MakePoly(BufferedImage spr, int d, int angle, int baseX, int baseY)
  3. makeTintedCopy(BufferedImage bi, Color tint)
  4. markImageBorders(final BufferedImage img, final int startW, final int startH)
  5. matBytesToBufferedImage(byte[] data, int cols, int rows, int type)
  6. mirror(final BufferedImage image, final boolean horizontal)
  7. newOptimizedImageLike(GraphicsConfiguration destination, BufferedImage img)
  8. newSubimage(BufferedImage src, int x, int y, int w, int h)
  9. nNeighbors(BufferedImage image, int i, int j)