This project has moved and is read-only. For the latest updates, please go here.

Can I convert a BinaryBitmap to a Bitmap for debugging?

Jun 1, 2014 at 10:16 PM
Hey there!

At first I'd like to say that ZXing really is an amazing open source library with a lot of interesting features! I've already dived deeply into the code and have a question:

Is it possible to convert a BinaryBitmap object into a black and white Bitmap?
I'd like to visualize the result after the HybridBinarizer has been applied to an input image to see what's going on.

I tried the following code (as a method of BinaryBitmap class):
public Bitmap ToBitmap()
          Bitmap resultBitmap = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);

          for (int y = 0; y < Height; y++)
              for (int x = 0; x < Width; x++)
                  if (BlackMatrix[x,y])
                      resultBitmap.SetPixel(x, y, Color.FromArgb(0,0,0));
                      resultBitmap.SetPixel(x, y, Color.FromArgb(255,255,255));

          return resultBitmap;
I know it's inefficient to use SetPixel and stuff, but that's not the point. I tried several different approaches and the results I get are very strange.

I tried a PDF417 sample code from Wikipedia:

And the result looks like there is some offset between the Bits in the BitMatrix.

What am I doing wrong?

Thanks in advance!

Peter Orange.
Jun 3, 2014 at 9:03 PM
Edited Jun 3, 2014 at 9:10 PM
You can take a look at the command line client. You can use it with the argument "--dump_black_point". In that case it generates a new image with the visualization of the BitMatrix after binarization and a version with the luminance values.
(method "dumpBlackPoint")
Primarly it does the same like your method.
But I never realized an offset between the bits.
Jun 8, 2014 at 6:25 PM
Edited Jun 8, 2014 at 6:36 PM
Hey micjahn!

Thanks a lot for Your reply. I tried Your approach and still received very strange results. Here's the mono-image I got from the previously mentioned sample image:

(Sorry just noticed I can't attach images in Discussion properly. See Issues here: Issues)

I guess something's wrong with the binarizer? Should i create an open issue?

Thanks in advance.

Peter Orange
Jun 9, 2014 at 8:36 PM
The image uses a pixel format of 4bpp indexed. That wasn't supported and was interpreted as 1bpp indexed.
It is now fixed in rev. 88737.