Barcode Reader App using ZXing - How to convert the UIImage class to a BinaryBitmap?

813 views
Skip to first unread message

Stefan Oltmann

unread,
Apr 10, 2015, 6:18:13 AM4/10/15
to rob...@googlegroups.com
I'm trying to create a barcode scanner app für iOS using RoboVM.

I'm using the ZXing framework for that, because it works fine under swing and android.

Now I wonder how I can convert the UIImage (iOS) class to an BinaryBitmap (ZXing model class).

This is how to use the framework in swing:

BufferedImage bImage = ImageIO.read(new File("barcode.png"));

LuminanceSource source = new BufferedImageLuminanceSource(bImage);

BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(source));

Reader reader = new MultiFormatReader();
Result result = reader.decode(binaryBitmap);
String text = result.getText();

This is the android way:

Bitmap mBitmap = BitmapFactory.decodeFile("barcode.png");

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

RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels);

BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(source));

Reader reader = new MultiFormatReader();
Result result = reader.decode(binaryBitmap);
String text = result.getText();

What is the RoboVM / iOS way to do that?

Niklas Therning

unread,
Apr 11, 2015, 3:25:05 AM4/11/15
to Stefan Oltmann, rob...@googlegroups.com
Something like this should give you the raw pixel data (untested):

    CGImage cgImage = UIImage.create(new File("barcode.png")).getCGImage();
    CGDataProvider dataProvider = cgImage.getDataProvider();
    byte[] bytes = dataProvider.getData().getBytes();

From there you might need to do some processing of the bytes to make zing happy I guess. It depends on the format of the original PNG file (bit depth, color space, etc). You can use cgImage.getBitmapInfo() to get info on the format of the bitmap. The CoreImage framework has filters that can be applied to images, e.g. convert to grayscale.

Hope that helps!


--
You received this message because you are subscribed to the Google Groups "RoboVM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robovm+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages