使用谷歌手机视觉API来检测第一个条形码并将数据发送到另一个活动

使用谷歌手机视觉API来检测第一个条形码并将数据发送到另一个活动

本文介绍了使用谷歌手机视觉API来检测第一个条形码并将数据发送到另一个活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

您好,我在github上从google检查了条形码读取器样本,我正试图让条形码检测器检测第一个条形码(仅一个),然后将其解码后的条形码发送到另一个活动. Mabye我错了,但我需要输入这段代码

Hi I checked the barcode reader sample from google on github , I am trying to just make the barcodedetector detect the first barcode (only one) and when it does it send the decoded barcode to another activity. Mabye I am wrong but I need to put this code

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<>

somewhere in this one so that the barcode is captured automatically without that I need to tap when barcode graphic appears.I also figured that I dont need the MultiProcessor.Builder<>

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

推荐答案

我也有同样的结果.这是我的完成方式.

I was after the same outcome. Here is how i accomplished it.

将侦听器添加到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方法,都会触发此侦听器.

This listener is fired whenever the create method is called when a new barcode is detected.

接下来,从BarcodeCaptureActivity中的createCameraSource方法下,附加一个新的侦听器,然后将条形码发送到您想要的任何位置.

Next, from the BarcodeCaptureActivity, under the createCameraSource method attach a new listener and send the barcode wherever you'd like it.

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);
        }
    });
...

希望有帮助!

这篇关于使用谷歌手机视觉API来检测第一个条形码并将数据发送到另一个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 21:26