0

カメラを使用してテキストを認識するには、Google のテキスト認識 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