title: Android: Converting a Bitmap to a Monochrome Bitmap (1 Bit per Pixel)
body: <p>I want to print a Bitmap to a mobile Bluetooth Printer (Bixolon SPP-R200) - the SDK doesn't offer direkt methods to print an in-memory image. So I thought about converting a Bitmap like this:</p>
  
  <pre><code>Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
  </code></pre>
  
  <p>To a Monochrome Bitmap. I am drawing black text on above given Bitmap using a Canvas, which works well. However, when I convert the above Bitmap to a ByteArray, the printer seems to be unable to handle those bytes. I suspect I need an Array with one Bit per Pixel (a Pixel would be either white = 1 or black = 0).</p>
  
  <p>As there seems to be no convenient, out of the box way to do that, one idea I had was to use: </p>
  
  <pre><code>bitmap.getPixels(pixels, offset, stride, x, y, width, height)
  </code></pre>
  
  <p>to Obtain the pixels. I assume, I'd have to use it as follows:</p>
  
  <pre><code>int width = bitmap.getWidth();
  int height = bitmap.getHeight();
  
  int [] pixels = new int [width * height];
  bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
  </code></pre>
  
  <p>However - I am not sure about a few things:</p>
  
  <ul>
  <li>In getPixels - does it make sense to simply pass the width as the "Stride" argument?</li>
  <li>I guess I'd have to evaluate the color information of each pixel and either switch it to black or white (And I'd write this value in a new target byte array which I would ultimately pass to the printer)?</li>
  <li>How to best evaluate each pixel color information in order to decide that it should be black or white? (The rendered Bitmap is black pain on a white background)</li>
  </ul>
  
  <p>Does this approach make sense at all? Is there an easier way? It's not enough to just make the bitmap black &amp; white, the main issue is to reduce the color information for each pixel into one bit.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>As suggested by Reuben I'll first convert the Bitmap to a monochrome Bitmap. and then I'll iterate over each pixel:</p>
  
  <pre><code>    int width = bitmap.getWidth();
      int height = bitmap.getHeight();
  
      int[] pixels = new int[width * height];
      bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
  
      // Iterate over height
      for (int y = 0; y &lt; height; y++) {
          int offset = y * height;
          // Iterate over width
          for (int x = 0; x &lt; width; x++) {
              int pixel = bitmap.getPixel(x, y);
          }
      }
  </code></pre>
  
  <p>Now Reuben suggested to "read the lowest byte of each 32-bit pixel" - that would relate to my question about how to evaluate the pixel color. My last question in this regard: Do I get the lowest byte by simply doing this:</p>
  
  <pre><code>// Using the pixel from bitmap.getPixel(x,y)
  int lowestByte = pixel &amp; 0xff;
  </code></pre>