3

嗨,我在 github 上检查了 google 的条形码阅读器示例,我试图让条形码检测器检测到第一个条形码(只有一个),当它检测到时,它将解码的条形码发送到另一个活动。Mabye我错了,但我需要输入这段代码

BarcodeGraphic graphic = mGraphicOverlay.getFirstGraphic();
    Barcode barcode = null;
    if (graphic != null) {
        barcode = graphic.getBarcode();
        if (barcode != null) {
            Intent data = new Intent();
            data.putExtra(BarcodeObject, barcode);
            setResult(CommonStatusCodes.SUCCESS, data);
            finish();
        }
        else {
            Log.d(TAG, "barcode data is null");
        }
    }
    else {
        Log.d(TAG,"no barcode detected");
    }
    return barcode != null;
}

在这个的某个地方,这样条形码就可以自动捕获,而无需在条形码图形出现时点击。我还认为我不需要 MultiProcessor.Builder<>

BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(context).build();
    BarcodeTrackerFactory barcodeFactory = new BarcodeTrackerFactory(mGraphicOverlay);
    barcodeDetector.setProcessor(
            new MultiProcessor.Builder<>(barcodeFactory).build());
4

1 回答 1

5

我追求同样的结果。这就是我如何完成它。

添加一个监听器BarcodeTracker

class BarcodeTrackerFactory implements MultiProcessor.Factory<Barcode> {
private GraphicOverlay<BarcodeGraphic> mGraphicOverlay;

private OnNewBarcodeListener newBarcodeListener;

BarcodeTrackerFactory(GraphicOverlay<BarcodeGraphic> barcodeGraphicOverlay) {
    mGraphicOverlay = barcodeGraphicOverlay;
}

@Override
public Tracker<Barcode> create(Barcode barcode) {
    BarcodeGraphic graphic = new BarcodeGraphic(mGraphicOverlay);
    newBarcodeListener.onNewItem(barcode);
    return new BarcodeGraphicTracker(mGraphicOverlay, graphic);
}

public interface OnNewBarcodeListener {
    void onNewItem(Barcode item);
}

public void setOnNewBarcodeListener(OnNewBarcodeListener newBarcodeListener) {
    this.newBarcodeListener = newBarcodeListener;
}

}

每当检测到新条形码时调用 create 方法时,都会触发此侦听器。

接下来,从BarcodeCaptureActivity,createCameraSource方法下附加一个新的侦听器并将条形码发送到您想要的任何地方。

private void createCameraSource(boolean autoFocus, boolean useFlash) {
    Context context = getApplicationContext();

    // A barcode detector is created to track barcodes.  An associated multi-processor instance
    // is set to receive the barcode detection results, track the barcodes, and maintain
    // graphics for each barcode on screen.  The factory is used by the multi-processor to
    // create a separate tracker instance for each barcode.
    BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(context).build();
    BarcodeTrackerFactory barcodeFactory = new BarcodeTrackerFactory(mGraphicOverlay);

    barcodeFactory.setOnNewBarcodeListener(new BarcodeTrackerFactory.OnNewBarcodeListener() {
        @Override
        public void onNewItem(Barcode item) {
            Log.d("BarcodeFound", "Found new barcode! " + item.rawValue);
            Intent intent = new Intent(this, DoSomethingWithBarcodeActivity.class);
            intent.putExtra("barcode", item.rawValue);
            startActivity(intent);
        }
    });
...

希望有帮助!

于 2016-03-13T19:10:00.983 回答