简体   繁体   English

如何从 Android firebase ML-Kit BarcodeScannerProcessor onSuccess 找到上下文并启动新活动

[英]How can I find context and start a new Activity from Android firebase ML-Kit BarcodeScannerProcessor onSuccess

I am using the quickstart-android code provided by google but after many attempts I cam unable to find a context that is not returning null.我正在使用 google 提供的 quickstart-android 代码,但经过多次尝试后,我无法找到不返回 null 的上下文。 The BarcodeScannerProcessor is not itself an Activity, so I have attempted to create an instance of the LivePreviewActivity and use that as the context in the intent, but it's null. BarcodeScannerProcessor 本身不是活动,因此我尝试创建 LivePreviewActivity 的实例并将其用作意图中的上下文,但它为空。

The goal is to once a valid barcode is recognized I want to open a new activity that allows a user to verify value and on the push of a button call a webservice to post the barcode to a database via API.目标是一旦识别出有效的条形码,我想打开一个新活动,允许用户验证值并按下按钮调用 Web 服务以通过 API 将条形码发布到数据库。 I am having a hard time finding a valid context and the app is crashing when it trys to execute the Intent.我很难找到有效的上下文,并且应用程序在尝试执行 Intent 时崩溃了。

Starting at line 97-107:从第 97-107 行开始:

https://github.com/jamiekeefer/quickstart-android/blob/master/mlkit/app/src/main/java/com/google/firebase/samples/apps/mlkit/java/barcodescanning/BarcodeScanningProcessor.java https://github.com/jamiekeefer/quickstart-android/blob/master/mlkit/app/src/main/java/com/google/firebase/samples/apps/mlkit/java/barcodescanning/BarcodeScanningProcessor.java

        for (int i = 0; i < barcodes.size(); ++i) {
        FirebaseVisionBarcode barcode = barcodes.get(i);
        BarcodeGraphic barcodeGraphic = new BarcodeGraphic(graphicOverlay, barcode);
        graphicOverlay.add(barcodeGraphic);

        System.out.println(barcode.getRawValue());

        if (!barcode.getRawValue().equals("") ) {

            System.out.println("Got the number:" + barcode.getRawValue() + " Context: " + mContext); //OLD SCHOOL DEBUG OUTPUT

            //enter code to start activity

                Intent intent = new Intent(mContext, SendScannedBarcode.class);
                String message = scannedBarcode;
                intent.putExtra(EXTRA_MESSAGE, message);
                mContext.startActivity(intent);
        }


    }

You can back up in the repo to see the instance of the LivePreviewActivity where I trying to get context.您可以在 repo 中备份以查看我尝试获取上下文LivePreviewActivity的实例

I have tried a number of things and read about Context, Views and Activities and basically have completely confused myself.我已经尝试了很多东西并阅读了关于上下文、视图和活动的内容,但基本上我自己已经完全糊涂了。 The only tuts I can find are using Kotlin, which is not helping clarify things.我能找到的唯一 tuts 是使用 Kotlin,这无助于澄清事情。

I appreacite any help in indentifying or contruting a valid Intent from this Context.我很感激任何帮助从这个上下文中识别或构建一个有效的意图。 Thank you.谢谢你。

So I am assuming that in your LivePreviewActivity you are creating an object of the class BarcodeScanningProcessor .所以我假设在你的LivePreviewActivity你正在创建一个BarcodeScanningProcessor类的对象。 What you can do is change the constructor in the BarcodeScanningProcessor class to accept a context and then you pass in your LivePreviewActivity 's context.您可以做的是更改BarcodeScanningProcessor类中的构造函数以接受上下文,然后传入LivePreviewActivity的上下文。

This is what the code should look like:代码应该是这样的:

In BarcodeScanningProcessor :BarcodeScanningProcessor

  public BarcodeScanningProcessor(Context context) {
    // Note that if you know which format of barcode your app is dealing with, detection will be
    // faster to specify the supported barcode formats one by one, e.g.
    // new FirebaseVisionBarcodeDetectorOptions.Builder()
    // .setBarcodeFormats(FirebaseVisionBarcode.FORMAT_QR_CODE)
    // .build();
    detector = FirebaseVision.getInstance().getVisionBarcodeDetector();
    this.mContext = context;
}

Then in LivePreviewActivity :然后在LivePreviewActivity

In the particular case of your activity you would do:在您的活动的特定情况下,您将执行以下操作:

  case BARCODE_DETECTION:
          Log.i(TAG, "Using Barcode Detector Processor");
          cameraSource.setMachineLearningFrameProcessor(new BarcodeScanningProcessor(getApplicationContext()));
          break;

Or if you just wanted to create an object of the class you could do: BarcodeScanningProcessor bsp = new BarcodeScanningProcessor(getApplicationContext());或者,如果您只想创建该类的对象,您可以这样做: BarcodeScanningProcessor bsp = new BarcodeScanningProcessor(getApplicationContext());

This should now give your BarcodeScanningProcessor class the context of your activity.这现在应该为您的BarcodeScanningProcessor类提供您活动的上下文。 Now, in BarcodeScanningProcessor , mContext should not be null and will have the context of your activity.现在,在BarcodeScanningProcessor , mContext 不应为 null 并且将包含您的活动的上下文。 I hope this answers your question.我希望这回答了你的问题。

try this create Application class试试这个创建Application

import android.app.Application;

public class MyApplication  extends Application {
    static MyApplication instance;

    @Override
    public void onCreate() {
        super.onCreate();
        instance=this;
    }

    public static MyApplication getInstance() {
        return instance;
    }
}

Register in manifest file在清单文件中注册

<application
        ..
        android:name="com.yourpackage.MyApplication"
        ..>
.
.
.
</application>

start activity using this MyApplication.使用此 MyApplication 启动活动。

Intent intent = new Intent(MyApplication.getInstance(), SendScannedBarcode.class);
                String message = scannedBarcode;
                intent.putExtra(EXTRA_MESSAGE, message);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
               MyApplication. getInstance().startActivity(intent);

Another way of handling the issue is create new constructor of BarcodeScanningProcessor which takes interface call back and once processing is done pass back result to caller.处理该问题的另一种方法是创建 BarcodeScanningProcessor 的新构造函数,它接受接口回调,一旦处理完成,将结果传回给调用者。

   public interface BarcodeUpdateListener {
      @UiThread
      void onBarcodeDetected(Barcode barcode);
   } 


   private BarcodeUpdateListener callback;

   public BarcodeScanningProcessor(BarcodeUpdateListener callback){
      this.callback = callback;
      detector = FirebaseVision.getInstance().getVisionBarcodeDetector();
   } 

Once you get the result pass result to caller一旦你得到结果将结果传递给调用者

callback.onBarcodeDetected(<Barcode>)

您可以从graphicOverlay获取上下文:

Context context = graphicOverlay.getContext();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM