开发者

Extracting results from Android ZXing IntentIntegrator

开发者 https://www.devze.com 2023-03-11 13:48 出处:网络
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 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 form开发者_如何学JAVAat = intent.getStringExtra("SCAN_RESULT_FORMAT");
        // Handle successful scan
        //How to get name from contents?

    } else if (resultCode == RESULT_CANCELED) {
        // Handle cancel
    }
}
}


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.


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.


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


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"

0

精彩评论

暂无评论...
验证码 换一张
取 消