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

http://eyes-free.googlecode.com/ · Java · 161 lines · 92 code · 33 blank · 36 comment · 7 complexity · bbed6a226882a0cc1d8cc66647e5f30d 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.graphics.RectF;
  18. import android.os.Bundle;
  19. import android.view.View;
  20. import com.googlecode.eyesfree.ocr.R;
  21. import com.googlecode.eyesfree.opticflow.DebugView;
  22. import com.googlecode.eyesfree.opticflow.FrameLooper;
  23. import com.googlecode.eyesfree.opticflow.ImageBlurProcessor;
  24. import com.googlecode.eyesfree.opticflow.OpticalFlowProcessor;
  25. import com.googlecode.eyesfree.opticflow.TextDetectionProcessor;
  26. import com.googlecode.eyesfree.opticflow.TextTrackerProcessor;
  27. import com.googlecode.eyesfree.opticflow.TextTrackerProcessor.TrackedRect;
  28. import java.util.ArrayList;
  29. import java.util.LinkedList;
  30. /**
  31. * Analyzes the camera preview and provides feedback on whether there is text on
  32. * the screen. Useful for helping users with visual impairments decide whether
  33. * something is worth performing OCR on.
  34. *
  35. * @author alanv@google.com (Alan Viverette)
  36. */
  37. public class DetectActivity extends CaptureActivity {
  38. private FrameLooper mPreviewLooper;
  39. private DebugView mDebugView;
  40. private NoisyDetector mNoisyDetector;
  41. @Override
  42. public void onCreate(Bundle savedInstanceState) {
  43. super.onCreate(savedInstanceState);
  44. setContentView(R.layout.detect);
  45. mNoisyDetector = new NoisyDetector(this);
  46. mDebugView = (DebugView) findViewById(R.id.debug_view);
  47. findViewById(R.id.preview).setOnClickListener(clickListener);
  48. }
  49. /*
  50. * private void processIntent(Intent settings) { Display display =
  51. * getWindowManager().getDefaultDisplay(); int width =
  52. * settings.getIntExtra(Intents.Detect.WIDTH, -1); int height =
  53. * settings.getIntExtra(Intents.Detect.HEIGHT, -1); String flashMode =
  54. * settings.getStringExtra(Intents.Detect.FLASH_MODE); boolean flashlight =
  55. * settings.getBooleanExtra( Intents.Detect.FLASHLIGHT, DEFAULT_FLASHLIGHT);
  56. * mCameraManager.setPictureSize(width, height);
  57. * mCameraManager.setFlashlight(flashlight);
  58. * mCameraManager.setFlashMode(flashMode); }
  59. */
  60. @Override
  61. protected void onResume() {
  62. super.onResume();
  63. mNoisyDetector.start();
  64. if (mPreviewLooper != null) {
  65. mPreviewLooper.startLoop();
  66. }
  67. }
  68. @Override
  69. protected void onPause() {
  70. mNoisyDetector.pause();
  71. if (mPreviewLooper != null) {
  72. mPreviewLooper.stopLoop();
  73. }
  74. super.onPause();
  75. }
  76. @Override
  77. protected void onCameraStarted() {
  78. initializeContinuous();
  79. }
  80. private void initializeContinuous() {
  81. CameraManager cameraManager = getCameraManager();
  82. final int[] delayMillis = {
  83. 0, 10, 100, 1000
  84. };
  85. mPreviewLooper = new FrameLooper(cameraManager, mDebugView, delayMillis);
  86. mDebugView.setCallback(mPreviewLooper);
  87. final OpticalFlowProcessor opticalFlow = new OpticalFlowProcessor();
  88. mPreviewLooper.addPreviewProcessor(opticalFlow, 1);
  89. final TextDetectionProcessor textDetect = new TextDetectionProcessor();
  90. mPreviewLooper.addPreviewProcessor(textDetect, 2);
  91. final TextTrackerProcessor textTracker = new TextTrackerProcessor(
  92. opticalFlow.getOpticalFlow());
  93. mPreviewLooper.addPreviewProcessor(textTracker, 2);
  94. final ImageBlurProcessor imageBlur = new ImageBlurProcessor(cameraManager);
  95. mPreviewLooper.addPreviewProcessor(imageBlur, 3);
  96. // This is a workaround for an issue where the previewLooper isn't
  97. // running properly when the activity is restarted.
  98. // TODO(mrcasey): Figure out why this seems to fix the issue.
  99. if (mPreviewLooper.isRunning()) {
  100. mPreviewLooper.stopLoop();
  101. }
  102. textTracker.setListener(textListener);
  103. mPreviewLooper.initAllProcessors();
  104. mPreviewLooper.startLoop();
  105. }
  106. private final TextTrackerProcessor.Listener textListener = new TextTrackerProcessor.Listener() {
  107. @Override
  108. public void onTextDetected(RectF bounds, LinkedList<TrackedRect> trackedRects) {
  109. ArrayList<RectF> trackedBounds = new ArrayList<RectF>(trackedRects.size());
  110. for (TrackedRect rect : trackedRects) {
  111. trackedBounds.add(rect.rect);
  112. }
  113. mNoisyDetector.onTextDetected(bounds, trackedBounds);
  114. }
  115. };
  116. private final View.OnClickListener clickListener = new View.OnClickListener() {
  117. @Override
  118. public void onClick(View v) {
  119. switch (v.getId()) {
  120. case R.id.preview:
  121. mNoisyDetector.pause();
  122. mPreviewLooper.stopLoop();
  123. takePreview();
  124. break;
  125. }
  126. }
  127. };
  128. }