convert To Binary Colors - Java 2D Graphics

Java examples for 2D Graphics:Color

Description

convert To Binary Colors

Demo Code


//package com.java2s;
import java.awt.Color;
import java.awt.image.BufferedImage;

public class Main {
    /**/*from  ww w . ja  va  2 s .  c  om*/
     * {@value}
     */
    public static final int INTENSITY_FOREGROUND = 1;
    /**
     * {@value}
     */
    public static final int INTENSITY_BACKGROUND = 0;

    public static int[][] convertToBinaryColors(BufferedImage image) {

        final int[][] result = new int[image.getHeight()][image.getWidth()];

        for (int i = 0; i < image.getHeight(); ++i) {
            for (int j = 0; j < image.getWidth(); ++j) {
                final int rgb = image.getRGB(j, i);
                final Color color = new Color(rgb);
                if (Color.WHITE.equals(color)) {
                    result[i][j] = INTENSITY_FOREGROUND;
                } else {
                    result[i][j] = INTENSITY_BACKGROUND;
                }
            }
        }

        return result;
    }
}

Related Tutorials