/ocr/worldreader/src/com/google/marvin/worldreader/RectsView.java

http://eyes-free.googlecode.com/ · Java · 98 lines · 62 code · 16 blank · 20 comment · 3 complexity · be77b031f9f108e0c5435ca05dc97389 MD5 · raw file

  1. /*
  2. * Copyright (C) 2009 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.google.marvin.worldreader;
  17. import android.content.Context;
  18. import android.graphics.Canvas;
  19. import android.graphics.Matrix;
  20. import android.graphics.Paint;
  21. import android.graphics.Rect;
  22. import android.graphics.RectF;
  23. import android.util.AttributeSet;
  24. import android.util.Log;
  25. import android.view.View;
  26. import java.util.LinkedList;
  27. /**
  28. * Displays colored bounding boxes scaled to screen resolution.
  29. *
  30. * @author alanv@google.com (Alan Viverette)
  31. */
  32. public class RectsView extends View {
  33. private static final String TAG = "RectsView";
  34. private Matrix mMatrix;
  35. private Paint mPaint;
  36. private LinkedList<Pair> mRects;
  37. public RectsView(Context context, AttributeSet attrs) {
  38. super(context, attrs);
  39. mRects = new LinkedList<Pair>();
  40. mPaint = new Paint();
  41. mPaint.setStyle(Paint.Style.STROKE);
  42. mPaint.setStrokeWidth(5.0f);
  43. }
  44. public void addRect(Rect rect) {
  45. if (rect != null) {
  46. RectF rectf = new RectF(rect);
  47. int argb = (int) (Math.random() * Integer.MAX_VALUE) | 0xFF000000;
  48. Pair entry = new Pair(rectf, argb);
  49. mMatrix.mapRect(rectf);
  50. mRects.add(entry);
  51. postInvalidate();
  52. }
  53. }
  54. public void setScaling(int srcW, int srcH, int dstW, int dstH) {
  55. float scaleX = dstW / (float) srcW;
  56. float scaleY = dstH / (float) srcH;
  57. float scale = Math.min(scaleX, scaleY);
  58. float scaledW = scale * srcW;
  59. float scaledH = scale * srcH;
  60. float deltaX = (dstW - scaledW) / 2.0f;
  61. float deltaY = (dstH - scaledH) / 2.0f;
  62. mMatrix = new Matrix();
  63. mMatrix.postScale(scale, scale);
  64. mMatrix.postTranslate(deltaX, deltaY);
  65. Log.i(TAG, "Set scaling to scale " + scale + " transform (" + deltaX + "," + deltaY + ")");
  66. }
  67. @Override
  68. public void onDraw(Canvas canvas) {
  69. for (Pair entry : mRects) {
  70. mPaint.setColor(entry.argb);
  71. canvas.drawRect(entry.rectf, mPaint);
  72. }
  73. }
  74. private class Pair {
  75. public RectF rectf;
  76. public int argb;
  77. public Pair(RectF rectf, int argb) {
  78. this.rectf = rectf;
  79. this.argb = argb;
  80. }
  81. }
  82. }