PageRenderTime 24ms CodeModel.GetById 2ms app.highlight 16ms RepoModel.GetById 2ms app.codeStats 0ms

/ocr/ocrservice/src/com/googlecode/eyesfree/ocr/client/OcrResult.java

http://eyes-free.googlecode.com/
Java | 158 lines | 70 code | 24 blank | 64 comment | 5 complexity | a4e335d193c63bb25393d068a80aca6e 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.client;
 18
 19import android.graphics.Rect;
 20import android.os.Parcel;
 21import android.os.Parcelable;
 22
 23/**
 24 * This class represents the result of text recognition. It includes the text
 25 * itself as well as the word confidence values.
 26 *
 27 * @author alanv@google.com (Alan Viverette)
 28 */
 29public class OcrResult implements Parcelable {
 30    /** The text area boundary. */
 31    private final Rect mBounds;
 32
 33    /** The character contents of this text area. */
 34    private final String mString;
 35
 36    /** The word confidences of this text area. */
 37    private final int[] mConfidences;
 38
 39    private final float mAngle;
 40
 41    /**
 42     * The average word confidence of this text area. Initially -1 and lazily
 43     * generated when getAverageConfidence() is called.
 44     */
 45    private float mAvgConfidence;
 46
 47    /**
 48     * Creates an OCR result.
 49     *
 50     * @param result The result of text recognition.
 51     * @param confidences The array of word confidences.
 52     */
 53    public OcrResult(Rect bounds, String result, int[] confidences, float angle) {
 54        mBounds = bounds;
 55        mString = result;
 56        mConfidences = confidences;
 57        mAngle = angle;
 58        mAvgConfidence = -1;
 59    }
 60
 61    /**
 62     * Returns the bounding box containing the text result as a Rect.
 63     *
 64     * @return The bounding box containing the text result.
 65     */
 66    public Rect getBounds() {
 67        return mBounds;
 68    }
 69
 70    /**
 71     * Returns the result of text recognition as a String.
 72     *
 73     * @return The result of text recognition.
 74     */
 75    public String getString() {
 76        return mString;
 77    }
 78
 79    /**
 80     * Returns the array of word confidences. Each entry corresponds to a single
 81     * space-delimited word in the result string.
 82     *
 83     * @return The array of word confidences.
 84     */
 85    public int[] getConfidences() {
 86        return mConfidences;
 87    }
 88
 89    /**
 90     * Returns the angle of this text. Rotation is relative to the center of the
 91     * source image.
 92     *
 93     * @return The source image center-relative angle of this text.
 94     */
 95    public float getAngle() {
 96        return mAngle;
 97    }
 98
 99    /**
100     * Returns the average confidence of the words in this result.
101     *
102     * @return The average confidence of the words in this result.
103     */
104    public float getAverageConfidence() {
105        if (mAvgConfidence >= 0) {
106            return mAvgConfidence;
107        } else if (mConfidences == null) {
108            return 0.0f;
109        }
110
111        mAvgConfidence = 0.0f;
112
113        for (int i = 0; i < mConfidences.length; i++) {
114            mAvgConfidence += mConfidences[i];
115        }
116
117        mAvgConfidence /= mConfidences.length;
118
119        return mAvgConfidence;
120    }
121
122    /************************
123     * Parcelable functions *
124     ************************/
125
126    private OcrResult(Parcel src) {
127        mString = src.readString();
128        mConfidences = src.createIntArray();
129        mBounds = src.readParcelable(null);
130        mAngle = src.readFloat();
131    }
132
133    @Override
134    public int describeContents() {
135        return 0;
136    }
137
138    @Override
139    public void writeToParcel(Parcel dest, int flags) {
140        dest.writeString(mString);
141        dest.writeIntArray(mConfidences);
142        dest.writeParcelable(mBounds, Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
143        dest.writeFloat(mAngle);
144    }
145
146    public static final Parcelable.Creator<OcrResult> CREATOR = new Parcelable.Creator<OcrResult>() {
147        @Override
148        public OcrResult createFromParcel(Parcel in) {
149            return new OcrResult(in);
150        }
151
152        @Override
153        public OcrResult[] newArray(int size) {
154            return new OcrResult[size];
155        }
156    };
157
158}