get White Pixels For BufferedImage - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage Color

Description

get White Pixels For BufferedImage

Demo Code


import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;

public class Main{
    public static int getWhitePixelsFor(BufferedImage img) {
        int res = 0;
        for (int i = 0; i < img.getWidth(); i++) {
            for (int j = 0; j < img.getHeight(); j++) {
                if (ImgUtil.isWhitePixel(img, i, j)) {
                    res++;/*from  w w  w.  j a va  2s .  c o m*/
                }
            }
        }

        return res;
    }
    public static boolean isWhitePixel(BufferedImage i, int x, int y) {
        if (x < 0) {
            return false;
        }
        if (y < 0) {
            return false;
        }

        if (x >= i.getWidth()) {
            return false;
        }

        if (y >= i.getHeight()) {
            return false;
        }

        return ((short) i.getRGB(x, y) != 0);
    }
    public static int[] getRGB(BufferedImage i, int x, int y) {
        int rgb = i.getRGB(x, y);

        int[] res = new int[3];
        res[0] = (rgb >> 16) & 255;
        res[1] = (rgb >> 8) & 255;
        res[2] = (rgb) & 255;
        return res;
    }
}

Related Tutorials