PageRenderTime 26ms CodeModel.GetById 13ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/ocr/ocrservice/src/com/googlecode/eyesfree/ocr/intent/RecognizeActivity.java

http://eyes-free.googlecode.com/
Java | 122 lines | 72 code | 24 blank | 26 comment | 6 complexity | e709ad96b5e28e80ab9d2a79afdd885f MD5 | raw file
  1/*
  2 * Copyright (C) 2011 Google Inc.
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5 * use this file except in compliance with the License. You may obtain a copy of
  6 * the License at
  7 *
  8 * http://www.apache.org/licenses/LICENSE-2.0
  9 *
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 13 * License for the specific language governing permissions and limitations under
 14 * the License.
 15 */
 16
 17package com.googlecode.eyesfree.ocr.intent;
 18
 19import android.content.Intent;
 20import android.os.Bundle;
 21import android.util.Log;
 22
 23import com.googlecode.eyesfree.ocr.client.Intents;
 24import com.googlecode.eyesfree.ocr.client.Ocr;
 25import com.googlecode.eyesfree.ocr.client.OcrResult;
 26
 27import java.util.ArrayList;
 28import java.util.List;
 29
 30/**
 31 * This activity runs text recognition and displays bounding box results. If the
 32 * OCR service fails or is missing, this activity will return null.
 33 *
 34 * @author alanv@google.com (Alan Viverette)
 35 */
 36public class RecognizeActivity extends BaseRecognizeActivity {
 37    private static final String TAG = "RecognizeActivity";
 38
 39    private Ocr mOcr;
 40
 41    private Ocr.Job mJob;
 42
 43    @Override
 44    public void onCreate(Bundle savedInstanceState) {
 45        super.onCreate(savedInstanceState);
 46
 47        mOcr = null;
 48        mJob = null;
 49    }
 50
 51    @Override
 52    public void onDestroy() {
 53        if (mJob != null) {
 54            mJob.cancel();
 55        }
 56
 57        if (mOcr != null) {
 58            mOcr.release();
 59        }
 60
 61        super.onDestroy();
 62    }
 63
 64    @Override
 65    protected void initializeOcrEngine() {
 66        Ocr.InitCallback onInit = new Ocr.InitCallback() {
 67            @Override
 68            public void onInitialized(int status) {
 69                processConfig();
 70            }
 71        };
 72
 73        mOcr = new Ocr(this, onInit);
 74        mOcr.setResultCallback(onResult);
 75        mOcr.setCompletionCallback(onCompleted);
 76    }
 77
 78    private void processConfig() {
 79        mOcr.setParameters(getParameters());
 80
 81        mJob = mOcr.enqueue(getFile());
 82
 83        if (mJob == null) {
 84            Log.e(TAG, "Text recognition call failed");
 85
 86            onCompleted.onCompleted(null);
 87        }
 88    }
 89
 90    protected void processResults(List<OcrResult> results) {
 91        ArrayList<OcrResult> arrayResults = new ArrayList<OcrResult>(results);
 92
 93        Intent result = new Intent();
 94        result.putExtra(Intents.Recognize.EXTRA_RESULTS, arrayResults);
 95        setResult(RESULT_OK, result);
 96
 97        finish();
 98    }
 99
100    /**
101     * Draw the bounding box of a single result.
102     *
103     * @param result
104     */
105    protected void processResult(OcrResult result) {
106        addOverlayRect(result.getString().trim(), result.getBounds());
107    }
108
109    private final Ocr.CompletionCallback onCompleted = new Ocr.CompletionCallback() {
110        @Override
111        public void onCompleted(List<OcrResult> results) {
112            processResults(results);
113        }
114    };
115
116    private final Ocr.ResultCallback onResult = new Ocr.ResultCallback() {
117        @Override
118        public void onResult(OcrResult result) {
119            processResult(result);
120        }
121    };
122}