title: Android: Convert Grayscale to Binary Image
body: <p>i hava done with get grayscale value, but i don't know how to use function to convert the grayscale to be binary image. Please help me, here my function code:</p>
  
  <pre><code>public Bitmap toBinary(Bitmap bmpOriginal) {
      int width, height, threshold;
      height = bmpOriginal.getHeight();
      width = bmpOriginal.getWidth();
      threshold = 127;
      final Bitmap bmpBinary = null;
  
      for(int x = 0; x &lt; width; ++x) {
          for(int y = 0; y &lt; height; ++y) {
              // get one pixel color
              int pixel = bmpOriginal.getPixel(x, y);
  
              //get grayscale value
              int gray = (int)(pixel &amp; 0xFF);
  
              //get binary value
              if(gray &lt; threshold){
                  bmpBinary.setPixel(x, y, 0);
              } else{
                  bmpBinary.setPixel(x, y, 255);
              }
  
          }
      }
      return bmpBinary;
  }
  </code></pre>
  
  <p>here my full code:</p>
  
  <pre><code>public class MainActivity extends Activity {
  
      ImageView img;
      Button btn;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          //convert imageview to bitmap
          img =(ImageView) findViewById(R.id.imageView1);
          BitmapDrawable drawable = (BitmapDrawable) img.getDrawable();
          final Bitmap imgbitmap = drawable.getBitmap();
  
  
          btn.setOnClickListener(new View.OnClickListener() {
              public void onClick(View v) {
                  //convert bitmap to grayscale 
                  Bitmap imgnew;
                  imgnew = toGrayscale(imgbitmap);    
                  //convert to binary
  imgnew = toBinary(imgnew);
  
                  //convert bitmap to imageview 
                  ImageView imgbit;
                  imgbit = (ImageView) findViewById(R.id.imageView2);
                  imgbit.setImageBitmap(imgnew);
              }
          });
  
      }
  
      public Bitmap toGrayscale(Bitmap bmpOriginal){        
          int width, height;
          height = bmpOriginal.getHeight();
          width = bmpOriginal.getWidth();    
  
          Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
          Canvas c = new Canvas(bmpGrayscale);
          Paint paint = new Paint();
          ColorMatrix cm = new ColorMatrix();
          cm.setSaturation(0);
          ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
          paint.setColorFilter(f);
          c.drawBitmap(bmpOriginal, 0, 0, paint);
          return bmpGrayscale;
      }
  
  
  public Bitmap toBinary(Bitmap bmpOriginal) {
      int width, height, threshold;
      height = bmpOriginal.getHeight();
      width = bmpOriginal.getWidth();
      threshold = 127;
      final Bitmap bmpBinary = null;
  
      for(int x = 0; x &lt; width; ++x) {
          for(int y = 0; y &lt; height; ++y) {
              // get one pixel color
              int pixel = bmpOriginal.getPixel(x, y);
  
              //get grayscale value
              int gray = (int)(pixel &amp; 0xFF);
  
              //get binary value
              if(gray &lt; threshold){
                  bmpBinary.setPixel(x, y, 0);
              } else{
                  bmpBinary.setPixel(x, y, 255);
              }
  
          }
      }
      return bmpBinary;
  }
  
  }
  </code></pre>