1

I am able to invoke the barcode scanner via intents, but when I try to capture the values returned by the scan based on this, I get the following errors when trying to build the solution.

  • Android.Content.Intent' does not contain a definition for 'getStringExtras' and no extension method 'getStringExtras' accepting a first argument of type 'Android.Content.Intent' could be found (are you missing a using directive or an assembly reference?)
  • The name 'RESULT_CANCELED' does not exist in the current context
  • The name 'RESULT_OK' does not exist in the current context

Here is my code for both invoking the scanner and trying to return results. Click event is in onCreate. onActivityResult is a method in the same.cs, same class, same namespace as the scan.Click event.

    Button scan = (Button)FindViewById(Resource.Id.scan);
        scan.Click += (o, e) =>
        {
            Intent intent = new Intent("com.google.zxing.client.android.SCAN");
            intent.SetPackage("com.google.zxing.client.android");
            intent.PutExtra("com.google.zxing.client.android.SCAN.SCAN_MODE", "QR_CODE_MODE");
            StartActivityForResult(intent, 0);
        };

    public void onActivityResult(int requestCode, int resultCode, Intent intent)
    {
        if (requestCode == 0)
        {
            if (resultCode == RESULT_OK)
            {
                String contents = intent.getStringExtra("SCAN_RESULT");
                String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
            }
            else if (resultCode == RESULT_CANCELED)
            {

            }
        }
    }
Community
  • 1
  • 1
jmease
  • 2,507
  • 5
  • 49
  • 89
  • This approach is probably not the best way to implement this, as your app will crash if zxing's barcode scanner is not installed. – Charles Munger Dec 27 '11 at 21:39
  • (He just has to catch ActivityNotFoundException or check for the package first, or use IntentIntegrator -- easy to not trip on that.) – Sean Owen Dec 27 '11 at 22:39

2 Answers2

5

The problem here is that your onActivityResult method has not been properly translated from Java to Mono for Android.

It should look more like:

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
    if (requestCode == 0)
    {
        if (resultCode == Result.Ok)
        {
            String contents = data.GetStringExtra("SCAN_RESULT");
            String format = data.GetStringExtra("SCAN_RESULT_FORMAT");
        }
        else if (resultCode == Result.Canceled)
        {

        }
    }
}

The highlights of the translation are:

  1. Correct signature (and override) for OnActivityResult
  2. Use the Result enumeration
  3. The getStringExtra method in Java is GetStringExtra in C#
Greg Shackles
  • 10,009
  • 2
  • 29
  • 35
1

Try using the utility class provided by zxing, which can be found here. This will also prevent your application from crashing if the Barcode Scanner app is not installed.

Charles Munger
  • 1,417
  • 12
  • 11
  • I would love to implement this way, but the link is just a whole lot of java which as you can tell is not my wheelhouse. If there is a monodroid example of just opening the scanner and getting the results using IntentIntegrator, it would be much appreciated. I haven't been able to find one on google. – jmease Dec 28 '11 at 13:58