This tip shows you the way to convert BufferedImage object of java to BitMatrix object in ZXing library. Since in Image processing you must have information about the bits of images so in ZXing library BitMatrix object represents image in bits form.
This conversion is the first step during Image processing using ZXing library.
First thing first , I would like to introduce ZXing library to you:
What is ZXing library ?
ZXing (pronounced “zebra crossing”) is an open-source, multi-format 1D/2D barcode image processing library implemented in Java. This library is mainly used to process any type of barcode images (1D/2D).
To know more please visit: http://code.google.com/p/zxing/
Why conversion of BufferedImage to BitMatrix is required during Image processing?
When you are going to use ZXing library with java, this conversion is very important because in ZXing library to represent an image the most basic class is BitMatrix (Represents bits of image) .
Now we move ahead and see how we can convert it.
Suppose we have a BufferedImage object of java for any Image file. We want to get data of Image in Bits form for image processing through ZXing library so for this purpose ZXing provides you a class BitMatrix to represent data of image in matrix of bits form.
There are some simple steps:
Step 1:You need to create an object of BufferedImageLuminanceSource(A class of ZXing API). The constructor of this class takes BufferdImage object of java as parameter.
BufferedImageLuminanceSource bils = new BufferedImageLuminanceSource(bufferedImage); // Here I assumed the BufferdImage object name is "bufferedImage"
Step 2: Now there is a class “HybridBinarizer” in ZXing API which takes BufferedImageLuminanceSource object as parameter in constructor.
So go ahead and create an object of “HybridBinarizer” like this :
HybridBinarizer hb = new HybridBinarizer(bils);//bils is BufferedImageLuminanceSource object
Step 3: There is a method in HybridBinarizerclass named “getBlackMatrix()” , just call this method and you will get your BitMatrix object corrosponding to your corresponding.
BitMatrix bm = hb.getBlackMatrix();
All the above steps are pretty simple. In this way you can easily convert your “BufferedImage” object of java to “BitMatrix” object of ZXing library.
Now you can process this BitMatrix object as per your requirement.
Hope it will be helpful.