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

http://eyes-free.googlecode.com/ · Java · 165 lines · 111 code · 34 blank · 20 comment · 13 complexity · 550ac994516f688cfe1700c545e191d2 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.Context;
  18. import android.graphics.Canvas;
  19. import android.graphics.Color;
  20. import android.graphics.Matrix;
  21. import android.graphics.Matrix.ScaleToFit;
  22. import android.graphics.Paint;
  23. import android.graphics.Paint.Align;
  24. import android.graphics.Paint.Style;
  25. import android.graphics.Rect;
  26. import android.graphics.RectF;
  27. import android.graphics.drawable.Drawable;
  28. import android.util.AttributeSet;
  29. import android.widget.ImageView;
  30. import java.util.LinkedList;
  31. /**
  32. * Displays colored bounding boxes scaled to screen resolution.
  33. *
  34. * @author alanv@google.com (Alan Viverette)
  35. */
  36. public class TextRectsView extends ImageView {
  37. private Matrix mMatrix;
  38. private Paint mPaint;
  39. private LinkedList<TextRect> mRects;
  40. private int mWidth;
  41. private int mHeight;
  42. private int mSrcWidth;
  43. private int mSrcHeight;
  44. public class TextRect {
  45. String text;
  46. Rect rect;
  47. int color;
  48. public TextRect(String text, Rect rect) {
  49. this.text = text;
  50. this.rect = rect;
  51. this.color = (int) (Math.random() * Integer.MAX_VALUE) | 0xFF000000;
  52. }
  53. }
  54. public TextRectsView(Context context, AttributeSet attrs) {
  55. super(context, attrs);
  56. mRects = new LinkedList<TextRect>();
  57. mPaint = new Paint();
  58. mPaint.setTextAlign(Align.CENTER);
  59. mPaint.setAntiAlias(true);
  60. }
  61. public LinkedList<TextRect> getRects() {
  62. return new LinkedList<TextRect>(mRects);
  63. }
  64. public void clearRects() {
  65. mRects.clear();
  66. }
  67. public void addRect(String text, Rect rect) {
  68. if (text != null && rect != null) {
  69. TextRect entry = new TextRect(text, rect);
  70. mRects.add(entry);
  71. }
  72. }
  73. protected Matrix getScalingMatrix() {
  74. return mMatrix;
  75. }
  76. @Override
  77. protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  78. super.onSizeChanged(w, h, oldw, oldh);
  79. mWidth = w;
  80. mHeight = h;
  81. updateScaling();
  82. }
  83. @Override
  84. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  85. super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  86. Drawable drawable = getDrawable();
  87. if (drawable != null) {
  88. mSrcWidth = drawable.getIntrinsicWidth();
  89. mSrcHeight = drawable.getIntrinsicHeight();
  90. }
  91. updateScaling();
  92. }
  93. protected void updateScaling() {
  94. if (mSrcWidth <= 0 || mSrcHeight <= 0 || mWidth <= 0 || mHeight <= 0) {
  95. return;
  96. }
  97. RectF src = new RectF(0, 0, mSrcWidth, mSrcHeight);
  98. RectF dst = new RectF(0, 0, mWidth, mHeight);
  99. mMatrix = new Matrix();
  100. mMatrix.setRectToRect(src, dst, ScaleToFit.CENTER);
  101. }
  102. @Override
  103. protected void onDraw(Canvas canvas) {
  104. super.onDraw(canvas);
  105. if (mMatrix == null) {
  106. return;
  107. }
  108. int saveCount = canvas.save();
  109. canvas.setMatrix(mMatrix);
  110. for (TextRect entry : mRects) {
  111. mPaint.setColor(0xDDFFFFFF);
  112. mPaint.setStyle(Style.FILL);
  113. canvas.drawRect(entry.rect, mPaint);
  114. mPaint.setColor(entry.color);
  115. mPaint.setStyle(Style.STROKE);
  116. mPaint.setStrokeWidth(3.0f);
  117. canvas.drawRect(entry.rect, mPaint);
  118. float w = entry.rect.width();
  119. float h = entry.rect.height();
  120. float textSize = 2.0f * h / 3.0f;
  121. float cx = entry.rect.left + w / 2.0f;
  122. float cy = entry.rect.top + textSize;
  123. mPaint.setColor(Color.BLACK);
  124. mPaint.setStyle(Style.FILL);
  125. mPaint.setTextSize(textSize);
  126. mPaint.setTextAlign(Align.CENTER);
  127. canvas.drawText(entry.text, cx, cy, mPaint);
  128. }
  129. canvas.restoreToCount(saveCount);
  130. }
  131. }