PageRenderTime 15ms CodeModel.GetById 2ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

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