This project has moved. For the latest updates, please go here.

Uri to WriteableBitmap WP8

Jan 21, 2013 at 10:25 PM

I've got a URI from a PhoneGap project image file, and I'm trying to convert it to work with this project. The trouble comes in after I create a new BitMapImage from the URI. The line 'var bitmap = await BitmapFactory.New(1, 1).FromResource(myUri.LocalPath);' is giving me trouble with an error ..."Imaging.WriteableBitmap' is defined in an assembly that is not refereced. You must add a reference to assembly 'System.Windows, Version=2.0.6.0, ..." I have no idea how to add such a reference in a Windows Runtime Component targeting windows 8 phones. There was other errors from targeting any CPU so I changed to arm, which is the standard cpu for windows phone 8 systems anyway. Any clues?

public static string Decode(Uri myUri)
{
    var bitmap = await System.Windows.Media.Imaging.BitmapFactory.New(1, 1).FromResource(myUri.LocalPath);
    var reader = new DataMatrixReader();
    var bitmap2 = new ZXing.BinaryBitmap(new HybridBinarizer(new BitmapLuminanceSource(bitmap)));
    var temp = reader.decode(bitmap2);
    if (temp == null)
        return "Erro: ZXing Bridge, decode returned null";
    else
    {
        return temp.Text;
    }
}

Coordinator
Jan 22, 2013 at 5:07 PM
Edited Jan 22, 2013 at 5:08 PM

Can you send me a sample project? It would be easier to talk about this issue.

On the other hand if you want to build a Windows Runtime Component I think you need a version of ZXing.Net which I prepare in a different branch of the repository:
https://zxingnet.svn.codeplex.com/svn/branches/WINMD
That's a special version for Windows Runtime Components. It can be used for HTML5/JS based apps for example. But there is no pre-built binary available at the moment. You have to build it by yourself.

One hint related to your code snippet. If you don't need the low-level control over the decoding functionality than you should try the following code snippet:

 

public static string Decode(Uri myUri)
{
    var bitmap = await System.Windows.Media.Imaging.BitmapFactory.New(1, 1).FromResource(myUri.LocalPath);
    var reader = new BarcodeReader { PossibleFormats = new List<BarcodeFormat> { BarcodeFormat.DATA_MATRIX }};
    var temp = reader.Decode(bitmap);
    if (temp == null)
        return "Erro: ZXing Bridge, decode returned null";
    else
    {
        return temp.Text;
    }
}

Hopefully I didn't made a mistake. Can't compile it at the moment.
If you have to use the WINMD version of ZXing.Net you have to use the BarcodeReader class, too. The reader classes for the different barcode formats are not exported.

Jan 22, 2013 at 5:46 PM
Edited Jan 22, 2013 at 5:53 PM

Oh, missed the WINMD package. So perfect. I've just now looked through the example app for Windows Phone 8 using the WinMD reference, but I'm dubious of its helpfulness already. First it won't compile due to it referencing Barcode format, and second its same problem of using javascript to make the calls, which your sample doesn't do? The problem I was, and still am having is using the Writeable bitmap class, which is not a valid class in a WINMD, which is the only thing Javascript can directly call. In your example you have Xaml call C# which then uses the classes that WINMD can't directly reference, such as Writeable bitmap. To be clear, WINMD libraries do allow a reference to writeablebitmap, but its the wrong one 'WIndows.UI.Xaml.Media.Imaging' which can't use the conversion functions, or even be directly consumed. To sum the problem, we are trying to make a multi-platform app, that uses a single UI through HTML5. We then plan to implement some functions that don't work well directly in Javascript (such as ZXIng) through interface calls that are mapped for each platform. We are trying very hard to keep all computations client side as the codes themselves are LOB and need to be secure, as well as avoiding network traffic and a service that is infrequently used. The ideal implementation would involve a Javascript that reads the 'src' or a canvas or img tag or some other useful attribute and sends that into some WINMD library that converts it to a 'bitmap' or whatever it will take and then uses ZXING.winmd. Any ideas?

Coordinator
Jan 22, 2013 at 7:09 PM

There is a HTML5/Javascript demo in the source code repository. If you have checked out the WinMD branch you have it already on your disk.

Because of the lacking support for WriteableBitmap you can only use the method Decode(byte[] rawRGB, ....). That means you have to convert your source image to the raw rgb values before you can decode it.
The demo uses the FileOpenPicker to get a sample image. You have to replace it with your uri.

Or if you have a canvas or img tag you can use that one putting it into the Windows.Graphics.Imaging.BitmapDecoder after loading it as a stream and go on with the sample code of the demo. Should be easy to implement I think.

Jan 24, 2013 at 9:39 PM

Thanks, turns out my problems are a bit deeper than that. Any chance of this library supporting multiple image formats with FromByteArray or FromStream?

Coordinator
Jan 27, 2013 at 7:32 PM

Direct support of different image formats (like JPG, PNG, ...) isn't the goal of this library.
The library uses Bitmap, WriteableBitmap or RGB raw data.

Perhaps I can help you in another way. Please describe your current problem with more details.