0

我需要使用谷歌的文本识别 API 来使用相机识别文本。我下载了代码,它运行良好。但是我正在开发一个需要识别某个单词的项目,该单词保存在全局变量中。识别出单词后,我无法开始另一项活动。这是使文本识别的代码部分:

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.util.SparseArray;

import com.google.android.gms.samples.vision.ocrreader.ui.camera.GraphicOverlay;
import com.google.android.gms.vision.Detector;
import com.google.android.gms.vision.text.TextBlock;

/**
 * A very simple Processor which gets detected TextBlocks and adds them to the overlay
 * as OcrGraphics.
 * TODO: Make this implement Detector.Processor<TextBlock> and add text to the GraphicOverlay
 */
public final class OcrDetectorProcessor extends Activity implements Detector.Processor<TextBlock> {


private GraphicOverlay<OcrGraphic> mGraphicOverlay;

OcrDetectorProcessor(GraphicOverlay<OcrGraphic> ocrGraphicOverlay, String word) {


  //  System.out.println("VARIAVEIL GLOBAL no detector:" + word);
    //   System.out.println("VARIAVEIL GLOBAL no detector da classe:" +s);
    mGraphicOverlay = ocrGraphicOverlay;
    // String lala = receiveDetections(ocrGraphicOverlay);


}



  @Override
public void receiveDetections(Detector.Detections<TextBlock> detections) {


    mGraphicOverlay.clear();
   System.out.println("CLEAR : " + mGraphicOverlay);
    SparseArray<TextBlock> items = detections.getDetectedItems();
    for (int i = 0; i < items.size(); ++i) {
        TextBlock item = items.valueAt(i);
        if (item != null && item.getValue() != null) {
            Log.d("Processor", "Text detected! " + item.getValue());

            String letra = item.getValue();

            // get
         //   String s = ((MyApplication) this.getApplication()).getSomeVariable();

           // System.out.println("Variavei global : "+s);

            /*if(letra.equals(palavra))
            {
                System.out.println("LETRA : " +letra);
                System.out.println("LETRA IDENTIFICADA");
            }*/

                 Intent intent = new Intent(getApplicationContext(),Resultado.class);
                startActivity(intent);

     }

        OcrGraphic graphic = new OcrGraphic(mGraphicOverlay, item);
        mGraphicOverlay.add(graphic);
    }
}

private String PalavraGerada() {
    System.out.println("Veio no palavra gerada");

    // get
    String s = ((MyApplication) this.getApplication()).getSomeVariable();
    return s;
}

@Override
public void release() {

    mGraphicOverlay.clear();
}



}

当我尝试开始另一个活动时,我收到以下错误:

11-03 13:23:14.347 21422-21852/com.google.android.gms.samples.vision.barcodereader E/OpenCameraSource: Exception thrown from receiver.
                                                                                                   java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
                                                                                                       at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:106)
                                                                                                       at com.google.android.gms.samples.vision.ocrreader.OcrDetectorProcessor.receiveDetections(OcrDetectorProcessor.java:76)
                                                                                                       at com.google.android.gms.vision.Detector.receiveFrame(Unknown Source)
                                                                                                       at com.google.android.gms.samples.vision.ocrreader.ui.camera.CameraSource$FrameProcessingRunnable.run(CameraSource.java:1209)
                                                                                                       at java.lang.Thread.run(Thread.java:818)

我实际上不需要开始另一个活动,我需要将文本识别与某个单词进行比较。

此外,当我尝试获取全局变量的值时,出现以下错误:

11-03 13:29:18.029 23276-23495/com.google.android.gms.samples.vision.barcodereader E/OpenCameraSource: Exception thrown from receiver.
                                                                                                   java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.android.gms.samples.vision.ocrreader.MyApplication.getSomeVariable()' on a null object reference
                                                                                                       at com.google.android.gms.samples.vision.ocrreader.OcrDetectorProcessor.receiveDetections(OcrDetectorProcessor.java:66)
                                                                                                       at com.google.android.gms.vision.Detector.receiveFrame(Unknown Source)
                                                                                                       at com.google.android.gms.samples.vision.ocrreader.ui.camera.CameraSource$FrameProcessingRunnable.run(CameraSource.java:1209)
                                                                                                       at java.lang.Thread.run(Thread.java:818)

我真的不知道如何解决它,我很感激任何帮助。谢谢

4

1 回答 1

0

问题是您无法获取上下文。尝试将上下文对象作为参数传递给构造函数方法,例如

private Context mContext;
OcrDetectorProcessor(GraphicOverlay<OcrGraphic> ocrGraphicOverlay, String word. Context context) {

    mGraphicOverlay = ocrGraphicOverlay;
    mContext = context;

}

然后在你的方法

private String PalavraGerada() {
    System.out.println("Veio no palavra gerada");
    //String s = ((MyApplication) this.getApplication()).getSomeVariable();
    String s = ((MyApplication)mContext).getSomeVariable();
    // or try some other way to get your application.
    return s;
}
于 2017-07-20T09:00:28.127 回答