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:cn.z.Ocr5.java

public static boolean isNotEight(BufferedImage img) {
    final int width = img.getWidth();
    final int height = img.getHeight();
    int minCount = width;
    for (int y = height / 2 - 2; y < height / 2 + 2; ++y) {
        int count = 0;
        for (int x = 0; x < width / 2 + 2; ++x) {
            if (CommonUtil.isWhite(img.getRGB(x, y), whiteThreshold) == 0) {
                count++;/*from   ww  w . ja  va  2s  .c  om*/
            }
        }
        minCount = Math.min(count, minCount);
    }
    return minCount < 2;
}

From source file:cn.z.Ocr5.java

public static boolean isNotThree(BufferedImage img) {
    final int width = img.getWidth();
    final int height = img.getHeight();
    int minCount = width;
    for (int y = height / 2 - 3; y < height / 2 + 3; ++y) {
        int count = 0;
        for (int x = 0; x < width / 2 + 1; ++x) {
            if (CommonUtil.isWhite(img.getRGB(x, y), whiteThreshold) == 0) {
                count++;//from w  w w  . jav a2s.c  o m
            }
        }
        minCount = Math.min(count, minCount);
    }
    return minCount > 0;
}

From source file:nl.b3p.imagetool.ImageTool.java

/**
 *
 *///from  w ww.  j  a v  a 2s  . c  om
public static BufferedImage changeColor(BufferedImage im, Color color, Color newColor) {
    for (int x = 0; x < im.getWidth(); x++) {
        for (int y = 0; y < im.getHeight(); y++) {
            if (im.getRGB(x, y) == color.getRGB()) {
                im.setRGB(x, y, newColor.getRGB());
            }
        }
    }
    return im;
}

From source file:com.t3.image.ImageUtil.java

public static BufferedImage rgbToGrayscale(BufferedImage image) {

    if (image == null) {
        return null;
    }/*from  ww w  .ja  v a 2 s  .  c om*/

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

From source file:org.eclipse.swt.snippets.Snippet156.java

static ImageData convertToSWT(BufferedImage bufferedImage) {
    if (bufferedImage.getColorModel() instanceof DirectColorModel) {
        DirectColorModel colorModel = (DirectColorModel) bufferedImage.getColorModel();
        PaletteData palette = new PaletteData(colorModel.getRedMask(), colorModel.getGreenMask(),
                colorModel.getBlueMask());
        ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(),
                colorModel.getPixelSize(), palette);
        for (int y = 0; y < data.height; y++) {
            for (int x = 0; x < data.width; x++) {
                int rgb = bufferedImage.getRGB(x, y);
                int pixel = palette.getPixel(new RGB((rgb >> 16) & 0xFF, (rgb >> 8) & 0xFF, rgb & 0xFF));
                data.setPixel(x, y, pixel);
                if (colorModel.hasAlpha()) {
                    data.setAlpha(x, y, (rgb >> 24) & 0xFF);
                }// ww w .ja  v a  2  s .c  om
            }
        }
        return data;
    } else if (bufferedImage.getColorModel() instanceof IndexColorModel) {
        IndexColorModel colorModel = (IndexColorModel) bufferedImage.getColorModel();
        int size = colorModel.getMapSize();
        byte[] reds = new byte[size];
        byte[] greens = new byte[size];
        byte[] blues = new byte[size];
        colorModel.getReds(reds);
        colorModel.getGreens(greens);
        colorModel.getBlues(blues);
        RGB[] rgbs = new RGB[size];
        for (int i = 0; i < rgbs.length; i++) {
            rgbs[i] = new RGB(reds[i] & 0xFF, greens[i] & 0xFF, blues[i] & 0xFF);
        }
        PaletteData palette = new PaletteData(rgbs);
        ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(),
                colorModel.getPixelSize(), palette);
        data.transparentPixel = colorModel.getTransparentPixel();
        WritableRaster raster = bufferedImage.getRaster();
        int[] pixelArray = new int[1];
        for (int y = 0; y < data.height; y++) {
            for (int x = 0; x < data.width; x++) {
                raster.getPixel(x, y, pixelArray);
                data.setPixel(x, y, pixelArray[0]);
            }
        }
        return data;
    }
    return null;
}

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

public static void clearNoise(BufferedImage image) {
    final int width = image.getWidth();
    final int height = image.getHeight();

    /*//from  ww w .  j a va  2 s . com
    for (int x = 0; x < width; x++) {
    for (int y = 0; y < height; y++) {
        image.setRGB(x, y, Color.WHITE.getRGB());
    }
    }
    */
    for (int x = image.getMinX(); x < width; x++) {
        for (int y = image.getMinY(); y < height; y++) {

            int point = image.getRGB(x, y);

            // 
            int top = (y > 0) ? image.getRGB(x, y - 1) : -1;
            // ?
            int right = (x < width - 1) ? image.getRGB(x + 1, y) : -1;
            // 
            int bottom = (y < height - 1) ? image.getRGB(x, y + 1) : -1;
            // 
            int left = (x > 0) ? image.getRGB(x - 1, y) : -1;
            // ?
            int topRight = ((x < width - 1) && (y > 0)) ? image.getRGB(x + 1, y - 1) : -1;
            // 
            int topLeft = ((x > 0) && (y > 0)) ? image.getRGB(x - 1, y - 1) : -1;
            // ?
            int bottomRight = ((x < width - 1) && (y < height - 1)) ? image.getRGB(x + 1, y + 1) : -1;
            // 
            int bottomLeft = ((x > 0) && (y < height - 1)) ? image.getRGB(x - 1, y + 1) : -1;

            int i = 0;
            i = (top != -1) ? i + 1 : i;
            i = (right != -1) ? i + 1 : i;
            i = (bottom != -1) ? i + 1 : i;
            i = (left != -1) ? i + 1 : i;

            i = (topRight != -1) ? i + 1 : i;
            i = (topLeft != -1) ? i + 1 : i;
            i = (bottomRight != -1) ? i + 1 : i;
            i = (bottomLeft != -1) ? i + 1 : i;

            // System.out.println("i:" + i + ", top:" + top + ", right:" + right + ", bottom:" + bottom + ", left:" + left + ", topRight:" + topRight + ", topLeft:" + topLeft + ", bottomRight:" + bottomRight + ", bottomLeft:" + bottomLeft);
            if (i < 5) {
                image.setRGB(x, y, Color.WHITE.getRGB());
            }

            /*
            int leftNearby = 0;
            if(left != -1) {
            int secondLeft = (x > 1) ? image.getRGB(x - 2, y) : -1;
            int threeLeft = (x > 2) ? image.getRGB(x - 3, y) : -1;
                    
            leftNearby = ((left == -1) ? 0 : 1) + ((secondLeft == -1) ? 0 : 1) + ((threeLeft == -1) ? 0 : 1);
            }
                    
            int rightNearby = 0;
            if(right != -1) {
            int secondRight = (x + 1 < width - 1) ? image.getRGB(x + 2, y) : -1;
            int threeRight = (x + 2 < width - 1) ? image.getRGB(x + 3, y) : -1;
                    
            rightNearby = ((right == -1) ? 0 : 1) + ((secondRight == -1) ? 0 : 1) + ((threeRight == -1) ? 0 : 1);
            }
                    
            int topNearby = 0;
            if(top != -1) {
            int secondTop = (y > 1) ? image.getRGB(x, y - 2) : -1;
            int threeTop = (y > 2) ? image.getRGB(x, y - 3) : -1;
                    
            topNearby = ((top == -1) ? 0 : 1) + ((secondTop == -1) ? 0 : 1) + ((threeTop == -1) ? 0 : 1);
            }
                    
            int bottomNearby = 0;
            if(bottom != -1) {
            int secondBottom = (y + 1 < height - 1) ? image.getRGB(x, y + 2) : -1;
            int threeBottom = (y + 2 < height - 1) ? image.getRGB(x, y + 3) : -1;
                    
            bottomNearby = ((bottom == -1) ? 0 : 1) + ((secondBottom == -1) ? 0 : 1) + ((threeBottom == -1) ? 0 : 0);
            }
                    
            System.out.println(leftNearby + " " + rightNearby + " " + topNearby + " " + bottomNearby);
            if (leftNearby != 2 && rightNearby != 2 && topNearby != 2 && bottomNearby != 2) {
            image.setRGB(x, y, Color.WHITE.getRGB());
            }
            */

            /*
            System.out.println(point - top);
            System.out.println(point - right);
            System.out.println(point - bottom);
            System.out.println(point - left);
            System.out.println(point - topRight);
            System.out.println(point - topLeft);
            System.out.println(point - bottomRight);
            System.out.println(point - bottomLeft);
            */

            // if (point != top && point != right && point != bottom && point != left && point != topRight && point != topLeft && point != bottomRight && point != bottomLeft) {
            //     image.setRGB(x, y, Color.WHITE.getRGB());
            // }

            /*
            Color color = new Color(image.getRGB(x, y));
            if((color.getBlue() < 120) || ((color.getRed() + color.getGreen() + color.getBlue()) < 50)) { //
            image.setRGB(x, y, Color.WHITE.getRGB());
            } else if((color.getRed() + color.getGreen() + color.getBlue()) < 400) {
            image.setRGB(x, y, Color.BLACK.getRGB());
            }
            */

            Color color = new Color(image.getRGB(x, y));
            if ((color.getRed() + color.getGreen() + color.getBlue()) < 600) {
                image.setRGB(x, y, Color.BLACK.getRGB());
            } else {
                image.setRGB(x, y, Color.WHITE.getRGB());
            }
        }
    }

}

From source file:preprocessing.Utils.java

public static BufferedImage cropper(BufferedImage img) {
    int height = img.getHeight();
    int width = img.getWidth();
    int pixel;/*from  w w w  .  ja  v  a2s  .c o  m*/
    int startX, endX, startY, endY;
    //startX = startY = 1000000;
    startX = img.getWidth();
    startY = img.getHeight();
    endX = endY = 0;
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            pixel = img.getRGB(x, y);
            //determine whether the pixel is black or red. if red and firstred == null, assign to firstred. same with black. each red or black pixel read can be assigned to lastred or lastblack as when it's done looping you'll automatically have the last black or red. Also, assign the x and y values to a variable.
            if (pixel != -1) {
                if (x < startX)
                    startX = x;
                if (x > endX)
                    endX = x;
                if (y < startY)
                    startY = y;
                if (y > endY)
                    endY = y;
            }

            //invertir colores
            /*
            if(pixel!=-1)
            img.setRGB(x,y,-1);
            else
            img.setRGB(x, y, -16777216);
            */
            //System.out.println("las +"+pixel);
        }
    }
    if (endX <= startX)
        return img;
    if (endY <= startY)
        return img;
    // create a cropped image from the original image
    BufferedImage croppedImage = img.getSubimage(startX, startY, endX - startX, endY - startY);

    return croppedImage;
}

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

public static byte[][] imgToByteArray(BufferedImage src) {
    int w = src.getWidth();
    int h = src.getHeight();
    byte[][] result = new byte[w][h];

    for (int x = 0; x < w; ++x) {
        for (int y = 0; y < h; ++y) {
            int rgb = getRed(src.getRGB(x, y));
            result[x][y] = (byte) (rgb == 255 ? 0 : 1);
        }// w ww  .  j  a  va  2 s.co  m
    }

    return result;
}

From source file:preprocessing.Utils.java

public static int validarPlanillon(BufferedImage test) {
    if (test.getWidth() < 1200 || test.getHeight() < 500)
        return -1;
    int inicioY = 0;
    int inicioX = 0;
    int finY = test.getHeight() - 1;
    int finX = test.getWidth() - 1;
    for (int i = 5; i < test.getHeight() / 4; i++) {
        int[] linea = new int[test.getWidth()];
        for (int j = 0; j < test.getWidth(); j++)
            linea[j] = test.getRGB(j, i);
        if (esLineaNegra(linea)) {
            inicioY = i + 1;//from  w w w.j av  a 2 s .  co  m
            break;
        }
    }
    for (int i = 5; i < test.getWidth() / 4; i++) {
        int[] linea = new int[test.getHeight()];
        for (int j = 0; j < test.getHeight(); j++)
            linea[j] = test.getRGB(i, j);
        if (esLineaNegra(linea)) {
            inicioX = i + 1;
            break;
        }
    }
    for (int i = test.getWidth() - 7; i > test.getWidth() / 2; i--) {
        int[] linea = new int[test.getHeight()];
        for (int j = 0; j < test.getHeight(); j++)
            linea[j] = test.getRGB(i, j);
        if (esLineaNegra(linea)) {
            finX = i - 1;
            break;
        }
    }
    for (int i = test.getHeight() - 5; i > test.getHeight() / 2; i--) {
        int[] linea = new int[test.getWidth()];
        for (int j = 0; j < test.getWidth(); j++)
            linea[j] = test.getRGB(j, i);
        if (esLineaNegra(linea)) {
            finY = i - 1;
            break;
        }
    }
    if (finY < inicioY || finX < inicioX)
        return -1;
    if ((finY - inicioY) < 400 || (finX - inicioX) < 1200)
        return -1;
    return 1;
}

From source file:de.ppi.selenium.util.ScreenshotUtils.java

/**
 * Calculate how similar the 2 immages are.
 *
 * @param var picture1/*from  w  w  w  . j av a  2  s  .  c o m*/
 * @param cont picture2
 * @return a value between 0 and 1.
 */
private static double similarity(BufferedImage var, BufferedImage cont) {

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

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

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