Zxing比较本地数据的结果?

时间:2017-11-22 02:14:06

标签: java arrays zxing barcode-scanner

@Override
public void onActivityResult ( int requestCode, int resultCode, Intent data){
    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
    if (result != null) {
        if (result.getContents() == null) {
            Log.d("MainActivity", "Cancelled");
            Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
        } else {
            Log.d("MainActivity", "Scanned");
            Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
        }
    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

当前的工作方法,不与Data ^

进行比较

下面是将Result与Data进行比较以查看是否存在重复(不可用)

的方法
@Override
public void onActivityResult ( int requestCode, int resultCode, Intent data){
    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
    if (result != null) {
        if (result.getContents() == 555821282,8882288,888822884,1145698,248635887,4477996) {
            Log.d("MainActivity", "Yes");
            Toast.makeText(this, "Yes", Toast.LENGTH_LONG).show();
        } else {
            Log.d("MainActivity", "No");
            Toast.makeText(this, "No: " + result.getContents(), Toast.LENGTH_LONG).show();
        }
    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

我得到的整数太大了。我有500 UPC放在那里,但我甚至不能放一个。我可以将这些数字放在Android Studio上的某个文件中并在那里引用它吗?这个问题的解决方案是什么?

1 个答案:

答案 0 :(得分:0)

IntentResut.getContents()返回一个String。要比较Java中的两个字符串,您应该使用String.equals(String)方法。您的onActivityResult应该是这样的

@Override
public void onActivityResult ( int requestCode, int resultCode, Intent data){
    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
    if (result != null) {
        String data = result.getContents();
        if (data != null) {
            if (data.equals("555821282") 
                    || data.equals("8882288")
                    || data.equals("888822884")
                    /* ... rest of the options ... */ ) {
                Log.d("MainActivity", "Yes");
                Toast.makeText(this, "Yes", Toast.LENGTH_LONG).show();
            } else {
                Log.d("MainActivity", "No");
                Toast.makeText(this, "No: " + result.getContents(), Toast.LENGTH_LONG).show();
            }
        } else {
            // No data
        }
    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

由于您要比较500个UPC,因此您可能不希望编写500行if语句。相反,我建议将所有UPC(作为字符串)放入List中,然后if语句可以检查列表是否包含条形码。

List<String> knownUpcs = Arrays.asList("555821282", "8882288", "888822884", "1145698", "248635887", "4477996");
...
if (knownUpcs.contains(data)) {
    // do your thing
}

此处列表是静态定义的,但没有理由通过从磁盘上的文件中读取UPC来创建列表。