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

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