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