Convert WriteableBitmap of win phone 8.1 (silverlight) to BinaryBitmap of com.google.zxing

67 views Asked by At

Spent almost a day searching but couldn't find a proper solution. I am developing a QR reading module for my win 8.1 phone app which is Silverlight based (not win 8.1 phone native)

I am using zxing lib to add complete the QR module. I have reached the point where I have the image from the camera (MediaCapture) which of object WriteableBitmap and I want to use the api QRCodeReader.decode(BinaryBitmap bb).

I have tried using RGBLuminanceSource as stated by most articles but that works in native app (as it needs reference to System.Windows which is not valid for Silverlight based apps.

Can someone guide me on converting WriteableBitmap to BinaryBitmap?

1

There are 1 answers

0
Nkosi On

I've used the following code on pre window phone 8.1 with zxing using the PhotoCamera class. Now I'm not sure if this is still valid for you purposes but here is the LuminanceSource derived class.

internal class PhotoCameraLuminanceSource : LuminanceSource
{
    public byte[] PreviewBufferY { get; private set; }

    public PhotoCameraLuminanceSource(int width, int height)
        : base(width, height)
    {
        PreviewBufferY = new byte[width * height];
    }

    public override byte[] Matrix
    {
        get { return (byte[])(Array)PreviewBufferY; }
    }

    public override byte[] getRow(int y, byte[] row)
    {
        if (row == null || row.Length < Width)
        {
            row = new byte[Width];
        }

        for (int i = 0; i < Height; i++)
            row[i] = (byte)PreviewBufferY[i * Width + y];

        return row;
    }
}

That is then used like this.

PhotoCamera.GetPreviewBufferY(_luminance.PreviewBufferY);

var binarizer = new HybridBinarizer(_luminance);

var binBitmap = new BinaryBitmap(binarizer);

//Use readers to decode possible barcodes.
var result = _QRCodeReader.decode(binBitmap);

where _luminance is of type PhotoCameraLuminanceSource