title: Android : Converting color image to grayscale
body: <p>I am trying to convert color image into grayscale using the average of red, green, blue. But it comes out with errors.</p>
  <p><strong>Here is my code</strong></p>
  <pre><code>imgWidth = myBitmap.getWidth();
  imgHeight = myBitmap.getHeight();
                      
  for(int i =0;i&lt;imgWidth;i++) {
      for(int j=0;j&lt;imgHeight;j++) {
       int s = myBitmap.getPixel(i, j)/3;
       myBitmap.setPixel(i, j, s);
      }
  }
                      
  ImageView img = (ImageView)findViewById(R.id.image1);
  img.setImageBitmap(myBitmap);
  </code></pre>
  <p>But when I run my application on Emulator, it's force close. Any idea?</p>
  <p>I have solved my problem use the following code:</p>
  <pre><code>for(int x = 0; x &lt; width; ++x) {
              for(int y = 0; y &lt; height; ++y) {
                  // get one pixel color
                  pixel = src.getPixel(x, y);
                  // retrieve color of all channels
                  A = Color.alpha(pixel);
                  R = Color.red(pixel);
                  G = Color.green(pixel);
                  B = Color.blue(pixel);
                  // take conversion up to one single value
                  R = G = B = (int)(0.299 * R + 0.587 * G + 0.114 * B);
                  // set new pixel color to output bitmap
                  bmOut.setPixel(x, y, Color.argb(A, R, G, B));
              }
          }
  </code></pre>