/ocr/ocrservice/src/com/googlecode/eyesfree/ocr/intent/BaseRecognizeActivity.java
Java | 175 lines | 113 code | 36 blank | 26 comment | 5 complexity | 95d2a490edb0167842dd6e4cf899247e 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.app.Activity; 20import android.content.Context; 21import android.content.Intent; 22import android.graphics.Bitmap; 23import android.graphics.BitmapFactory; 24import android.graphics.Rect; 25import android.os.Bundle; 26import android.view.Display; 27import android.view.View; 28import android.view.View.OnClickListener; 29import android.view.WindowManager; 30import android.widget.ProgressBar; 31import android.widget.TextView; 32 33import com.googlecode.eyesfree.ocr.R; 34import com.googlecode.eyesfree.ocr.client.Intents; 35import com.googlecode.eyesfree.ocr.client.Ocr; 36 37import java.io.File; 38 39/** 40 * This activity runs text recognition and displays bounding box results. If the 41 * OCR service fails or is missing, this activity will return null. 42 * 43 * @author alanv@google.com (Alan Viverette) 44 */ 45public abstract class BaseRecognizeActivity extends Activity { 46 private TextRectsView mOverlayView; 47 48 private ProgressBar mProgress; 49 50 private Ocr.Parameters mParams; 51 52 private File mFile; 53 54 @Override 55 protected void onCreate(Bundle savedInstanceState) { 56 super.onCreate(savedInstanceState); 57 58 setContentView(R.layout.recognize); 59 60 mProgress = (ProgressBar) findViewById(R.id.progress); 61 mProgress.setIndeterminate(true); 62 63 mOverlayView = (TextRectsView) findViewById(R.id.overlay); 64 65 findViewById(R.id.cancel_processing).setOnClickListener(onClickListener); 66 67 // Load extras from intent 68 Intent intent = getIntent(); 69 mParams = intent.getParcelableExtra(Intents.Recognize.EXTRA_PARAMETERS); 70 mFile = new File(intent.getStringExtra(Intents.Recognize.EXTRA_INPUT)); 71 72 // Load background image in a separate thread 73 Thread setBackground = new Thread() { 74 @Override 75 public void run() { 76 setBackgroundAsync(); 77 } 78 }; 79 setBackground.start(); 80 } 81 82 protected void addOverlayRect(String text, Rect rect) { 83 mOverlayView.addRect(text, rect); 84 } 85 86 protected Ocr.Parameters getParameters() { 87 return mParams; 88 } 89 90 protected File getFile() { 91 return mFile; 92 } 93 94 protected abstract void initializeOcrEngine(); 95 96 private static Bitmap getScaledBitmap(File file, int width) { 97 String path = file.getAbsolutePath(); 98 99 BitmapFactory.Options sizeOpts = new BitmapFactory.Options(); 100 sizeOpts.inJustDecodeBounds = true; 101 BitmapFactory.decodeFile(path, sizeOpts); 102 103 BitmapFactory.Options opts = new BitmapFactory.Options(); 104 opts.inSampleSize = sizeOpts.outWidth / width; 105 106 Bitmap bitmap = BitmapFactory.decodeFile(path, opts); 107 108 return bitmap; 109 } 110 111 private void setBackgroundAsync() { 112 WindowManager manager = (WindowManager) getSystemService(Context.WINDOW_SERVICE); 113 Display display = manager.getDefaultDisplay(); 114 115 final Bitmap scaled = getScaledBitmap(mFile, display.getWidth()); 116 117 runOnUiThread(new Runnable() { 118 @Override 119 public void run() { 120 mOverlayView.setImageBitmap(scaled); 121 122 // Since we have a very limited amount of memory, we don't want 123 // this to run until we're done loading the background bitmap. 124 initializeOcrEngine(); 125 } 126 }); 127 } 128 129 protected void updateStatus(String status) { 130 TextView txtStatus = (TextView) findViewById(R.id.progress_status); 131 txtStatus.setText(status); 132 txtStatus.postInvalidate(); 133 134 // TODO Notify TTS that the status has updated? 135 } 136 137 protected void updateProgress(int current, int max) { 138 ProgressBar progress = (ProgressBar) findViewById(R.id.progress); 139 TextView txtPercent = (TextView) findViewById(R.id.progress_percent); 140 TextView txtNumber = (TextView) findViewById(R.id.progress_number); 141 142 if (current < 0 || max <= 0) { 143 progress.setIndeterminate(true); 144 txtPercent.setVisibility(View.INVISIBLE); 145 txtNumber.setVisibility(View.INVISIBLE); 146 return; 147 } else if (progress.isIndeterminate()) { 148 progress.setIndeterminate(false); 149 txtPercent.setVisibility(View.VISIBLE); 150 txtNumber.setVisibility(View.VISIBLE); 151 } 152 153 int intPercent = 100 * current / max; 154 155 String strPercent = getString(R.string.percent, intPercent); 156 String strNumber = getString(R.string.ratio, current, max); 157 158 progress.setMax(max); 159 progress.setProgress(current); 160 txtPercent.setText(strPercent); 161 txtNumber.setText(strNumber); 162 } 163 164 private final OnClickListener onClickListener = new OnClickListener() { 165 @Override 166 public void onClick(View v) { 167 switch (v.getId()) { 168 case R.id.cancel_processing: { 169 onBackPressed(); 170 break; 171 } 172 } 173 } 174 }; 175}