title: How to convert color image to pure black and white image(0-255 format)
body: <pre><code>public class BlackWhite {
  
      public static void main(String[] args) 
      {
          try 
          {
           BufferedImage original = ImageIO.read(new File("colorimage"));
           BufferedImage binarized = new BufferedImage(original.getWidth(), original.getHeight(),BufferedImage.TYPE_BYTE_BINARY);
  
           int red;
           int newPixel;
           int threshold =230;
  
              for(int i=0; i&lt;original.getWidth(); i++) 
              {
                  for(int j=0; j&lt;original.getHeight(); j++)
                  {
  
                      // Get pixels
                    red = new Color(original.getRGB(i, j)).getRed();
  
                    int alpha = new Color(original.getRGB(i, j)).getAlpha();
  
                    if(red &gt; threshold)
                      {
                          newPixel = 0;
                      }
                      else
                      {
                          newPixel = 255;
                      }
                      newPixel = colorToRGB(alpha, newPixel, newPixel, newPixel);
                      binarized.setRGB(i, j, newPixel);
  
                  }
              } 
              ImageIO.write(binarized, "jpg",new File("blackwhiteimage") );
           }
          catch (IOException e) 
          {
                  e.printStackTrace();
          }    
      }
  
       private static int colorToRGB(int alpha, int red, int green, int blue) {
              int newPixel = 0;
              newPixel += alpha;
              newPixel = newPixel &lt;&lt; 8;
              newPixel += red; newPixel = newPixel &lt;&lt; 8;
              newPixel += green; newPixel = newPixel &lt;&lt; 8;
              newPixel += blue;
  
              return newPixel;
          }
  }
  </code></pre>
  
  <p>I got a black and white output image, but as I zoomed the image I discovered some gray area. I want the output image to only contain the colors black or white.</p>
  
  <p>Please let me know if I am correct or incorrect in my current approach? And if I am, please suggest another way.</p>