Forum Cross Platform with Xamarin

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

ZxingSharp.Mobile - Cross Platform Barcode Scanning

OldRedthOldRedth USMember, Insider ✭✭✭
edited October 2012 in Cross Platform with Xamarin

Hey everyone! I'm very excited to finally show off a very beta version of ZxingSharp.Mobile:

ZxingSharp.Mobile is a C#/.NET library based on the open source Barcode Library: ZXing (Zebra Crossing). It works with MonoTouch, Mono for Android, and Windows Phone. The goal of ZxingSharp.Mobile is to make scanning barcodes as effortless and painless as possible in your own applications.

https://github.com/Redth/ZxingSharp.Mobile

Here's the simplest example of using the library:

var scanner = new ZxingSharp.Mobile.ZxingScanner();
scanner.StartScanning((result) => {   
    if (result != null)
        Console.WriteLine("Scanned Barcode: " + result.Value);
 });

Anyway, follow the github project for updates. It's a work in progress, but I think it's a good start!
Let me know what you think!

«1345

Posts

  • BryanCostanichBryanCostanich USMember, Xamarin Team Xamurai

    Awesome, @Redth!

  • DayUpDayUp CNMember

    thank you...

  • JamesLaveryJamesLavery GBBeta, University ✭✭✭✭✭

    That's great - thanks.
    Does it support Data Matrix? We have an application which would need this.

  • MatteoPolitoMatteoPolito ITMember ✭✭

    Great!!

  • OldRedthOldRedth USMember, Insider ✭✭✭

    @JamesLavery I haven't tested Data matrix specifically, but there is some code in the zxing project for scanning data matrix... YMMV, but let me know if you do try it out :)

  • JamesLaveryJamesLavery GBBeta, University ✭✭✭✭✭

    I've just tried it with Data Matrix, and I'm afraid it doesn't seem to detect or scan it. :-(

  • JonCoxJonCox USMember, Beta

    Hi there, I have just downloaded your source, but cannot load the solution into MonoTouch. MonoTouch reports a Cannot set MTouchArch with default value error. Is there somthing I need to do to load the solution?

  • OldRedthOldRedth USMember, Insider ✭✭✭

    @JamesLavery yeah it appears it does not work for me either :(. I am currently looking into improving the zxing core code in the library. One of the options I'm exploring is using ikvm (there's a monotouch branch someone made) to convert the java binaries. Sharpen is another possibility too. The c# port of zxing is quite old, doesn't seem to have working support for PDF417 or DataMatrix, and is missing several other formats completely that the java version now supports (plus it's missing barcode writers for many formats).

    So, for the time being there's no good solution to reading data matrix. But I'm always open to having some help getting it working ;)

    @JonCox I don't believe there's anything special needed... I'm not sure why you'd be getting that error though.

  • OldRedthOldRedth USMember, Insider ✭✭✭

    @JonCox Give it a try now. I just pushed some code to github that should make the projects open in monodevelop.

    @JonCox I've started experimenting with IKVM and haven't got it to decode datamatrix yet :s... I'm wondering if I'm doing something wrong... Anyway, it's a work in progress...

  • JonCoxJonCox USMember, Beta

    @Redth Thank you for the update, I will give it a try and see how it goes.

  • Hello All,

    I've been battling to try to do something with datamatrix but with no success. We really need datamatrix (its a standard in the industry where we work).

    Anyway what seems to be the problem with the datamtrix? Is it the detection part, is it an overall performance issue? or is just because de c# port is outdated?

    Regards,

  • OldRedthOldRedth USMember, Insider ✭✭✭

    Hey there!

    The c# port I was originally using was quite out of date. I will be updating github very shortly with some changes that include switching to a better c# port where data matrix does indeed work.

    Hold tight, it may even be available later today!

  • Great News Jonathan, i will surely test it as soon as you make it available.

    Tell me if you need anything else apart from testing!:)

    Best regards,

  • I forgot to mention that the actual port works with datamatrix, i just have problems with small datamatrix!

  • OldRedthOldRedth USMember, Insider ✭✭✭

    When's the last time you pulled from source? I thought I hadn't committed my changes that use the new port, but it looks like I did a little while ago, so the ZXing.Net port I switched to is already in use.

  • I never used yours, i was using JohnACarruthers port! Here's the link.

    So you saying that your current version works with small datamatrix? With the latter port i manage to fire 3cm-5cm squares of datamatrix but not below that...

    Regards,

  • OldRedthOldRedth USMember, Insider ✭✭✭
    edited November 2012

    I'm quite familiar with John's port. His work with getting it working on MonoTouch is what started me down this road. However, I use a completely different c# port of ZXing now, based off ZXing.Net which is a hand crafted port.

    Here's the current history (as I understand it) of ZXing and MonoTouch / Mono for Android:

    1. The Official ZXing project has a C# port which is very outdated from where the java version is now. Some combination of work from JohnACarruthers, Nirvanai, and Alex Carrado used this port (as far as I know) to make it compile and add 'live scanning' for MonoTouch and Mono for Android UI's.

    2. Martin Bowling came up with some code (not sure where he found the original) to use the camera picker instead for the live barcode scanning on MonoTouch - I found this more stable and I made some fixes to some memory leaking.

    3. I then created ZxingSharp based on all the above code, but wrapped it up into a nice easy to use library.

    4. For another project, I started testing all the formats and trying to generate barcodes, and found the C# port was sorely lacking. I started testing IKVM'ing the updated java lib, and also using Sharpen to convert the java source. Before I was finished, I then discovered ZXing.Net and had great success with it.

    5. I created and submitted patches to ZXing.Net project to support compiling on MonoTouch and Mono for Android.

    6. After this, I renamed ZxingSharp to ZXing.Net.Mobile and included ZXing.Net as a submodule in it. So my library is now using the much improved C# port of ZXing.Net that now has MonoTouch and Mono for Android support.

    So you can think of the whole solution as two parts:

    1. ZXing.Net - A really good, up to date C# port of ZXing that works with MonoTouch and Mono for Android as well. This project can decode images, but has no code to assist in the 'live scanning' for iOS and Android - http://zxingnet.codeplex.com/

    2. ZXing.Net.Mobile - This is my project includes the code to do the 'live scanning' on Android, iOS, and Windows Phone. It uses ZXing.Net as a submodule to handle the actual decoding. https://github.com/Redth/ZXing.Net.Mobile

    Now, to (not) answer your original question, I'm not sure how small of data matrix barcodes this port will decode, but I would give it a try. I'm guessing you'll have more success with it.

    Finally, you should check out the Xamarin Seminar that I'm leading on November 29th which is on Barcode Scanning :)

    Hope that helps everyone!

  • Hi Jonathan, Im working with JohnACarruthers since the beginning. The barcode reading is just a little part of our app but i finally turn into it.

    I work with camera in Monotouch/Monodroid for a while now so setting up a live scanning is not a big deal to me, as long as i have the library!

    Im trying to use your port but im still trying to work with it! I guess ill try both suggestions that you made (yours zxing.net.mobile and zxing.net).

    It's pretty annoying but in our industry we need to deal with really small datamatrix(it works so well in the java native app and so differently in the c# outdated port i was becoming a little bit sad).

    I will surely attend to the seminar!

    Thanks for the detailed answer/history. Ill give you some feedback after getting it to work!

  • OldRedthOldRedth USMember, Insider ✭✭✭

    You should be able to swap in ZXing.Net instead of the original C# port to work with your existing live scanning code with relatively little effort. ZXing.Net.Mobile just gives you some really easy live scanning if you are unfamiliar with it, but there's definitely no requirement to use it. You can easily still use ZXing.Net alone.

  • Thanks Jonathan,

    I'll give it a try and get back to you with some feedback! Thanks for the support!

  • Jonathan,

    Somehow i get some kind of incompatibility with System.Drawing. Is it necessary to do something about this? (running mac os x, with mono2.1).

    All the best,

  • OldRedthOldRedth USMember, Insider ✭✭✭

    Need more info about what you're seeing... sorry.

  • I just tried to add zxing.net into a simple project. The error log on compilation:

    /Library/Frameworks/Mono.framework/External/xbuild/Novell/Novell.MonoDroid.Common.targets: Error: Exception while loading assemblies: System.IO.FileNotFoundException: Could not load assembly 'System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Perhaps it doesn't exist in the Mono for Android profile?
    File name: 'System.Drawing.dll'
    at Monodroid.Tuner.MonoDroidResolver.Resolve (Mono.Cecil.AssemblyNameReference reference, Mono.Cecil.ReaderParameters parameters) [0x00000] in :0
    at Monodroid.Tuner.MonoDroidResolver.Resolve (Mono.Cecil.AssemblyNameReference reference) [0x00000] in :0
    at Xamarin.Android.Tasks.ResolveAssemblies.AddAssemblyReferences (System.Collections.Generic.List1 assemblies, Mono.Cecil.AssemblyDefinition assembly) [0x00000] in <filename unknown>:0 at Xamarin.Android.Tasks.ResolveAssemblies.AddAssemblyReferences (System.Collections.Generic.List1 assemblies, Mono.Cecil.AssemblyDefinition assembly) [0x00000] in :0
    at Xamarin.Android.Tasks.ResolveAssemblies.Execute () [0x00000] in :0 (DatasetCollector)

    I use the following imports:

    using System;
    using System.IO;
    using System.Collections;
    using System.Collections.Generic;

    using Android.App;
    using Android.Content;
    using Android.Views;
    using Android.Widget;
    using Android.OS;
    using Android.Hardware;
    using Android.Util;
    using Android.Media;

    using ZXing;

    using System.Threading;

    Is there some kind of incompatibility between versions of mono?

    If i try to run your zxing.net.mobile (mono4droid sample) i get an error on Size declarations: _Error CS0433: The imported type `System.Drawing.Size' is defined multiple times (CS0433) _

    Regards,

  • OldRedthOldRedth USMember, Insider ✭✭✭

    Can you hop on IRC or the chatroom, this is going to take more than a couple posts back and forth, and I don't want to have a 'conversation' on the forums :)

    http://chat.xamarin.com/#/rooms/Android

    or #monodroid on irc

  • Hey Jonathan,

    It worked like a charm! Thanks for pointing me out to the updated ports!

    All the best!

  • JonCoxJonCox USMember, Beta

    Hi there

    I have been attempting to use your sample application but Monotouch is throwing me an error, saying that I am missing a using directive or reference. I have imported the Zxing.Net.Mobile solution and the Zxing.net dll version 3.5, but still no joy.

    The item that is throwing the error is in the MobileBarcodeScanningOptions class at line 31.
    The error is Type unknown for BarcodeReader.

    I am sure that it is somthing that I have done wrong with the project setup. Do you have any idea what it could be?

  • OldRedthOldRedth USMember, Insider ✭✭✭

    Well that was fun! I ran into lots of issues trying to have ZXing.Net as a submodule of ZXing.Net.Mobile... I've ultimately (for now anyway) just made a copy of the ZXing.Net code inside the ZXing.Net.Mobile project.

    You'll still need to reference both projects in your own application of course, but try getting the latest code from https://github.com/Redth/ZXing.Net.Mobile and using it now, I think it should compile for you!

  • antoniolopesantoniolopes GBMember
    edited November 2012

    Hi Jonathan and all!

    I tried the latest code and still working as a charm but i suspect that the ZXing.Net monotouch version has a memory leak if you use

        public override void DidOutputSampleBuffer (AVCaptureOutput captureOutput, CMSampleBuffer sampleBuffer, AVCaptureConnection connection)
               {
    
                       var image = ImageFromSampleBuffer (sampleBuffer);
    
                       prevImage = image;
    
                       try{
                               result = reader.Decode(prevImage);
    
                               if(!string.IsNullOrEmpty(result.Text)){
                                       Console.WriteLine(result.Text);
                               }    
                       }catch(Exception e){
                               Console.WriteLine(e.ToString());
                       }
    
       }
    

    After less than a minute the app crash with memory warning. The context of this code is just a simple camera preview where DidOutputSampleBuffer gets called whenever a preview is available. reader is a BarcodeReader object. I shall try with other Reader objects.

    Regards

  • OldRedthOldRedth USMember, Insider ✭✭✭

    @antoniolopes you may want to check your code for leaks. I just ran Instruments on my sample application and there are no leaks apparent. I'd suggest that you make sure to dispose of your image after you're done using it. Sorry, but I can't find any leaks in my sample application!

    There's a good post on the forums for using Instruments to find leaks, try running it on your app. If there is something in my library, this may help uncover it too. Good luck!

  • @Redth

    Hi Jonathan. I forget to update this thread. Well i never used Instruments but i should then. Anyway, with the same context/code using MultiFormatReader instead of BarcodeReader i dont have memory leaks at all. I do dispose buffer on the end of this method DidOutputSampleBuffer but i shall try it again monday (with dispose() on the image and see if i can get the memory leak). Anyway i use MultiFormatReader and it's just fine.

    Regards,

  • Hi there, I've been having problems running the sample application on my iPhone (running iOS 6 with MonoDevelop 3.0.5 MonoTouch 6.0.6). Basically, when in Debug/iPhone mode after scanning a barcode (Default View) it the view disappears and returns to the HomeViewController. When attempting to scan again using the default view it crashes with the attached runtime error. In Release/iPhone mode when attempting to scan first time it crashes. Thanks for your help.

  • mklmkl USMember, Beta

    @KonstantinosZachos

    I'm not sure if the project should handle threads automatically, but you can get around the issue by using a TaskScheduler:

    scanner.Scan(options).ContinueWith((task) => {
        if (task.Status == TaskStatus.RanToCompletion) {
            if (task.Result != null && !string.IsNullOrEmpty(task.Result.Text)) {
                // Do something with task.Result.Text
            }
        }
    }, TaskScheduler.FromCurrentSynchronizationContext()); // Continue on main thread
    

    @Redth

    I just migrated from a quite unstable project to yours. Good work!

  • OldRedthOldRedth USMember, Insider ✭✭✭

    @KonstantinosZachos Please see the source code on the github master branch now for the sample. The problem looks to be that in my sample I forgot to wrap displaying the barcode result in a InvokeOnMainThread(..) call. That looks like the error you are getting anyway.

  • @Redth
    Excellent, that works now. On another note, what do I have to change to scan QR codes as it doesn't seem to work with the current sample. Ta

  • ssommerssommer USMember

    First, a big thanks Redth and anyone else who has had a hand in this project.

    I've been playing with the Monotouch sample with the intent of using this in a future app. I'll need to scan both Code 39 and Datamatrix. I've tested the sample on an iPhone 4 running 6.0, a 4S running 5.1, and an iPad 3 running 6.0.1.

    Both phones scan Code 39 well. Datamatrix is a bit temperamental even with TryHarder set to true, and only Datamatrix in the PossibleFormats list. I wrote a small universal test app and was able to get the iPad to recognize both Datamatrix and Code 39, but again it is temperamental and a bit slow to process.

    Is there a way that I can restrict the area processed to help speed things up a bit? I noticed in the seminar that your scanning happened almost immediately once the barcode was in frame, but I haven't been able to replicate this on any of the devices I have access to.

    Also, the Datamatrix I'm reading is only 1.5 cm^2 so I imagine this could also be causing some issues. Thanks in advance.

  • @Redth Awesome work fella! Any idea why QR codes don't scan though? I've tried creating custom options with just QR_CODE as PossibleFormats, and TrYHarder set to true, but nothing seems to work. Cheers

  • OldRedthOldRedth USMember, Insider ✭✭✭

    @ssomer currently there's no way to restrict the area. It's technically possible to implement this feature, but it's not something that's currently supported. The device I used in the seminar was very old, so the performance is pretty decent considering, but like you said, a very small data matrix is probably not the easiest to accurately scan :s

    @RichardKershaw I'm not sure, I'd need more info... Perhaps there's a problem with the QR code itself? I haven't run into problems scanning them with the QR_CODE format specified explicitly :s

  • Hi Jonathan, I downloaded the source.
    I opened ZXing.Net.Mobile.Monotouch solution in Monodevelop, and I run it on my device (Ipad).
    It works, but no barcodes are scanned, in output window appears always: "ZxingSharpMonoTouchSample[921:907] Object reference not set to an instance of an object"
    I tried with simple EAN barcodes from the product I found in my house.
    Wich could be the problem?
    My best regards, Maurizio.

  • RubyRuby USMember

    Guys, I'm using the ZXing.Net.Mobile.monoandroid right now and it's amazing, the only thing is, I can't scan qr codes, it's good for scanning 1d code but qr code is no luck.

    Guys can you provide me a step by step procedure on scanning qr codes?

    in the MobileBarCodeScanning.cs I replace the barcodeformat.all.1d to qr_code, and still no luck

    public static MobileBarcodeScanningOptions Default { get { return new MobileBarcodeScanningOptions() { PossibleFormats = new List<BarcodeFormat>() { BarcodeFormat.QR_CODE} }; } }

    The library is great, there is no problem reading barcodes, but I really love if I can read qr codes

    thanks and more power

  • @Redth Hi Jonathan - I've tried the scanning the following QR code I generated online at - http://qrfree.kaywa.com/?s=8&amp;d=http://www.corethree.net

    but it doesn't scan on either an LG Optimus One (Froyo) or Google Nexus S (JellyBean).

    Any thoughts?

Sign In or Register to comment.