嗨,我在github上查看了来自google的条形码阅读器示例,我只是想让条形码检测器检测到第一个条形码(只有一个),当它检测到时,它会将解码的条形码发送到另一个活动。玛比,我错了,但我需要把这个密码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;}在这个程序中的某个地方,当条码图形出现时,我不需要点击就可以自动捕获条码。我还发现我不需要多处理器。builderBarcodeDetector barcodeDetector = new BarcodeDetector.Builder(context).build(); BarcodeTrackerFactory barcodeFactory = new BarcodeTrackerFactory(mGraphicOverlay); barcodeDetector.setProcessor( new MultiProcessor.Builder<>(barcodeFactory).build()); (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 我也在追求同样的结果。这就是我是怎么做到的。向BarcodeTracker添加侦听器:class BarcodeTrackerFactory implements MultiProcessor.Factory<Barcode> {private GraphicOverlay<BarcodeGraphic> mGraphicOverlay;private OnNewBarcodeListener newBarcodeListener;BarcodeTrackerFactory(GraphicOverlay<BarcodeGraphic> barcodeGraphicOverlay) { mGraphicOverlay = barcodeGraphicOverlay;}@Overridepublic 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); } });...希望能有帮助! (adsbygoogle = window.adsbygoogle || []).push({});
08-24 19:33