1

So, I've been trying to use the Google Goggles Intent, so I can use the scanner as an OCR device. I've used the following code in my Activity:

Intent intent = new Intent("com.google.zxing.client.android.SCAN");
        //intent.setPackage("com.google.zxing.client.android");
        intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
        Log.d("TAG", "start goggles!");
        startActivityForResult(intent, 0);    

And as for my onActivityResult, it looks like this :

public void onActivityResult(int requestCode, int resultCode, Intent intent) {

        super.onActivityResult(requestCode, resultCode, intent);
        if (requestCode == 0) {
            if (resultCode == RESULT_OK) {
                String contents = intent.getStringExtra("SCAN_RESULT");
                String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
                Log.d("TAG", "result back!"+contents);
                Toast.makeText(getBaseContext(), contents, Toast.LENGTH_LONG).show();

            } else if (resultCode == RESULT_CANCELED) {
                Toast.makeText(getBaseContext(), "CANCELLED", Toast.LENGTH_LONG).show();
                // TODO: Handle cancel
            }
        }
    }

The above code successfully starts the other application but fails to bring back the result and always ends up in the RESULT_CANCELLED resultCode. ( I'm using the back button to return to my application, am I doing anything wrong here?)

Any help would be appreciated. Thanks!

harshalizee
  • 129
  • 1
  • 3
  • 12

1 Answers1

2

Well, if you use the back button to return to your app, of course the resultCode will be RESULT_CANCELED, because you literally cancelled the Goggles request. If you scan a valid qr code while in Googles, it should automatically close and return to your Activity with RESULT_OK.

Note that if you start Goggles for the first time, it will show a tutorial and ask for some intial settings. When this appears, it will NOT return to your app after scanning the qr code.

tknell
  • 9,007
  • 3
  • 24
  • 28
  • No, I don't get the initial prompt( that pops up at the first launch). What I do get is Goggles successfully scans my text, but then it displays the result just like it would, if it were launched on its own... it doesn't automatically go back to my app. Ok, I just realized it works fine with an actual QR Code, but not with text scanning. I guess my problem is I need the right SCAN mode for text. I can't seem to find any API documentation for goggles. – harshalizee Jul 05 '14 at 20:29
  • You can look up the possible modes in the source code of the zxing app, but apparently it doesn't support scanning of only text... https://github.com/zxing/zxing/blob/master/android/src/com/google/zxing/client/android/Intents.java ... Maybe Goggles has additional modes, I'm still searching for that. – tknell Jul 05 '14 at 21:06
  • Didn't find anything... The only thing I can think of that you could try, is to remove the scan_mode to scan everything, and in the result intent than check this attribute: `intent.getStringExtra("SCAN_RESULT_FORMAT");` and see what you get there if you scan texts. Maybe that helps. – tknell Jul 05 '14 at 21:38
  • Yeah, I found the source code for zxing too, and removing the SCAN_MODE and replacing it with SCAN_FORMATS didn't change its behaviour. Any ideas on waht else I can try? – harshalizee Jul 06 '14 at 02:21
  • don't set the SCAN_MODE pr SCAN_FORMAT at all and check in your app what was scanned... maybe you can get the type of the text scan from the result with `intent.getStringExtra("SCAN_RESULT_FORMAT");` – tknell Jul 06 '14 at 13:55
  • Yeah, I tried that. The goggles app doesn't scan anything and never returns control back to the calling app, i.e. only way to get back would be a back button which, as usual returns RESULT_CANCELLED – harshalizee Jul 06 '14 at 15:40