Example usage for java.awt.image BufferedImage getRGB

List of usage examples for java.awt.image BufferedImage getRGB

Introduction

In this page you can find the example usage for java.awt.image BufferedImage getRGB.

Prototype

public int getRGB(int x, int y) 

Source Link

Document

Returns an integer pixel in the default RGB color model (TYPE_INT_ARGB) and default sRGB colorspace.

Usage

From source file:Main.java

/**
 * Rotates given image by given degrees which should be a multiple of 90
 * @param source image to be rotated/*from  w w w .  jav a  2 s .co m*/
 * @param degrees the angle by which to rotate, should be a multiple of 90
 * @return the rotated image
 */
public static BufferedImage rotateByRightAngle(BufferedImage source, int degrees) {
    assert degrees % 90 == 0;
    degrees = degrees % 360;

    int w = source.getWidth();
    int h = source.getHeight();
    int w1, h1;
    switch (degrees) {
    case 90:
    case 270:
        w1 = h;
        h1 = w;
        break;
    default:
        w1 = w;
        h1 = h;
    }
    BufferedImage rotated = new BufferedImage(w1, h1, source.getType());

    for (int x = 0; x < w; x++) {
        for (int y = 0; y < h; y++) {
            int v = source.getRGB(x, y);
            int x1, y1;
            switch (degrees) {
            case 90:
                x1 = h - y - 1;
                y1 = x;
                break;
            case 180:
                x1 = w - x - 1;
                y1 = h - y - 1;
                break;
            case 270:
                x1 = y;
                y1 = w - x - 1;
                break;
            default:
                x1 = x;
                y1 = y;
                break;
            }

            rotated.setRGB(x1, y1, v);
        }
    }

    return rotated;
}

From source file:ImageProcessing.ImageProcessing.java

public static double[] extractGrayColor(BufferedImage source) {
    //Extracts the gray value from the pixels at the source by 
    //calculating the average of the RGB value at the given pixel.
    int imageWidth = source.getWidth();
    int imageHeight = source.getHeight();
    double[] values = new double[imageWidth * imageHeight];

    for (int i = 0; i < imageHeight; i++) {
        for (int j = 0; j < imageWidth; j++) {
            int rgbValue = source.getRGB(j, i);
            Color currentPixel = new Color(rgbValue, true);
            int value = (currentPixel.getRed() + currentPixel.getGreen() + currentPixel.getBlue()) / 3;
            values[(i * imageWidth) + j] = value;
        }//from   w  w w .  j a v a 2  s .  c  o m
    }

    return values;
}

From source file:net.cloudkit.relaxation.CaptchaTest.java

public static List<BufferedImage> splitImage(BufferedImage img) throws Exception {
    final List<BufferedImage> subImgs = new ArrayList<BufferedImage>();
    final int width = img.getWidth();
    final int height = img.getHeight();
    final List<Integer> weightList = new ArrayList<Integer>();
    for (int x = 0; x < width; ++x) {
        int count = 0;
        for (int y = 0; y < height; ++y) {
            if (isWhite(img.getRGB(x, y), whiteThreshold) == 0) {
                count++;/*from   w w w .  j a  va2s . c om*/
            }
        }
        weightList.add(count);
    }
    // System.out.println(weightList.size());
    for (int i = 0; i < weightList.size(); i++) {
        int length = 0;
        while (i < weightList.size() && weightList.get(i) > 0) {
            i++;
            length++;
        }
        if (length > 18) {
            subImgs.add(removeBlank(img.getSubimage(i - length, 0, length / 2, height), whiteThreshold, 0));
            subImgs.add(removeBlank(img.getSubimage(i - length / 2, 0, length / 2, height), whiteThreshold, 0));
        } else if (length > 2) {
            subImgs.add(removeBlank(img.getSubimage(i - length, 0, length, height), whiteThreshold, 0));
        }
    }

    return subImgs;
}

From source file:org.tinymediamanager.core.ImageCache.java

private static boolean hasTransparentPixels(BufferedImage image) {
    for (int x = 0; x < image.getWidth(); x++) {
        for (int y = 0; y < image.getHeight(); y++) {
            int pixel = image.getRGB(x, y);
            if ((pixel >> 24) == 0x00) {
                return true;
            }//www .jav a  2s .  c  o m
        }
    }
    return false;
}

From source file:cn.z.Ocr5.java

public static int getBlackCount(BufferedImage img) {
    final int width = img.getWidth();
    final int height = img.getHeight();
    int count = 0;
    for (int x = 0; x < width; ++x) {
        for (int y = 0; y < height; ++y) {
            if (CommonUtil.isWhite(img.getRGB(x, y), whiteThreshold) == 0) {
                count++;//from w  w w  .j a va 2  s.  co m
            }
        }
    }
    return count;
}

From source file:ImageUtils.java

/**
 * Creates a <code>BufferedImage</code> from an <code>Image</code>. This method can
 * function on a completely headless system. This especially includes Linux and Unix systems
 * that do not have the X11 libraries installed, which are required for the AWT subsystem to
 * operate. This method uses nearest neighbor approximation, so it's quite fast. Unfortunately,
 * the result is nowhere near as nice looking as the createHeadlessSmoothBufferedImage method.
 * /*from  w w  w  .  j  a  v  a  2 s.com*/
 * @param image  The image to convert
 * @param w The desired image width
 * @param h The desired image height
 * @return The converted image
 * @param type int
 */
public static BufferedImage createHeadlessBufferedImage(BufferedImage image, int type, int width, int height) {
    if (type == ImageUtils.IMAGE_PNG && hasAlpha(image)) {
        type = BufferedImage.TYPE_INT_ARGB;
    } else {
        type = BufferedImage.TYPE_INT_RGB;
    }

    BufferedImage bi = new BufferedImage(width, height, type);

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            bi.setRGB(x, y, image.getRGB(x * image.getWidth() / width, y * image.getHeight() / height));
        }
    }

    return bi;
}

From source file:org.finra.jtaf.ewd.utils.ScreenshotUtils.java

private static double similarity(BufferedImage var, BufferedImage cont) {

    double[] varArr = new double[var.getWidth() * var.getHeight() * 3];
    double[] contArr = new double[cont.getWidth() * cont.getHeight() * 3];

    if (varArr.length != contArr.length)
        throw new IllegalStateException("The pictures are different sizes!");

    //unroll pixels
    for (int i = 0; i < var.getHeight(); i++) {
        for (int j = 0; j < var.getWidth(); j++) {
            varArr[i * var.getWidth() + j + 0] = new Color(var.getRGB(j, i)).getRed();
            contArr[i * cont.getWidth() + j + 0] = new Color(cont.getRGB(j, i)).getRed();
            varArr[i * var.getWidth() + j + 1] = new Color(var.getRGB(j, i)).getGreen();
            contArr[i * cont.getWidth() + j + 1] = new Color(cont.getRGB(j, i)).getGreen();
            varArr[i * var.getWidth() + j + 2] = new Color(var.getRGB(j, i)).getBlue();
            contArr[i * cont.getWidth() + j + 2] = new Color(cont.getRGB(j, i)).getBlue();
        }/*from   ww w . ja va 2 s  .co  m*/
    }

    double mins = 0;
    double maxs = 0;
    for (int i = 0; i != varArr.length; i++) {
        if (varArr[i] > contArr[i]) {
            mins += contArr[i];
            maxs += varArr[i];
        } else {
            mins += varArr[i];
            maxs += contArr[i];
        }
    }
    return mins / maxs;
}

From source file:net.sf.firemox.tools.Picture.java

/**
 * Return the border color of this card. If the picture of this card is not
 * <code>null</code> the returned color corresponds to the pixel placed on
 * the topmost leftmost pixel.//from  ww w .  j av a2 s  .co  m
 * 
 * @return the border color of this card.
 */
private static Color getBorderColor(BufferedImage image) {
    Color borderColor;
    // The border color is not yet cached
    if (CardFactory.borderColor != null) {
        // manual border
        borderColor = CardFactory.borderColor;
    } else {
        // auto border
        if (image != null) {
            borderColor = new Color(image.getRGB(0, 0));
            if (borderColor.getRed() > 175 && borderColor.getGreen() > 175 && borderColor.getBlue() > 175) {
                borderColor = Color.WHITE.darker();
            } else {
                borderColor = Color.BLACK.brighter();
            }
        } else {
            borderColor = CardFactory.borderColor;
        }
    }
    return borderColor;
}

From source file:cn.z.Ocr5.java

public static boolean isNotFive(BufferedImage img) {
    final int width = img.getWidth();
    final int height = img.getHeight();
    int minCount = width;
    for (int y = 0; y < height / 3; ++y) {
        int count = 0;
        for (int x = width * 2 / 3; x < width; ++x) {
            if (CommonUtil.isWhite(img.getRGB(x, y), whiteThreshold) == 0) {
                count++;/* www.  j av  a2s .com*/
            }
        }
        minCount = Math.min(count, minCount);
    }
    return minCount > 0;
}

From source file:net.rptools.lib.image.ImageUtil.java

public static BufferedImage rgbToGrayscale(BufferedImage image) {
    if (image == null) {
        return null;
    }/*from  w  w w  .j av a  2  s .com*/
    BufferedImage returnImage = new BufferedImage(image.getWidth(), image.getHeight(),
            pickBestTransparency(image));
    for (int y = 0; y < image.getHeight(); y++) {
        for (int x = 0; x < image.getWidth(); x++) {
            int encodedPixel = image.getRGB(x, y);

            int alpha = (encodedPixel >> 24) & 0xff;
            int red = (encodedPixel >> 16) & 0xff;
            int green = (encodedPixel >> 8) & 0xff;
            int blue = (encodedPixel) & 0xff;

            int average = (int) ((red + blue + green) / 3.0);

            // y = 0.3R + 0.59G + 0.11B luminance formula
            int value = (alpha << 24) + (average << 16) + (average << 8) + average;
            returnImage.setRGB(x, y, value);
        }
    }
    return returnImage;
}