Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Converting a Bitmap to a Monochrome Bitmap (1 Bit per Pixel)

Tags:

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:

Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 

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).

As there seems to be no convenient, out of the box way to do that, one idea I had was to use:

bitmap.getPixels(pixels, offset, stride, x, y, width, height) 

to Obtain the pixels. I assume, I'd have to use it as follows:

int width = bitmap.getWidth(); int height = bitmap.getHeight();  int [] pixels = new int [width * height]; bitmap.getPixels(pixels, 0, width, 0, 0, width, height); 

However - I am not sure about a few things:

  • In getPixels - does it make sense to simply pass the width as the "Stride" argument?
  • 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)?
  • 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)

Does this approach make sense at all? Is there an easier way? It's not enough to just make the bitmap black & white, the main issue is to reduce the color information for each pixel into one bit.

UPDATE

As suggested by Reuben I'll first convert the Bitmap to a monochrome Bitmap. and then I'll iterate over each pixel:

    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 < height; y++) {         int offset = y * height;         // Iterate over width         for (int x = 0; x < width; x++) {             int pixel = bitmap.getPixel(x, y);         }     } 

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:

// Using the pixel from bitmap.getPixel(x,y) int lowestByte = pixel & 0xff; 
like image 671
AgentKnopf Avatar asked Feb 21 '12 12:02

AgentKnopf


People also ask

How do I change the bitmap color of a specific pixel in Android?

To set the colors of the pixels in your pixels array, get values from the static methods of Android's Color class and assign them into your array. When you're done, use setPixels to copy the pixels back to the bitmap.

What is monochrome BMP?

The monochrome bitmap is another bitmap format available in Fastgraph. Like other bitmaps, a monochrome bitmap is a rectangular grid of pixels, but each pixel is represented by a single bit. This means we can store eight pixels in each byte of the bitmap.


2 Answers

You can convert the image to monochrome 32bpp using a ColorMatrix.

Bitmap bmpMonochrome = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bmpMonochrome); ColorMatrix ma = new ColorMatrix(); ma.setSaturation(0); Paint paint = new Paint(); paint.setColorFilter(new ColorMatrixColorFilter(ma)); canvas.drawBitmap(bmpSrc, 0, 0, paint); 

That simplifies the color->monochrome conversion. Now you can just do a getPixels() and read the lowest byte of each 32-bit pixel. If it's <128 it's a 0, otherwise it's a 1.

like image 187
Reuben Scratton Avatar answered Oct 11 '22 02:10

Reuben Scratton


Well I think its quite late now to reply to this thread but I was also working on this stuff sometimes back and decided to build my own library that will convert any jpg or png image to 1bpp .bmp. Most printers that require 1bpp images will support this image (tested on one of those :)). Here you can find library as well as a test project that uses it to make a monochrome single channel image. Feel free to change it..:)

https://github.com/acdevs/1bpp-monochrome-android

Enjoy..!! :)

like image 20
chetan Avatar answered Oct 11 '22 03:10

chetan