您好我检查了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());
答案 0 :(得分: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);
}
});
...
希望有所帮助!