Java Utililty Methods BufferedImage Operation

List of utility methods to do BufferedImage Operation

Description

The list of methods to do BufferedImage Operation are organized into topic(s).

Method

BufferedImageintensityArrayToBufferedImage(byte[] array, int w, int h)
Converts the given intensity array into a grayscale image of w x h dimensions.
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
int[] nBits = { 8 };
ColorModel cm = new ComponentColorModel(cs, nBits, false, true, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
SampleModel sm = cm.createCompatibleSampleModel(w, h);
DataBufferByte db = new DataBufferByte(array, w * h);
WritableRaster raster = Raster.createWritableRaster(sm, db, null);
BufferedImage bm = new BufferedImage(cm, raster, false, null);
return bm;
...
voidinterp(BufferedImage img1, BufferedImage img2, int weight1, int weight2)
interp
float alpha = weight1;
alpha /= (weight1 + weight2);
Graphics2D g2 = img1.createGraphics();
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
g2.drawImage(img2, 0, 0, null);
g2.dispose();
voidknit(BufferedImage[] buffImages)
knit
int chunkWidth, chunkHeight;
int type;
type = buffImages[0].getType();
chunkWidth = buffImages[0].getWidth();
chunkHeight = buffImages[0].getHeight();
BufferedImage finalImg = new BufferedImage(chunkWidth * cols, chunkHeight * rows, type);
int num = 0;
for (int i = 0; i < rows; i++) {
...
BufferedImagelayer(BufferedImage source, BufferedImage destination, int x, int y)
layer
Graphics2D graphics = destination.createGraphics();
graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));
graphics.drawImage(source, null, null);
graphics.dispose();
return destination;
BufferedImagemakeBinary(BufferedImage image, int threshold)
Returns a binary image of a given image and a given threshold.
BufferedImage copiedImage = copyBufferedImage(image);
for (int x = 0; x < image.getWidth(); x++) {
    for (int y = 0; y < image.getHeight(); y++) {
        if (grayscaleFromRgb(image.getRGB(y, x)) <= threshold) {
            copiedImage.setRGB(y, x, rgbFromGrayscale(0));
        } else {
            copiedImage.setRGB(y, x, rgbFromGrayscale(255));
return copiedImage;
CursormakeCursor(BufferedImage img, int hotx, int hoty, String name)
make Cursor
try {
    Toolkit tk = Toolkit.getDefaultToolkit();
    Dimension d = tk.getBestCursorSize(img.getWidth(), img.getHeight());
    if (d.width <= 0 || d.height <= 0) {
        System.err.println("Notice: System does not support custom cursors. Returning generic cursor.");
        return Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR);
    if (d.width < img.getWidth() || d.height < img.getHeight()) {
...
voidmakeGhost(BufferedImage image)
Makes a ghost image suitable for dragging.
for (int i = 0; i < image.getWidth(); i++) {
    for (int j = 0; j < image.getHeight(); j++) {
        int pixel = image.getRGB(i, j);
        if (pixel == -1) {
            image.setRGB(i, j, 0);
        } else {
            int alpha = (pixel & 0xff000000);
            int red = (pixel & 0x00ff0000) >> 16;
...
BufferedImagemakeImageTranslucent(BufferedImage source, float alpha)
make Image Translucent
BufferedImage target = new BufferedImage(source.getWidth(), source.getHeight(),
        java.awt.Transparency.TRANSLUCENT);
Graphics2D g = target.createGraphics();
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
g.drawImage(source, null, 0, 0);
g.dispose();
return target;
BufferedImagemakeItBlack(BufferedImage image)
Changes the foreground colors to black
for (int w = 0; w < image.getWidth(); w++) {
    for (int h = 0; h < image.getHeight(); h++) {
        if (image.getRGB(w, h) != Color.WHITE.getRGB() && image.getRGB(w, h) != 0) {
            image.setRGB(w, h, Color.BLACK.getRGB());
return image;
...
ImagemakeMouseover(final BufferedImage src)
Make the specified source image mouseover.
BufferedImage dest = new BufferedImage(src.getColorModel(), src.copyData(null), src.isAlphaPremultiplied(),
        null);
MOUSEOVER_OP.filter(src, dest);
return dest;