3

I am rather new to ZXing and has been exploring their API. I have seen Using ZXing to create an android barcode scanning app and getting scan result when using zxing?

I understand that the details of the scan result is in the string "contents". How do I extract out the details? Looking at the QR generator, http://zxing.appspot.com/generator/ there are many fields like name, company and phone number. How do i extract these details?

I would need something like

//String extractedName = contents.getName() 

Sorry, I am very new to this. I would appreciate if someone can provide me detailed steps. Thanks.

TestActivity

public Button.OnClickListener mScan = new Button.OnClickListener() {
public void onClick(View v) {
    IntentIntegrator.initiateScan(MenuActivity.this);
}
};


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");
        // Handle successful scan
        //How to get name from contents?

    } else if (resultCode == RESULT_CANCELED) {
        // Handle cancel
    }
}
}
Community
  • 1
  • 1
ZXingIT
  • 325
  • 1
  • 5
  • 14

4 Answers4

2

I have successfully extracted it. The scanned results is inside the string 'contents'. Lets say you scanned a code with name and id. The string will contain "name" "id" separated by a whitespace. You can use a for loop to handle it.

ZXingIT
  • 325
  • 1
  • 5
  • 14
1

create object in IntentIntegrator

protected void onCreate(Bundle savedInstanceState) { <br>
        super.onCreate(savedInstanceState);<br>
        setContentView(R.layout.activity_main);<br>
        new IntentIntegrator(this).initiateScan();<br>
    }

Override onActivityResult method

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) { <br>
        IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);<br>
        if (scanResult != null) {<br>
            TextView t = (TextView) findViewById(R.id.textView2);<br>
            String data[] =scanResult.getContents().split("\n");<br>
            for(String k:data){<br>
                t.append(k+"\n");<br>
            }<br>
        }<br>

        // else continue with any other code you need in the method

    }

In this I got data using getContents method and split that data line. Then appeded that data to the textview2

Bacteria
  • 7,810
  • 10
  • 47
  • 61
Ishan Fernando
  • 2,238
  • 1
  • 24
  • 32
1

How do i extract these details?

A QR code is simply is a file in the form of a picture. Just as you need to know the format of a file before you can use it, you need to know the format of a QR code's contents before you can use it.

This ZXing Wiki page has information about the types of QR code contents that they have seen.

CommonsWare
  • 910,778
  • 176
  • 2,215
  • 2,253
  • 1
    In particular use http://code.google.com/p/zxing/source/browse/trunk/core/src/com/google/zxing/client/result/ResultParser.java from the project to parse the raw content into a structured object. – Sean Owen Jun 05 '11 at 08:32
  • Someone please help me out with this. – ZXingIT Jun 12 '11 at 18:26
0

first check the beginning of the string for example if it startsWith("xyxy") like "vcard". Be careful! "vcard" is not the correct string!

If it's a vCard i used to splitt it into Parts.

String[] vCardContent = content.split("\n");

Next Step is to check every part if it starts with e.g. "N:" alias "Name"

Martin
  • 71
  • 4