/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. package com.googlecode.eyesfree.ocr.intent;
  17. import android.content.Intent;
  18. import android.os.Bundle;
  19. import android.util.Log;
  20. import com.googlecode.eyesfree.ocr.client.Intents;
  21. import com.googlecode.eyesfree.ocr.client.Ocr;
  22. import com.googlecode.eyesfree.ocr.client.OcrResult;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. /**
  26. * This activity runs text recognition and displays bounding box results. If the
  27. * OCR service fails or is missing, this activity will return null.
  28. *
  29. * @author alanv@google.com (Alan Viverette)
  30. */
  31. public class RecognizeActivity extends BaseRecognizeActivity {
  32. private static final String TAG = "RecognizeActivity";
  33. private Ocr mOcr;
  34. private Ocr.Job mJob;
  35. @Override
  36. public void onCreate(Bundle savedInstanceState) {
  37. super.onCreate(savedInstanceState);
  38. mOcr = null;
  39. mJob = null;
  40. }
  41. @Override
  42. public void onDestroy() {
  43. if (mJob != null) {
  44. mJob.cancel();
  45. }
  46. if (mOcr != null) {
  47. mOcr.release();
  48. }
  49. super.onDestroy();
  50. }
  51. @Override
  52. protected void initializeOcrEngine() {
  53. Ocr.InitCallback onInit = new Ocr.InitCallback() {
  54. @Override
  55. public void onInitialized(int status) {
  56. processConfig();
  57. }
  58. };
  59. mOcr = new Ocr(this, onInit);
  60. mOcr.setResultCallback(onResult);
  61. mOcr.setCompletionCallback(onCompleted);
  62. }
  63. private void processConfig() {
  64. mOcr.setParameters(getParameters());
  65. mJob = mOcr.enqueue(getFile());
  66. if (mJob == null) {
  67. Log.e(TAG, "Text recognition call failed");
  68. onCompleted.onCompleted(null);
  69. }
  70. }
  71. protected void processResults(List<OcrResult> results) {
  72. ArrayList<OcrResult> arrayResults = new ArrayList<OcrResult>(results);
  73. Intent result = new Intent();
  74. result.putExtra(Intents.Recognize.EXTRA_RESULTS, arrayResults);
  75. setResult(RESULT_OK, result);
  76. finish();
  77. }
  78. /**
  79. * Draw the bounding box of a single result.
  80. *
  81. * @param result
  82. */
  83. protected void processResult(OcrResult result) {
  84. addOverlayRect(result.getString().trim(), result.getBounds());
  85. }
  86. private final Ocr.CompletionCallback onCompleted = new Ocr.CompletionCallback() {
  87. @Override
  88. public void onCompleted(List<OcrResult> results) {
  89. processResults(results);
  90. }
  91. };
  92. private final Ocr.ResultCallback onResult = new Ocr.ResultCallback() {
  93. @Override
  94. public void onResult(OcrResult result) {
  95. processResult(result);
  96. }
  97. };
  98. }