Search code examples
android-studiozxing

Android studio ZXing library scan barcode and get the data into a custom class


So i have my custom class with its own properties:

public class PlayerClass {

public String name;

public int age;

public String id;

public String teamTag;

and i want to scan a barcode and create a player from the barcode info.

so i scan the barcode

        IntentIntegrator scanIntegrator = new IntentIntegrator(this);
        scanIntegrator.initiateScan();

and then i get the info

String scanContent = scanningResult.getContents();

So how do i get the info from the scan into a PlayerClass in order to fill its properties?


Solution

  • The most simple way is to create the barcode such that it has information stored in the format:

    name|age|id|teamTag

    So now when you get the scanned content in scanContent String you will have this info.

    In your Class you can make constructor like:

    PlayerClass(String content){
         String part[] = content.split("|");
         name = part[0];
         age = Integer.parseInt(part[1]);
         id = part[2];
         teamTag = part[3];
    }