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:testing.test.java

private static int[] getPixelData(BufferedImage img, int x, int y) {

    int argb = img.getRGB(x, y);

    int rgb[] = new int[] { (argb >> 16) & 0xff, //red
            (argb >> 8) & 0xff, //green
            (argb) & 0xff //blue
    };//  w w  w.  ja v  a 2s.co  m

    //System.out.println("rgb: " + rgb[0] + " " + rgb[1] + " " + rgb[2]);
    return rgb;
}

From source file:app.utils.ImageUtilities.java

public static Image readImageFromFile(File imgFile) throws IOException {
    String imgName = imgFile.getName();
    BufferedImage img = ImageIO.read(imgFile);
    Pixel[][] pixels = new Pixel[img.getWidth()][img.getHeight()];
    for (int x = 0; x < img.getWidth(); x++) {
        for (int y = 0; y < img.getHeight(); y++) {
            int redValue = new Color(img.getRGB(x, y)).getRed();
            int greenValue = new Color(img.getRGB(x, y)).getGreen();
            int blueValue = new Color(img.getRGB(x, y)).getBlue();
            pixels[x][y] = new Pixel(redValue, greenValue, blueValue);
        }/*from w w  w  .  j  av  a 2  s .c o m*/
    }
    return new Image(imgName, pixels, img.getRaster().getNumDataElements());
}

From source file:filtros.histograma.Histograma.java

public static void GetData(BufferedImage image) {
    //Obtem a largura e a altura da imagem original
    int width = image.getWidth();
    int height = image.getHeight();

    //Inicializa o array
    data = new int[256];

    //Aplica o filtro de escala de cinza na imagem
    BufferedImage temp = GrayScale.Apply(image);

    //Loop que percorre todos os pixels da imagem
    for (int i = 0; i < width; i++) {
        for (int j = 0; j < height; j++) {
            //Obtem o valor RGB do pixel
            int pixel = temp.getRGB(i, j);
            int r = (int) ((pixel & 0x00FF0000) >>> 16); //Recupera o valor de Red
            int g = (int) ((pixel & 0x0000FF00) >>> 8); //Recupera o valor de Green
            int b = (int) (pixel & 0x000000FF); //Recupera o valor de Blue

            //Incrementa o valor do array na posio referente ao tom RGB
            data[r] += 1;/*  ww  w  . j av  a2s.  com*/
            data[g] += 1;
            data[b] += 1;
        }
    }
}

From source file:com.jaeksoft.searchlib.util.ImageUtils.java

public static final boolean checkIfManyColors(BufferedImage image) {
    int w = image.getWidth();
    int h = image.getHeight();
    if (w == 0 && h == 0)
        return false;
    int unicolor = image.getRGB(0, 0);
    for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++) {
            int pixel = image.getRGB(x, y);
            if (pixel != unicolor)
                return true;
        }/*from w ww .j  a v  a2s.c  om*/
    }
    return false;
}

From source file:Main.java

public static byte[] getCompressedImage(byte[] rgb) {

    BufferedImage image;
    int width;//from   w  w  w .  j  av a  2  s  . c  o  m
    int height;

    try {
        image = ImageIO.read(new ByteArrayInputStream(rgb));
        width = image.getWidth();
        height = image.getHeight();

        for (int i = 0; i < height; i++) {

            for (int j = 0; j < width; j++) {

                Color c = new Color(image.getRGB(j, i));
                int red = (int) (c.getRed() * 0.299);
                int green = (int) (c.getGreen() * 0.587);
                int blue = (int) (c.getBlue() * 0.114);
                Color newColor = new Color(red + green + blue,

                        red + green + blue, red + green + blue);

                image.setRGB(j, i, newColor.getRGB());
            }
        }

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(image, "jpg", baos);
        baos.flush();
        byte[] imageInByte = baos.toByteArray();
        baos.close();

        return imageInByte;

    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:contactangle.ImageControl.java

public static BufferedImage copyByPixel(BufferedImage ii) {
    BufferedImage oi = new BufferedImage(ii.getWidth(), ii.getHeight(), BufferedImage.TYPE_INT_RGB);
    for (int x = 0; x < ii.getWidth(); x++) {
        for (int y = 0; y < ii.getHeight(); y++) {
            int pixelData = ii.getRGB(x, y);
            oi.setRGB(x, y, pixelData);/*from   w  w  w  .  j a  v a  2s .c  om*/
        }
    }
    return oi;
}

From source file:imageprocessingproject.ImageHistogram.java

private static void createHistogram(BufferedImage image) {
    histogram = new double[256];
    int height = image.getHeight();
    int width = image.getWidth();

    int sum = 0;// ww  w.  j a  v  a  2 s .c  o  m
    for (int i = 0; i < width; i++) {
        for (int j = 0; j < height; j++) {
            int grayVal = getGrayValue(image.getRGB(i, j));
            histogram[grayVal]++;
            sum++;
        }
    }

    for (int i = 0; i < 256; i++) {
        histogram[i] = histogram[i] / sum;
    }

}

From source file:se.llbit.chunky.world.Biomes.java

private static void loadColorsFromTexture(int[] dest, BufferedImage texture) {
    for (int i = 0; i < biomes.length; ++i) {
        double temp = QuickMath.clamp(biomes[i].temp, 0, 1);
        double rain = QuickMath.clamp(biomes[i].rain, 0, 1);
        rain *= temp;/*  ww  w . ja v  a2 s.  co m*/
        int color = texture.getRGB((int) ((1 - temp) * 255), (int) ((1 - rain) * 255));
        dest[i] = color;
    }
    // swamp get special treatment
    dest[SWAMP_ID] = ((dest[SWAMP_ID] & 0xFEFEFE) + 0x4E0E4E) / 2;
}

From source file:ImageProcessing.ImageProcessing.java

public static void processFlipVertical(BufferedImage image) {
    //Flips image vertically.
    for (int i = 0; i < image.getHeight() / 2; i++) {
        for (int j = 0; j < image.getWidth(); j++) {
            int upper_rgb = image.getRGB(j, i);
            int below_rgb = image.getRGB(j, image.getHeight() - (i + 1));
            image.setRGB(j, i, below_rgb);
            image.setRGB(j, image.getHeight() - (i + 1), upper_rgb);
        }//from  w ww  . j a  va2  s .c om
    }
}

From source file:ImageProcessing.ImageProcessing.java

public static void processFlipHorizontal(BufferedImage image) {
    //Flips image horizontally.
    for (int i = 0; i < image.getHeight(); i++) {
        for (int j = 0; j < image.getWidth() / 2; j++) {
            int left_rgb = image.getRGB(j, i);
            int right_rgb = image.getRGB(image.getWidth() - (j + 1), i);

            image.setRGB(j, i, right_rgb);
            image.setRGB(image.getWidth() - (j + 1), i, left_rgb);
        }/*from   w ww  .  j  a  v  a  2  s  . com*/
    }
}