ZXing converts Bitmap to BinaryBitmap

I use OpenCV and Zxing, and I would like to add 2d code scan. I have several types of images that I could send. Probably the best is Bitmat (another option is OpenCV Mat).

It looks like you used to be able to convert like this:

Bitmap frame = //this is the frame coming in LuminanceSource source = new RGBLuminanceSource(frame); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); //then I can use reader.decode(bitmap) to decode the BinaryBitmap 

However, RGBLuminaceSource looks as if it does not accept a bitmap as input. So, how else can I convert the input image to BinaryBitmap ???

Edit:

Good, so I think I have made some progress, but I still have a problem. I think I have code that converts the bitmap to the correct format, however now I get an array ofIndexOutOfBounds

 public void zxing(){ Bitmap bMap = Bitmap.createBitmap(frame.width(), frame.height(), Bitmap.Config.ARGB_8888); Utils.matToBitmap(frame, bMap); byte[] array = BitmapToArray(bMap); LuminanceSource source = new PlanarYUVLuminanceSource(array, bMap.getWidth(), bMap.getHeight(), 0, 0, bMap.getWidth(), bMap.getHeight(), false); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); Reader reader = new DataMatrixReader(); String sResult = ""; try { Result result = reader.decode(bitmap); sResult = result.getText(); Log.i("Result", sResult); } catch (NotFoundException e) { Log.d(TAG, "Code Not Found"); e.printStackTrace(); } } public byte[] BitmapToArray(Bitmap bmp){ ByteArrayOutputStream stream = new ByteArrayOutputStream(); bmp.compress(Bitmap.CompressFormat.JPEG, 50, stream); byte[] byteArray = stream.toByteArray(); return byteArray; } 

I get an error

 02-14 10:19:27.469: E/AndroidRuntime(29736): java.lang.ArrayIndexOutOfBoundsException: length=33341; index=34560 02-14 10:19:27.469: E/AndroidRuntime(29736): at com.google.zxing.common.HybridBinarizer.calculateBlackPoints(HybridBinarizer.java:199) 

I wrote the byte size [], and this is the length shown above. I can't understand why zxing expects it to be bigger

+5
source share
3 answers

OK I understood. As Sean Owen said, PlanarYUVLuminaceSource will only be used for the default camera format for Android, and I think OpenCV doesn't use it. In short, here is how you do it:

 //(note, mTwod is the CV Mat that contains my datamatrix code) Bitmap bMap = Bitmap.createBitmap(mTwod.width(), mTwod.height(), Bitmap.Config.ARGB_8888); Utils.matToBitmap(mTwod, bMap); int[] intArray = new int[bMap.getWidth()*bMap.getHeight()]; //copy pixel data from the Bitmap into the 'intArray' array bMap.getPixels(intArray, 0, bMap.getWidth(), 0, 0, bMap.getWidth(), bMap.getHeight()); LuminanceSource source = new RGBLuminanceSource(bMap.getWidth(), bMap.getHeight(),intArray); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); Reader reader = new DataMatrixReader(); //....doing the actually reading Result result = reader.decode(bitmap); 

So this is not at all difficult. It was just necessary to convert Android Bitmap into an integer array and its piece of cake.

+8
source
 public static String readQRImage(Bitmap bMap) { String contents = null; int[] intArray = new int[bMap.getWidth()*bMap.getHeight()]; //copy pixel data from the Bitmap into the 'intArray' array bMap.getPixels(intArray, 0, bMap.getWidth(), 0, 0, bMap.getWidth(), bMap.getHeight()); LuminanceSource source = new RGBLuminanceSource(bMap.getWidth(), bMap.getHeight(), intArray); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); Reader reader = new MultiFormatReader();// use this otherwise ChecksumException try { Result result = reader.decode(bitmap); contents = result.getText(); //byte[] rawBytes = result.getRawBytes(); //BarcodeFormat format = result.getBarcodeFormat(); //ResultPoint[] points = result.getResultPoints(); } catch (NotFoundException e) { e.printStackTrace(); } catch (ChecksumException e) { e.printStackTrace(); } catch (FormatException e) { e.printStackTrace(); } return contents; } 
+6
source

Bitmap is an Android class. The default image format for Android by default is the flat format YUV. Therefore, only PlanarYUVLuminanceSource is required for Android. RGBLuminanceSource must be ported.

You put the wrong data type in the class. The pixels are expected in YUV layout format. You transfer the compressed bytes of a JPEG file.

+2
source

All Articles